-
Notifications
You must be signed in to change notification settings - Fork 2
/
signalk-send-notification.js
68 lines (62 loc) · 1.81 KB
/
signalk-send-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module.exports = function(RED) {
function signalKSendNotification(config) {
RED.nodes.createNode(this,config);
var node = this;
var app = node.context().global.get('app')
const _ = node.context().global.get('lodash')
let showingStatus = false
function showStatus(text) {
if ( ! showingStatus ) {
node.status({fill:"green",shape:"dot",text:text});
showingStatus = true;
setTimeout( () => {
node.status({});
showingStatus = false
}, 1000)
}
}
node.on('input', msg => {
let info = _.isObject(msg.payload) ? msg.payload : null
let path = info && info.path ? info.path : config.path
let state = info && info.state ? info.state : config.state
let message = info && info.message ? info.message : config.message
let source = info && info.$source ? info.$source : config.source
let method
if ( info && info.method ){
method = info.method
} else {
method = []
if ( config.visual ) {
method.push('visual')
}
if ( config.sound ) {
method.push('sound')
}
}
if ( typeof source !== 'undefined' && source.length === 0 ) {
source = undefined
}
let delta = {
updates: [
{
$source: source,
values: [
{
path: 'notifications.' + path,
value: {
state: state,
method: method,
message: message
}
}
]
}
]
}
showStatus(state)
app.handleMessage('signalk-node-red', delta)
//node.send({payload: delta})
})
}
RED.nodes.registerType("signalk-send-notification", signalKSendNotification);
}