-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignalk-on-delta.js
72 lines (65 loc) · 2.24 KB
/
signalk-on-delta.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
module.exports = function(RED) {
function SignalKOnDelta(config) {
RED.nodes.createNode(this,config);
var node = this;
var signalk = node.context().global.get('signalk')
var app = node.context().global.get('app')
let showingStatus = false
function showStatus() {
if ( ! showingStatus ) {
node.status({fill:"green",shape:"dot",text:"sending"});
showingStatus = true;
setTimeout( () => {
node.status({});
showingStatus = false
}, 1000)
}
}
function on_delta(delta) {
if ( delta.updates ) {
if ( delta.context === config.context ||
(config.context === 'vessels.self' &&
delta.context == app.selfContext) ) {
if ( typeof config.flatten === 'undefined' || !config.flatten ) {
var copy = JSON.parse(JSON.stringify(delta))
copy.updates = []
delta.updates.forEach(update => {
if ( update.values &&
(!update.$source || !update.$source.startsWith('signalk-node-red') )) {
copy.updates.push(update)
}
})
if ( copy.updates.length > 0 ) {
showStatus()
if ( copy.context == app.selfContext ) {
copy.context = 'vessels.self'
}
node.send({ payload: copy })
}
} else {
delta.updates.forEach(update => {
if ( update.values &&
(!update.$source || !update.$source.startsWith('signalk-node-red') )) {
showStatus()
update.values.forEach(pathValue => {
node.send({
$source: update.$source,
source: update.source,
context: delta.context == app.selfContext ? 'vessels.self' : delta.context,
payload: pathValue.value,
topic: pathValue.path
})
})
}
})
}
}
}
}
signalk.on('delta', on_delta)
node.on('close', function() {
signalk.removeListener('delta', on_delta)
})
}
RED.nodes.registerType("signalk-on-delta", SignalKOnDelta);
}