Skip to content

Commit 2d3fc89

Browse files
authored
feature: add ability to respond with error from put handlers (#45)
1 parent efefee2 commit 2d3fc89

8 files changed

Lines changed: 114 additions & 5 deletions

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"signalk-geofence-switch": "signalk-geofence-switch.js",
3333
"signalk-delay": "signalk-delay.js",
3434
"signalk-put-handler": "signalk-put-handler.js",
35+
"signalk-put-error": "signalk-put-error.js",
36+
"signalk-put-success": "signalk-put-success.js",
3537
"signalk-input-handler-next": "signalk-input-handler-next.js",
3638
"signalk-input-handler": "signalk-input-handler.js"
3739
}

signalk-put-error.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType('signalk-put-error',{
3+
category: 'Signal K',
4+
color: '#ffcc01',
5+
defaults: {
6+
name: {value:""}
7+
},
8+
inputs: 1,
9+
outputs: 0,
10+
icon: "bridge.png",
11+
align: 'right',
12+
label: function() {
13+
return this.name||"signalk-put-error";
14+
},
15+
paletteLabel: 'put error'
16+
});
17+
</script>
18+
19+
<script type="text/x-red" data-template-name="signalk-put-error">
20+
<div class="form-row">
21+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
22+
<input type="text" id="node-input-name" placeholder="Name">
23+
</div>
24+
</script>
25+
26+
<script type="text/x-red" data-help-name="signalk-put-error">
27+
<p>Respond to a pending PUT request with an error.</p>
28+
</script>

signalk-put-error.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
module.exports = function(RED) {
3+
function SignalK(config) {
4+
RED.nodes.createNode(this,config);
5+
var node = this;
6+
7+
var app = node.context().global.get('app')
8+
9+
node.on('input', msg => {
10+
let cb = msg.putCallBack
11+
if ( cb ) {
12+
cb({ state: 'COMPLETED', statusCode: msg.payload?.statusCode || 500, message: msg.payload?.message || 'Error'})
13+
} else {
14+
node.error('No callback provided for put response')
15+
}
16+
})
17+
}
18+
RED.nodes.registerType("signalk-put-error", SignalK);
19+
}
20+

signalk-put-handler.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
color: '#ffcc01',
55
defaults: {
66
name: {value:""},
7-
path: {value:""}
7+
path: {value:""},
8+
pending: {value: false},
89
},
910
inputs:0,
1011
outputs:1,
@@ -25,8 +26,13 @@
2526
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
2627
<input type="text" id="node-input-path" placeholder="Path">
2728
</div>
29+
<div class="form-row">
30+
<label for="node-input-pending"><i class="icon-tag"></i> Use Put Response</label>
31+
<input type="checkbox" id="node-input-pending">
32+
</div>
2833
</script>
2934

3035
<script type="text/x-red" data-help-name="signalk-put-handler">
3136
<p>Input that sends messages when a PUT is sent to the server for the given path</p>
37+
<p>If "Use Put Response" is checked, the node will respond with status PENDING, use the signalk-put-response node to handle the response.</p>
3238
</script>

signalk-put-handler.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ module.exports = function(RED) {
77
var app = node.context().global.get('app')
88

99
function handlePut(context, path, value, cb) {
10-
node.send({topic: path, payload: value})
11-
return { state: 'SUCCESS' }
10+
if ( config.pending ) {
11+
node.send({topic: path, payload: value, putCallBack: cb})
12+
return { state: 'PENDING' }
13+
} else {
14+
node.send({topic: path, payload: value})
15+
return { state: 'SUCCESS' }
16+
}
1217
}
1318

1419
let deReg = app.registerActionHandler('vessels.self', config.path,

signalk-put-success.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType('signalk-put-success',{
3+
category: 'Signal K',
4+
color: '#ffcc01',
5+
defaults: {
6+
name: {value:""}
7+
},
8+
inputs: 1,
9+
outputs: 0,
10+
icon: "bridge.png",
11+
align: 'right',
12+
label: function() {
13+
return this.name||"signalk-put-success";
14+
},
15+
paletteLabel: 'put success'
16+
});
17+
</script>
18+
19+
<script type="text/x-red" data-template-name="signalk-put-success">
20+
<div class="form-row">
21+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
22+
<input type="text" id="node-input-name" placeholder="Name">
23+
</div>
24+
</script>
25+
26+
<script type="text/x-red" data-help-name="signalk-put-success">
27+
<p>Respond to a pending PUT request with success.</p>
28+
</script>

signalk-put-success.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
module.exports = function(RED) {
3+
function SignalK(config) {
4+
RED.nodes.createNode(this,config);
5+
var node = this;
6+
7+
var app = node.context().global.get('app')
8+
9+
node.on('input', msg => {
10+
let cb = msg.putCallBack
11+
if ( cb ) {
12+
cb({ state: 'COMPLETED', statusCode: msg.payload?.statusCode || 200, message: msg.payload?.message || 'OK'})
13+
} else {
14+
node.error('No callback provided for put response')
15+
}
16+
})
17+
}
18+
RED.nodes.registerType("signalk-put-success", SignalK);
19+
}
20+

signalk-send-put.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ module.exports = function(RED) {
2020
if ( reply.state === 'COMPLETED' ) {
2121
if ( reply.statusCode === 200 ) {
2222
node.status({fill:'green',shape:"dot",text:`value: ${msg.payload}`})
23-
node.send([{ payload: reply}, null])
23+
node.send([{ payload: reply, putCallBack: msg.putCallBack}, null])
2424
} else if ( reply.state === 'PENDING' ) {
2525
node.status({fill:'yellow',shape:"dot",text:'pending...'})
2626
} else {
2727
node.status({fill:'red',shape:"dot",text:`error`})
2828
node.error(`put error ${reply.statusCode} ${reply.message || ''}`)
29-
node.send([null, { payload: reply}])
29+
node.send([null, { payload: reply, putCallBack: msg.putCallBack}])
3030
}
3131
}
3232
}, config.source && config.source.length > 0 ? config.source : undefined)

0 commit comments

Comments
 (0)