diff --git a/create-device.html b/create-device.html new file mode 100644 index 0000000..a475ed2 --- /dev/null +++ b/create-device.html @@ -0,0 +1,52 @@ + + + + + \ No newline at end of file diff --git a/create-device.js b/create-device.js new file mode 100644 index 0000000..110b791 --- /dev/null +++ b/create-device.js @@ -0,0 +1,68 @@ +const axios = require('axios') + +module.exports = function(RED) { + function CreateDevice(config) { + RED.nodes.createNode(this,config); + // Retrieve the config node + this.server = RED.nodes.getNode(config.server); + this.idents = {} + 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('ident') && node.server.token && + !this.idents[msg.payload.ident]) { + axios.post( + "https://" + (node.server.host || "flespi.io") + "/gw/devices", + [ + { + "name": config.name.replace('%ident%', msg.payload.ident), + "device_type_id": parseInt(config.devicetype || 0) , + "messages_ttl": parseInt(config.messagesttl || 0), + "configuration": { + "ident": msg.payload.ident + } + } + ], + { + headers: {Authorization: "FlespiToken " + node.server.token.replace('FlespiToken ', '')} + } + ).then( + (response) => { + this.idents[msg.payload.ident] = true; + 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) => { + this.idents[msg.payload.ident] = true; + 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) => { + // done(JSON.stringify(e)) + }) + } + }); + } else { + // No server config + } + } + RED.nodes.registerType("create-device", CreateDevice, { + credentials: { + token: {type:"password"} + } + }); +} \ No newline at end of file diff --git a/flespi-server.html b/flespi-server.html new file mode 100644 index 0000000..06af601 --- /dev/null +++ b/flespi-server.html @@ -0,0 +1,23 @@ + + + \ No newline at end of file diff --git a/flespi-server.js b/flespi-server.js new file mode 100644 index 0000000..42de600 --- /dev/null +++ b/flespi-server.js @@ -0,0 +1,8 @@ +module.exports = function(RED) { + function FlespiServer(n) { + RED.nodes.createNode(this,n); + this.host = n.host; + this.token = n.token; + } + RED.nodes.registerType("flespi-server",FlespiServer); +} \ No newline at end of file diff --git a/flespi.png b/flespi.png new file mode 100644 index 0000000..8dbda2f Binary files /dev/null and b/flespi.png differ diff --git a/icons/flespi.png b/icons/flespi.png new file mode 100644 index 0000000..2f91f32 Binary files /dev/null and b/icons/flespi.png differ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..8e739ce --- /dev/null +++ b/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "node-red-flespi", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "axios": { + "version": "0.21.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.0.tgz", + "integrity": "sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, + "follow-redirects": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", + "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..82c6d2a --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "node-red-flespi", + "version": "1.0.0", + "description": "flespi nodes for node-red", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "node-red": { + "nodes": { + "stream-device": "stream-device.js", + "create-device": "create-device.js", + "flespi-server": "flespi-server.js" + } + }, + "keywords": [ + "node-red", + "flespi" + ], + "author": "Evgenij Spitsyn ", + "license": "MIT", + "dependencies": { + "axios": "^0.21.0" + } +} diff --git a/stream-device.html b/stream-device.html new file mode 100644 index 0000000..7be5446 --- /dev/null +++ b/stream-device.html @@ -0,0 +1,38 @@ + + + + + \ No newline at end of file diff --git a/stream-device.js b/stream-device.js new file mode 100644 index 0000000..9e8e842 --- /dev/null +++ b/stream-device.js @@ -0,0 +1,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"} + } + }); +} \ No newline at end of file