simple two node dag
This commit is contained in:
1 parent
d8747a7ee2
commit
69edca7685
11 files changed
+431
-15
No files matched your search
@@ -27,10 +27,6 @@ export const DAGEdge = ({
|
||||
cx1 = x1 + 2 * l1 * Math.cos(theta)
|
||||
cy2 = y2 + 0.5 * l2 * Math.sin(theta)
|
||||
cx2 = x2 - 2 * l2 * Math.cos(theta)
|
||||
} else if (theta === Math.PI) {
|
||||
cy1 *= 1.5
|
||||
cx1 = x1 - 2 * l1 * Math.cos(theta)
|
||||
cy2 *= 0.9
|
||||
} else if ((theta > Math.PI / 2) && (theta < Math.PI)) {
|
||||
cy2 += l2 * Math.sin(theta)
|
||||
cx2 += 10 * l2 * Math.cos(theta)
|
||||
|
||||
@@ -25,19 +25,29 @@ export const Basic: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
<DAGNode {...args}/>
|
||||
</SVGBoard>
|
||||
|
||||
export const WithEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
export const WithFromEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} />
|
||||
</SVGBoard>
|
||||
WithEdge.args = {
|
||||
...(WithEdge.args ?? {}),
|
||||
WithFromEdge.args = {
|
||||
...(WithFromEdge.args ?? {}),
|
||||
edge: {
|
||||
color: 'white',
|
||||
loc: [ NODE_WIDTH * 4, NODE_HEIGHT * 2 ]
|
||||
loc: [NODE_WIDTH * 4, NODE_HEIGHT * 2],
|
||||
direction: '1->2',
|
||||
}
|
||||
}
|
||||
|
||||
export const WithToEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} edge={{
|
||||
color: 'white',
|
||||
loc: [args.loc[0] - args.width, args.loc[1] + args.height / 2],
|
||||
direction: '1<-2',
|
||||
}}/>
|
||||
</SVGBoard>
|
||||
export const WithManyEdges: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Color from "chroma-js";
|
||||
import { PencilIcon } from "components/Icons"
|
||||
import { useNodeStyles } from "./styles"
|
||||
import { Node, Point } from "types/graphs";
|
||||
import { Edge, Node, Point } from "types/graphs";
|
||||
import { DAGEdge } from "./DAGEdge";
|
||||
|
||||
|
||||
@@ -11,9 +11,11 @@ export interface NodeProp extends Node {
|
||||
width: number;
|
||||
onXClick?: () => void;
|
||||
onOpenClick?: () => void;
|
||||
debug?: boolean;
|
||||
edge?: {
|
||||
color: string;
|
||||
loc: Point;
|
||||
direction: Edge['direction'];
|
||||
}
|
||||
}
|
||||
export const DAGNode = ({
|
||||
@@ -25,6 +27,7 @@ export const DAGNode = ({
|
||||
onOpenClick = () => { },
|
||||
color,
|
||||
edge,
|
||||
debug=false,
|
||||
}: NodeProp) => {
|
||||
const { classes, theme } = useNodeStyles({
|
||||
color,
|
||||
@@ -51,11 +54,22 @@ export const DAGNode = ({
|
||||
</div>
|
||||
</div>
|
||||
</foreignObject>
|
||||
{edge && <DAGEdge
|
||||
p1={[x + width, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction="1->2"
|
||||
color={edge.color}
|
||||
/>}
|
||||
{
|
||||
edge && (edge.direction === '1->2' ?
|
||||
<DAGEdge
|
||||
p1={[x + width, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction={edge.direction}
|
||||
color={edge.color}
|
||||
debug={debug}
|
||||
/> :
|
||||
<DAGEdge
|
||||
p1={[x, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction={edge.direction}
|
||||
color={edge.color}
|
||||
debug={debug}
|
||||
/>)
|
||||
}
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { SequenceNodes } from './index';
|
||||
import { SVGBoard } from '../DAGSvgs/Board';
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants';
|
||||
|
||||
|
||||
export default {
|
||||
title: 'DAGSvgs/SequenceNodes',
|
||||
component: SequenceNodes,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
nodeHeight: NODE_HEIGHT,
|
||||
nodeWidth: NODE_WIDTH,
|
||||
distanceFactor: 1.5,
|
||||
startLoc: [50,50]
|
||||
}
|
||||
} as ComponentMeta<typeof SequenceNodes>
|
||||
|
||||
export const Simple: ComponentStory<typeof SequenceNodes> = args => <SVGBoard>
|
||||
<SequenceNodes
|
||||
{...args}
|
||||
/>
|
||||
</SVGBoard>
|
||||
|
||||
Simple.args = {
|
||||
...Simple.args,
|
||||
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",
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { FC } from "react";
|
||||
import { useTheme } from "styles/theme";
|
||||
import { Point } from "types/graphs";
|
||||
import { AutomationSequenceNode } from '../../types/automations/index';
|
||||
import { computeNodesEdges } from './location';
|
||||
|
||||
|
||||
export const SequenceNodes: FC<{
|
||||
startLoc: Point,
|
||||
sequence: AutomationSequenceNode[],
|
||||
nodeHeight: number,
|
||||
nodeWidth: number,
|
||||
distanceFactor: number,
|
||||
}> = ({
|
||||
startLoc,
|
||||
sequence,
|
||||
nodeHeight,
|
||||
nodeWidth,
|
||||
distanceFactor,
|
||||
}) => {
|
||||
const theme = useTheme();
|
||||
|
||||
return <>
|
||||
{Array.from(computeNodesEdges(
|
||||
startLoc,
|
||||
nodeWidth,
|
||||
nodeHeight,
|
||||
distanceFactor,
|
||||
sequence,
|
||||
theme.secondaryAccent,
|
||||
))}
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { DAGEdge } from "components/DAGSvgs/DAGEdge";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { Point } from "types/graphs";
|
||||
import { DAGNode } from '../DAGSvgs/DAGNode';
|
||||
|
||||
|
||||
export function* computeNodesEdges(
|
||||
startPoint: Point,
|
||||
nodeWidth: number,
|
||||
nodeHeight: number,
|
||||
distanceFactor: number,
|
||||
sequence: AutomationSequenceNode[],
|
||||
edgeColor: string,
|
||||
): Generator<JSX.Element, [Point, Point], never> {
|
||||
let lastStartPoint: Point = [...startPoint];
|
||||
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,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
lastNodeLoc = [...nodeLoc]
|
||||
}
|
||||
return [startPoint, lastStartPoint]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { AutomationSequenceNode } from ".";
|
||||
import { AutomationTime } from "./common";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
|
||||
interface AutomationActionNodeBase<Name extends String,Data extends Object> {
|
||||
$smType: 'action';
|
||||
action: Name;
|
||||
action_data: Data & {
|
||||
alias?: string;
|
||||
variables?: Record<string, any>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ServiceAction extends AutomationActionNodeBase<"service", {
|
||||
service: string;
|
||||
target: any;
|
||||
data: any;
|
||||
}>{}
|
||||
export interface RepeatAction extends AutomationActionNodeBase<"repeat", {
|
||||
repeat: {
|
||||
count: number;
|
||||
sequence: AutomationAction[];
|
||||
}
|
||||
}>{}
|
||||
export interface WaitAction extends AutomationActionNodeBase<"wait", {
|
||||
wait_template: string;
|
||||
timeout?: AutomationTime;
|
||||
continue_on_timeout?: boolean;
|
||||
}>{}
|
||||
export interface FireEventAction extends AutomationActionNodeBase<"event", {
|
||||
event: string;
|
||||
event_data: any;
|
||||
}>{}
|
||||
export interface DeviceAction extends AutomationActionNodeBase<"device", {
|
||||
type: string;
|
||||
device_id: string;
|
||||
entity_id?: string;
|
||||
domain: string;
|
||||
}>{}
|
||||
export interface ChooseAction extends AutomationActionNodeBase<"choose", {
|
||||
choose: Array<{
|
||||
conditions: AutomationCondition[];
|
||||
sequence: AutomationSequenceNode[];
|
||||
}>;
|
||||
default: AutomationAction[];
|
||||
}>{}
|
||||
export type AutomationAction =
|
||||
| ServiceAction
|
||||
| RepeatAction
|
||||
| WaitAction
|
||||
| FireEventAction
|
||||
| DeviceAction
|
||||
| ChooseAction
|
||||
;
|
||||
export const ActionTypes = [
|
||||
'Service',
|
||||
'Repeat',
|
||||
'Wait',
|
||||
'Event',
|
||||
'Device',
|
||||
'Choose'
|
||||
] as const;
|
||||
export type ActionType = typeof ActionTypes[number];
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface AutomationTime {
|
||||
hours?: number;
|
||||
minutes?: number;
|
||||
seconds?: number;
|
||||
milliseconds?: number;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
interface AutomationConditionNodeBase<Name extends String,Data extends Object> {
|
||||
$smType: 'condition';
|
||||
condition: Name;
|
||||
condition_data: Data & {
|
||||
alias?: string;
|
||||
};
|
||||
}
|
||||
export interface LogicCondition extends AutomationConditionNodeBase<'and' | 'or' | 'not',{
|
||||
conditions: AutomationCondition[];
|
||||
}> {}
|
||||
export interface NumericCondition extends AutomationConditionNodeBase<'numeric_state', {
|
||||
entity_id: string | string[];
|
||||
above?: string;
|
||||
below?: string;
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
}> {}
|
||||
export interface StateCondition extends AutomationConditionNodeBase<'state',{
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
for?: AutomationTime;
|
||||
attribute?: string;
|
||||
}>{}
|
||||
export interface TemplateCondition extends AutomationConditionNodeBase<'template',{
|
||||
value_template: string;
|
||||
}> {}
|
||||
export interface TimeCondition extends AutomationConditionNodeBase<'time', {
|
||||
after?: AutomationTime;
|
||||
before?: AutomationTime;
|
||||
weekday?: Array<'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'>;
|
||||
}> {}
|
||||
export interface TriggerCondition extends AutomationConditionNodeBase <'trigger', {
|
||||
id: string | string[];
|
||||
}>{}
|
||||
export interface ZoneCondition extends AutomationConditionNodeBase <'zone', {
|
||||
zone: string;
|
||||
entity_id: string | string[];
|
||||
state: string | string[];
|
||||
}>{}
|
||||
export type AutomationCondition =
|
||||
| LogicCondition
|
||||
| NumericCondition
|
||||
| StateCondition
|
||||
| TemplateCondition
|
||||
| TimeCondition
|
||||
| TriggerCondition
|
||||
| ZoneCondition
|
||||
;
|
||||
@@ -0,0 +1,26 @@
|
||||
import { AutomationAction } from "./actions";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
import { AutomationTrigger } from "./triggers";
|
||||
|
||||
|
||||
export interface AutomationMetadata {
|
||||
id: string;
|
||||
alias: string;
|
||||
description: string;
|
||||
trigger_variables?: Record<string, string>;
|
||||
mode: "single" | "restart" | "queued" | "parallel";
|
||||
}
|
||||
export interface AutomationData {
|
||||
metadata: AutomationMetadata;
|
||||
trigger: AutomationTrigger[];
|
||||
sequence: AutomationSequenceNode[];
|
||||
}
|
||||
export type AutomationSequenceNode = AutomationAction | AutomationCondition;
|
||||
type AutomationNodeMapping = {
|
||||
"trigger": AutomationTrigger;
|
||||
"action": AutomationAction;
|
||||
"condition": AutomationCondition;
|
||||
"sequence": AutomationSequenceNode;
|
||||
}
|
||||
export type AutomationNodeTypes = keyof AutomationNodeMapping;
|
||||
export type AutomationNode<N extends AutomationNodeTypes = any> = AutomationNodeMapping[N];
|
||||
@@ -0,0 +1,110 @@
|
||||
import { AutomationTime } from "./common";
|
||||
|
||||
interface AutomationTriggerBase {
|
||||
alias?: string;
|
||||
}
|
||||
|
||||
export interface AutomationTriggerEvent extends AutomationTriggerBase {
|
||||
platform: 'event';
|
||||
event_type: string | string[];
|
||||
event_data?: any;
|
||||
context?: any;
|
||||
}
|
||||
export interface AutomationTriggerHA extends AutomationTriggerBase {
|
||||
platform: 'homeassistant';
|
||||
event: 'start' | 'shutdown';
|
||||
}
|
||||
export interface AutomationTriggerMQTT extends AutomationTriggerBase {
|
||||
platform: 'mqtt';
|
||||
topic: string;
|
||||
payload: string;
|
||||
value_template: string;
|
||||
}
|
||||
export interface AutomationTriggerNumericState extends AutomationTriggerBase {
|
||||
platform: 'numeric_state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
value_template?: string;
|
||||
above?: string;
|
||||
below?: string;
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerState extends AutomationTriggerBase {
|
||||
platform: 'state';
|
||||
entity_id: string | string[];
|
||||
attribute?: string;
|
||||
from?: string | string[];
|
||||
to?: string | string[];
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerTag extends AutomationTriggerBase {
|
||||
platform: 'tag';
|
||||
tag_id: string | string[];
|
||||
device_id: string | string[];
|
||||
}
|
||||
export interface AutomationTriggerTemplate extends AutomationTriggerBase {
|
||||
platform: 'template';
|
||||
value_template: string;
|
||||
for?: AutomationTime;
|
||||
}
|
||||
export interface AutomationTriggerTime extends AutomationTriggerBase {
|
||||
platform: 'time';
|
||||
at: string | string[];
|
||||
}
|
||||
export interface AutomationTriggerTimePattern extends AutomationTriggerBase {
|
||||
platform: 'time_pattern';
|
||||
hours?: string;
|
||||
minutes?: string;
|
||||
seconds?: string;
|
||||
}
|
||||
export interface AutomationTriggerWebhook extends AutomationTriggerBase {
|
||||
platform: 'webhook';
|
||||
webhook_id: string;
|
||||
}
|
||||
export interface AutomationTriggerZone extends AutomationTriggerBase {
|
||||
platform: 'zone';
|
||||
entity_id: string;
|
||||
zone: string;
|
||||
event: 'enter' | 'leave';
|
||||
}
|
||||
export interface AutomationTriggerDevice extends AutomationTriggerBase {
|
||||
platform: 'device';
|
||||
device_id: string;
|
||||
domain: string;
|
||||
type: string;
|
||||
subtype: string;
|
||||
}
|
||||
export interface AutomationTriggerGeneric extends AutomationTriggerBase {
|
||||
platform: string;
|
||||
}
|
||||
export type AutomationTrigger =
|
||||
| AutomationTriggerEvent
|
||||
| AutomationTriggerHA
|
||||
| AutomationTriggerMQTT
|
||||
| AutomationTriggerNumericState
|
||||
| AutomationTriggerState
|
||||
| AutomationTriggerTag
|
||||
| AutomationTriggerTemplate
|
||||
| AutomationTriggerTime
|
||||
| AutomationTriggerTimePattern
|
||||
| AutomationTriggerWebhook
|
||||
| AutomationTriggerZone
|
||||
| AutomationTriggerDevice
|
||||
| AutomationTriggerGeneric
|
||||
;
|
||||
export const triggerTypes = [
|
||||
'Event',
|
||||
'Home Assistant',
|
||||
'MQTT',
|
||||
'Numeric State',
|
||||
'State',
|
||||
'Tag',
|
||||
'Template',
|
||||
'Time',
|
||||
'Time Pattern',
|
||||
'Webhook',
|
||||
'Zone',
|
||||
'Device',
|
||||
'Generic',
|
||||
] as const;
|
||||
export type TriggerType = typeof triggerTypes[number];
|
||||
Reference in new issue
Block a user