frontend/smart-hut/src/components/dashboard/devices/Videocam.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

// vim: set ts=2 sw=2 et tw=80:
2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import { Grid, Checkbox } from 'semantic-ui-react';
import { connect } from 'react-redux';
import { StyledDivCamera } from './styleComponents';
import { RemoteService } from '../../../remote';
import { endpointURL } from '../../../endpoint';
import VideocamModal from './VideocamModal';
import mapStateToProps from '../../../deviceProps';
2020-04-22 12:25:24 +00:00
class Videocam extends Component {
constructor(props) {
super(props);
2020-04-22 18:14:18 +00:00
this.state = { selectedVideo: undefined };
2020-05-03 12:39:44 +00:00
this.setOnOff = this.setOnOff.bind(this);
}
openModal = () => {
2020-05-12 13:18:33 +00:00
this.setState((state) => ({ selectedVideo: true }));
};
closeModal = () => {
2020-05-12 13:18:33 +00:00
this.setState((state) => ({ selectedVideo: undefined }));
2020-04-22 18:14:18 +00:00
};
2020-05-03 12:39:44 +00:00
setOnOff(onOff) {
const turn = onOff;
2020-05-12 13:18:33 +00:00
if (this.props.tab === 'Devices' || this.props.tab === 'Hosts') {
2020-05-03 12:39:44 +00:00
this.props
.saveDevice({ ...this.props.device, on: turn })
2020-05-12 13:18:33 +00:00
.then((res) => (turn ? this.refs.vidRef.play() : this.refs.vidRef.pause()))
.catch((err) => console.error('videocamera update error', err));
2020-05-03 12:39:44 +00:00
} else {
this.props.updateState(
2020-05-04 14:09:56 +00:00
{ id: this.props.stateOrDevice.id, on: turn },
2020-05-12 13:18:33 +00:00
this.props.stateOrDevice.kind,
2020-05-03 12:39:44 +00:00
);
}
}
2020-04-23 15:18:43 +00:00
get url() {
return endpointURL() + this.props.device.path;
}
render() {
2020-04-22 12:25:24 +00:00
const VideocamView = (
<div>
2020-04-22 18:14:18 +00:00
<StyledDivCamera>
<div onClick={this.openModal}>
2020-05-03 12:39:44 +00:00
<video ref="vidRef" autoPlay loop muted width="100%" height="auto">
2020-04-23 15:18:43 +00:00
<source src={this.url} type="video/mp4" />
</video>
</div>
2020-04-22 18:14:18 +00:00
</StyledDivCamera>
2020-04-26 11:38:54 +00:00
<Grid columns="equal" padded>
2020-04-22 18:14:18 +00:00
<Grid.Row textAlign="center">
<Grid.Column>
<VideocamModal
selectedVideo={this.state.selectedVideo}
closeModal={this.closeModal}
2020-04-23 15:18:43 +00:00
url={this.url}
2020-04-22 18:14:18 +00:00
/>
</Grid.Column>
</Grid.Row>
2020-05-08 12:55:26 +00:00
<Grid.Row>
<Grid.Column>
<Checkbox
checked={this.props.stateOrDevice.on}
toggle
2020-05-08 12:55:26 +00:00
label="Turn on/off"
onChange={(e, val) => this.setOnOff(val.checked)}
/>
</Grid.Column>
</Grid.Row>
2020-04-22 18:14:18 +00:00
</Grid>
</div>
);
2020-04-22 18:14:18 +00:00
return <div>{VideocamView}</div>;
}
}
2020-04-22 12:25:24 +00:00
const VideocamContainer = connect(mapStateToProps, RemoteService)(Videocam);
export default VideocamContainer;