-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added create-device, stream-device nodes
- Loading branch information
Showing
10 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script type="text/javascript"> | ||
RED.nodes.registerType('create-device',{ | ||
category: 'flespi', | ||
color: '#ff6666', | ||
labelStyle: 'text-white', | ||
defaults: { | ||
nodename: {value:""}, | ||
name: {value:"", default: "%ident%", required: true}, | ||
check: {value:true}, | ||
messagesttl: {value:31536000, validate:RED.validators.number(), required: true, default: 31536000}, | ||
devicetype: {value:0, validate:RED.validators.number(), required: true, default: 0}, | ||
server: {type:"flespi-server",required:true} | ||
}, | ||
inputs:1, | ||
outputs:1, | ||
icon: "flespi.png", | ||
label: function() { | ||
return "Create device " + this.nodename || ""; | ||
} | ||
}); | ||
</script> | ||
|
||
<script type="text/html" data-template-name="create-device"> | ||
<div class="form-row"> | ||
<label for="node-input-server"><i class="fa fa-globe"></i> flespi</label> | ||
<input type="text" id="node-input-server"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-nodename"><i class="fa fa-tag"></i> Node name</label> | ||
<input type="text" id="node-input-nodename" placeholder="Node name"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-name"><i class="fa fa-tag"></i> Device name (use %ident%)</label> | ||
<input type="text" id="node-input-name" placeholder="Device name"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-messagesttl"><i class="fa fa-tag"></i> Messages ttl (s)</label> | ||
<input type="number" id="node-input-messagesttl" placeholder="Messages ttl (s)"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-devicetype"><i class="fa fa-tag"></i> Device type id</label> | ||
<input type="number" id="node-input-devicetype" placeholder="Device type id"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-check"><i class="fa fa-tag"></i> Check before create</label> | ||
<input type="checkbox" id="node-input-check"> | ||
</div> | ||
</script> | ||
|
||
<script type="text/html" data-help-name="create-device"> | ||
<p>A simple node that create new flespi device with received ident</p> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script type="text/javascript"> | ||
RED.nodes.registerType('flespi-server',{ | ||
category: 'config', | ||
defaults: { | ||
host: {value:"flespi.io",required:true}, | ||
token: {type:"password",required:true} | ||
}, | ||
label: function() { | ||
return this.host; | ||
} | ||
}); | ||
</script> | ||
|
||
<script type="text/html" data-template-name="flespi-server"> | ||
<div class="form-row"> | ||
<label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label> | ||
<input type="text" id="node-config-input-host"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-config-input-token"><i class="fa fa-tag"></i> FlespiToken</label> | ||
<input type="password" id="node-config-input-token"> | ||
</div> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^0.21.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script type="text/javascript"> | ||
RED.nodes.registerType('stream-device',{ | ||
category: 'flespi', | ||
color: '#ff6666', | ||
labelStyle: 'text-white', | ||
defaults: { | ||
nodename: {value:""}, | ||
streamid: {value:0, validate:RED.validators.number(), required: true, default: 0}, | ||
server: {type:"flespi-server",required:true} | ||
}, | ||
inputs:1, | ||
outputs:1, | ||
icon: "flespi.png", | ||
align: "right", | ||
label: function() { | ||
return "Stream device " + this.nodename || ""; | ||
} | ||
}); | ||
</script> | ||
|
||
<script type="text/html" data-template-name="stream-device"> | ||
<div class="form-row"> | ||
<label for="node-input-server"><i class="fa fa-globe"></i> flespi</label> | ||
<input type="text" id="node-input-server"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-nodename"><i class="fa fa-tag"></i> Node name</label> | ||
<input type="text" id="node-input-nodename" placeholder="Node name"> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-streamid"><i class="fa fa-tag"></i> Stream id</label> | ||
<input type="number" id="node-input-streamid" placeholder="Stream id"> | ||
</div> | ||
</script> | ||
|
||
<script type="text/html" data-help-name="stream-device"> | ||
<p>A simple node that create new flespi device-stream link</p> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
} | ||
}); | ||
} |