-
Notifications
You must be signed in to change notification settings - Fork 29
/
lowbattery.yaml
117 lines (112 loc) · 3.23 KB
/
lowbattery.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
117
name: lowbattery
doc: |-
Send a notification when a device's battery is low. After some
interval (e.g., seven days), if the battery is still low, send a
reminder.
This specification works with multiple devices simultaneously.
When we hear "senLowBat" for any device, request a notification.
Also schedule the same notification (as a reminder) after an
interval. If/when we hear "senLowBatRes", then cancel any pending
reminder.
Ignore duplicate "senLowBat" or "senLowBatRes" messages.
This specification uses a 'nag' node that is called initially and
then repeatedly (by invoking itself).
patternsyntax: json
paramspecs:
interval:
doc: The time interval before sending a reminder.
primitiveType: duration
requires:
- spec: notifs
id: notifs
- spec: timers
id: timers
nodes:
start:
action:
doc: Initialize our timers map.
interpreter: ecmascript
source: |-
_.bindings.timers = {};
return _.bindings;
branching:
branches:
- target: listen
listen:
branching:
type: message
branches:
- pattern: |
{"device":"?d","props":{"name":"cvTroubleValue","value":"senLowBat"}}
target: low
- pattern: |
{"device":"?d","props":{"name":"cvTroubleValue","value":"senLowBatRes"}}
target: resolved
- pattern: |
{"nag":"?d"}
target: nag
- pattern: |
{"ctl":"reset"}
target: reset
low:
action:
doc: Request nagging.
interpreter: ecmascript
source: |-
var deviceId = _.bindings["?d"];
var timerId = _.bindings.timers[deviceId];
if (!timerId) {
_.out({nag: deviceId});
} // Otherwise the message was a duplicate.
delete _.bindings["?d"];
return _.bindings;
branching:
branches:
- target: listen
nag:
action:
interpreter: ecmascript
doc: Send a notification and schedule another reminder.
source: |-
var deviceId = _.bindings["?d"];
// Send a notification.
_.out({notice: "Device " + deviceId + " battery is low."});
// Schedule the reminder.
var timerId = _.gensym();
_.bindings.timers[deviceId] = timerId;
_.out({makeTimer: {id: timerId, in: _.bindings.interval, message: {nag: deviceId}}});
delete _.bindings["?d"];
return _.bindings;
branching:
branches:
- target: listen
resolved:
action:
doc: Cancel the pending reminder
interpreter: ecmascript
source: |-
var deviceId = _.bindings["?d"];
var timerId = _.bindings.timers[deviceId];
if (timerId) {
_.out({deleteTimer: timerId});
delete _.bindings.timers[deviceId];
}
delete _.bindings["?d"];
return _.bindings;
branching:
branches:
- target: listen
reset:
action:
doc: Cancel all pending reminders
interpreter: ecmascript
source: |-
for (var deviceId in _.bindings.timers) {
var timerId = _.bindings.timers[deviceId];
_.out({deleteTimer: timerId});
}
delete _.bindings.timers;
return _.bindings;
branching:
branches:
- target: listen