-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
209 lines (196 loc) · 6.73 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict"
module.exports = function (app) {
const debug = app.debug || (msg => { console.log(msg) })
var plugin = {}
var unsubscribes = []
plugin.id = "zones-edit"
plugin.name = "Edit Zones"
plugin.description = "Plugin to configure path zones and zone notification states."
plugin.schema = {
"description": "Zones provide a series of hints to the consumer. These hints help in properly setting a range of colors on display gauges. This visual indication helps identify normal or dangerous operating conditions. Additionally, Zones emit data state notification messages.",
type: "object",
properties: {
zones: {
type: "array",
title: " ",
items: {
title: "Signal K Path",
type: "object",
"required": [
"key"
],
properties: {
"active": {
title: "Active",
type: "boolean",
default: true
},
"key": {
title: "Path",
type: "string",
default: "",
},
"zones": {
"type": "array",
"minItems": 1,
"title": "Zones",
"description": "Each Signal K Path can define multiple Zones. If the value of a Path does not fall within one of its Zones, the default Zone, 'normal', is used. It's possible for multiple zones to share the same state, but have different ranges.",
"items": {
"type": "object",
"title": "Zone",
"required": ["state"],
"properties": {
"lower": {
"id": "lower",
"type": "number",
"title": "Lower",
"description": "The lowest value in this zone, based on its default unit,",
"name": "lower"
},
"upper": {
"id": "upper",
"type": "number",
"title": "Upper",
"description": "The highest value in this zone, based on its default unit.",
"name": "upper"
},
"state": {
"type": "string",
"title": "State",
"description": "The state when the value is in this zone.",
"default": "alert",
"enum": ["nominal", "alert", "warn", "alarm", "emergency"]
},
"method": {
"description": "Notification options to use when the value is in this zone.",
"type": "array",
"maxItems": 2,
"items": {
"type": "string",
"enum": ["visual", "sound"]
},
default: ["visual", "sound"]
},
"message": {
"id": "message",
"type": "string",
"title": "Message",
"description": "Custom message to display for the notification. If not set, a default message containing 'zone.lower < value < zone.upper' will be generated.",
"default": ""
}
}
}
}
}
}
}
}
}
plugin.start = function (options) {
debug('Starting zones plugin with options:', options)
if (options.zones && options.zones.length) {
sendZonesMetaDeltas(options.zones)
}
unsubscribes = (options.zones || []).reduce((acc, {
key,
active,
zones,
}) => {
if (active) {
var stream = app.streambundle.getSelfStream(key)
const tests = zones.map((zone, i) => {
if (typeof zone.upper != 'undefined') {
if (typeof zone.lower != 'undefined') {
return value => value < zone.upper && value >= zone.lower
} else {
return value => value < zone.upper
}
} else {
return value => value > zone.lower
}
})
acc.push(stream.map(value => {
const zoneIndex = tests.findIndex(test => test(value));
debug(`Value: ${value}, Zone Index: ${zoneIndex}`);
return zoneIndex;
}).skipDuplicates().onValue(zoneIndex => {
debug(`Sending notification for key: ${key}, Zone Index: ${zoneIndex}`);
sendNotificationUpdate(key, zoneIndex, zones)
}))
}
return acc
}, [])
return true
}
plugin.stop = function () {
unsubscribes.forEach(f => f())
unsubscribes = []
}
function sendNotificationUpdate(key, zoneIndex, zones) {
var value = null
if (zoneIndex >= 0) {
const zone = zones[zoneIndex]
value = {
state: zone.state,
message: zone.message || zone.lower + ' < value < ' + zone.upper,
method: zone.method
}
} else {
// Default to "normal" zone
value = {
state: "normal",
message: "Value is within normal range",
method: [],
}
}
const notificationDelta = {
context: "vessels." + app.selfId,
updates: [
{
source: {
label: "self.notificationhandler"
},
values: [{
path: "notifications." + key,
value: value
}]
}
]
}
debug('Sending path zones notification:', JSON.stringify(notificationDelta))
app.handleMessage(plugin.id, notificationDelta)
}
function sendZonesMetaDeltas(zoneEntries) {
const metaDelta = {
context: "vessels." + app.selfId,
updates:
zoneEntries.map(zoneEntry => ({
source: {
label: "self.notificationhandler"
},
meta: [
{
path: zoneEntry.key,
value: {
zones: zoneEntry.zones.map(z => ({
state: z.state,
lower: z.lower,
upper: z.upper,
message: z.message
})),
nominalMethod: (zoneEntry.zones.find(z => z.state === 'nominal') || {}).method,
alertMethod: (zoneEntry.zones.find(z => z.state === 'alert') || {}).method,
warnMethod: (zoneEntry.zones.find(z => z.state === 'warn') || {}).method,
alarmMethod: (zoneEntry.zones.find(z => z.state === 'alarm') || {}).method,
emergencyMethod: (zoneEntry.zones.find(z => z.state === 'emergency') || {}).method,
}
}
]
})
)
}
debug('Sending path metadata configuration:', JSON.stringify(metaDelta))
app.handleMessage(plugin.id, metaDelta)
}
return plugin
}