-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignalk-notification.js
49 lines (41 loc) · 1.37 KB
/
signalk-notification.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
module.exports = function(RED) {
function SignalKNotification(config) {
RED.nodes.createNode(this,config);
var node = this;
var unsubscribes = []
let showingStatus = false
function showStatus() {
if ( ! showingStatus ) {
node.status({fill:"green",shape:"dot",text:"sending"});
showingStatus = true;
setTimeout( () => {
node.status({});
showingStatus = false
}, 1000)
}
}
var subscriptionmanager = node.context().global.get('subscriptionmanager')
var path = config.notification === 'any' || config.notification.length === 0 ? 'notifications.*' : 'notifications.' + config.notification
var command = {
context: "vessels.self",
subscribe: [{
path: path,
policy: 'instant'
}]
}
subscriptionmanager.subscribe(command, unsubscribes, error => {
node.error('subscription error: ' + error)
}, delta => {
let notification = delta.updates[0].values[0]
if ( config.state === 'any' || (notification.value && notification.value.state == config.state) ) {
showStatus()
node.send({ payload: notification})
}
})
node.on('close', function() {
unsubscribes.forEach(function(func) { func() })
unsubscribes = []
})
}
RED.nodes.registerType("signalk-notification", SignalKNotification);
}