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"; import Videocam from "./Videocam"; import Curtains from "./Curtain"; import Thermostat from "./Thermostats"; import { Segment, Grid, Header, Button, Icon, Card } from "semantic-ui-react"; import { RemoteService } from "../../../remote"; import { connect } from "react-redux"; import DeviceSettingsModal from "./DeviceSettingsModal"; const centerComponent = { marginLeft: "50%", transform: "translateX(-50%)", marginTop: "10%", marginBottom: "10%", }; 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); this.deleteState = this.deleteState.bind(this); } edit() { console.log("editing device with id=" + this.props.id); this.modalRef.current.openModal(); } resetSmartPlug() { this.props .smartPlugReset(this.props.id) .catch((err) => console.error(`Smart plug reset error`, err)); } deleteState() { //console.log("alpaca "+this.props); this.props.deleteState(this.props.id, this.props.type); } renderDeviceComponent() { switch ( this.props.tab === "Devices" ? this.props.stateOrDevice.kind : this.props.type ) { case "curtains": return ( <Curtains tab={this.props.tab} sceneState={this.props.sceneState} id={this.props.id} /> ); case "thermostat": return ( <Thermostat tab={this.props.tab} sceneState={this.props.sceneStat} id={this.props.stateOrDevice.id} /> ); case "regularLight": return ( <Light tab={this.props.tab} sceneState={this.props.sceneState} id={this.props.stateOrDevice.id} /> ); case "sensor": return ( <Sensor tab={this.props.tab} sceneState={this.props.sceneState} id={this.props.stateOrDevice.id} /> ); case "motionSensor": return <Sensor tab={this.props.tab} id={this.props.stateOrDevice.id} />; case "buttonDimmer": return ( <ButtonDimmer tab={this.props.tab} id={this.props.stateOrDevice.id} /> ); case "knobDimmer": return ( <KnobDimmer tab={this.props.tab} id={this.props.stateOrDevice.id} /> ); case "smartPlug": return ( <SmartPlug tab={this.props.tab} id={this.props.stateOrDevice.id} /> ); case "switch": return ( <Switcher tab={this.props.tab} id={this.props.stateOrDevice.id} /> ); case "dimmableLight": return <Light id={this.props.stateOrDevice.id} tab={this.props.tab} />; case "securityCamera": return ( <Videocam id={this.props.stateOrDevice.id} tab={this.props.tab} /> ); default: throw new Error("Device type unknown"); } } deviceDescription() { return ( <div className="ui two buttons"> <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} </div> ); } stateDescription() { return ( <div class="ui one button"> <Button color="red" icon onClick={this.deleteState} labelPosition="left" > <Icon name="undo" /> Delete </Button> </div> ); } get deviceName() { return this.props.tab === "Devices" ? this.props.stateOrDevice.name : this.props.device.name; } render() { { return ( <Card> <Card.Content> <Card.Header textAlign="center"> <Header as="h3">{this.deviceName}</Header> {this.props.tab === "Scenes" ? ( <Header as="h4">{this.props.roomName}</Header> ) : ( "" )} </Card.Header> <Card.Description style={centerComponent}> {this.renderDeviceComponent()} </Card.Description> </Card.Content> <Card.Content extra> {this.props.tab === "Devices" ? this.deviceDescription() : this.stateDescription()} </Card.Content> {this.props.tab === "Devices" ? ( <DeviceSettingsModal ref={this.modalRef} id={this.props.stateOrDevice.id} /> ) : null} </Card> ); } } } const mapStateToProps = (state, ownProps) => ({ get stateOrDevice() { if (state.active.activeTab === "Devices") { return state.devices[ownProps.id]; } else { return state.sceneStates[ownProps.id]; } }, get device() { if (state.active.activeTab === "Devices") { return state.devices[ownProps.id]; } else { return state.devices[state.sceneStates[ownProps.id].deviceId]; } }, get roomName() { if (state.active.activeTab === "Scenes") { const device = state.devices[state.sceneStates[ownProps.id].deviceId]; return state.rooms[device.roomId].name; } else { return ""; } }, get type() { if (state.active.activeTab === "Scenes") { if (state.sceneStates[ownProps.id]) { //console.log(state.sceneStates[ownProps.id], ownProps.id); const id = state.sceneStates[ownProps.id].deviceId; //console.log(id, state.devices[id].kind); return state.devices[id].kind; } else { return ""; } } else { return null; } }, }); const DeviceContainer = connect(mapStateToProps, RemoteService)(Device); export default DeviceContainer;