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-23 14:02:02 +00:00
|
|
|
import Curtains from "./Curtain";
|
|
|
|
import Thermostat from "./Thermostats";
|
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 {
|
2020-04-23 14:55:44 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-04-10 15:25:52 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
this.modalRef = React.createRef();
|
|
|
|
this.edit = this.edit.bind(this);
|
|
|
|
this.resetSmartPlug = this.resetSmartPlug.bind(this);
|
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
edit() {
|
|
|
|
console.log("editing device with id=" + this.props.id);
|
|
|
|
this.modalRef.current.openModal();
|
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
resetSmartPlug() {
|
|
|
|
this.props
|
|
|
|
.smartPlugReset(this.props.id)
|
|
|
|
.catch((err) => console.error(`Smart plug reset error`, err));
|
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
renderDeviceComponent() {
|
2020-04-25 12:46:49 +00:00
|
|
|
console.log("check here")
|
|
|
|
console.log(this.props.stateOrDevice.kind)
|
2020-04-23 14:55:44 +00:00
|
|
|
switch (this.props.stateOrDevice.kind) {
|
|
|
|
case "curtains":
|
|
|
|
return <Curtains tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "thermostat":
|
|
|
|
return <Thermostat tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "regularLight":
|
|
|
|
return <Light tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "sensor":
|
|
|
|
return <Sensor tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "motionSensor":
|
|
|
|
return <Sensor tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "buttonDimmer":
|
|
|
|
return <ButtonDimmer tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "knobDimmer":
|
|
|
|
return <KnobDimmer tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "smartPlug":
|
|
|
|
return <SmartPlug tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "switch":
|
|
|
|
return <Switcher tab={this.props.tab} id={this.props.id} />;
|
|
|
|
case "dimmableLight":
|
|
|
|
return <Light id={this.props.id} />;
|
|
|
|
case "securityCamera":
|
|
|
|
return <Videocam id={this.props.id} />;
|
|
|
|
default:
|
|
|
|
throw new Error("Device type unknown");
|
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Segment>
|
|
|
|
<Grid columns={2}>
|
|
|
|
<Grid.Column>{this.renderDeviceComponent()}</Grid.Column>
|
|
|
|
{this.props.tab === "Devices" ? (
|
|
|
|
<Grid.Column textAlign="center">
|
|
|
|
<Header as="h3">
|
|
|
|
{this.props.stateOrDevice.name}
|
|
|
|
</Header>
|
|
|
|
<Button
|
|
|
|
color="blue"
|
|
|
|
icon
|
|
|
|
onClick={this.edit}
|
|
|
|
labelPosition="left"
|
|
|
|
>
|
|
|
|
<Icon name="pencil" />
|
|
|
|
Edit
|
|
|
|
</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-21 12:58:36 +00:00
|
|
|
|
2020-04-23 14:55:44 +00:00
|
|
|
<DeviceSettingsModal
|
|
|
|
ref={this.modalRef}
|
|
|
|
id={this.props.id}
|
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
</Segment>
|
|
|
|
);
|
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
2020-04-23 14:55:44 +00:00
|
|
|
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;
|