diff --git a/smart-hut/src/components/dashboard/ScenesPanel.js b/smart-hut/src/components/dashboard/ScenesPanel.js index 6a56428..ee840c2 100644 --- a/smart-hut/src/components/dashboard/ScenesPanel.js +++ b/smart-hut/src/components/dashboard/ScenesPanel.js @@ -3,19 +3,32 @@ import { connect } from "react-redux"; import { RemoteService } from "../../remote"; import Device from "./devices/Device"; import NewSceneDevice from "./NewSceneDevice"; -import { Grid } from "semantic-ui-react"; +import { Grid, Button } from "semantic-ui-react"; class ScenesPanel extends Component { constructor(props) { super(props); + + this.applyScene = this.applyScene.bind(this); + } + + applyScene() { + console.log(this.props.activeScene); + this.props.sceneApply(this.props.activeScene); } render() { return ( + {!this.props.isActiveDefaultScene ? ( + + + + ) : null} {!this.props.isActiveDefaultScene ? this.props.sceneStates.map((e, i) => { - console.log(this.props.sceneStates); return ( diff --git a/smart-hut/src/components/dashboard/devices/Thermostats.js b/smart-hut/src/components/dashboard/devices/Thermostats.js index 0a1e086..d66e6ae 100644 --- a/smart-hut/src/components/dashboard/devices/Thermostats.js +++ b/smart-hut/src/components/dashboard/devices/Thermostats.js @@ -20,7 +20,8 @@ class Thermostats extends Component { super(props); this.state = { targetTemperature: this.props.stateOrDevice.targetTemperature, - internalSensorTemperature: this.props.stateOrDevice.internalSensorTemperature, + internalSensorTemperature: this.props.stateOrDevice + .internalSensorTemperature, mode: this.props.stateOrDevice.mode, measuredTemperature: this.props.stateOrDevice.measuredTemperature, useExternalSensors: this.props.stateOrDevice.useExternalSensors, @@ -63,34 +64,43 @@ class Thermostats extends Component { //i came to the conclusion that is not possible to set mode. //this.mode = "HEATING"; const turnOn = mode; - if(this.props.tab==="Devices"){ + if (this.props.tab === "Devices") { this.props - .saveDevice({ ...this.props.stateOrDevice, turnOn }) - .catch((err) => console.error("thermostat update error", err)); - }else{ - this.props.updateState({ id: this.props.sceneState.id, turnOn: turnOn },this.props.sceneState.kind); + .saveDevice({ ...this.props.stateOrDevice, turnOn }) + .catch((err) => console.error("thermostat update error", err)); + } else { + this.props.updateState( + { id: this.props.sceneState.id, turnOn: turnOn }, + this.props.sceneState.kind + ); } } onClickDevice = () => { const on = !this.turnedOn; - if(this.props.tab==="Devices"){ + if (this.props.tab === "Devices") { this.props - .saveDevice({ ...this.props.stateOrDevice, on }) - .catch((err) => console.error("thermostat update error", err)); - }else{ - this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind); + .saveDevice({ ...this.props.stateOrDevice, on }) + .catch((err) => console.error("thermostat update error", err)); + } else { + this.props.updateState( + { id: this.props.sceneState.id, on: on }, + this.props.sceneState.kind + ); } }; //It seems to work saveTargetTemperature(targetTemperature) { - if(this.props.tab==="Devices"){ + if (this.props.tab === "Devices") { this.props - .saveDevice({ ...this.props.stateOrDevice, targetTemperature }) - .catch((err) => console.error("thermostat update error", err)); - }else{ - this.props.updateState({id: this.props.sceneState.id, targetTemperature: targetTemperature},this.props.sceneState.kind); + .saveDevice({ ...this.props.stateOrDevice, targetTemperature }) + .catch((err) => console.error("thermostat update error", err)); + } else { + this.props.updateState( + { id: this.props.sceneState.id, targetTemperature: targetTemperature }, + this.props.sceneState.kind + ); } } @@ -113,12 +123,18 @@ class Thermostats extends Component { //I have no idea why it doesn't want to update the temperature saveInternalSensorTemperature(internalSensorTemperature) { - if(this.props.tab==="Devices"){ + if (this.props.tab === "Devices") { this.props - .saveDevice({ ...this.props.device, internalSensorTemperature }) - .catch((err) => console.error("thermostat update error", err)); - }else{ - this.props.updateState({ id: this.props.sceneState.id, internalSensorTemperature:internalSensorTemperature },this.props.sceneState.kind); + .saveDevice({ ...this.props.device, internalSensorTemperature }) + .catch((err) => console.error("thermostat update error", err)); + } else { + this.props.updateState( + { + id: this.props.sceneState.id, + internalSensorTemperature: internalSensorTemperature, + }, + this.props.sceneState.kind + ); } } @@ -151,7 +167,6 @@ class Thermostats extends Component { render() { return ( -

{this.props.stateOrDevice.name}

@@ -183,12 +198,11 @@ class Thermostats extends Component { } const mapStateToProps = (state, ownProps) => ({ - get stateOrDevice(){ - if(state.active.activeTab==="Devices"){ + get stateOrDevice() { + if (state.active.activeTab === "Devices") { return state.devices[ownProps.id]; - }else{ - const sceneState = state.sceneStates[ownProps.id]; - return state.devices[sceneState]; + } else { + return state.sceneStates[ownProps.id]; } }, }); diff --git a/smart-hut/src/components/dashboard/devices/Videocam.js b/smart-hut/src/components/dashboard/devices/Videocam.js index f541b8b..889233f 100644 --- a/smart-hut/src/components/dashboard/devices/Videocam.js +++ b/smart-hut/src/components/dashboard/devices/Videocam.js @@ -40,7 +40,7 @@ class Videocam extends Component {
- + { - dispatch(actions.stateSave(res.data)); + dispatch(actions.stateUpdate(res.data)); return res.data; }) .catch((err) => { @@ -396,33 +396,33 @@ export const RemoteService = { }; }, - deleteState: (id, type)=>{ - return (dispatch)=>{ + deleteState: (id, type) => { + return (dispatch) => { let url; if (type === "dimmableState") { url = "/dimmableState"; } else { url = "/switchableState"; } - return Endpoint.delete(url+`/${id}`) - .then((_) => dispatch(actions.stateDelete(id))) - .catch((err) => { - console.warn("state delete error", err); - throw new RemoteError(["Network error"]); - }); + return Endpoint.delete(url + `/${id}`) + .then((_) => dispatch(actions.stateDelete(id))) + .catch((err) => { + console.warn("state delete error", err); + throw new RemoteError(["Network error"]); + }); }; }, - sceneApply: (id)=>{ - return (dispatch)=>{ - let url=`/scene/${id}/apply`; + sceneApply: (id) => { + return (dispatch) => { + let url = `/scene/${id}/apply`; return Endpoint.post(url) - .then((res)=> dispatch(actions.sceneApply(id))) - .catch((err)=>{ - console.warn('scene apply error',err); - throw new RemoteError(["Network error"]); - }); + .then((res) => dispatch(actions.sceneApply(res.data))) + .catch((err) => { + console.warn("scene apply error", err); + throw new RemoteError(["Network error"]); + }); }; }, diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 466d6a3..0e91ef8 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -105,7 +105,7 @@ function reducer(previousState, action) { createOrUpdateScene(scene); } break; - case "STATE_UPDATE": + case "STATES_UPDATE": //console.log(action.sceneStates); newState = previousState; change = null; @@ -164,6 +164,9 @@ function reducer(previousState, action) { newState = update(newState, change); + break; + case "STATE_UPDATE": + //update the state break; case "DEVICES_UPDATE": change = null; @@ -368,7 +371,9 @@ function reducer(previousState, action) { sceneStates: { $unset: [action.stateId] }, }; - if (previousState.scenes[previousState.sceneStates[action.stateId].sceneId]) { + if ( + previousState.scenes[previousState.sceneStates[action.stateId].sceneId] + ) { change.scenes = { [previousState.sceneStates[action.stateId].sceneId]: { sceneStates: { $remove: [action.stateId] }, @@ -380,12 +385,12 @@ function reducer(previousState, action) { break; case "SCENE_APPLY": + console.log(action); //checking that the scene actually exists - if (!(action.sceneId in previousState.scenes)) { + /*if (!(action.sceneId in previousState.scenes)) { console.warn(`Scene ${action.sceneId} does not exist`); break; - } - + }*/ break; case "DEVICE_DELETE": diff --git a/smart-hut/src/storeActions.js b/smart-hut/src/storeActions.js index 84213ae..8c14f83 100644 --- a/smart-hut/src/storeActions.js +++ b/smart-hut/src/storeActions.js @@ -30,21 +30,25 @@ const actions = { sceneState, }), statesUpdate: (sceneId, sceneStates) => ({ - type: "STATE_UPDATE", + type: "STATES_UPDATE", sceneId, sceneStates, }), + stateUpdate: (state) => ({ + type: "STATE_UPDATE", + state, + }), devicesUpdate: (roomId, devices, partial = false) => ({ type: "DEVICES_UPDATE", roomId, devices, partial, }), - stateDelete: (stateId)=>({ + stateDelete: (stateId) => ({ type: "STATE_DELETE", stateId, }), - sceneApply: (sceneId)=>({ + sceneApply: (sceneId) => ({ type: "SCENE_APPLY", sceneId, }),