From c9d3f4a49e50fcc38536eb31e60a2ee774105670 Mon Sep 17 00:00:00 2001 From: Evgenij Spitsyn Date: Thu, 19 Nov 2020 12:55:33 +0300 Subject: [PATCH] added create-device, stream-device nodes --- create-device.html | 52 ++++++++++++++++++++++++++++++++++ create-device.js | 68 +++++++++++++++++++++++++++++++++++++++++++++ flespi-server.html | 23 +++++++++++++++ flespi-server.js | 8 ++++++ flespi.png | Bin 0 -> 1010 bytes icons/flespi.png | Bin 0 -> 1010 bytes package-lock.json | 21 ++++++++++++++ package.json | 24 ++++++++++++++++ stream-device.html | 38 +++++++++++++++++++++++++ stream-device.js | 54 +++++++++++++++++++++++++++++++++++ 10 files changed, 288 insertions(+) create mode 100644 create-device.html create mode 100644 create-device.js create mode 100644 flespi-server.html create mode 100644 flespi-server.js create mode 100644 flespi.png create mode 100644 icons/flespi.png create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 stream-device.html create mode 100644 stream-device.js 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 0000000000000000000000000000000000000000..8dbda2feeff7806bfa4d2e1e9da0f2fef953c3d9 GIT binary patch literal 1010 zcmVEX>4Tx04R}tkv&MmKpe$iQ>9WWB4$u=$WWau6cusQDionYs1;guFdzMbCJjl7 zi=*ILaPVWX>fqw6tAnc`2!4P#IXWr2NQwVT3N2zhIPS;0dyl(!0N1D}n$;BtG~G7S z$%K&2tqP%6gfRpjar8@yWz30U3clm(9s#!A#aNdAxj#p*nzs}nAP~ zITlcb2Fdk<{lV{Ut-|DlmlR0=-7k*wF$#osfo9!tzK)BVfh)c3uQq_0Ptxmc zEqVm>Z37qAZB5<-E_Z;zCqptNR|?YP3I*W(jJ_!c4BP_YHLthUK29Hi40V;d0S*p< zu_9%!`@FlSv$ucGwEFu2mQ8ZCw~@?600006VoOIv00000008+zyMF)x010qNS#tmY z4c7nw4c7reD4Tcy000McNlirueSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00GKLL_t(Y$L*FqPZVJkg`al=tb#!(Odtd*LJYMgVvJHi zw6f+$SkY0~S@0h$O>88XD2YT1TQD(3BZUwX3ZpTg5+U*->_hVwo6WGZyR$PDx0}qJ zIdjf?&zTG7RZxXyn$-YY00x13zyvT348)dyz#8xYcnPcn`yqsrmcLN#RVURa>W=Ak zqAsh`YFF+1IvNBW2Oa=3Kz~a6F7O`s2y6o7sM1k0EKBMwbwxc+Vfamb6#IwO&Q_%j z>V&$fR_e(4L!DQL)N-5yvYhEvb*&D=1NDV^qiv(E_NfbzQBrNGb826!7-tRFeP9|W zCEVKsmVlqYU3EL!XD5VE$-;I(U8*B-MLmpCAE^84w)#}P*62``5&~ijxRVk(UC}1x z$ox~hH&qoTO-bDeOahl0c=jFe3|Ilagb=o}$lRm8N|E`yI;)PT9qpRt-ss!2Kv&h9 znZ23-++W8+%j#$@zp4nFlv1yA1FZtC&EyUBcH8AmIOFx2SbUiKR-ksP&#Qnxt5@>C zs*S`W;BD3Q<4pc+lJ#}mD?WiNy6aR2}S07*qoM6N<$f|NPA{r~^~ literal 0 HcmV?d00001 diff --git a/icons/flespi.png b/icons/flespi.png new file mode 100644 index 0000000000000000000000000000000000000000..2f91f32f89e9484a404eff0f15c26a82e2fde919 GIT binary patch literal 1010 zcmVEX>4Tx04R}tkv&MmKpe$iQ>9WWB4$u=$WWau6cusQDionYs1;guFdzMbCJjl7 zi=*ILaPVWX>fqw6tAnc`2!4P#IXWr2NQwVT3N2zhIPS;0dyl(!0N1D}n$;BtG~G7S z$%K&2tqP%6gfRpjar8@yWz30U3clm(9s#!A#aNdAxj#p*nzs}nAP~ zITlcb2Fdk<{lV{Ut-|DlmlR0=-7k*wF$#osfo9!tzK)BVfh)c3uQq_0Ptxmc zEqVm>Z37qAZB5<-E_Z;zCqptNR|?YP3I*W(jJ_!c4BP_YHLthUK29Hi40V;d0S*p< zu_9%!`@FlSv$ucGwEFu2mQ8ZCw~@?600006VoOIv00000008+zyMF)x010qNS#tmY z4c7nw4c7reD4Tcy000McNlirueSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00GKLL_t(Y$L*FqPZVJkg`al=tb#!(Odtd*LJYMgVvJHi zw6f+$SkY0~S@0h$O>88XD2YT1TQD(3BZUwX3ZpTg5+U*->_hVwo6WGZyR$PDx0}qJ zIdjf?&zTG7RZxXyn$-YY00x13zyvT348)dyz#8xYcnPcn`yqsrmcLN#RVURa>W=Ak zqAsh`YFF+1IvNBW2Oa=3Kz~a6F7O`s2y6o7sM1k0EKBMwbwxc+Vfamb6#IwO&Q_%j z>V&$fR_e(4L!DQL)N-5yvYhEvb*&D=1NDV^qiv(E_NfbzQBrNGb826!7-tRFeP9|W zCEVKsmVlqYU3EL!XD5VE$-;I(U8*B-MLmpCAE^84w)#}P*62``5&~ijxRVk(UC}1x z$ox~hH&qoTO-bDeOahl0c=jFe3|Ilagb=o}$lRm8N|E`yI;)PT9qpRt-ss!2Kv&h9 znZ23-++W8+%j#$@zp4nFlv1yA1FZtC&EyUBcH8AmIOFx2SbUiKR-ksP&#Qnxt5@>C zs*S`W;BD3Q<4pc+lJ#}mD?WiNy6aR2}S07*qoM6N<$f-?2FPyhe` literal 0 HcmV?d00001 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