diff --git a/webapp/src/components/DAGFlow/index.stories.tsx b/webapp/src/components/DAGFlow/index.stories.tsx index b8dee79..9d557d2 100644 --- a/webapp/src/components/DAGFlow/index.stories.tsx +++ b/webapp/src/components/DAGFlow/index.stories.tsx @@ -18,7 +18,10 @@ export default { nodeWidth: dgconst.NODE_WIDTH, distanceFactor: dgconst.DISTANCE_FACTOR, circleSize: dgconst.CIRCLE_SIZE, - padding: dgconst.PADDING, + padding: { + x: dgconst.PADDING, + y: dgconst.PADDING, + }, } } } as ComponentMeta @@ -120,6 +123,197 @@ Simple.args = { } +export const Complex = Template.bind({}) +Complex.args = { + ...Complex.args, + automation: { + tags: { + "Room": "Bathroom", + "For": "Toliet", + "Type": "Smell", + "Use": "Flushing" + }, + metadata: { + id: "random", + alias: "Random", + description: "Example Metadata", + trigger_variables: { + 'wowo': '!' + }, + mode: 'single', + }, + trigger: [ + { + "$smType": undefined, + "platform": "numeric_state", + "entity_id": "test", + "alias": "1", + }, + { + "$smType": undefined, + "platform": "homeassistant", + "event": "start", + "alias": "2", + } + ], + 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": "service", + "action_data": { + "alias": "Start Music In Kitchen", + "service": "media_player.play_media", + "target": { + "entity_id": "media_player.kitchen_dot" + }, + "data": { + "media_content_id": "Good Morning", + "media_content_type": "SPOTIFY" + } + } + }, + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [ + { + "conditions": [ + { + "$smType": "condition", + "condition": "state", + "condition_data": { + "entity_id": "lights.bathroom", + "state": "off", + } + } + ], + "sequence": [ + { + "$smType": "action", + "action": "service", + "action_data": { + "service": "light.turn_on", + "target": { + "entity_id": "lights.bathroom" + }, + "data": {} + } + }, + { + "$smType": "action", + "action": "service", + "action_data": { + "alias": "Turn off bedroom", + "service": "light.turn_off", + "target": { + "entity_id": "lights.bedroom" + }, + "data": {} + } + } + ] + }, + { + "conditions": [ + { + "$smType": "condition", + "condition": "state", + "condition_data": { + "entity_id": "lights.bedroom", + "state": "off", + } + } + ], + "sequence": [ + { + "$smType": "action", + "action": "service", + "action_data": { + "alias": "Turn on bedroom", + "service": "light.turn_on", + "target": { + "entity_id": "lights.bedroom" + }, + "data": {} + } + }, + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [ + { + "conditions": [], + "sequence": [ + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [], + } + } + ] + } + ], + "default": [ + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [], + } + } + ], + } + } + ] + } + ], + "default": [ + { + "$smType": "action", + "action": "service", + "action_data": { + "service": "light.turn_off", + "target": { + "entity_id": "lights.bathroom" + }, + "data": {} + } + } + ] + } + } + ] + } +} + export const EmptyStart = Template.bind({}) EmptyStart.args = { ...EmptyStart.args, diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 40fd8db..e3f5e77 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -4,8 +4,9 @@ import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer'; import { AutomationData } from "types/automations"; import { triggerToFlow } from "./triggerConvertor"; import { DAGNode } from "./DAGNode"; -import { DAGFlowDims, TriggerMakerOptions } from "./types"; +import { DAGFlowDims, TriggerMakerOptions, FlowData } from './types'; import { DAGCircle } from './DAGCircle'; +import { sequenceToFlow } from "./sequenceConvertor"; const nodeTypes = { 'dagnode': DAGNode, @@ -25,22 +26,40 @@ export const DAGFlow: FC = ({ opts, dims, }) => { - const { - nodes, - edges, - } = triggerToFlow(automation.trigger, dims, opts.triggers); + const condPoint = makeConditionPoint(dims); + const flowData: FlowData = { + nodes: [condPoint], + edges: [], + } + triggerToFlow(flowData, automation.trigger, condPoint.id, dims, opts.triggers); + sequenceToFlow(flowData, automation.sequence, condPoint.id, dims); return ( ); -} \ No newline at end of file +} + + +const makeConditionPoint = ({ + padding, + nodeWidth, + nodeHeight, + circleSize, +}: DAGFlowDims) => ({ + id: `c`, + type: 'dagcircle', + position: { x: padding.x + nodeWidth * 2, y: padding.y + nodeHeight * 0.25 }, + data: { + size: circleSize, + backgroundColor: 'blue', + } +}) \ No newline at end of file diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts new file mode 100644 index 0000000..67b9873 --- /dev/null +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -0,0 +1,98 @@ +import { AutomationSequenceNode } from 'types/automations'; +import { DAGFlowDims, FlowData } from './types'; +import { getDescriptionFromAutomationNode } from 'utils/formatting'; +import { XYPosition } from 'react-flow-renderer'; +import { ChooseAction } from 'types/automations/actions'; + + +export const sequenceToFlow = ( + flowData: FlowData, + sequence: AutomationSequenceNode[], + fromPoint: string, + dims: DAGFlowDims, + prefix: string = "", +) => { + let lastPointId = fromPoint; + for (let i = 0; i < sequence.length; i++) { + const node = sequence[i]; + const nodeId = `${prefix}n-${i}`; + const position = computeNodePos(i, dims); + if (node.$smType === 'action') { + if (node.action === 'choose') { + addChooseNode(flowData, node, i, nodeId, lastPointId, dims) + } else { + addSingleNode(flowData, node, position, nodeId, dims); + } + } else { + addSingleNode(flowData, node, position, nodeId, dims); + + } + flowData.edges.push({ + id: `${lastPointId}-${nodeId}`, + source: lastPointId, + target: nodeId, + }); + lastPointId = nodeId; + } +} + +const computeNodePos = (i: number, { padding, distanceFactor, nodeWidth }: DAGFlowDims): XYPosition => ({ + x: padding.x + nodeWidth * distanceFactor * (i + 2), + y: padding.y +}) + +const addSingleNode = ( + flowData: FlowData, + node: AutomationSequenceNode, + position: XYPosition, + nodeId: string, + { + nodeHeight, + nodeWidth, + }: DAGFlowDims, + extra: Record = {}, +) => flowData.nodes.push({ + id: nodeId, + type: 'dagnode', + data: { + label: getDescriptionFromAutomationNode(node), + height: nodeHeight, + width: nodeWidth, + color: node.$smType === 'action' ? 'green' : 'blue', + // onEdit: () => opts.onEdit(i), + // onXClick: () => opts.onDelete(i, flowId), + hasInput: true, + }, + position, + ...extra +}); + + +const addChooseNode = ( + flowData: FlowData, + node: ChooseAction, + i: number, + nodeId: string, + lastPointId: string, + dims: DAGFlowDims, +) => { + const { x, y } = computeNodePos(i, dims); + addSingleNode(flowData, node, { x, y }, nodeId, dims); + node.action_data.choose.forEach(({ sequence, conditions }, j) => { + sequenceToFlow(flowData, sequence, nodeId, { + ...dims, + padding: { + x: x - dims.nodeWidth, + y: y + dims.nodeHeight * (j + 0.25) * dims.distanceFactor, + }, + }, `${nodeId}.${j}.`) + }) + const totalChildren = node.action_data.choose.length; + sequenceToFlow(flowData, node.action_data.default, nodeId, { + ...dims, + padding: { + x: x - dims.nodeWidth, + y: y + dims.nodeHeight * (totalChildren + 0.25) * dims.distanceFactor, + }, + }, `${nodeId}.${totalChildren}.`) +} \ No newline at end of file diff --git a/webapp/src/components/DAGFlow/triggerConvertor.ts b/webapp/src/components/DAGFlow/triggerConvertor.ts index cbbfd52..90a3c5a 100644 --- a/webapp/src/components/DAGFlow/triggerConvertor.ts +++ b/webapp/src/components/DAGFlow/triggerConvertor.ts @@ -1,19 +1,11 @@ -import { Edge, Node } from "react-flow-renderer"; import { AutomationTrigger } from 'types/automations/triggers'; -import { DAGFlowDims, FlowData, TriggerMakerOptions, DAGFlowNode } from './types'; +import { DAGFlowDims, FlowData, TriggerMakerOptions } from './types'; -// export const makeTriggerMakerOptions = ( -// triggers: AutomationTrigger[], -// onUpdate: (t: AutomationTrigger[]) => void, -// ): TriggerMakerOptions => ({ -// onAdd: () => onUpdate([...triggers, { -// alias: "", - -// }]) -// }) export const triggerToFlow = ( + flowData: FlowData, trigger: AutomationTrigger[], + toPoint: string, { distanceFactor, nodeHeight, @@ -22,27 +14,11 @@ export const triggerToFlow = ( padding, }: DAGFlowDims, opts: TriggerMakerOptions, -): FlowData => { - // outputs - const nodes: DAGFlowNode[] = [], - edges: Edge[] = []; - - // target node - const condPos = { x: padding + nodeWidth * 2, y: padding + nodeHeight * 0.25 }; - nodes.push({ - id: `c`, - type: 'dagcircle', - position: condPos, - data: { - size: circleSize, - backgroundColor: 'blue', - } - }) - +) => { // convert all triggers to dag nodes trigger.forEach((t, i) => { const flowId = `t-${i}`; - nodes.push({ + flowData.nodes.push({ id: flowId, type: 'dagnode', data: { @@ -51,24 +27,24 @@ export const triggerToFlow = ( width: nodeWidth, color: 'red', onEdit: () => opts.onEdit(i), - onXClick: () => opts.onDelete(i, flowId), + onXClick: () => opts.onDelete(i), }, - position: { x: padding, y: padding + nodeHeight * distanceFactor * i } + position: { x: padding.x, y: padding.y + nodeHeight * distanceFactor * i } }) - edges.push({ - id: `e(${flowId})-(c)`, + flowData.edges.push({ + id: `e(${flowId})-(${toPoint})`, source: flowId, - target: 'c', + target: toPoint, }) }); // create a 'add button' - nodes.push({ + flowData.nodes.push({ id: `trigger-add`, type: 'dagcircle', position: { - x: padding + nodeWidth * 0.4, - y: padding + nodeHeight * distanceFactor * (nodes.length - 1), + x: padding.x + nodeWidth * 0.4, + y: padding.y + nodeHeight * distanceFactor * trigger.length, }, data: { size: circleSize, @@ -78,10 +54,4 @@ export const triggerToFlow = ( disableTarget: true, } }) - - - return { - nodes, - edges - } } \ No newline at end of file diff --git a/webapp/src/components/DAGFlow/types.ts b/webapp/src/components/DAGFlow/types.ts index 5a05e41..5d26a6b 100644 --- a/webapp/src/components/DAGFlow/types.ts +++ b/webapp/src/components/DAGFlow/types.ts @@ -1,11 +1,10 @@ -import { Edge, Node } from 'react-flow-renderer'; -import { AutomationTrigger } from 'types/automations/triggers'; +import { Edge, Node, XYPosition } from 'react-flow-renderer'; import { DAGCircleProps } from './DAGCircle'; import { DAGNodeProps } from './DAGNode'; export type DAGFlowDims = { - padding: number; + padding: XYPosition; nodeHeight: number; nodeWidth: number; // addHeight: number; @@ -14,8 +13,9 @@ export type DAGFlowDims = { distanceFactor: number; } +export type DAGFlowNode = Node; export type FlowData = { - nodes: Node[], + nodes: DAGFlowNode[], edges: Edge[], } @@ -23,5 +23,4 @@ export type TriggerMakerOptions = { onAdd: () => void, onEdit: (i: number) => void, onDelete: (i: number) => void, -} -export type DAGFlowNode = Node; \ No newline at end of file +} \ No newline at end of file