-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem-action-node.js
More file actions
48 lines (40 loc) · 1.46 KB
/
system-action-node.js
File metadata and controls
48 lines (40 loc) · 1.46 KB
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
module.exports = function (RED) {
function SystemActionNode(config) {
RED.nodes.createNode(this, config);
var node = this;
var outputToMsg = config.outputToMsg;
node.on('input', async function (msg, send, done) {
let thingActionValue = JSON.parse(config.thingActionValue);
let thingURI = thingActionValue.uri;
let action = thingActionValue.action;
let output = await fetch(
thingURI + "/actions/" + action,
{
method: "POST",
headers: new Headers({
"Content-Type": "application/json"
}),
body: JSON.stringify(msg.payload) //Should be a list of parameters
}
)
.catch((reason) => {
node.error("Failed to invoke action. " + reason);
});
if (outputToMsg) {
try {
msg.payload = await output.json();
}
catch(error) {
if(error.message === "Unexpected end of JSON input"){ //Handle null action output
msg.payload = null;
}
else {
msg.payload = error;
}
}
}
node.send(msg);
});
}
RED.nodes.registerType("system-action-node", SystemActionNode);
}