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