mini sequence diagram

This commit is contained in:
ari committed 2021-11-25 12:36:19 -05:00
1 parent 69edca7685
commit e83e074fe8
2 files changed
+162 -20

No files matched your search

@@ -17,12 +17,13 @@ export default {
} }
} as ComponentMeta<typeof SequenceNodes> } as ComponentMeta<typeof SequenceNodes>
export const Simple: ComponentStory<typeof SequenceNodes> = args => <SVGBoard> const Template: ComponentStory<typeof SequenceNodes> = args => <SVGBoard>
<SequenceNodes <SequenceNodes
{...args} {...args}
/> />
</SVGBoard> </SVGBoard>
export const Simple = Template.bind({})
Simple.args = { Simple.args = {
...Simple.args, ...Simple.args,
sequence: [ 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"
}
}
}
]
}
@@ -11,29 +11,69 @@ export function* computeNodesEdges(
distanceFactor: number, distanceFactor: number,
sequence: AutomationSequenceNode[], sequence: AutomationSequenceNode[],
edgeColor: string, edgeColor: string,
): Generator<JSX.Element, [Point, Point], never> { keyPrefix: string = "",
let lastStartPoint: Point = [...startPoint]; ): Generator<JSX.Element, [Point, Point | null], never> {
let lastStartPoint: Point | null = null;
let lastNodeLoc: Point | null = null; let lastNodeLoc: Point | null = null;
for (let i = 0; i < sequence.length; i++) { for (let i = 0; i < sequence.length; i++) {
const node = sequence[i]; const node = sequence[i];
const nodeLoc: Point = lastStartPoint = [lastStartPoint[0] + nodeWidth * distanceFactor, lastStartPoint[1]]; const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...startPoint] : [
// const curretEdgeLoc: Point = [nodeLoc[0], nodeLoc[1] + nodeHeight / 2]; lastStartPoint[0] + nodeWidth * distanceFactor,
// const nextAddLoc: Point | null = i < sequence.length ? null : [nodeLoc[0] + nodeWidth * distanceFactor, nodeLoc[1] + nodeHeight / 2]; lastStartPoint[1]
yield <> ];
<DAGNode let nextNodeLoc: Point = [nodeLoc[0] + distanceFactor * nodeWidth, nodeLoc[1] + nodeHeight / 2];
key={`node.${i}`} yield <DAGNode
height={nodeHeight} key={`node${keyPrefix}.${i}`}
width={nodeWidth} height={nodeHeight}
loc={nodeLoc} width={nodeWidth}
text={node.$smType} loc={nodeLoc}
color={node.$smType === 'action' ? 'green' : 'blue'} text={node.$smType}
edge={lastNodeLoc === null ? undefined : { color={node.$smType === 'action' ? 'green' : 'blue'}
direction: '1<-2', edge={lastNodeLoc === null ? undefined : {
loc: [lastNodeLoc[0] + nodeWidth, lastNodeLoc[1] + nodeHeight / 2], direction: '1<-2',
color: edgeColor, loc: [lastNodeLoc[0] + nodeWidth, lastNodeLoc[1] + nodeHeight / 2],
}} color: edgeColor,
}}
/>
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 <DAGEdge
p1={[nodeLoc[0] + nodeWidth, nodeLoc[1] + nodeHeight / 2]}
p2={[subStartLoc[0], subStartLoc[1] + nodeHeight / 2]}
direction="1->2"
color={edgeColor}
/>
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 <DAGEdge
p1={[nodeLoc[0] + nodeWidth, nodeLoc[1] + nodeHeight / 2]}
p2={nextNodeLoc}
direction="1->2"
color={edgeColor}
/> />
</> }
lastNodeLoc = [...nodeLoc] lastNodeLoc = [...nodeLoc]
} }
return [startPoint, lastStartPoint] return [startPoint, lastStartPoint]