Skip to content

Commit 246620f

Browse files
committed
feature: added send-notification node
1 parent 7071fe5 commit 246620f

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"signalk-flatten-delta": "signalk-flatten-delta.js",
1616
"signalk-send-delta": "signalk-send-delta.js",
1717
"signalk-send-pathvalue": "signalk-send-pathvalue.js",
18-
"signalk-send-put": "signalk-send-put.js"
18+
"signalk-send-put": "signalk-send-put.js",
19+
"signalk-send-notification": "signalk-send-notification.js"
1920
}
2021
}
2122
}

signalk-send-notification.html

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType('signalk-send-notification',{
3+
category: 'output',
4+
color: '#a6bbcf',
5+
defaults: {
6+
name: {value:""},
7+
path: {value: ""},
8+
state: {value:"alarm"},
9+
message: {value: ""},
10+
visual: {value: true},
11+
sound: {value: true}
12+
},
13+
inputs:1,
14+
outputs:0,
15+
align: 'right',
16+
icon: "bridge.png",
17+
label: function() {
18+
return this.name||"signalk-send-notification";
19+
}
20+
});
21+
</script>
22+
23+
<script type="text/x-red" data-template-name="signalk-send-notification">
24+
<div class="form-row">
25+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
26+
<input type="text" id="node-input-name" placeholder="Name">
27+
</div>
28+
<div class="form-row">
29+
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
30+
<input type="text" id="node-input-path" placeholder="Path">
31+
</div>
32+
<div class="form-row">
33+
<label for="node-input-state"><i class="icon-tag"></i> State</label>
34+
<select id="node-input-state" placeholder="State">
35+
<option value="normal">Normal</option>
36+
<option value="alert">Alert</option>
37+
<option value="warn">Warn</option>
38+
<option value="alarm">Alarm</option>
39+
<option value="emergency">Emergency</option>
40+
</select>
41+
</div>
42+
<div class="form-row">
43+
<label for="node-input-visual"><i class="icon-tag"></i> Visual</label>
44+
<input type="checkbox" id="node-input-visual" placeholder="Visual">
45+
</div>
46+
<div class="form-row">
47+
<label for="node-input-sound"><i class="icon-tag"></i> Sound</label>
48+
<input type="checkbox" id="node-input-sound" placeholder="Sound">
49+
</div>
50+
<div class="form-row">
51+
<label for="node-input-message"><i class="icon-tag"></i> Message</label>
52+
<input type="text" id="node-input-message" placeholder="Message">
53+
</div>
54+
</script>
55+
56+
<script type="text/x-red" data-help-name="signalk-send-notification">
57+
58+
<p>Output that sends a SignalK notification</p>
59+
60+
<p>If the input payload is an object, then it will use the keys path, state, method, and message. Example below. Otherwise it will use the configured values.</p
61+
62+
<p>To specify all the info, send:</p>
63+
<pre><code class="javascript">
64+
{payload: {
65+
"path":"notifications.testNotification",
66+
"state":"alarm",
67+
"method":["visual","sound"],
68+
"message":"this is a notification message"
69+
}}
70+
</code></pre>
71+
72+
<p>Or to specify some of the info, send:</p>
73+
<pre><code class="javascript">
74+
{payload: {
75+
"state":"normal",
76+
}}
77+
</code></pre>
78+
79+
80+
81+
</script>

signalk-send-notification.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
module.exports = function(RED) {
3+
function signalKSendNotification(config) {
4+
RED.nodes.createNode(this,config);
5+
var node = this;
6+
7+
var app = node.context().global.get('app')
8+
const _ = node.context().global.get('lodash')
9+
10+
node.on('input', msg => {
11+
let info = _.isObject(msg.payload) ? msg.payload : null
12+
let path = info && info.path ? info.path : config.path
13+
let state = info && info.state ? info.state : config.state
14+
let message = info && info.message ? info.message : config.message
15+
let method
16+
if ( info && info.method ){
17+
method = info.method
18+
} else {
19+
method = []
20+
if ( config.visual ) {
21+
method.push('visual')
22+
}
23+
if ( config.sound ) {
24+
method.push('sound')
25+
}
26+
}
27+
28+
29+
let delta = {
30+
updates: [
31+
{
32+
values: [
33+
{
34+
path: 'notifications.' + path,
35+
value: {
36+
state: state,
37+
method: method,
38+
message: message
39+
}
40+
}
41+
]
42+
}
43+
]
44+
}
45+
node.log(JSON.stringify(delta))
46+
app.handleMessage('signalk-node-red', delta)
47+
})
48+
}
49+
RED.nodes.registerType("signalk-send-notification", signalKSendNotification);
50+
}

0 commit comments

Comments
 (0)