diff --git a/smart-hut/src/components/HeaderController.js b/smart-hut/src/components/HeaderController.js index f67edfe..47954c9 100644 --- a/smart-hut/src/components/HeaderController.js +++ b/smart-hut/src/components/HeaderController.js @@ -77,14 +77,20 @@ export class MyHeader extends React.Component { - - Share cameras} - checked={this.props.cameraEnabled} - toggle - onChange={(e, val) => this.setCameraEnabled(val.checked)} - /> + + Share cameras} + checked={this.props.cameraEnabled} + toggle + onChange={(e, val) => this.setCameraEnabled(val.checked)} + /> @@ -108,14 +114,20 @@ export class MyHeader extends React.Component { - - Share cameras} - checked={this.props.cameraEnabled} - toggle - onChange={(e, val) => this.setCameraEnabled(val.checked)} - /> + + Share cameras} + checked={this.props.cameraEnabled} + toggle + onChange={(e, val) => this.setCameraEnabled(val.checked)} + /> diff --git a/smart-hut/src/components/SceneModal.js b/smart-hut/src/components/SceneModal.js index 8b935cc..d9cce65 100644 --- a/smart-hut/src/components/SceneModal.js +++ b/smart-hut/src/components/SceneModal.js @@ -115,11 +115,11 @@ class SceneModal extends Component { }; openModal = (e) => { - this.setState({ ...this.state, openModal: true }); + this.setState({ ...this.state, openModal: true }); }; updateIcon(e) { - this.setState({ ...this.state, selectedIcon: e }); + this.setState({ ...this.state, selectedIcon: e }); } setCopyFrom(_, copyFrom) { @@ -128,7 +128,7 @@ class SceneModal extends Component { setGuestAccessEnabled(val) { console.log(this.state, val); - this.setState({ ...this.state, guestAccessEnabled: val }); + this.setState({ ...this.state, guestAccessEnabled: val }); } render() { @@ -224,15 +224,15 @@ class SceneModal extends Component { )} {this.type === "modify" ? ( - - - this.setGuestAccessEnabled(val.checked) - } - /> + + + this.setGuestAccessEnabled(val.checked) + } + /> ) : null} diff --git a/smart-hut/src/components/dashboard/AutomationCreationModal.js b/smart-hut/src/components/dashboard/AutomationCreationModal.js index 867f677..e0ef5f6 100644 --- a/smart-hut/src/components/dashboard/AutomationCreationModal.js +++ b/smart-hut/src/components/dashboard/AutomationCreationModal.js @@ -1,4 +1,4 @@ -import React, { Component, useState } from "react"; +import React, { Component, useState, useRef } from "react"; import { connect } from "react-redux"; import { RemoteService } from "../../remote"; import update from "immutability-helper"; @@ -51,6 +51,8 @@ const deviceStateOptions = [ const CreateTrigger = (props) => { const [activeOperand, setActiveOperand] = useState(true); + const operandsRef = useRef(null); + const valuesRef = useRef(null); const notAdmitedDevices = ["buttonDimmer"]; const hasOperand = new Set([ "knobDimmer", @@ -72,6 +74,10 @@ const CreateTrigger = (props) => { const onChange = (e, val) => { props.inputChange(val); setActiveOperand(hasOperand.has(props.devices[val.value].kind)); + + if (operandsRef.current) operandsRef.current.setValue(""); + if (valuesRef.current) + valuesRef.current.inputRef.current.valueAsNumber = undefined; }; return ( @@ -94,6 +100,7 @@ const CreateTrigger = (props) => { props.inputChange(val)} + ref={operandsRef} name="operand" compact selection @@ -102,7 +109,10 @@ const CreateTrigger = (props) => { props.inputChange(val)} + onChange={(e, val) => { + props.inputChange(val); + }} + ref={valuesRef} name="value" type="number" placeholder="Value" @@ -207,7 +217,10 @@ class AutomationSaveModal extends Component { }, trigger.kind === "booleanTrigger" ? { on: trigger.on } - : { operand: trigger.operator, value: trigger.value } + : { + operand: trigger.operator, + value: trigger.value, + } ) ); } @@ -245,29 +258,62 @@ class AutomationSaveModal extends Component { } triggerKind(trigger) { - if ("on" in trigger) { - return "booleanTrigger"; - } else if ("operand" in trigger && "value" in trigger) { + if ("operand" in trigger && "value" in trigger) { return "rangeTrigger"; + } else if ("on" in trigger) { + return "booleanTrigger"; } else { - throw new Error("Trigger kind not handled"); + return false; + //throw new Error("Trigger kind not handled"); } } _checkNewTrigger(trigger) { - // Check for missing fields for creation const error = { result: false, message: "There are missing fields!", }; + let device = Object.values(this.props.devices).filter( + (d) => d.id === trigger.device + )[0]; - switch (this.triggerKind(trigger)) { + let triggerKind = this.triggerKind(trigger); + + if (!device || !triggerKind) { + error.message = "There are missing fields"; + return error; + } + let deviceKind = device.kind; + const devicesWithPercentage = ["dimmableLight", "curtains", "knobDimmer"]; + + switch (triggerKind) { case "booleanTrigger": if (!trigger.device || trigger.on === null || trigger.on === undefined) return error; break; case "rangeTrigger": - if (!trigger.device || !trigger.operand || !trigger.value) return error; + if (!trigger.device || !trigger.operand || !trigger.value) { + return error; + } + if (trigger.value < 0) { + error.message = "Values cannot be negative"; + return error; + } + // If the device's range is a percentage, values cannot exceed 100 + else if ( + devicesWithPercentage.includes(deviceKind) && + trigger.value > 100 + ) { + error.message = "The value can't exceed 100, as it's a percentage"; + return error; + } else if ( + deviceKind === "sensor" && + device.sensor === "HUMIDITY" && + trigger.value > 100 + ) { + error.message = "The value can't exceed 100, as it's a percentage"; + return error; + } break; default: throw new Error("theoretically unreachable statement"); @@ -290,7 +336,9 @@ class AutomationSaveModal extends Component { if (result) { this.setState( - update(this.state, { triggerList: { $push: [this.state.newTrigger] } }) + update(this.state, { + triggerList: { $push: [this.state.newTrigger] }, + }) ); } else { alert(message); @@ -305,7 +353,14 @@ class AutomationSaveModal extends Component { // This gets triggered when the devices dropdown changes the value. onInputChange(val) { - this.setNewTrigger({ ...this.state.newTrigger, [val.name]: val.value }); + if (val.name === "device") { + this.setNewTrigger({ [val.name]: val.value }); + } else { + this.setNewTrigger({ + ...this.state.newTrigger, + [val.name]: val.value, + }); + } } onChangeName(_, val) { @@ -392,14 +447,16 @@ class AutomationSaveModal extends Component { deviceId: trigger.device, kind, }, - kind + kind === "booleanTrigger" ? { on: trigger.on } - : { operator: trigger.operand, value: trigger.value } + : { + operator: trigger.operand, + range: parseInt(trigger.value), + } ) ); } - console.log(automation); this.props .fastUpdateAutomation(automation) .then(this.closeModal) diff --git a/smart-hut/src/components/dashboard/AutomationsPanel.js b/smart-hut/src/components/dashboard/AutomationsPanel.js index 9d6c972..8530213 100644 --- a/smart-hut/src/components/dashboard/AutomationsPanel.js +++ b/smart-hut/src/components/dashboard/AutomationsPanel.js @@ -161,7 +161,6 @@ const mapStateToProps = (state, _) => ({ return Object.values(state.devices); }, get automations() { - console.log(state.automations); return Object.values(state.automations); }, }); diff --git a/smart-hut/src/components/dashboard/devices/Sensor.js b/smart-hut/src/components/dashboard/devices/Sensor.js index 4206d8c..2583df0 100644 --- a/smart-hut/src/components/dashboard/devices/Sensor.js +++ b/smart-hut/src/components/dashboard/devices/Sensor.js @@ -57,13 +57,6 @@ class Sensor extends Component { this.name = "Sensor"; } - // setName = () => { - // if (this.props.device.name.length > 15) { - // return this.props.device.name.slice(0, 12) + "..."; - // } - // return this.props.device.name; - // }; - componentDidUpdate(prevProps) { if ( this.props.stateOrDevice.kind === "sensor" && diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index cbc8ba2..ecfa94a 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -93,7 +93,9 @@ function reducer(previousState, action) { newState = update(previousState, { login: { $set: action.login } }); break; case "USER_INFO_UPDATE": - newState = update(previousState, { userInfo: { $set: action.userInfo } }); + newState = update(previousState, { + userInfo: { $set: action.userInfo }, + }); break; case "ROOMS_UPDATE": newState = previousState; @@ -138,7 +140,9 @@ function reducer(previousState, action) { // and remove any join between that scene and deleted // sceneStates change = { - scenes: { [action.sceneId]: { sceneStates: { $set: new Set() } } }, + scenes: { + [action.sceneId]: { sceneStates: { $set: new Set() } }, + }, sceneStates: { $unset: [] }, }; @@ -199,7 +203,9 @@ function reducer(previousState, action) { // devices if (action.roomId) { change = { - rooms: { [action.roomId]: { devices: { $set: new Set() } } }, + rooms: { + [action.roomId]: { devices: { $set: new Set() } }, + }, devices: { $unset: [] }, }; @@ -369,7 +375,9 @@ function reducer(previousState, action) { case "AUTOMATION_SAVE": change = { - automations: { [action.automation.id]: { $set: action.automation } }, + automations: { + [action.automation.id]: { $set: action.automation }, + }, }; newState = update(previousState, change); @@ -378,7 +386,9 @@ function reducer(previousState, action) { case "STATE_SAVE": change = { - sceneStates: { [action.sceneState.id]: { $set: action.sceneState } }, + sceneStates: { + [action.sceneState.id]: { $set: action.sceneState }, + }, }; if (previousState.scenes[action.sceneState.sceneId]) { @@ -528,7 +538,6 @@ function reducer(previousState, action) { } return a; }, {}); - console.log(devices); newState = reducer(previousState, { type: "DEVICES_UPDATE", partial: true,