-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-device.js
54 lines (52 loc) · 2.26 KB
/
stream-device.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
const axios = require('axios')
module.exports = function(RED) {
function StreamDevice(config) {
RED.nodes.createNode(this,config);
// Retrieve the config node
this.server = RED.nodes.getNode(config.server);
if (this.server) {
var node = this;
node.on('input', function(msg, send, done) {
send = send || function() { node.send.apply(node,arguments) }
if (typeof msg.payload === 'object' && msg.payload.hasOwnProperty('id') && node.server.token) {
axios.post(
"https://" + (node.server.host || "flespi.io") + "/gw/streams/" + config.streamid + "/devices/" + msg.payload.id,
null,
{
headers: {Authorization: "FlespiToken " + node.server.token.replace('FlespiToken ', '')}
}
).then(
(response) => {
if (response.data.result && response.data.result[0]) {
msg.payload = response.data.result[0];
send([msg, null]);
}
if (response.data.errors) {
msg.payload = response.data.errors;
send([null, msg]);
}
},
(error) => {
if (errors.response.data.result && errors.response.data.result[0]) {
msg.payload = errors.response.data.result[0];
send([msg, null]);
}
if (error.response.data.errors) {
msg.payload = error.response.data.errors;
send([null, msg]);
}
// done(JSON.stringify(msg));
}
).catch((e)=>{})
}
});
} else {
// No server config
}
}
RED.nodes.registerType("stream-device", StreamDevice, {
credentials: {
token: {type:"password"}
}
});
}