frontend/smart-hut/src/components/dashboard/devices/SmartPlug.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
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.
2020-05-12 13:18:33 +00:00
* */
import React, { Component } from 'react';
import { Image } from 'semantic-ui-react';
import { connect } from 'react-redux';
import { BottomPanel, StyledDiv } from './styleComponents';
2020-03-25 16:20:53 +00:00
import {
energyConsumedStyle,
imageStyle,
kwhStyle,
nameStyle,
2020-05-12 13:18:33 +00:00
} from './SmartPlugStyle';
import { RemoteService } from '../../../remote';
import mapStateToProps from '../../../deviceProps';
2020-04-10 15:25:52 +00:00
class SmartPlug extends Component {
2020-03-23 20:24:17 +00:00
constructor(props) {
super(props);
2020-05-12 13:18:33 +00:00
this.iconOn = '/img/smart-plug.svg';
this.iconOff = '/img/smart-plug-off.svg';
}
get turnedOn() {
return this.props.stateOrDevice.on;
}
get energyConsumed() {
2020-05-02 11:58:02 +00:00
return (this.props.device.totalConsumption / 1000).toFixed(3);
2020-03-25 15:04:02 +00:00
}
onClickDevice = () => {
const on = !this.turnedOn;
2020-05-12 13:18:33 +00:00
if (this.props.tab === 'Devices') {
2020-04-25 13:37:40 +00:00
this.props
2020-04-27 13:47:59 +00:00
.saveDevice({ ...this.props.stateOrDevice, on })
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('smart plug update error', err));
2020-04-27 13:47:59 +00:00
} else {
this.props.updateState(
2020-05-12 13:18:33 +00:00
{ id: this.props.stateOrDevice.id, on },
this.props.stateOrDevice.kind,
2020-04-27 13:47:59 +00:00
);
2020-04-25 13:37:40 +00:00
}
2020-03-26 08:36:00 +00:00
};
2020-05-12 13:18:33 +00:00
getIcon = () => (this.turnedOn ? this.iconOn : this.iconOff);
2020-03-23 20:24:17 +00:00
render() {
return (
2020-05-04 09:13:19 +00:00
<StyledDiv onClick={this.props.disabled ? () => {} : this.onClickDevice}>
2020-03-23 20:24:17 +00:00
<Image src={this.getIcon()} style={imageStyle} />
2020-04-12 15:49:29 +00:00
<span style={nameStyle}>Smart Plug</span>
2020-03-25 16:20:53 +00:00
<BottomPanel
style={
this.turnedOn
2020-05-12 13:18:33 +00:00
? { backgroundColor: '#505bda' }
: { backgroundColor: '#1a2849' }
2020-03-25 16:20:53 +00:00
}
>
<span style={energyConsumedStyle}>{this.energyConsumed}</span>
2020-03-25 16:20:53 +00:00
<span style={kwhStyle}>KWh</span>
</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-04-10 15:25:52 +00:00
const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug);
export default SmartPlugContainer;