Conditions to automations done (not connected to backend). I followed the same structure as in triggers

This commit is contained in:
Christian Capeáns Pérez 2020-05-15 20:57:42 +02:00
parent 364f58fa24
commit fa4bae7c10
2 changed files with 12775 additions and 549 deletions

View file

@ -192,10 +192,12 @@ class AutomationSaveModal extends Component {
super(props); super(props);
this.state = { this.state = {
triggerList: [], triggerList: [],
conditionsList: [],
order: [], order: [],
automationName: 'New Automation', automationName: 'New Automation',
editName: false, editName: false,
newTrigger: {}, newTrigger: {},
newCondition: {},
scenesFilter: null, scenesFilter: null,
openModal: false, openModal: false,
}; };
@ -226,13 +228,18 @@ class AutomationSaveModal extends Component {
this.setautomationName = this._setter('automationName'); this.setautomationName = this._setter('automationName');
this.setEditName = this._setter('editName'); this.setEditName = this._setter('editName');
this.setNewTrigger = this._setter('newTrigger'); this.setNewTrigger = this._setter('newTrigger');
this.addTrigger = this.addTrigger.bind(this); this.addTrigger = this.addTrigger.bind(this);
this.removeTrigger = this.removeTrigger.bind(this); this.removeTrigger = this.removeTrigger.bind(this);
this.onInputChange = this.onInputChange.bind(this); this.onInputChange = this.onInputChange.bind(this);
this.searchScenes = this.searchScenes.bind(this); this.searchScenes = this.searchScenes.bind(this);
this.orderScenes = this.orderScenes.bind(this); this.orderScenes = this.orderScenes.bind(this);
this.onChangeName = this.onChangeName.bind(this); this.onChangeName = this.onChangeName.bind(this);
//Conditions
this.setNewCondition = this._setter("newCondition");
this.addCondition = this.addCondition.bind(this);
this.removeCondition = this.removeCondition.bind(this);
} }
openModal = (e) => { openModal = (e) => {
@ -254,14 +261,15 @@ class AutomationSaveModal extends Component {
triggerKind(trigger) { triggerKind(trigger) {
if ('operand' in trigger && 'value' in trigger) { if ('operand' in trigger && 'value' in trigger) {
return 'rangeTrigger'; return 'rangeTrigger';
} if ('on' in trigger) { }
if ('on' in trigger) {
return 'booleanTrigger'; return 'booleanTrigger';
} }
return false; return false;
// throw new Error("Trigger kind not handled"); // throw new Error("Trigger kind not handled");
} }
_checkNewTrigger(trigger) { _checkNewTrigger(trigger, isCondition = false) {
const error = { const error = {
result: false, result: false,
message: 'There are missing fields!', message: 'There are missing fields!',
@ -298,7 +306,8 @@ class AutomationSaveModal extends Component {
) { ) {
error.message = "The value can't exceed 100, as it's a percentage"; error.message = "The value can't exceed 100, as it's a percentage";
return error; return error;
} if ( }
if (
deviceKind === 'sensor' deviceKind === 'sensor'
&& device.sensor === 'HUMIDITY' && device.sensor === 'HUMIDITY'
&& trigger.value > 100 && trigger.value > 100
@ -311,21 +320,29 @@ class AutomationSaveModal extends Component {
throw new Error('theoretically unreachable statement'); throw new Error('theoretically unreachable statement');
} }
const isNotDuplicate = !this.state.triggerList.some( let isNotDuplicate = null;
if(isCondition === true){
isNotDuplicate = !this.state.conditionsList.some(
(t) => t.device === trigger.device && t.operand === trigger.operand, (t) => t.device === trigger.device && t.operand === trigger.operand,
); );
}else{
isNotDuplicate = !this.state.triggerList.some(
(t) => t.device === trigger.device && t.operand === trigger.operand,
);
}
const type = isCondition ? "condition" : "trigger"
const duplicationMessage = `You have already created a ${type} for this device with the same conditions`;
return { return {
result: isNotDuplicate, result: isNotDuplicate,
message: isNotDuplicate message: isNotDuplicate
? null ? null
: 'You have already created a trigger for this device with the same conditions', : duplicationMessage
}; };
} }
addTrigger() { addTrigger() {
const {result, message} = this._checkNewTrigger(this.state.newTrigger); const {result, message} = this._checkNewTrigger(this.state.newTrigger);
if (result) { if (result) {
this.setState( this.setState(
update(this.state, { update(this.state, {
@ -478,6 +495,39 @@ Create new automation
); );
} }
// CONDITIONS
addCondition() {
// Same method used to check triggers and conditions, not a mistake
const {result, message} = this._checkNewTrigger(this.state.newCondition, true);
if (result) {
this.setState(
update(this.state, {
conditionsList: {$push: [this.state.newCondition]},
}),
);
} else {
alert(message);
}
}
removeCondition(index) {
this.setState(
update(this.state, {conditionsList: {$splice: [[index, 1]]}}),
);
}
onInputChangeCondition = (val) => {
if (val.name === 'device') {
this.setNewCondition({[val.name]: val.value});
} else {
this.setNewCondition({
...this.state.newCondition,
[val.name]: val.value,
});
}
}
render() { render() {
return ( return (
<Modal <Modal
@ -499,7 +549,6 @@ Create new automation
this.state.automationName this.state.automationName
)} )}
</Header> </Header>
<Button <Button
onClick={() => this.setEditName((prev) => !prev)} onClick={() => this.setEditName((prev) => !prev)}
style={{display: 'inline'}} style={{display: 'inline'}}
@ -578,6 +627,42 @@ Create new automation
</Grid.Row> </Grid.Row>
</Grid> </Grid>
</Segment> </Segment>
<Grid columns={1} stackable textAlign="center">
<Grid.Row verticalAlign="middle">
<Grid.Column>
<Header>Add Conditions</Header>
<List divided relaxed>
{this.state.conditionsList.length > 0
&& this.state.conditionsList.map((condition, i) => {
const deviceName = this.deviceList.filter(
(d) => d.id === condition.device,
)[0].name;
const key = this._generateKey(condition);
return (
<Trigger
key={key}
index={i}
deviceName={deviceName}
trigger={condition}
onRemove={this.removeCondition}
/>
);
})}
<CreateTrigger
devices={this.props.devices}
inputChange={this.onInputChangeCondition}
/>
</List>
<Button
onClick={this.addCondition}
circular
icon="add"
color="blue"
size="huge"
/>
</Grid.Column>
</Grid.Row>
</Grid>
<Grid> <Grid>
<Grid.Row> <Grid.Row>
<Grid.Column style={{marginRight: '1rem'}}> <Grid.Column style={{marginRight: '1rem'}}>

12141
smart-hut/yarn.lock Normal file

File diff suppressed because it is too large Load diff