/** 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.device.on; } get energyConsumed() { return (this.props.device.totalConsumption / 1000).toFixed(3); } onClickDevice = () => { const on = !this.turnedOn; this.props.saveDevice({ ...this.props.device, on }) .catch((err) => console.error('smart plug update error', err)); }; getIcon = () => { return this.turnedOn ? this.iconOn : this.iconOff; }; render() { return ( Smart Plug {this.energyConsumed} KWh ); } } const mapStateToProps = (state, ownProps) => ({ device: state.devices[ownProps.id], }); const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug); export default SmartPlugContainer;