From f579ea80fe49d9e6735d4316c6f8ee5e1f37a2d3 Mon Sep 17 00:00:00 2001 From: britea Date: Mon, 23 Mar 2020 19:08:31 +0100 Subject: [PATCH] addes switches creation and update --- smart-hut/src/client_server.js | 13 +++++- smart-hut/src/components/SelectIcons.js | 1 - .../src/components/dashboard/DevicePanel.js | 4 +- .../dashboard/devices/DeviceTypeController.js | 4 +- .../src/components/dashboard/devices/Light.js | 2 +- .../components/dashboard/devices/NewDevice.js | 10 ++++- .../components/dashboard/devices/Sensor.js | 40 +++++++++++++++---- .../components/dashboard/devices/SmartPlug.js | 13 +++++- .../components/dashboard/devices/Switch.js | 23 ++++++++++- smart-hut/src/views/Dashboard.js | 2 +- smart-hut/src/views/Navbar.js | 1 - 11 files changed, 92 insertions(+), 21 deletions(-) diff --git a/smart-hut/src/client_server.js b/smart-hut/src/client_server.js index 67eb2d0..4d56e34 100644 --- a/smart-hut/src/client_server.js +++ b/smart-hut/src/client_server.js @@ -63,13 +63,22 @@ export var call = { return axios.post(config + 'room', data, { headers: { Authorization : "Bearer " + tkn } }) }, updateRoom: function(data, headers) { - return axios.put(config + 'room?name='+ data.name, data, { headers: { Authorization : "Bearer " + tkn } }) + return axios.put(config + 'room/'+ data.id, data, { headers: { Authorization : "Bearer " + tkn } }) }, deleteRoom: function(data, headers) { return axios.delete(config + 'room/'+data.id, { headers: { Authorization : "Bearer " + tkn } }); }, devicePost: function(data, headers) { return axios.post(config + data.device, data.params, { headers: { Authorization : "Bearer " + tkn } }) + .then(res => { + if (res.status === 200 && (data.device === "switch")) { + data.params.lights.forEach(e => { + let urlUp = config + data.device + '/' + res.data.id + '/lights?switchableId=' + e; + axios.post(urlUp, {}, { headers: { Authorization : "Bearer " + tkn } }) + }); + } + return res; + }) }, deviceUpdate: function(data, typeDevice) { let url = 'device'; @@ -79,7 +88,7 @@ export var call = { return axios.put(config + url, data, { headers: { Authorization : "Bearer " + tkn } }) }, deviceDelete: function(data, headers) { - return axios.delete(config + data.device + '/' + data.id, {}, { headers: { Authorization : "Bearer " + tkn } }) + return axios.delete(config + data.device + '/' + data.id, { headers: { Authorization : "Bearer " + tkn } }) }, deviceGetById: function(data, headers) { return axios.get(config + data.device + '/' + data.id) diff --git a/smart-hut/src/components/SelectIcons.js b/smart-hut/src/components/SelectIcons.js index f1a7994..eaf671d 100644 --- a/smart-hut/src/components/SelectIcons.js +++ b/smart-hut/src/components/SelectIcons.js @@ -13,7 +13,6 @@ export default class SelectIcons extends Component { selectIcon = (e) => { let el = e.target.name; if (e.target.tagName === "I"){ - console.log(e.target.parentNode); el = e.target.parentNode.name; } this.props.updateIcon(el); diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index bca0212..c70f8b7 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -108,7 +108,6 @@ export default class DevicePanel extends Component { removeDevice = () => { const item = this.props.devices.filter(d => d.id === this.state.settingsDeviceId)[0]; - console.log(item) const data = { device: item.kind, id: this.state.settingsDeviceId @@ -116,9 +115,10 @@ export default class DevicePanel extends Component { call.deviceDelete(data) .then(res => { - console.log(res) if (res.status === 200) { + this.openModal(); this.getDevices(); + this.forceUpdate(); } }).catch(err => { diff --git a/smart-hut/src/components/dashboard/devices/DeviceTypeController.js b/smart-hut/src/components/dashboard/devices/DeviceTypeController.js index 8f21cfa..0ff8741 100644 --- a/smart-hut/src/components/dashboard/devices/DeviceTypeController.js +++ b/smart-hut/src/components/dashboard/devices/DeviceTypeController.js @@ -12,9 +12,9 @@ const DeviceType = (props) => { return () case "sensor": return - case "dimmer": + case "buttonDimmer": return - case "smartplug": + case "smartPlug": return case "switch": return diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index 64460ff..952a6b9 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -99,7 +99,7 @@ export default class Light extends Component { deviceId={this.props.device.id} edit={this.props.edit} onChangeData={(id, newSettings) => this.props.onChangeData(id, newSettings)}/> - {this.props.device.intensity ? (intensityLightView) : (normalLightView)} + {this.props.device.intensity >= 0 ? (intensityLightView) : (normalLightView)} ) } diff --git a/smart-hut/src/components/dashboard/devices/NewDevice.js b/smart-hut/src/components/dashboard/devices/NewDevice.js index e3ddf2a..d3a6a03 100644 --- a/smart-hut/src/components/dashboard/devices/NewDevice.js +++ b/smart-hut/src/components/dashboard/devices/NewDevice.js @@ -79,9 +79,17 @@ export default class NewDevice extends Component { case "dimmableLight": data.params["intensity"] = 1; break; + case "sensor": + data.params["sensor"] = this.state.typeOfSensor; + data.params["value"] = 0; + break; + case "switch": + data.params["lights"] = this.state.lightsAttached; + break; default: break; } + console.log(data) this.props.addDevice(data); this.resetState(); }; @@ -122,7 +130,7 @@ export default class NewDevice extends Component { { key: 'dimmer', text: 'Dimmer', - value: 'dimmer', + value: 'buttonDimmer', image: {avatar: true, src: '/img/dimmer.svg'}, }, ]; diff --git a/smart-hut/src/components/dashboard/devices/Sensor.js b/smart-hut/src/components/dashboard/devices/Sensor.js index 23cbc51..d2168b1 100644 --- a/smart-hut/src/components/dashboard/devices/Sensor.js +++ b/smart-hut/src/components/dashboard/devices/Sensor.js @@ -6,9 +6,22 @@ * For this story, make the sensors return a constant value with some small random error. */ + /* + + OPTIONAL STATE + error: 2.4 + + + ±{this.state.error} + + + + errorStyle, + */ + import React, {Component} from "react"; import {CircularInput, CircularProgress, CircularTrack} from "react-circular-input"; -import {errorStyle, sensorText, style, valueStyle} from "./SensorStyle"; +import { sensorText, style, valueStyle} from "./SensorStyle"; import Settings from "./DeviceSettings"; import {StyledDiv} from "./styleComponents"; @@ -17,10 +30,9 @@ export default class Sensor extends Component { super(props); this.state = { turnedOn: false, - value: 20, - error: 2.4 + value: 0, }; - this.units = "ºC" + this.units = "" } setName = () => { @@ -31,6 +43,23 @@ export default class Sensor extends Component { }; componentDidMount() { + switch(this.props.device.sensor) { + case "TEMPERATURE": + this.units = "ºC"; + break; + case "HUMIDITY" : + this.units = "%"; + break; + case "LIGHT": + this.units = "lm"; + break; + default: + this.units = ""; + + } + this.setState({ + value : this.props.device.value + }); } @@ -51,9 +80,6 @@ export default class Sensor extends Component { {Math.round(this.state.value)}{this.units} - - ±{this.state.error} - {this.setName()} diff --git a/smart-hut/src/components/dashboard/devices/SmartPlug.js b/smart-hut/src/components/dashboard/devices/SmartPlug.js index 0c07ba4..658e6e8 100644 --- a/smart-hut/src/components/dashboard/devices/SmartPlug.js +++ b/smart-hut/src/components/dashboard/devices/SmartPlug.js @@ -9,6 +9,7 @@ import {StyledDiv} from "./styleComponents"; import Settings from "./DeviceSettings"; import {Image} from "semantic-ui-react"; import {energyConsumedStyle, imageStyle, nameStyle} from "./SmartPlugStyle"; +import { call } from '../../../client_server'; export default class SmartPlug extends Component { constructor(props){ @@ -22,7 +23,13 @@ export default class SmartPlug extends Component { } onClickDevice = () => { - this.setState((prevState) => ({turnedOn: !prevState.turnedOn})); + this.props.device.on = !this.state.turnedOn; + call.deviceUpdate(this.props.device, 'smartPlug') + .then(res => { + if (res.status === 200 ){ + this.setState((prevState) => ({turnedOn: !prevState.turnedOn})); + } + }) }; getIcon = () => { @@ -33,6 +40,10 @@ export default class SmartPlug extends Component { }; componentDidMount() { + this.setState({ + turnedOn : this.props.device.on, + energyConsumed : this.props.device.totalConsumption + }) } render(){ diff --git a/smart-hut/src/components/dashboard/devices/Switch.js b/smart-hut/src/components/dashboard/devices/Switch.js index 20080b3..ea41295 100644 --- a/smart-hut/src/components/dashboard/devices/Switch.js +++ b/smart-hut/src/components/dashboard/devices/Switch.js @@ -10,6 +10,7 @@ import {StyledDiv} from "./styleComponents"; import Settings from "./DeviceSettings"; import {Image} from "semantic-ui-react"; import {imageStyle, nameStyle} from "./SwitchStyle"; +import { call } from '../../../client_server'; export default class Switch extends Component { constructor(props){ @@ -23,7 +24,23 @@ export default class Switch extends Component { } onClickDevice = () => { - this.setState((prevState) => ({turnedOn: !prevState.turnedOn})); + this.props.device.on = !this.state.turnedOn; + let state = ""; + if(this.props.device.on) { + state = "ON" + } else { + state = "OFF" + } + let data = { + "type" : state, + "id" : this.props.device.id + } + call.deviceUpdate(data, 'switch/operate') + .then(res => { + if (res.status === 200 ){ + this.setState((prevState) => ({turnedOn: !prevState.turnedOn})); + } + }) }; getIcon = () => { @@ -34,7 +51,9 @@ export default class Switch extends Component { }; componentDidMount() { - + this.setState({ + turnedOn: this.props.device.on + }) } render(){ diff --git a/smart-hut/src/views/Dashboard.js b/smart-hut/src/views/Dashboard.js index f35e947..686ccab 100644 --- a/smart-hut/src/views/Dashboard.js +++ b/smart-hut/src/views/Dashboard.js @@ -99,7 +99,7 @@ getRooms() { } updateRoom(data) { - console.log(data) + data.id = this.state.activeItem; call.updateRoom(data) .then(res => { if (res.status === 200 && res.data) { diff --git a/smart-hut/src/views/Navbar.js b/smart-hut/src/views/Navbar.js index 7e65b38..24d0c65 100644 --- a/smart-hut/src/views/Navbar.js +++ b/smart-hut/src/views/Navbar.js @@ -17,7 +17,6 @@ class Navbar extends Component { editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode })); handleClick = (e, {id, name}) => { - console.log(id); let obj = undefined; this.props.rooms.forEach((e) => { if (e.id === id) {