/** A smart plug is a plug that has a boolean internal state, i.e., that can be turned on or off, either with the SmartHut interface or by a switch. The smart plug also stores the total energy consumed while the plug is active, in terms of kilowatt-hours 2(kWh) . The user can reset this value. **/ import React, { Component } from "react"; import { BottomPanel, StyledDiv } from "./styleComponents"; import { Image } from "semantic-ui-react"; import { energyConsumedStyle, imageStyle, kwhStyle, nameStyle, } from "./SmartPlugStyle"; import { RemoteService } from "../../../remote"; import { connect } from "react-redux"; class SmartPlug extends Component { constructor(props) { super(props); this.iconOn = "/img/smart-plug.svg"; this.iconOff = "/img/smart-plug-off.svg"; } get turnedOn() { return this.props.stateOrDevice.on; } get energyConsumed() { return (this.props.stateOrDevice.totalConsumption / 1000).toFixed(3); } onClickDevice = () => { const on = !this.turnedOn; if(this.props.tab==="Devices"){ this.props .saveDevice({ ...this.props.stateOrDevice, on }) .catch((err) => console.error("smart plug update error", err)); }else{ this.props.updateState({ id: this.props.sceneState.id, on: on},this.props.sceneState.kind); } }; getIcon = () => { return this.turnedOn ? this.iconOn : this.iconOff; }; render() { return ( Smart Plug {this.energyConsumed} KWh ); } } const mapStateToProps = (state, ownProps) => ({ get stateOrDevice(){ if(state.active.activeTab==="Devices"){ return state.devices[ownProps.id]; }else{ return state.sceneStates[ownProps.id]; } }, }); const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug); export default SmartPlugContainer;