-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsm-cat-flow.json
177 lines (177 loc) · 6.33 KB
/
sm-cat-flow.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
[
{
"id": "3a57f4c3.6a129c",
"type": "tab",
"label": "SM-Cat Example",
"disabled": false,
"info": ""
},
{
"id": "2161d04e.04188",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "given food",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 260,
"y": 300,
"wires": [
[
"6673b726.95c6b8"
]
]
},
{
"id": "8055f738.2d2408",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "tired",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 250,
"y": 460,
"wires": [
[
"6673b726.95c6b8"
]
]
},
{
"id": "6203b5b5.1bf03c",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "bored",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 250,
"y": 500,
"wires": [
[
"6673b726.95c6b8"
]
]
},
{
"id": "c4052841.b88358",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "given toy",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 260,
"y": 420,
"wires": [
[
"6673b726.95c6b8"
]
]
},
{
"id": "fb2ba5a4.7d69d8",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "wake up",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 260,
"y": 240,
"wires": [
[
"6673b726.95c6b8"
]
]
},
{
"id": "df45ec1f.3afd6",
"type": "debug",
"z": "3a57f4c3.6a129c",
"name": "Cat output",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 790,
"y": 440,
"wires": []
},
{
"id": "7ea8c4d5.401bbc",
"type": "debug",
"z": "3a57f4c3.6a129c",
"name": "State",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 770,
"y": 360,
"wires": []
},
{
"id": "6673b726.95c6b8",
"type": "smxstate",
"z": "3a57f4c3.6a129c",
"name": "sm-cat ",
"xstateDefinition": "// Import shorthands from xstate object\nconst { assign } = xstate; \n\n// First define names guards, actions, ...\n\n/**\n * Guards\n */\nconst bellyFull = (context, event) => {\n return context.belly >= 100;\n};\n\nconst bellyEmpty = (context, event) => {\n return context.belly <= 0;\n}\n\nconst bellyNotEmpty = (context, event) => {\n return context.belly > 0;\n}\n\n/**\n * Actions\n */\nconst updateBelly = assign({\n belly: (context, ev) => Math.max(\n Math.min(\n context.belly+ev.value,100\n ), 0)\n});\n\n/**\n * Activities\n */\nconst meow = () => {\n const interval = setInterval(() => node.send({payload: \"MEOW!\"}), 2000);\n return () => clearInterval(interval);\n};\n\n/**\n * Services\n * see https://xstate.js.org/docs/guides/communication.html#invoking-callbacks\n */\nconst eat = (ctx, ev) => (cb) => {\n const id = setInterval(() => cb({\n type: 'eaten',\n value: 5\n }),500);\n return () => clearInterval(id);\n};\n\nconst digest = (ctx, ev) => (cb) => {\n const id = setInterval(() => cb({\n type: 'digested',\n value: -5\n }),500);\n return () => clearInterval(id);\n}\n\n/***************************\n * Main machine definition * \n ***************************/\n return {\n machine: {\n context: {\n belly: 0 // Belly state, 100 means full, 0 means empty\n },\n initial: 'sleep',\n states: {\n sleep: {\n on: {\n 'wake up': 'meow',\n }\n },\n meow: {\n invoke: { src: 'digest' },\n on: {\n 'given toy': { target: 'play', cond: 'belly not empty' },\n 'given food': 'eat',\n 'digested': { actions: 'updateBelly' }\n },\n activities: ['meow']\n },\n play: {\n invoke: { src: 'digest' },\n on: {\n tired: 'sleep',\n bored: 'sleep',\n 'digested': { actions: 'updateBelly' },\n '': { target: 'meow', cond: 'belly empty' }\n },\n after: {\n 5000: 'sleep',\n }\n },\n eat: {\n invoke: { src: 'eat' },\n on: {\n '': { target: 'sleep', cond: 'belly full' },\n 'no more food': { target: 'meow' },\n 'eaten': { actions: 'updateBelly' }\n }\n }\n }\n },\n // Configuration containing guards, actions, activities, ...\n // see above\n config: {\n guards: { \"belly full\": bellyFull, \"belly not empty\": bellyNotEmpty, \"belly empty\": bellyEmpty },\n activities: { meow },\n actions: { updateBelly },\n services: { eat, digest }\n }\n}",
"noerr": 0,
"x": 530,
"y": 400,
"wires": [
[
"7ea8c4d5.401bbc"
],
[
"df45ec1f.3afd6"
]
]
},
{
"id": "ea1f8a96.72e8e8",
"type": "inject",
"z": "3a57f4c3.6a129c",
"name": "",
"topic": "no more food",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 270,
"y": 340,
"wires": [
[
"6673b726.95c6b8"
]
]
}
]