2020-03-15 14:50:25 +00:00
|
|
|
/**
|
|
|
|
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.
|
2020-03-17 22:24:40 +00:00
|
|
|
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.
|
2020-03-15 14:50:25 +00:00
|
|
|
**/
|
2020-03-25 16:20:53 +00:00
|
|
|
import React, { Component } from "react";
|
2020-03-26 08:36:00 +00:00
|
|
|
import {
|
|
|
|
BottomPanel,
|
2020-04-11 11:57:03 +00:00
|
|
|
StyledDiv
|
2020-03-26 08:36:00 +00:00
|
|
|
} from "./styleComponents";
|
2020-03-26 08:51:25 +00:00
|
|
|
import { Image } from "semantic-ui-react";
|
2020-03-25 16:20:53 +00:00
|
|
|
import {
|
|
|
|
energyConsumedStyle,
|
|
|
|
imageStyle,
|
|
|
|
kwhStyle,
|
|
|
|
nameStyle,
|
|
|
|
} from "./SmartPlugStyle";
|
2020-04-10 15:25:52 +00:00
|
|
|
import { RemoteService } from "../../../remote";
|
|
|
|
import { connect } from "react-redux";
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-04-10 15:25:52 +00:00
|
|
|
class SmartPlug extends Component {
|
2020-03-23 20:24:17 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.iconOn = "/img/smart-plug.svg";
|
|
|
|
this.iconOff = "/img/smart-plug-off.svg";
|
2020-04-10 22:42:23 +00:00
|
|
|
}
|
2020-03-24 17:17:04 +00:00
|
|
|
|
2020-04-10 22:42:23 +00:00
|
|
|
get turnedOn() {
|
|
|
|
return this.props.device.on;
|
2020-03-24 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:23 +00:00
|
|
|
get energyConsumed() {
|
|
|
|
return (this.props.device.totalConsumption / 1000).toFixed(3);
|
2020-03-25 15:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:23 +00:00
|
|
|
onClickDevice = () => {
|
|
|
|
const on = !this.turnedOn;
|
|
|
|
this.props.saveDevice({ ...this.props.device, on })
|
|
|
|
.catch((err) => console.error('smart plug update error', err));
|
2020-03-26 08:36:00 +00:00
|
|
|
};
|
|
|
|
|
2020-03-23 20:24:17 +00:00
|
|
|
getIcon = () => {
|
2020-04-10 22:42:23 +00:00
|
|
|
return this.turnedOn ? this.iconOn : this.iconOff;
|
2020-03-23 20:24:17 +00:00
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-23 20:24:17 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2020-04-10 15:25:52 +00:00
|
|
|
<StyledDiv onClick={this.onClickDevice}>
|
2020-03-23 20:24:17 +00:00
|
|
|
<Image src={this.getIcon()} style={imageStyle} />
|
2020-03-25 23:15:02 +00:00
|
|
|
<span style={nameStyle}>
|
2020-04-10 22:42:23 +00:00
|
|
|
Smart Plug
|
2020-03-25 23:15:02 +00:00
|
|
|
</span>
|
2020-03-25 11:13:15 +00:00
|
|
|
|
2020-03-25 16:20:53 +00:00
|
|
|
<BottomPanel
|
|
|
|
style={
|
2020-04-10 22:42:23 +00:00
|
|
|
this.turnedOn
|
2020-03-25 16:20:53 +00:00
|
|
|
? { backgroundColor: "#505bda" }
|
|
|
|
: { backgroundColor: "#1a2849" }
|
|
|
|
}
|
|
|
|
>
|
2020-04-10 22:42:23 +00:00
|
|
|
<span style={energyConsumedStyle}>{this.energyConsumed}</span>
|
2020-03-25 16:20:53 +00:00
|
|
|
<span style={kwhStyle}>KWh</span>
|
2020-03-25 11:13:15 +00:00
|
|
|
</BottomPanel>
|
2020-03-23 20:24:17 +00:00
|
|
|
</StyledDiv>
|
2020-03-25 16:20:53 +00:00
|
|
|
);
|
2020-03-23 20:24:17 +00:00
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
|
|
|
device: state.devices[ownProps.id],
|
|
|
|
});
|
|
|
|
const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug);
|
|
|
|
export default SmartPlugContainer;
|