-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
473 lines (399 loc) · 12.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
const fs = require('fs')
const PLUGIN_ID = 'signalk-n2k-virtual-switch'
const PLUGIN_NAME = 'NMEA 2000 Virtual Switch'
const numIndicators = 28
const pdStateSuffix = '-powerDownState.json'
module.exports = function(app) {
var plugin = {};
var virtualSwitch = {};
var vsTimer;
let onStop = []
let registeredPaths = []
var n2kCallback
var vsOptions
plugin.id = PLUGIN_ID;
plugin.name = PLUGIN_NAME;
plugin.description = 'Emulates a NMEA 2000 Virtual Switch';
function createChannelSchema(schema) {
let channels = {
type: 'object',
title: 'Switch Channels',
properties: {}
}
for (let i = 1; i <= numIndicators; i++) {
let channel = {
type: "object",
title: "Channel " + i,
required: ['label'],
properties: {
enableChannel: {
type: 'boolean',
title: 'Enable the channel',
default: true
},
label: {
type: 'string',
title: 'Name of Channel',
description: '',
default: i.toString()
},
defaultState: {
type: 'string',
title: 'Power Up State',
description: 'Default state when the plugin starts.',
enum: [
"OFF",
"ON",
"Previous"
],
default: "OFF"
},
enableTTL: {
type: 'boolean',
title: 'Enable Time to Live',
description: 'Enable the TTL for this channel so that state will timeout and stop sending updates. Great for dummy indicators..',
default: false
}
}
}
channels.properties[i] = channel
}
schema.properties["channels"] = channels
return schema
}
plugin.schema = function() {
var schema = {
type: "object",
properties: {
virtualInstance: {
type: 'number',
title: 'Instance # of the Virtual Switch',
default: 107
},
sendRate: {
type: 'number',
title: 'Send Rate(seconds)',
description: 'Switch updates will be sent periodically and on switch change.',
default: 15
},
stateTTL: {
type: 'number',
title: 'Time to Live(seconds)',
description: 'Switch state will be cached for the time to live period. A setting of 0 = unlimited.',
default: 60
}
}
}
createChannelSchema(schema)
return schema
}
plugin.start = function(options, restartPlugin) {
vsOptions = options
subscribeUpdates()
if (typeof virtualSwitch === 'object' && Object.keys(virtualSwitch).length < 1) {
initializeSwitch()
vsTimer = setInterval(function() {
sendState()
sendPeriodicDeltas()
}, vsOptions.sendRate * 1000)
}
n2kCallback = (msg) => {
try {
var fields = msg['fields']
if (msg.pgn == 127502 &&
(fields['Switch Bank Instance'] == vsOptions.virtualInstance || fields['Instance'] == vsOptions.virtualInstance) ||
(msg.pgn == 126208 && fields['Function Code'] == 'Command' &&
fields['PGN'] == 127501 && fields['list'][0].Value == vsOptions.virtualInstance)) {
app.debug('Received a virtual switch control update.')
app.debug('msg: ' + JSON.stringify(msg))
handleChange(msg.pgn, fields)
}
} catch (e) {
console.error(e)
}
}
app.on("N2KAnalyzerOut", n2kCallback)
}
plugin.registerWithRouter = function(router) {
router.put("/virtualSwitch", (req, res) => {
var msg = req.body
let example = 'Must include object for the 127502 PGN. ex. {"pgn":127502,"fields":{"Switch Bank Instance": X, "SwitchY": 0|1}}'
if (typeof msg == 'undefined') {
app.debug('invalid request: ' + data)
res.status(400)
res.send('Invalid Request. ' + example)
return
}
try {
let pgn = msg.pgn
let fields = msg['fields']
let instance = fields['Switch Bank Instance']
if (instance !== vsOptions.virtualInstance) {
res.status(400)
res.send('Invalid Request. Incorrect instance number. ' + example)
return
}
let key = Object.keys(fields).filter((key) => /Switch\d+/.test(key)).toString()
let switchNum = key.match(/\d+/g).map(Number)
if (switchNum < 1 || switchNum > 28) {
res.status(400)
res.send('Invalid Request. Incorrect Switch number. ' + example)
return
}
if (fields[key] !== 0 && fields[key] !== 1) {
res.status(400)
res.send('Invalid Request. Switch state must be a 0 or 1. ' + example)
return
}
handleChange(pgn, fields)
res.send('Instance ' + instance + ' ' + key + ' switched ' + (fields[key] ? 'on' : 'off'))
} catch (err) {
app.debug(err)
res.status(400)
res.send('Invalid Request. ' + example)
}
})
}
plugin.stop = function() {
if (n2kCallback) {
app.removeListener("N2KAnalyzerOut", n2kCallback)
n2kCallback = undefined
}
if (vsTimer) {
clearInterval(vsTimer)
}
onStop.forEach(f => f())
onStop = []
saveState()
virtualSwitch = {}
app.debug('Plugin stopped.')
}
function initializeSwitch() {
app.debug("Initializing vSwitch")
let channels = vsOptions.channels
let pdState = getPowerDownState()
for (let i = 1; i <= numIndicators; i++) {
let channelEnabled = true
let label
let state
if (channels) {
channelEnabled = channels[i].hasOwnProperty('enableChannel') ? channels[i].enableChannel : true
label = channels[i].label
if (pdState) {
state = channels[i].defaultState === "Previous" ? pdState[i] : channels[i].defaultState
} else {
state = channels[i].defaultState === "Previous" ? "OFF" : channels[i].defaultState
}
} else {
label = i.toString()
state = "OFF"
}
if(channelEnabled){
virtualSwitch[i] = {
"label": label,
"state": state === "ON" ? 1 : 0,
"lastUpdated": Date.now()
}
sendDelta(vsOptions.virtualInstance, label, 2)
}
}
app.setPluginStatus('Virtual Switch initialized')
}
function handleChange(pgn, fields) {
let instance, switchNum, value
switch (pgn) {
case 127502:
instance = fields['Switch Bank Instance']
if(!instance)
instance = fields['Instance']
let key = Object.keys(fields).filter((key) => /Switch\d+/.test(key)).toString()
switchNum = key.match(/\d+/g).map(Number)
value = fields[key] === 'On' || fields[key] === 1 ? 1 : 0
break
case 126208:
instance = fields['list'][0].Value
switchNum = fields['list'][1].Parameter
switchNum--
value = fields['list'][1].Value === 'On' || fields['list'][1].Value === 1 ? 1 : 0
break
}
//always update SK
let label = vsOptions.channels ? vsOptions.channels[switchNum].label :switchNum
sendDelta(instance, label, value)
//update the virtual switch state
virtualSwitch[switchNum].lastUpdated = Date.now()
let currentState = virtualSwitch[switchNum].state
if (currentState !== value) {
//the state has changed so send an update on the NMEA network
clearInterval(vsTimer)
virtualSwitch[switchNum].state = value
sendState()
if (vsOptions.channels && vsOptions.channels[switchNum].defaultState === 'Previous') {
saveState()
}
vsTimer = setInterval(function() {
sendState()
sendPeriodicDeltas()
}, vsOptions.sendRate * 1000)
}
}
function sendState() {
//send status update 127501
const pgn = {
pgn: 127501,
dst: 255,
"Instance": vsOptions.virtualInstance
}
let keys = Object.keys(virtualSwitch)
for (let i = 0; i < keys.length; i++) {
let key = keys[i]
let notExpired = ((Date.now() - virtualSwitch[key].lastUpdated) <= (vsOptions.stateTTL * 1000)) ? true : false
if (vsOptions.stateTTL === 0 || !vsOptions.channels[key].enableTTL || (vsOptions.channels[key].enableTTL && notExpired)) {
pgn[`Indicator${key}`] = virtualSwitch[key].state === 1 ? 'On' : 'Off'
}
}
if (Object.keys(pgn).length > 3) {
app.debug('sending pgn %j', pgn)
app.emit('nmea2000JsonOut', pgn)
} else {
app.debug('nothing to send %j', pgn)
}
}
function sendDelta(instance, indicator, value) {
let delta = {
"updates": [{
"values": [{
"path": `electrical.switches.bank.${instance}.${indicator}.state`,
"value": value
}]
}]
}
app.debug(JSON.stringify(delta))
app.handleMessage(PLUGIN_ID, delta)
}
function subscribeUpdates() {
let command = {
context: "vessels.self",
subscribe: [{
path: `electrical.switches.bank.${vsOptions.virtualInstance}.*`,
period: 1000
}]
}
app.debug('subscribe %j', command)
app.subscriptionmanager.subscribe(command, onStop, subscription_error, delta => {
delta.updates.forEach(update => {
update.values.forEach(value => {
const path = value.path
const key = `${path}.${update.$source}`
if (path.endsWith('state') && registeredPaths.indexOf(key) === -1) {
app.debug('register action handler for path %s source %s', path, update.$source)
app.registerActionHandler('vessels.self',
path,
(context, path, value, cb) => {
return actionHandler(context, path, update.$source, value, cb)
},
update.$source)
registeredPaths.push(key)
}
})
})
})
}
function actionHandler(context, path, dSource, value, cb) {
app.debug(`setting ${path} to ${value}`)
const parts = path.split('.')
let instance = Number(parts[3])
let label = parts[4]
let labelPaths = findPaths(vsOptions, "label", label)
let switchNum = labelPaths.length > 0 ? labelPaths[0].split('.')[1] : label
let msg = {
"pgn": 127502,
"fields": {
"Switch Bank Instance": instance
}
}
msg.fields[`Switch${switchNum}`] = value
let pgn = msg.pgn
let fields = msg['fields']
try {
handleChange(pgn, fields)
cb({
state: 'SUCCESS'
})
} catch (err) {
app.error(err)
cb({
state: 'FAILURE'
})
}
return {
state: 'SUCCESS'
}
}
function subscription_error(err) {
app.setProviderError(err)
}
function sendPeriodicDeltas() {
let keys = Object.keys(virtualSwitch)
let values = (keys.map(key => ({
"path": `electrical.switches.bank.${vsOptions.virtualInstance}.${vsOptions.channels ? vsOptions.channels[key].label : key}.state`,
"value": virtualSwitch[key].state
})))
let delta = {
"updates": [{
"values": values
}]
}
app.debug(JSON.stringify(delta))
app.handleMessage(PLUGIN_ID, delta)
}
function saveState() {
let pdState = {}
let keys = Object.keys(virtualSwitch)
for (let i = 0; i < keys.length; i++) {
let key = keys[i]
pdState[key] = virtualSwitch[key].state === 1 ? "ON" : "OFF"
}
let filepath = app.getDataDirPath() + pdStateSuffix
app.debug('Writing ' + JSON.stringify(pdState) + ' to file ' + filepath)
try {
fs.writeFileSync(filepath, JSON.stringify(pdState, null, 2))
} catch (e) {
app.error('Could not write to ' + filepath + ' ERROR: ' + err)
}
}
function getPowerDownState() {
let pdStateAsString = '{}'
let pdState
let filepath = app.getDataDirPath() + pdStateSuffix
try {
pdStateAsString = fs.readFileSync(filepath, 'utf8')
} catch (e) {
app.debug('Could not get powerDownState from ' + filepath + ' - ' + e)
}
try {
pdState = JSON.parse(pdStateAsString)
} catch (e) {
app.debug('Could not parse pdState - ' + e)
}
return pdState
}
function findPaths(obj, propName, value, prefix = '', store = []) {
for (let key in obj) {
const curPath = prefix.length > 0 ? `${prefix}.${key}` : key
if (typeof obj[key] === 'object') {
if (!propName || curPath.includes(propName)) {
store.push(curPath)
}
findPaths(obj[key], propName, value, curPath, store);
} else {
if ((!propName || curPath.includes(propName)) &&
(!value || obj[key] == value)) {
store.push(curPath)
}
}
}
return store;
}
return plugin;
};