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 } from "semantic-ui-react"; 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); 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 ( ); case "thermostat": return ( ); case "regularLight": return ( ); case "sensor": return ( ); case "motionSensor": return ; case "buttonDimmer": return ( ); case "knobDimmer": return ( ); case "smartPlug": return ( ); case "switch": return ( ); case "dimmableLight": return ; case "securityCamera": return ( ); default: //throw new Error("Device type unknown"); return undefined; } } render() { { if (this.props.type !== "") { return ( {this.renderDeviceComponent()} {this.props.tab === "Devices" ? (
{this.props.stateOrDevice.name}
{this.props.stateOrDevice.kind === "smartPlug" ? ( ) : null}
) : (
{this.props.device.name}
{this.props.tab === "Scenes" ? (
{this.props.roomName}
) : ( "" )}
)}
{this.props.stateOrDevice && this.props.tab === "Devices" ? ( ) : ( "" )}
); } else { return null; } } } } /* {this.props.stateOrDevice ? : "" } */ 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() { console.log("ALPACA", state, ownProps); 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;