-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdoor.yaml
116 lines (116 loc) · 3.44 KB
/
door.yaml
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
name: doorOpenTooLong
doc: |-
A machine that notifies you when you leave your door open.
paramspecs:
door:
doc: The id for the door (or whatever).
primitiveType: string
required: true
doorName:
doc: The pretty name for the door (or whatever).
primitiveType: string
required: true
during:
doc: |-
A specification of when this automation should be active.
Example {"startTime": "19:00", "stopTime": "22:00"}
primitiveType: activeTimeSpec
interval:
doc: The time interval before sending a notification.
primitiveType: duration
required: true
audience:
doc: The id for the entity to receive the notification.
primitiveType: string
requires:
- spec: timers # Need a timers machine.
id: timers
- spec: notices # Need (not really) a notices machine.
id: notices
patternsyntax: json
nodes:
start:
branching:
branches:
- target: listenForOpen
listenForOpen:
branching:
type: message
branches:
- pattern: |
{"device":"?door","state":"open"}
guard:
interpreter: ecmascript
source:
code: |-
%inline("door-time.js") // See tools/inline.go
return isCurrent(_.bindings.during) ? _.bindings : null;
target: startTimer
- pattern: |
{"ctl":"disable"}
target: disable
startTimer:
action:
doc: Make a timer.
interpreter: ecmascript
source: |-
// Make an id for the timer we're going to create.
_.bindings.timerId = _.gensym();
// Compose a message for the notification that our timer will request.
var msg = "Did you leave the " + _.bindings.doorName + " open?";
// Construct the message that the timer will emit.
var notice = {notice: msg, re: _.bindings["?door"]};
// If we are using a notice machine, we could route this message directly to that machine.
notice.to = {mid: "notices"};
// Emit a request to make the timer.
_.out({makeTimer: {id: _.bindings.timerId, in: _.bindings.interval, message: notice}});
// Return the current bindings, which have been updated with our timer id.
return _.bindings;
branching:
branches:
- target: listenForClose
listenForClose:
branching:
type: message
branches:
- pattern: |
{"device":"?door","state":"closed"}
target: closed
- pattern: |
{"ctl":"disable"}
target: disable
closed:
action:
doc: Cancel pending timer then restart.
interpreter: ecmascript
source: |-
// Send a request to cancel our timer.
// It's okay if the timer no longer exists.
_.out({deleteTimer: _.bindings.timerId});
// Forget our timer id.
delete _.bindings.timerId;
// Return the bindings (without that timer id).
return _.bindings;
branching:
branches:
- target: listenForOpen
disable:
action:
doc: Cancel any pending timer then stop.
interpreter: ecmascript
source: |-
if (_.bindings.timerId) {
_.out({to: "timers", message: {deleteTimer: _.bindings.timerId}});
delete _.bindings.timerId;
}
return _.bindings;
branching:
branches:
- target: listenForEnable
listenForEnable:
branching:
type: message
branches:
- pattern: |
{"ctl":"enable"}
target: listenForOpen