From 6ecfe9653d367d469eff41f00be8e36fa8ac9d47 Mon Sep 17 00:00:00 2001 From: britea Date: Fri, 24 Apr 2020 23:34:01 +0200 Subject: [PATCH] fix turn on/off thermostat --- .../dashboard/devices/Thermostats.js | 287 +++++++++--------- 1 file changed, 145 insertions(+), 142 deletions(-) diff --git a/smart-hut/src/components/dashboard/devices/Thermostats.js b/smart-hut/src/components/dashboard/devices/Thermostats.js index cd3e461..a3870a9 100644 --- a/smart-hut/src/components/dashboard/devices/Thermostats.js +++ b/smart-hut/src/components/dashboard/devices/Thermostats.js @@ -4,135 +4,138 @@ import { RemoteService } from "../../../remote"; import { connect } from "react-redux"; import "./Thermostat.css"; import { - stateTag, - container, - deviceName, - targetTemperature, - slider, - line, - toggle, - stateTagContainer, + stateTag, + container, + deviceName, + targetTemperature, + slider, + line, + toggle, + stateTagContainer, } from "./ThermostatStyle"; //not quite working yet class Thermostats extends Component { - constructor(props) { - super(props); - this.state = { - targetTemperature: this.props.device.targetTemperature, - internalSensorTemperature: this.props.device - .internalSensorTemperature, - mode: this.props.device.mode, - measuredTemperature: this.props.device.measuredTemperature, - useExternalSensors: this.props.device.useExternalSensors, - timeout: null, - }; - this.setMode = this.setMode.bind(this); - this.setTargetTemperature = this.setTargetTemperature.bind(this); - this.setInternalSensorTemperature = this.setInternalSensorTemperature.bind( - this - ); - } - - //getters - get getMode() { - return this.props.device.mode; - } - - get getTargetTemperature() { - return this.props.device.targetTemperature; - } - - get getInternalSensorTemperature() { - return this.props.device.internalSensorTemperature; - } - - get getMeasuredTemperature() { - return this.props.device.measuredTemperature; - } - - get getUseExternalSensors() { - return this.props.device.useExternalSensors; - } - - setMode(mode) { - if (this.state.timeout) { - clearTimeout(this.state.timeout); - } - - //i came to the conclusion that is not possible to set mode. - this.mode = "HEATING"; - } - - onClickDevice = () => { - const on = !this.turnedOn; - this.props - .saveDevice({ ...this.props.device, on }) - .catch((err) => console.error("regular light update error", err)); + constructor(props) { + super(props); + this.state = { + targetTemperature: this.props.device.targetTemperature, + internalSensorTemperature: this.props.device.internalSensorTemperature, + mode: this.props.device.mode, + measuredTemperature: this.props.device.measuredTemperature, + useExternalSensors: this.props.device.useExternalSensors, + timeout: null, }; + this.setMode = this.setMode.bind(this); + this.setTargetTemperature = this.setTargetTemperature.bind(this); + this.setInternalSensorTemperature = this.setInternalSensorTemperature.bind( + this + ); + } - //It seems to work - saveTargetTemperature(targetTemperature) { - this.props - .saveDevice({ ...this.props.device, targetTemperature }) - .catch((err) => console.error(" update error", err)); + //getters + get getMode() { + return this.props.device.mode; + } + + get getTargetTemperature() { + return this.props.device.targetTemperature; + } + + get getInternalSensorTemperature() { + return this.props.device.internalSensorTemperature; + } + + get getMeasuredTemperature() { + return this.props.device.measuredTemperature; + } + + get getUseExternalSensors() { + return this.props.device.useExternalSensors; + } + + setMode(mode) { + if (this.state.timeout) { + clearTimeout(this.state.timeout); + } + console.log(mode); + + //i came to the conclusion that is not possible to set mode. + //this.mode = "HEATING"; + const turnOn = mode; + this.props + .saveDevice({ ...this.props.device, turnOn }) + .catch((err) => console.error("regular light update error", err)); + } + + onClickDevice = () => { + const on = !this.turnedOn; + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("regular light update error", err)); + }; + + //It seems to work + saveTargetTemperature(targetTemperature) { + this.props + .saveDevice({ ...this.props.device, targetTemperature }) + .catch((err) => console.error(" update error", err)); + } + + setTargetTemperature(newTemp) { + if (this.state.timeout) { + clearTimeout(this.state.timeout); } - setTargetTemperature(newTemp) { - if (this.state.timeout) { - clearTimeout(this.state.timeout); - } - + this.setState({ + newTemp, + timeout: setTimeout(() => { + this.saveTargetTemperature(newTemp); this.setState({ - newTemp, - timeout: setTimeout(() => { - this.saveTargetTemperature(newTemp); - this.setState({ - targetTemperature: this.state.targetTemperature, - timeout: null, - }); - }, 100), + targetTemperature: this.state.targetTemperature, + timeout: null, }); + }, 100), + }); + } + + //I have no idea why it doesn't want to update the temperature + saveInternalSensorTemperature(internalSensorTemperature) { + this.props + .saveDevice({ ...this.props.device, internalSensorTemperature }) + .catch((err) => console.error(" update error", err)); + } + + setInternalSensorTemperature(newTemp) { + if (this.state.timeout) { + clearTimeout(this.state.timeout); } - //I have no idea why it doesn't want to update the temperature - saveInternalSensorTemperature(internalSensorTemperature) { - this.props - .saveDevice({ ...this.props.device, internalSensorTemperature }) - .catch((err) => console.error(" update error", err)); - } - - setInternalSensorTemperature(newTemp) { - if (this.state.timeout) { - clearTimeout(this.state.timeout); - } - + this.setState({ + newTemp, + timeout: setTimeout(() => { + this.saveInternalSensorTemperature(newTemp); this.setState({ - newTemp, - timeout: setTimeout(() => { - this.saveInternalSensorTemperature(newTemp); - this.setState({ - internalSensorTemperature: this.state - .internalSensorTemperature, - timeout: null, - }); - }, 100), + internalSensorTemperature: this.state.internalSensorTemperature, + timeout: null, }); - } + }, 100), + }); + } - helperMode = () => { - //this.setMode("HEATING"); - this.setTargetTemperature(20); - //this.setInternalSensorTemperature(42); - }; + helperMode = () => { + //this.setMode("HEATING"); + this.setTargetTemperature(20); + //this.setInternalSensorTemperature(42); + }; - handleChange = (value) => { - this.setTargetTemperature(value); - }; + handleChange = (value) => { + this.setTargetTemperature(value); + }; - render() { - return ( - /*
+ render() { + return ( + /*

"mode is: "{this.props.device.mode}

"internalsensortemperature is: " @@ -159,42 +162,42 @@ class Thermostats extends Component { />

*/ -
-

{this.props.device.name}

-
- console.log("CHANGE", val.checked)} - /> +
+

{this.props.device.name}

+
+ this.setMode(val.checked)} + /> - - {this.props.device.targetTemperature}ºC - - this.handleChange(event.target.value)} - id="targetTemperature" - /> -
- {this.props.device.mode} -
-
- ); - } + + {this.props.device.targetTemperature}ºC + + this.handleChange(event.target.value)} + id="targetTemperature" + /> +
+ {this.props.device.mode} +
+
+ ); + } } const mapStateToProps = (state, ownProps) => ({ - device: state.devices[ownProps.id], + device: state.devices[ownProps.id], }); const ThermostatContainer = connect( - mapStateToProps, - RemoteService + mapStateToProps, + RemoteService )(Thermostats); export default ThermostatContainer;