diff --git a/smart-hut/src/client_server.js b/smart-hut/src/client_server.js index fa20cd7..08e5a67 100644 --- a/smart-hut/src/client_server.js +++ b/smart-hut/src/client_server.js @@ -233,7 +233,7 @@ export var call = { let promiseRes = axios.put(config + url, data, { headers: { Authorization: "Bearer " + tkn }, }); - + // also for btn/knob dimmer if (typeDevice === "switch/operate") { promiseRes = promiseRes.then((e) => { if (e.status === 200) { diff --git a/smart-hut/src/components/HeaderController.js b/smart-hut/src/components/HeaderController.js index acc4259..e85fd0d 100644 --- a/smart-hut/src/components/HeaderController.js +++ b/smart-hut/src/components/HeaderController.js @@ -1,12 +1,7 @@ import React from "react"; import { Dropdown, Icon, Grid, Divider } from "semantic-ui-react"; import { Segment, Image } from "semantic-ui-react"; -import { - BrowserView, - MobileView, - isBrowser, - isMobile, -} from "react-device-detect"; +import { BrowserView, MobileView } from "react-device-detect"; const AvatarImage = () => ( @@ -22,16 +17,6 @@ const IconHomeImage = () => ( /> ); -const IconHomeImageMobile = () => ( - -); - const TitleImage = () => ; const BrowserStructure = (props) => ( diff --git a/smart-hut/src/components/SelectIcons.js b/smart-hut/src/components/SelectIcons.js index bbefcc1..3722168 100644 --- a/smart-hut/src/components/SelectIcons.js +++ b/smart-hut/src/components/SelectIcons.js @@ -1,5 +1,5 @@ import React, { Component } from "react"; -import { Button, Grid, Responsive } from "semantic-ui-react"; +import { Button, Grid } from "semantic-ui-react"; export default class SelectIcons extends Component { constructor(props) { diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index 4473904..ccbe36f 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -56,7 +56,7 @@ export default class DevicePanel extends Component { this.forceUpdate(); }; - getDevices() { + getDevices = () => { if (this.props.activeItem === -1) { call .getAllDevices() @@ -82,7 +82,7 @@ export default class DevicePanel extends Component { }) .catch((err) => {}); } - } + }; async addDevice(data) { const ds = await this.props.addDevice(data); @@ -96,10 +96,8 @@ export default class DevicePanel extends Component { const roomId = this.props.devices.filter( (d) => d.id === this.state.settingsDeviceId )[0].roomId; - console.log(roomId); data["id"] = this.state.settingsDeviceId; data["roomId"] = roomId; - console.log(data); call .deviceUpdate(data) .then((res) => { @@ -159,6 +157,7 @@ export default class DevicePanel extends Component { return ( { case "regularLight": return ( { case "sensor": return ( { case "buttonDimmer": return ( { case "smartPlug": return ( { case "switch": return ( { case "dimmableLight": return ( this.setState(Object.assign(this.state, e)); + this.stateCallback = (e) => { + this.setState(Object.assign(this.state, e)); + }; call.socketSubscribe(this.props.device.id, this.stateCallback); } @@ -95,7 +97,7 @@ export default class Sensor extends Component { dy="0.3em" fontWeight="bold" > - {Math.round(this.state.value)} + {+(Math.round(this.state.value + "e+2") + "e-2")} {this.units} { + this.setState( + Object.assign(this.state, { + energyConsumed: (e.totalConsumption / 1000).toFixed(3), + turnedOn: e.on, + }) + ); + }; + + call.socketSubscribe(this.props.device.id, this.stateCallback); + } + + componentWillUnmount() { + call.socketUnsubscribe(this.props.device.id, this.stateCallback); } onClickDevice = () => { diff --git a/smart-hut/src/components/modalform.js b/smart-hut/src/components/modalform.js index 1486e4e..41951fd 100644 --- a/smart-hut/src/components/modalform.js +++ b/smart-hut/src/components/modalform.js @@ -108,16 +108,6 @@ export default class ModalWindow extends Component { )} - - {this.props.type === "new" ? ( - - - ADD ROOM - - ) : ( - - )} - {this.props.type === "new" ? ( diff --git a/smart-hut/src/views/Dashboard.js b/smart-hut/src/views/Dashboard.js index 4875a8c..f55894a 100644 --- a/smart-hut/src/views/Dashboard.js +++ b/smart-hut/src/views/Dashboard.js @@ -21,6 +21,7 @@ export default class Dashboard extends Component { rooms: [], activeItem: -1, devices: [], + image: "", tkn: this.props.tkn, }; @@ -51,6 +52,7 @@ export default class Dashboard extends Component { .getAllDevicesByRoom(this.state.activeItem) .then((res) => { if (res.status === 200) { + console.log(res.data); this.setState({ devices: res.data, }); @@ -98,6 +100,7 @@ export default class Dashboard extends Component { .then((res) => { //remove room in state.rooms this.getRooms(); + this.handleItemClick(-1); }) .catch((err) => { console.log(err); @@ -118,8 +121,6 @@ export default class Dashboard extends Component { } handleItemClick(id) { - // el -> obj of name and id - //da fare richiesta get della room e settare activeItem this.setState({ activeItem: id, }); @@ -166,6 +167,20 @@ export default class Dashboard extends Component { .catch((err) => {}); } + updateDeviceUi = (data) => { + let ds = this.state.devices; + ds.forEach((e) => { + if (e.id === data.id) { + e = data; + } + }); + this.setState({ + devices: ds, + }); + + return; + }; + render() { return ( @@ -179,6 +194,7 @@ export default class Dashboard extends Component { diff --git a/smart-hut/src/views/Navbar.js b/smart-hut/src/views/Navbar.js index f094e9a..ee521cb 100644 --- a/smart-hut/src/views/Navbar.js +++ b/smart-hut/src/views/Navbar.js @@ -21,6 +21,12 @@ class Navbar extends Component { }; } + componentDidMount() { + this.setState({ + activeItem: this.props.activeItem, + }); + } + editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode }));