Merge branch '106-rangetrigger-values-must-be-validated-to-a-sensible-range-for-the-respective-device' into 'dev'

Check values for different types of sensors when creating a trigger for an automation

Closes #106

See merge request sa4-2020/the-sanmarinoes/frontend!139
This commit is contained in:
Claudio Maggioni 2020-05-08 16:07:59 +02:00
commit 0ec8dccb25
6 changed files with 127 additions and 57 deletions

View file

@ -77,14 +77,20 @@ export class MyHeader extends React.Component {
<Button basic inverted onClick={this.logout}>
Logout
</Button>
<Segment compact style={{margin: "auto", marginTop: "1em", textAlign: "center"}}>
<Checkbox
label={<label
>Share cameras</label>}
checked={this.props.cameraEnabled}
toggle
onChange={(e, val) => this.setCameraEnabled(val.checked)}
/>
<Segment
compact
style={{
margin: "auto",
marginTop: "1em",
textAlign: "center",
}}
>
<Checkbox
label={<label>Share cameras</label>}
checked={this.props.cameraEnabled}
toggle
onChange={(e, val) => this.setCameraEnabled(val.checked)}
/>
</Segment>
</Grid.Column>
</Grid.Row>
@ -108,14 +114,20 @@ export class MyHeader extends React.Component {
</Label>
<Divider />
<Button onClick={this.logout}>Logout</Button>
<Segment compact style={{margin: "auto", marginTop: "1em", textAlign: "center"}}>
<Checkbox
label={<label
>Share cameras</label>}
checked={this.props.cameraEnabled}
toggle
onChange={(e, val) => this.setCameraEnabled(val.checked)}
/>
<Segment
compact
style={{
margin: "auto",
marginTop: "1em",
textAlign: "center",
}}
>
<Checkbox
label={<label>Share cameras</label>}
checked={this.props.cameraEnabled}
toggle
onChange={(e, val) => this.setCameraEnabled(val.checked)}
/>
</Segment>
</Grid.Column>
</Grid.Row>

View file

@ -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" ? (
<Form.Field>
<Segment compact style={{ marginBottom: "1rem"}}>
<Checkbox
label="Enable guest access"
checked={this.state.guestAccessEnabled}
toggle
onChange={(e, val) =>
this.setGuestAccessEnabled(val.checked)
}
/>
<Segment compact style={{ marginBottom: "1rem" }}>
<Checkbox
label="Enable guest access"
checked={this.state.guestAccessEnabled}
toggle
onChange={(e, val) =>
this.setGuestAccessEnabled(val.checked)
}
/>
</Segment>
</Form.Field>
) : null}

View file

@ -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) => {
<Form.Field inline width={2}>
<Dropdown
onChange={(e, val) => props.inputChange(val)}
ref={operandsRef}
name="operand"
compact
selection
@ -102,7 +109,10 @@ const CreateTrigger = (props) => {
</Form.Field>
<Form.Field inline width={7}>
<Input
onChange={(e, val) => 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)

View file

@ -161,7 +161,6 @@ const mapStateToProps = (state, _) => ({
return Object.values(state.devices);
},
get automations() {
console.log(state.automations);
return Object.values(state.automations);
},
});

View file

@ -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" &&

View file

@ -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,