Skip to content

Commit 7dea6dc

Browse files
committed
feature: add signalk-subscribe to subscribe to specific messages
1 parent 99a69c7 commit 7dea6dc

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"signalk-send-put": "signalk-send-put.js",
1919
"signalk-send-notification": "signalk-send-notification.js",
2020
"signalk-send-nmea2000": "signalk-send-nmea2000.js",
21-
"signalk-send-nmea0183": "signalk-send-nmea0183.js"
21+
"signalk-send-nmea0183": "signalk-send-nmea0183.js",
22+
"signalk-subscribe": "signalk-subscribe.js"
2223
}
2324
}
2425
}

signalk-subscribe.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType('signalk-subscribe',{
3+
category: 'input',
4+
color: '#ffcc01',
5+
defaults: {
6+
name: {value:""},
7+
context: {value:"vessels.self"},
8+
path: {value:""},
9+
period: {value: 1000}
10+
},
11+
inputs:0,
12+
outputs:1,
13+
icon: "bridge.png",
14+
label: function() {
15+
return this.name||"signalk-subscribe";
16+
}
17+
});
18+
</script>
19+
20+
<script type="text/x-red" data-template-name="signalk-subscribe">
21+
<div class="form-row">
22+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
23+
<input type="text" id="node-input-name" placeholder="Name">
24+
</div>
25+
<div class="form-row">
26+
<label for="node-input-context"><i class="icon-tag"></i> Context</label>
27+
<input type="text" id="node-input-context" placeholder="Context">
28+
</div>
29+
<div class="form-row">
30+
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
31+
<input type="text" id="node-input-path" placeholder="Path">
32+
</div>
33+
<div class="form-row">
34+
<label for="node-input-period"><i class="icon-tag"></i> Period (ms)</label>
35+
<input type="text" id="node-input-period" placeholder="Period">
36+
</div>
37+
</script>
38+
39+
<script type="text/x-red" data-help-name="signalk-subscribe">
40+
<p>Input that sends messages for every delta from the given path</p>
41+
</script>

signalk-subscribe.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
module.exports = function(RED) {
3+
function input(config) {
4+
RED.nodes.createNode(this,config);
5+
var node = this;
6+
7+
var signalk = node.context().global.get('signalk')
8+
var app = node.context().global.get('app')
9+
var smanager = node.context().global.get('subscriptionmanager')
10+
var onStop = []
11+
12+
if ( !config.path || config.path.length == 0 ) {
13+
node.error('no path specified')
14+
return
15+
}
16+
17+
let subscription = {
18+
"context": config.context,
19+
subscribe: [{
20+
path: config.path,
21+
period: config.period
22+
}]
23+
}
24+
25+
26+
function on_delta(delta) {
27+
if ( delta.updates ) {
28+
var copy = JSON.parse(JSON.stringify(delta))
29+
copy.updates = []
30+
delta.updates.forEach(update => {
31+
if ( update.values &&
32+
(!update.$source || !update.$source.startsWith('signalk-node-red') )) {
33+
copy.updates.push(update)
34+
}
35+
})
36+
37+
if ( copy.updates.length > 0 ) {
38+
if ( copy.context == app.selfContext ) {
39+
copy.context = 'vessels.self'
40+
}
41+
node.send({ payload: copy })
42+
}
43+
}
44+
}
45+
46+
smanager.subscribe(subscription,
47+
onStop,
48+
(err) => {
49+
node.error('subscription error', err)
50+
},
51+
on_delta);
52+
53+
54+
node.on('close', function() {
55+
onStop.forEach(f => f());
56+
onStop = []
57+
})
58+
}
59+
RED.nodes.registerType("signalk-subscribe", input);
60+
}

0 commit comments

Comments
 (0)