diff --git a/smart-hut/src/components/dashboard/devices/Curtain.js b/smart-hut/src/components/dashboard/devices/Curtain.js index 54e98df..0348e5c 100644 --- a/smart-hut/src/components/dashboard/devices/Curtain.js +++ b/smart-hut/src/components/dashboard/devices/Curtain.js @@ -23,9 +23,13 @@ class Curtain extends Component { onClickDevice = () => { const on = !this.turnedOn; - this.props - .saveDevice({ ...this.props.device, on }) - .catch((err) => console.error("curtains update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("curtains update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } }; setIntensity(intensity) { @@ -49,9 +53,13 @@ class Curtain extends Component { saveIntensity = () => { const intensity = Math.round(this.state.intensity); - this.props - .saveDevice({ ...this.props.device, intensity }) - .catch((err) => console.error("curtains update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, intensity }) + .catch((err) => console.error("curtain update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } }; helper = () => { diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index 3a66170..b8d4acf 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -59,9 +59,14 @@ class Light extends Component { onClickDevice = () => { const on = !this.turnedOn; - this.props - .saveDevice({ ...this.props.device, on }) - .catch((err) => console.error("regular light update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("regular light update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } + }; getIcon = () => { @@ -89,9 +94,13 @@ class Light extends Component { saveIntensity = () => { const intensity = Math.round(this.state.intensity); - this.props - .saveDevice({ ...this.props.device, intensity }) - .catch((err) => console.error("intensity light update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, intensity }) + .catch((err) => console.error("regular light update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } }; render() { @@ -150,5 +159,6 @@ class Light extends Component { const mapStateToProps = (state, ownProps) => ({ device: state.devices[ownProps.id], }); + const LightContainer = connect(mapStateToProps, RemoteService)(Light); export default LightContainer; diff --git a/smart-hut/src/components/dashboard/devices/SmartPlug.js b/smart-hut/src/components/dashboard/devices/SmartPlug.js index a180c28..7a53886 100644 --- a/smart-hut/src/components/dashboard/devices/SmartPlug.js +++ b/smart-hut/src/components/dashboard/devices/SmartPlug.js @@ -33,9 +33,13 @@ class SmartPlug extends Component { onClickDevice = () => { const on = !this.turnedOn; - this.props - .saveDevice({ ...this.props.device, on }) - .catch((err) => console.error("smart plug update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("smart plug update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } }; getIcon = () => { diff --git a/smart-hut/src/components/dashboard/devices/Thermostats.js b/smart-hut/src/components/dashboard/devices/Thermostats.js index 1fd69cb..b08f2eb 100644 --- a/smart-hut/src/components/dashboard/devices/Thermostats.js +++ b/smart-hut/src/components/dashboard/devices/Thermostats.js @@ -63,23 +63,35 @@ class Thermostats extends Component { //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)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, turnOn }) + .catch((err) => console.error("thermostat update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } } onClickDevice = () => { const on = !this.turnedOn; - this.props - .saveDevice({ ...this.props.device, on }) - .catch((err) => console.error("regular light update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("thermostat update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } }; //It seems to work saveTargetTemperature(targetTemperature) { - this.props - .saveDevice({ ...this.props.device, targetTemperature }) - .catch((err) => console.error(" update error", err)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, targetTemperature }) + .catch((err) => console.error("thermostat update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } } setTargetTemperature(newTemp) { @@ -101,9 +113,13 @@ class Thermostats extends Component { //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)); + if(this.props.tab==="Devices"){ + this.props + .saveDevice({ ...this.props.device, internalSensorTemperature }) + .catch((err) => console.error("thermostat update error", err)); + }else{ + this.props.updateState({ ...this.props.device, on },this.props.type); + } } setInternalSensorTemperature(newTemp) { diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 6ea0454..2b6f730 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -374,6 +374,28 @@ export const RemoteService = { }; }, + // + updateState: (data,type)=>{ + return (dispatch) => { + let url; + if(type=="dimmableState"){ + url="/dimmableState" + }else{ + url="/switchableState" + } + + return Endpoint["put"](url, {}, data) + .then((res) => { + dispatch(actions.stateSave(res.data)); + return res.data; + }) + .catch((err) => { + console.warn("Update device: ", data, "error: ", err); + throw new RemoteError(["Network error"]); + }); + }; + }, + /** * Creates/Updates a device with the given data. If * data.id is truthy, then a update call is performed,