Merge branch '71-6-users-can-add-conditions-to-automations' into 'dev'

Resolve "6. Users can add conditions to automations"

Closes #71

See merge request sa4-2020/the-sanmarinoes/frontend!152
This commit is contained in:
Andrea Brites Marto 2020-05-21 16:17:17 +02:00
commit 9df8a7f19a
4 changed files with 12965 additions and 569 deletions

File diff suppressed because it is too large Load diff

View file

@ -17,7 +17,7 @@ import CreateAutomation, { operands } from './AutomationCreationModal';
const Automation = ({ const Automation = ({
automation, devices, scenes, removeAutomation, automation, devices, scenes, removeAutomation,
}) => { }) => {
const { triggers } = automation; const { triggers, conditions } = automation;
const scenePriorities = automation.scenes; const scenePriorities = automation.scenes;
const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text; const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text;
@ -85,6 +85,36 @@ const Automation = ({
</Grid.Row> </Grid.Row>
</Grid> </Grid>
</Segment> </Segment>
<Grid columns={1} stackable textAlign="center">
<Grid.Row verticalAlign="middle">
<Grid.Column>
<Header>Conditions</Header>
<List divided relaxed>
{conditions !== undefined
&& conditions.map((condition) => {
const device = devices.filter(
(d) => d.id === condition.deviceId,
)[0];
return (
<Menu key={condition.id} compact>
<Menu.Item as="span">
{device.name}
{' '}
{condition.operator
? `${getOperator(condition.operator)
} ${
condition.mode ? condition.mode : condition.range}`
: condition.on
? ' - on'
: ' - off'}
</Menu.Item>
</Menu>
);
})}
</List>
</Grid.Column>
</Grid.Row>
</Grid>
</> </>
); );
}; };

View file

@ -556,19 +556,31 @@ export const RemoteService = {
* with user-fiendly errors as a RemoteError * with user-fiendly errors as a RemoteError
*/ */
saveAutomation: (data) => { saveAutomation: (data) => {
const { automation, triggerList, order } = data; const {
automation, triggerList, order, conditionList,
} = data;
automation.triggers = []; automation.triggers = [];
automation.scenes = []; automation.scenes = [];
automation.condition = [];
return (dispatch) => { return (dispatch) => {
const urlAutomation = '/automation'; const urlAutomation = '/automation';
const urlBooleanTrigger = '/booleanTrigger'; const urlBooleanTrigger = '/booleanTrigger';
const urlRangeTrigger = '/rangeTrigger'; const urlRangeTrigger = '/rangeTrigger';
const urlScenePriority = '/scenePriority'; const urlScenePriority = '/scenePriority';
// conditions
const urlRangeCondition = '/rangeCondition';
const urlBooleanCondition = '/booleanCondition';
const urlThermostatCondition = '/thermostatCondition';
const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger); const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger);
const booleanTriggerList = triggerList.filter( const booleanTriggerList = triggerList.filter(
(trigger) => !('operand' in trigger), (trigger) => !('operand' in trigger),
); );
const rangeConditionList = conditionList.filter((condition) => 'operand' in condition && 'value' in condition);
const booleanConditionList = conditionList.filter((condition) => 'on' in condition);
debugger;
const thermostatConditionList = conditionList.filter((condition) => 'operand' in condition && 'mode' in condition);
return Endpoint.post(urlAutomation, {}, automation).then( return Endpoint.post(urlAutomation, {}, automation).then(
async (automationRes) => { async (automationRes) => {
@ -591,7 +603,7 @@ export const RemoteService = {
const trigger = { const trigger = {
automationId: id, automationId: id,
deviceId: t.device, deviceId: t.device,
on: t.value, on: t.on,
}; };
resBoolTriggers.push(Endpoint.post( resBoolTriggers.push(Endpoint.post(
urlBooleanTrigger, urlBooleanTrigger,
@ -601,6 +613,50 @@ export const RemoteService = {
} }
automation.triggers.push(...((await Promise.all(resBoolTriggers)).map((v) => v.data))); automation.triggers.push(...((await Promise.all(resBoolTriggers)).map((v) => v.data)));
// Conditions
const resRangeConditions = [];
for (const t of rangeConditionList) {
const condition = {
automationId: id,
deviceId: t.device,
operator: t.operand,
range: t.value,
};
resRangeConditions.push(Endpoint.post(urlRangeCondition, {}, condition));
}
automation.conditions = (await Promise.all(resRangeConditions)).map((v) => v.data);
const resBoolConditions = [];
for (const t of booleanConditionList) {
const condition = {
automationId: id,
deviceId: t.device,
on: t.on,
};
resBoolConditions.push(Endpoint.post(
urlBooleanCondition,
{},
condition,
));
}
automation.conditions.push(...((await Promise.all(resBoolConditions)).map((v) => v.data)));
const resThermoConditions = [];
for (const t of thermostatConditionList) {
const condition = {
automationId: id,
deviceId: t.device,
mode: t.mode,
operator: t.operand,
};
resThermoConditions.push(Endpoint.post(
urlThermostatCondition,
{},
condition,
));
}
automation.conditions.push(...((await Promise.all(resThermoConditions)).map((v) => v.data)));
const resScenePriorities = []; const resScenePriorities = [];
for (const [priority, sceneId] of order.entries()) { for (const [priority, sceneId] of order.entries()) {
const scenePriority = { const scenePriority = {

12141
smart-hut/yarn.lock Normal file

File diff suppressed because it is too large Load diff