From e83e074fe852f1a9abe73eadec78018db4d8ef77 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 25 Nov 2021 12:36:19 -0500 Subject: [PATCH] mini sequence diagram --- .../SequenceNodes/index.stories.tsx | 104 +++++++++++++++++- .../src/components/SequenceNodes/location.tsx | 78 +++++++++---- 2 files changed, 162 insertions(+), 20 deletions(-) diff --git a/webapp/src/components/SequenceNodes/index.stories.tsx b/webapp/src/components/SequenceNodes/index.stories.tsx index 4d2e719..6397855 100644 --- a/webapp/src/components/SequenceNodes/index.stories.tsx +++ b/webapp/src/components/SequenceNodes/index.stories.tsx @@ -17,12 +17,13 @@ export default { } } as ComponentMeta -export const Simple: ComponentStory = args => +const Template: ComponentStory = args => +export const Simple = Template.bind({}) Simple.args = { ...Simple.args, sequence: [ @@ -66,3 +67,104 @@ Simple.args = { } ] } + +export const Multinode = Template.bind({}) +Multinode.args = { + ...Multinode.args, + startLoc: [0,0], + sequence: [ + { + $smType: "condition", + condition: 'and', + condition_data: { + conditions: [ + { + $smType: 'condition', + condition: 'numeric_state', + condition_data: { + entity_id: 'sensor.temperature_kitchen', + below: '15', + } + }, + { + $smType: "condition", + condition: 'template', + condition_data: { + value_template: 'states(switch.kitchen_light) == "on"' + } + } + ] + } + }, + { + $smType: "action", + action: "choose", + action_data: { + choose: [ + { + conditions: [ + { + $smType: "condition", + condition: 'time', + condition_data: { + after: { + hours: 16, + minutes: 30 + } + } + } + ], + sequence: [ + { + $smType: 'action', + action: 'event', + action_data: { + event: "superImportant_time", + event_data: { + time: '4:30PM' + } + } + } + ] + }, + { + conditions: [ + { + $smType: "condition", + condition: 'time', + condition_data: { + after: { + hours: 18, + minutes: 45 + } + } + } + ], + sequence: [ + { + $smType: 'action', + action: 'event', + action_data: { + event: "superImportant_time", + event_data: { + time: '6:45PM' + } + } + } + ] + } + ] + } + }, + { + $smType: 'action', + action: "service", + action_data: { + service: "light.turn_on", + data: { + entity_id: "light.kitchen" + } + } + } + ] +} diff --git a/webapp/src/components/SequenceNodes/location.tsx b/webapp/src/components/SequenceNodes/location.tsx index 489738b..d07b887 100644 --- a/webapp/src/components/SequenceNodes/location.tsx +++ b/webapp/src/components/SequenceNodes/location.tsx @@ -11,29 +11,69 @@ export function* computeNodesEdges( distanceFactor: number, sequence: AutomationSequenceNode[], edgeColor: string, -): Generator { - let lastStartPoint: Point = [...startPoint]; + keyPrefix: string = "", +): Generator { + let lastStartPoint: Point | null = null; let lastNodeLoc: Point | null = null; for (let i = 0; i < sequence.length; i++) { const node = sequence[i]; - const nodeLoc: Point = lastStartPoint = [lastStartPoint[0] + nodeWidth * distanceFactor, lastStartPoint[1]]; - // const curretEdgeLoc: Point = [nodeLoc[0], nodeLoc[1] + nodeHeight / 2]; - // const nextAddLoc: Point | null = i < sequence.length ? null : [nodeLoc[0] + nodeWidth * distanceFactor, nodeLoc[1] + nodeHeight / 2]; - yield <> - + if ((node.$smType === 'action') && (node.action === 'choose')) { + for (let j = 0; j < node.action_data.choose.length; j++) { + const subSeq = node.action_data.choose[j].sequence; + const subStartLoc: Point = [ + nodeLoc[0] + nodeWidth * distanceFactor, + nodeLoc[1] + nodeHeight * (j + 1) + ]; + const it = computeNodesEdges( + subStartLoc, + nodeWidth, + nodeHeight, + distanceFactor, + subSeq, + edgeColor, + keyPrefix=`.${i}.${j}[${nodeLoc}]` + ); + yield + let next = it.next(); + while (!next.done) { + yield next.value + next = it.next() + } + nextNodeLoc[0] = (next.value as any)[1][0] + nodeWidth * distanceFactor + lastStartPoint = [(next.value as any)[1][0], lastStartPoint[1]] + } + } + if (i === sequence.length - 1) { + yield - + } lastNodeLoc = [...nodeLoc] } return [startPoint, lastStartPoint]