diff --git a/smart-hut/src/components/SimulationPanel.js b/smart-hut/src/components/SimulationPanel.js index aa518ae..3031388 100644 --- a/smart-hut/src/components/SimulationPanel.js +++ b/smart-hut/src/components/SimulationPanel.js @@ -4,6 +4,7 @@ import { Divider, Image, Modal, + Button, } from 'semantic-ui-react'; import { connect } from 'react-redux'; import { RemoteService } from '../remote'; @@ -265,6 +266,36 @@ class SimulationPanel extends Component { value: null, error: null, }; + this.updateSliderValues = this.updateSliderValues.bind(this); + this.saveChanges = this.saveChanges.bind(this); + } + + updateSliderValues(type, data, motion) { + console.log(data, motion); + this.setState({ + [type ? 'error' : 'value']: data, + }); + } + + saveChanges(device) { + console.log(this.state); + const { value } = this.state; + const { error } = this.state; + if (device.kind === 'sensor') { + this.props + .updateSimulation( + { ...device, typical: value, err: error }, + ) + .catch((err) => console.error('Simulation panel update error', err)); + } else { + const val = device.kind === 'motionSensor' ? 'detected' : 'typical'; + this.props + .saveDevice( + { ...device, [val]: value, err: error }, + null, + ) + .catch((err) => console.error('Simulation panel update error', err)); + } } renderImage(device) { @@ -320,14 +351,21 @@ class SimulationPanel extends Component { { this.props.devices - ? this.props.devices.map((e, i) => ( - + ? this.props.devices.map((d, i) => ( + - {this.renderImage(e)} + {this.renderImage(d)}

- {e.name} + {d.name}

+
@@ -339,10 +377,10 @@ class SimulationPanel extends Component { marginLeft: '30%', }} > - + { - e.kind === 'motionSensor' + d.kind === 'motionSensor' ? null : ( <> @@ -353,7 +391,7 @@ class SimulationPanel extends Component { marginLeft: '30%', }} > - + ) diff --git a/smart-hut/src/components/SimulationPanelSlider.js b/smart-hut/src/components/SimulationPanelSlider.js index 8384b64..e278655 100644 --- a/smart-hut/src/components/SimulationPanelSlider.js +++ b/smart-hut/src/components/SimulationPanelSlider.js @@ -1,6 +1,6 @@ import React, { Component } from 'react'; import { - Form, Grid, Header, Checkbox, + Form, Grid, Checkbox, Message, Label, } from 'semantic-ui-react'; import { connect } from 'react-redux'; import { RemoteService } from '../remote'; @@ -11,16 +11,17 @@ class SimulationPanelSlider extends Component { console.log(this.props.device); this.state = { value: this.internalValue, - error: null, + error: this.internalValue, }; this.updateSliderValues = this.updateSliderValues.bind(this); } updateSliderValues(data) { - console.log(data); this.setState({ [this.props.error ? 'error' : 'value']: data, }); + console.log(this.state); + this.props.update(this.props.error, data, this.props.device.kind === 'motionSensor'); } settings() { @@ -47,13 +48,20 @@ class SimulationPanelSlider extends Component { } get internalValue() { - switch (this.props.device.kind) { - case 'sensor': - return this.props.device.value.toFixed(2); - case 'thermostat': - return this.props.device.internalSensorTemperature; - case 'motionSensor': + if (this.props.device.kind === 'motionSensor') { return this.props.device.detected; + } + return this.props.error ? this.props.device.err : this.props.device.typical.toFixed(2); + } + + get getValue() { + switch (this.props.device.kind) { + case 'motionSensor': + return this.props.device.detected; + case 'thermostat': + return this.props.device.internalSensorTemperature; + case 'sensor': + return this.props.device.value.toFixed(2); default: return ''; } @@ -63,40 +71,51 @@ class SimulationPanelSlider extends Component { return ( -
{this.props.error ? 'Edit error' : 'Edit value'}
-

- {this.props.device.name} -, - {this.internalValue} -

+ + {this.props.error ? 'Edit error' : 'Edit value'} +

+ Actual value of + {' '} + {this.props.device.name} +: + {this.props.device.kind === 'motionSensor' + ? this.getValue ? 'on' : 'off' : this.getValue} +

+

+ {this.props.error ? 'error' : 'typical value'} + {' '} +: + {this.props.device.kind === 'motionSensor' + ? this.internalValue ? 'on' : 'off' : this.internalValue} +

+
{ this.props.device.kind === 'motionSensor' ? ( this.updateSliderValues(checked)} checked={this.state.value} + onChange={(e, { checked }) => this.updateSliderValues(checked)} /> -) + ) : ( - this.updateSliderValues(value)} - step={this.settings().step} - type="range" - value={this.state.value} - /> -) + <> + + this.updateSliderValues(value)} + step={this.settings().step} + type="range" + value={this.props.error ? this.state.error : this.state.value} + /> + + ) }
diff --git a/smart-hut/src/components/dashboard/AutomationCreationModal.js b/smart-hut/src/components/dashboard/AutomationCreationModal.js index 1cad28d..15563c3 100644 --- a/smart-hut/src/components/dashboard/AutomationCreationModal.js +++ b/smart-hut/src/components/dashboard/AutomationCreationModal.js @@ -653,6 +653,7 @@ class AutomationSaveModal extends Component { } render() { + console.log(this.state.conditionsList); return ( (dispatch) => { + const url = `/sensor/${data.id}/simulation`; + return Endpoint.put(url, {}, data) + .then((res) => { + dispatch(actions.deviceSave(res.data)); + return res.data; + }) + .catch((err) => { + console.warn('Update device: ', data, 'error: ', err); + throw new RemoteError(['Network error']); + }); + }, + fastUpdateAutomation: (automation) => (dispatch) => Endpoint.put('/automation/fast', {}, automation) .then((res) => dispatch(actions.automationSave(res.data))) .catch((err) => {