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

112 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-04-10 15:25:52 +00:00
import React from "react";
import Light from "./Light";
import SmartPlug from "./SmartPlug";
import Sensor from "./Sensor";
import { ButtonDimmer, KnobDimmer } from "./Dimmer";
import Switcher from "./Switch";
2020-04-22 12:25:24 +00:00
import Videocam from "./Videocam";
2020-04-10 21:13:23 +00:00
import { Segment, Grid, Header, Button, Icon } from "semantic-ui-react";
2020-04-10 15:25:52 +00:00
import { RemoteService } from "../../../remote";
import { connect } from "react-redux";
import DeviceSettingsModal from "./DeviceSettingsModal";
class Device extends React.Component {
constructor(props) {
super(props);
this.modalRef = React.createRef();
this.edit = this.edit.bind(this);
this.resetSmartPlug = this.resetSmartPlug.bind(this);
}
edit() {
console.log("editing device with id=" + this.props.id);
this.modalRef.current.openModal();
}
resetSmartPlug() {
this.props
2020-04-10 21:13:23 +00:00
.smartPlugReset(this.props.id)
2020-04-10 15:25:52 +00:00
.catch((err) => console.error(`Smart plug reset error`, err));
}
renderDeviceComponent() {
2020-04-22 18:14:18 +00:00
switch (this.props.stateOrDevice.kind) {
2020-04-10 15:25:52 +00:00
case "regularLight":
2020-04-18 14:26:12 +00:00
return <Light tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "sensor":
2020-04-18 14:26:12 +00:00
return <Sensor tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "motionSensor":
2020-04-18 14:26:12 +00:00
return <Sensor tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "buttonDimmer":
2020-04-18 14:26:12 +00:00
return <ButtonDimmer tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "knobDimmer":
2020-04-18 14:26:12 +00:00
return <KnobDimmer tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "smartPlug":
2020-04-18 14:26:12 +00:00
return <SmartPlug tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "switch":
2020-04-18 14:26:12 +00:00
return <Switcher tab={this.props.tab} id={this.props.id} />;
2020-04-10 15:25:52 +00:00
case "dimmableLight":
return <Light id={this.props.id} />;
2020-04-22 12:25:24 +00:00
case "securityCamera":
return <Videocam id={this.props.id} />;
2020-04-10 15:25:52 +00:00
default:
throw new Error("Device type unknown");
}
}
render() {
return (
<Segment>
2020-04-10 21:13:23 +00:00
<Grid columns={2}>
2020-04-12 15:49:29 +00:00
<Grid.Column>{this.renderDeviceComponent()}</Grid.Column>
{this.props.tab === "Devices" ? (
<Grid.Column textAlign="center">
<Header as="h3">{this.props.stateOrDevice.name}</Header>
2020-04-12 15:49:29 +00:00
<Button
color="blue"
2020-04-12 15:49:29 +00:00
icon
onClick={this.edit}
2020-04-12 15:49:29 +00:00
labelPosition="left"
>
<Icon name="pencil" />
Edit
2020-04-10 21:13:23 +00:00
</Button>
{this.props.stateOrDevice.kind === "smartPlug" ? (
<Button
color="orange"
icon
onClick={this.resetSmartPlug}
labelPosition="left"
>
<Icon name="undo" />
Reset
</Button>
) : null}
</Grid.Column>
) : (
<Grid.Column textAlign="center">
<Header as="h3">{this.props.stateOrDevice.name}</Header>
</Grid.Column>
)}
2020-04-10 21:13:23 +00:00
<DeviceSettingsModal ref={this.modalRef} id={this.props.id} />
</Grid>
2020-04-10 15:25:52 +00:00
</Segment>
);
}
}
const mapStateToProps = (state, ownProps) => ({
get stateOrDevice() {
if (state.active.activeTab === "Devices") {
return state.devices[ownProps.id];
} else {
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
2020-04-10 15:25:52 +00:00
});
const DeviceContainer = connect(mapStateToProps, RemoteService)(Device);
export default DeviceContainer;