-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignalk-send-pathvalue.js
62 lines (56 loc) · 1.57 KB
/
signalk-send-pathvalue.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
module.exports = function(RED) {
function signalKSendPathValue(config) {
RED.nodes.createNode(this,config);
var node = this;
var app = node.context().global.get('app')
var source = config.name ? 'node-red-' + config.name : 'node-red'
var sentMeta = false
function showStatus(text) {
node.status({fill:"green",shape:"dot",text:text});
}
node.on('input', msg => {
if ( !msg.topic ) {
node.error('no topic for incomming message')
return
}
if ( typeof config.meta !== 'undefined' && config.meta !== "" && !sentMeta ) {
let delta = {
updates: [
{
meta: [
{
value: JSON.parse(config.meta),
path: msg.topic
}
]
}
]
}
if ( config.source && config.source.length > 0 ) {
delta.updates[0].$source = config.source
}
app.handleMessage(source, delta)
sentMeta = true
}
let delta = {
updates: [
{
values: [
{
value: msg.payload,
path: msg.topic
}
]
}
]
}
if ( config.source && config.source.length > 0 ) {
delta.updates[0].$source = config.source
}
let c = msg.topic.lastIndexOf('.')
showStatus(`${msg.topic.substring(c+1)}: ${msg.payload}`)
app.handleMessage(source, delta)
})
}
RED.nodes.registerType("signalk-send-pathvalue", signalKSendPathValue);
}