1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Update node-red-nodes hardware nodes to use strict and pass jshint scan

This commit is contained in:
Dave C-J 2014-06-28 23:35:21 +01:00
parent 62956b0bb8
commit 9e0585a721
7 changed files with 496 additions and 481 deletions

View File

@ -14,138 +14,140 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var util = require("util"); "use strict";
var exec = require('child_process').exec; var util = require("util");
var fs = require('fs'); var exec = require('child_process').exec;
var fs = require('fs');
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
throw "Info : Ignoring Raspberry Pi specific node."; throw "Info : Ignoring Raspberry Pi specific node.";
} }
if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed
throw "Info : Can't find Raspberry Pi wiringPi gpio command."; throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
} }
// Map names of pins to Gordon's gpio PiFace pin numbers // Map names of pins to Gordon's gpio PiFace pin numbers
var pintable = { var pintable = {
// Physical : WiringPi // Physical : WiringPi
"Button S1":"200", "Button S1":"200",
"Button S2":"201", "Button S2":"201",
"Button S3":"202", "Button S3":"202",
"Button S4":"203", "Button S4":"203",
"Input 5":"204", "Input 5":"204",
"Input 6":"205", "Input 6":"205",
"Input 7":"206", "Input 7":"206",
"Input 8":"207", "Input 8":"207",
"Output0":"208", "Output0":"208",
"Output1":"209", "Output1":"209",
"Output2":"210", "Output2":"210",
"Output3":"211", "Output3":"211",
"Output4":"212", "Output4":"212",
"Output5":"213", "Output5":"213",
"Output6":"214", "Output6":"214",
"Output7":"215", "Output7":"215",
"LED 0 / Relay 0":"200", "LED 0 / Relay 0":"200",
"LED 1 / Relay 1":"201", "LED 1 / Relay 1":"201",
"LED 2":"202", "LED 2":"202",
"LED 3":"203", "LED 3":"203",
"LED 4":"204", "LED 4":"204",
"LED 5":"205", "LED 5":"205",
"LED 6":"206", "LED 6":"206",
"LED 7":"207" "LED 7":"207"
} }
var tablepin = { var tablepin = {
// WiringPi : Physical // WiringPi : Physical
"200":"S1", "200":"S1",
"201":"S2", "201":"S2",
"202":"S3", "202":"S3",
"203":"S4", "203":"S4",
"204":"I5", "204":"I5",
"205":"I6", "205":"I6",
"206":"I7", "206":"I7",
"207":"I8", "207":"I8",
"208":"O0", "208":"O0",
"209":"O1", "209":"O1",
"210":"O2", "210":"O2",
"211":"O3", "211":"O3",
"212":"O4", "212":"O4",
"213":"O5", "213":"O5",
"214":"O6", "214":"O6",
"215":"O7", "215":"O7",
"200":"L0", "200":"L0",
"201":"L1", "201":"L1",
"202":"L2", "202":"L2",
"203":"L3", "203":"L3",
"204":"L4", "204":"L4",
"205":"L5", "205":"L5",
"206":"L6", "206":"L6",
"207":"L7" "207":"L7"
} }
function PiFACEInNode(n) { function PiFACEInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.buttonState = -1; this.buttonState = -1;
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
this.intype = n.intype; this.intype = n.intype;
var node = this; var node = this;
if (node.pin) { if (node.pin) {
exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) { exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
else { else {
node._interval = setInterval( function() { node._interval = setInterval( function() {
exec("gpio -p read "+node.pin, function(err,stdout,stderr) { exec("gpio -p read "+node.pin, function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
else { else {
if (node.buttonState !== Number(stdout)) { if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState; var previousState = node.buttonState;
node.buttonState = Number(stdout); node.buttonState = Number(stdout);
if (previousState !== -1) { if (previousState !== -1) {
var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState}; var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState};
node.send(msg); node.send(msg);
}
} }
} }
} });
}); }, 200);
}, 200); }
} });
}
else {
node.error("Invalid PiFACE pin: "+node.pin);
}
node.on("close", function() {
clearInterval(node._interval);
}); });
} }
else {
node.error("Invalid PiFACE pin: "+node.pin); function PiFACEOutNode(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
var node = this;
if (node.pin) {
node.on("input", function(msg) {
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload);
if ((out === 0)|(out === 1)) {
exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) { node.error(err); }
});
}
else { node.warn("Invalid input - not 0 or 1"); }
});
}
else {
node.error("Invalid PiFACE pin: "+node.pin);
}
} }
node.on("close", function() {
clearInterval(node._interval);
exec("gpio load spi",function(err,stdout,stderr) {
if (err) {
util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.');
}
RED.nodes.registerType("rpi-piface in",PiFACEInNode);
RED.nodes.registerType("rpi-piface out",PiFACEOutNode);
}); });
} }
function PiFACEOutNode(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
var node = this;
if (node.pin) {
node.on("input", function(msg) {
if (msg.payload === "true") msg.payload = true;
if (msg.payload === "false") msg.payload = false;
var out = Number(msg.payload);
if ((out == 0)|(out == 1)) {
exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) node.error(err);
});
}
else node.warn("Invalid input - not 0 or 1");
});
}
else {
node.error("Invalid PiFACE pin: "+node.pin);
}
}
exec("gpio load spi",function(err,stdout,stderr) {
if (err) {
util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.');
}
RED.nodes.registerType("rpi-piface in",PiFACEInNode);
RED.nodes.registerType("rpi-piface out",PiFACEOutNode);
});

View File

@ -14,159 +14,161 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var util = require("util"); "use strict";
var exec = require('child_process').exec; var util = require("util");
var fs = require('fs'); var exec = require('child_process').exec;
var fs = require('fs');
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
throw "Info : Ignoring Raspberry Pi specific node."; throw "Info : Ignoring Raspberry Pi specific node.";
} }
if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed
throw "Info : Can't find Raspberry Pi wiringPi gpio command."; throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
} }
// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant) // Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant)
var pintable = { var pintable = {
// Physical : WiringPi // Physical : WiringPi
"Amber LED":"0", "Amber LED":"0",
"Buzzer ":"1", "Buzzer ":"1",
"Red LED":"2", "Red LED":"2",
"Out E":"3", "Out E":"3",
"Out F":"4", "Out F":"4",
"Out G":"5", "Out G":"5",
"Out H":"6", "Out H":"6",
"Green LED":"7", "Green LED":"7",
"In C":"10", "In C":"10",
"In B":"11", "In B":"11",
"In D":"12", "In D":"12",
"In A":"13", "In A":"13",
"Red Button":"14", "Red Button":"14",
} }
var tablepin = { var tablepin = {
// WiringPi : Physical // WiringPi : Physical
"0":"Amber", "0":"Amber",
"1":"Buzzer", "1":"Buzzer",
"2":"Red", "2":"Red",
"3":"E", "3":"E",
"4":"F", "4":"F",
"5":"G", "5":"G",
"6":"H", "6":"H",
"7":"Green", "7":"Green",
"10":"C", "10":"C",
"11":"B", "11":"B",
"12":"D", "12":"D",
"13":"A", "13":"A",
"14":"R", "14":"R",
} }
function PibrellaIn(n) { function PibrellaIn(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.buttonState = -1; this.buttonState = -1;
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
var node = this; var node = this;
if (node.pin) { if (node.pin) {
exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) { exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
else { else {
node._interval = setInterval( function() { node._interval = setInterval( function() {
exec("gpio read "+node.pin, function(err,stdout,stderr) { exec("gpio read "+node.pin, function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
else { else {
if (node.buttonState !== Number(stdout)) { if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState; var previousState = node.buttonState;
node.buttonState = Number(stdout); node.buttonState = Number(stdout);
if (previousState !== -1) { if (previousState !== -1) {
var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState}; var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState};
node.send(msg); node.send(msg);
}
} }
} }
} });
}); }, 200);
}, 200);
}
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
clearInterval(node._interval);
});
}
function PibrellaOut(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
var node = this;
if (node.pin == "1") {
exec("gpio mode 1 pwm");
process.nextTick(function() {
exec("gpio pwm-ms");
node.on("input", function(msg) {
var out = Number(msg.payload);
if (out == 1) { // fixed buzz
exec("gpio pwm 1 511");
exec("gpio pwmc 100");
}
else if ((out >= 2) && (out <= 9999)) { // set buzz to a value
exec("gpio pwm 1 511");
exec("gpio pwmc "+out);
}
else { exec("gpio pwm 1 0"); } // turn it off
});
});
}
else if (node.pin) {
process.nextTick(function() {
exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) node.error(err);
else {
node.on("input", function(msg) {
if (msg.payload === "true") msg.payload = true;
if (msg.payload === "false") msg.payload = false;
var out = Number(msg.payload);
if ((out == 0)|(out == 1)) {
exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) node.error(err);
});
}
else node.warn("Invalid input - not 0 or 1");
});
} }
}); });
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
clearInterval(node._interval);
}); });
} }
else {
node.error("Invalid GPIO pin: "+node.pin); function PibrellaOut(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
var node = this;
if (node.pin == "1") {
exec("gpio mode 1 pwm");
process.nextTick(function() {
exec("gpio pwm-ms");
node.on("input", function(msg) {
var out = Number(msg.payload);
if (out == 1) { // fixed buzz
exec("gpio pwm 1 511");
exec("gpio pwmc 100");
}
else if ((out >= 2) && (out <= 9999)) { // set buzz to a value
exec("gpio pwm 1 511");
exec("gpio pwmc "+out);
}
else { exec("gpio pwm 1 0"); } // turn it off
});
});
}
else if (node.pin) {
process.nextTick(function() {
exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
node.on("input", function(msg) {
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload);
if ((out === 0)|(out === 1)) {
exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) { node.error(err); }
});
}
else { node.warn("Invalid input - not 0 or 1"); }
});
}
});
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
exec("gpio mode "+node.pin+" in");
});
} }
node.on("close", function() { //exec("gpio mode 0 out",function(err,stdout,stderr) {
exec("gpio mode "+node.pin+" in"); //if (err) {
}); //util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.');
//}
//exec("gpio mode 1 out");
//exec("gpio mode 2 out");
//exec("gpio mode 3 out");
//exec("gpio mode 4 out");
//exec("gpio mode 5 out");
//exec("gpio mode 6 out");
//exec("gpio mode 7 out");
//exec("gpio mode 10 in");
//exec("gpio mode 11 in");
//exec("gpio mode 12 in");
//exec("gpio mode 13 in");
//exec("gpio mode 14 in");
//});
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
} }
//exec("gpio mode 0 out",function(err,stdout,stderr) {
//if (err) {
//util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.');
//}
//exec("gpio mode 1 out");
//exec("gpio mode 2 out");
//exec("gpio mode 3 out");
//exec("gpio mode 4 out");
//exec("gpio mode 5 out");
//exec("gpio mode 6 out");
//exec("gpio mode 7 out");
//exec("gpio mode 10 in");
//exec("gpio mode 11 in");
//exec("gpio mode 12 in");
//exec("gpio mode 13 in");
//exec("gpio mode 14 in");
//});
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);

View File

@ -14,50 +14,52 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var util = require('util'); "use strict";
var fs = require('fs'); var util = require('util');
var fs = require('fs');
// check if /dev/ledborg exists - if not then don't even show the node. // check if /dev/ledborg exists - if not then don't even show the node.
if (!fs.existsSync("/dev/ledborg")) { if (!fs.existsSync("/dev/ledborg")) {
throw "Info : PiBorg hardware : LedBorg not found"; throw "Info : PiBorg hardware : LedBorg not found";
} }
function LedBorgNode(n) { function LedBorgNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var p1 = /^[0-2][0-2][0-2]$/ var p1 = /^[0-2][0-2][0-2]$/
var p2 = /^\#[A-Fa-f0-9]{6}$/ var p2 = /^\#[A-Fa-f0-9]{6}$/
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (p1.test(msg.payload)) { if (p1.test(msg.payload)) {
fs.writeFile('/dev/ledborg', msg.payload, function (err) { fs.writeFile('/dev/ledborg', msg.payload, function (err) {
if (err) node.warn(msg.payload+" : No LedBorg found"); if (err) { node.warn(msg.payload+" : No LedBorg found"); }
}); });
} }
else if (p2.test(msg.payload)) { else if (p2.test(msg.payload)) {
var r = Math.floor(parseInt(msg.payload.slice(1,3),16)/88).toString(); var r = Math.floor(parseInt(msg.payload.slice(1,3),16)/88).toString();
var g = Math.floor(parseInt(msg.payload.slice(3,5),16)/88).toString(); var g = Math.floor(parseInt(msg.payload.slice(3,5),16)/88).toString();
var b = Math.floor(parseInt(msg.payload.slice(5),16)/88).toString(); var b = Math.floor(parseInt(msg.payload.slice(5),16)/88).toString();
fs.writeFile('/dev/ledborg', r+g+b, function (err) { fs.writeFile('/dev/ledborg', r+g+b, function (err) {
if (err) node.warn(r+g+b+" : No LedBorg found"); if (err) { node.warn(r+g+b+" : No LedBorg found"); }
});
}
else {
// you can add fancy colours by name here if you want...
// these are the @cheerlight ones.
var colors = {"red":"200","green":"020","blue":"002","cyan":"022","white":"222","pink":"201",
"warmwhite":"221","purple":"101","magenta":"202","yellow":"220","amber":"220","orange":"210","black":"000"}
if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()];
fs.writeFile('/dev/ledborg', c, function (err) {
if (err) node.warn(msg.payload+" : No LedBorg found");
}); });
} }
else { else {
node.warn("Invalid LedBorg colour code"); // you can add fancy colours by name here if you want...
// these are the @cheerlight ones.
var colors = {"red":"200","green":"020","blue":"002","cyan":"022","white":"222","pink":"201",
"warmwhite":"221","purple":"101","magenta":"202","yellow":"220","amber":"220","orange":"210","black":"000"}
if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()];
fs.writeFile('/dev/ledborg', c, function (err) {
if (err) { node.warn(msg.payload+" : No LedBorg found"); }
});
}
else {
node.warn("Invalid LedBorg colour code");
}
} }
} });
}); }
RED.nodes.registerType("ledborg",LedBorgNode);
} }
RED.nodes.registerType("ledborg",LedBorgNode);

View File

@ -14,49 +14,51 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var blinkstick = require("blinkstick"); "use strict";
var blinkstick = require("blinkstick");
Object.size = function(obj) { Object.size = function(obj) {
var size = 0, key; var size = 0;
for (key in obj) { if (obj.hasOwnProperty(key)) size++; } for (var key in obj) { if (obj.hasOwnProperty(key)) { size++; } }
return size; return size;
}; };
function BlinkStick(n) { function BlinkStick(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var p1 = /^\#[A-Fa-f0-9]{6}$/ var p1 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/ var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.led = blinkstick.findFirst(); // maybe try findAll() (one day) this.led = blinkstick.findFirst(); // maybe try findAll() (one day)
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { if (msg != null) {
if (Object.size(node.led) !== 0) { if (Object.size(node.led) !== 0) {
try { try {
if (p2.test(msg.payload)) { if (p2.test(msg.payload)) {
var rgb = msg.payload.split(","); var rgb = msg.payload.split(",");
node.led.setColor(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); node.led.setColor(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255);
}
else {
node.led.setColor(msg.payload.toLowerCase().replace(/\s+/g,''));
}
} }
else { catch (err) {
node.led.setColor(msg.payload.toLowerCase().replace(/\s+/g,'')); node.warn("BlinkStick missing ?");
node.led = blinkstick.findFirst();
} }
} }
catch (err) { else {
node.warn("BlinkStick missing ?"); //node.warn("No BlinkStick found");
node.led = blinkstick.findFirst(); node.led = blinkstick.findFirst();
} }
} }
else { });
//node.warn("No BlinkStick found"); if (Object.size(node.led) === 0) {
node.led = blinkstick.findFirst(); node.error("No BlinkStick found");
}
} }
});
if (Object.size(node.led) === 0) {
node.error("No BlinkStick found");
} }
RED.nodes.registerType("blinkstick",BlinkStick);
} }
RED.nodes.registerType("blinkstick",BlinkStick);

View File

@ -14,70 +14,73 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var Blink1 = require("node-blink1"); "use strict";
// create a single global blink1 object var Blink1 = require("node-blink1");
// all blink1 nodes affect the same (single) led // create a single global blink1 object
var blink1 = null; // all blink1 nodes affect the same (single) led
var blink1 = null;
function Blink1Node(n) { function Blink1Node(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.fade = Number(n.fade) || 0; this.fade = Number(n.fade) || 0;
var node = this; var node = this;
try { try {
var p1 = /^\#[A-Fa-f0-9]{6}$/ var p1 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/ var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.on("input", function(msg) { this.on("input", function(msg) {
blink1 = blink1 || new Blink1.Blink1(); blink1 = blink1 || new Blink1.Blink1();
if (blink1) { if (blink1) {
try { var r,g,b;
if (p1.test(msg.payload)) { try {
// if it is a hex colour string if (p1.test(msg.payload)) {
var r = parseInt(msg.payload.slice(1,3),16); // if it is a hex colour string
var g = parseInt(msg.payload.slice(3,5),16); r = parseInt(msg.payload.slice(1,3),16);
var b = parseInt(msg.payload.slice(5),16); g = parseInt(msg.payload.slice(3,5),16);
if (node.fade == 0) { blink1.setRGB( r, g, b ); } b = parseInt(msg.payload.slice(5),16);
else { blink1.fadeToRGB(node.fade, r, g, b ); } if (node.fade === 0) { blink1.setRGB( r, g, b ); }
}
else if (p2.test(msg.payload)) {
// if it is a r,g,b triple
var rgb = msg.payload.split(',');
if (node.fade == 0) { blink1.setRGB(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
else { blink1.fadeToRGB(node.fade, parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
}
else {
// you can add fancy colours by name here if you want...
// these are the @cheerlight ones.
var colors = {"red":"#FF0000","green":"#008000","blue":"#0000FF","cyan":"#00FFFF","white":"#FFFFFF","warmwhite":"#FDF5E6",
"purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"}
if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()];
var r = parseInt(c.slice(1,3),16);
var g = parseInt(c.slice(3,5),16);
var b = parseInt(c.slice(5),16);
if (node.fade == 0) { blink1.setRGB( r, g, b ); }
else { blink1.fadeToRGB(node.fade, r, g, b ); } else { blink1.fadeToRGB(node.fade, r, g, b ); }
} }
else { else if (p2.test(msg.payload)) {
node.warn("Blink1 : invalid msg : "+msg.payload); // if it is a r,g,b triple
var rgb = msg.payload.split(',');
if (node.fade === 0) { blink1.setRGB(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
else { blink1.fadeToRGB(node.fade, parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
} }
else {
// you can add fancy colours by name here if you want...
// these are the @cheerlight ones.
var colors = {"red":"#FF0000","green":"#008000","blue":"#0000FF","cyan":"#00FFFF","white":"#FFFFFF","warmwhite":"#FDF5E6",
"purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"}
if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()];
r = parseInt(c.slice(1,3),16);
g = parseInt(c.slice(3,5),16);
b = parseInt(c.slice(5),16);
if (node.fade === 0) { blink1.setRGB( r, g, b ); }
else { blink1.fadeToRGB(node.fade, r, g, b ); }
}
else {
node.warn("Blink1 : invalid msg : "+msg.payload);
}
}
} catch (e) { node.warn("Blink1 : error"); blink1 = null; }
} }
} catch (e) { node.warn("Blink1 : error"); blink1 = null; } else {
} node.warn("Blink1 : not found");
else { }
node.warn("Blink1 : not found"); });
} this.on("close", function() {
}); if (blink1 && typeof blink1.close == "function") {
this.on("close", function() { //blink1.close(); //This ought to work but seems to cause more hangs on closing than not...
if (blink1 && typeof blink1.close == "function") { }
//blink1.close(); //This ought to work but seems to cause more hangs on closing than not... blink1 = null;
} });
blink1 = null; }
}); catch(e) {
} node.error("No Blink1 found (" + e + ")");
catch(e) { }
node.error("No Blink1 found (" + e + ")");
} }
RED.nodes.registerType("blink1",Blink1Node);
} }
RED.nodes.registerType("blink1",Blink1Node);

View File

@ -14,56 +14,58 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var HID = require('node-hid'); "use strict";
var device; var HID = require('node-hid');
var node; var device;
var node;
function DigiRGBNode(n) { function DigiRGBNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
node=this; node=this;
var devices = HID.devices(0x16c0,0x05df); var devices = HID.devices(0x16c0,0x05df);
for (var i=0; i< devices.length; i++) { for (var i=0; i< devices.length; i++) {
if (devices[i].product == 'DigiUSB') { if (devices[i].product == 'DigiUSB') {
path = devices[i].path; path = devices[i].path;
node.log("found: " + path); node.log("found: " + path);
try { try {
device = new HID.HID(devices[i].path); device = new HID.HID(devices[i].path);
break; break;
} catch (e) { } catch (e) {
node.log(e) node.log(e)
}
}
}
var p1 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
if (device) {
this.on("input", function(msg) {
if (msg != null) {
if (p1.test(msg.payload)) {
var r = parseInt(msg.payload.slice(1,3),16);
var g = parseInt(msg.payload.slice(3,5),16);
var b = parseInt(msg.payload.slice(5),16);
device.sendFeatureReport([115,r,g,b]);
} else if (p2.test(msg.payload)) {
var args = msg.payload.split(',');
if (args.length == 3) {
device.sendFeatureReport([115,parseInt(args[0]),parseInt(args[1]),parseInt(args[2])]);
} }
} else {
node.warn("incompatable input - " + msg.payload);
} }
} }
});
} else {
node.warn("no digispark RGB found");
}
this.on('close', function() { var p1 = /^\#[A-Fa-f0-9]{6}$/
if (device) { device.close(); } var p2 = /[0-9]+,[0-9]+,[0-9]+/
});
if (device) {
this.on("input", function(msg) {
if (msg != null) {
if (p1.test(msg.payload)) {
var r = parseInt(msg.payload.slice(1,3),16);
var g = parseInt(msg.payload.slice(3,5),16);
var b = parseInt(msg.payload.slice(5),16);
device.sendFeatureReport([115,r,g,b]);
} else if (p2.test(msg.payload)) {
var args = msg.payload.split(',');
if (args.length == 3) {
device.sendFeatureReport([115,parseInt(args[0]),parseInt(args[1]),parseInt(args[2])]);
}
} else {
node.warn("incompatable input - " + msg.payload);
}
}
});
} else {
node.warn("no digispark RGB found");
}
this.on('close', function() {
if (device) { device.close(); }
});
}
RED.nodes.registerType("digiRGB",DigiRGBNode);
} }
RED.nodes.registerType("digiRGB",DigiRGBNode);

View File

@ -14,47 +14,49 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var WeMo = new require('wemo'); "use strict";
var WeMo = new require('wemo');
function WeMoOut(n) { function WeMoOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.ipaddr = n.ipaddr; this.ipaddr = n.ipaddr;
this.wemoSwitch = new WeMo(n.ipaddr); this.wemoSwitch = new WeMo(n.ipaddr);
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { if (msg != null) {
var state = 0; var state = 0;
if ( msg.payload == 1 || msg.payload == true || msg.payload == "on" ) { var state = 1; } if ( msg.payload == 1 || msg.payload === true || msg.payload == "on" ) { state = 1; }
node.wemoSwitch.setBinaryState(state, function(err, result) { node.wemoSwitch.setBinaryState(state, function(err, result) {
if (err) node.warn(err); if (err) { node.warn(err); }
//else { node.log(result); } //else { node.log(result); }
}); });
}
});
}
RED.nodes.registerType("wemo out",WeMoOut);
function WeMoIn(n) {
RED.nodes.createNode(this,n);
this.ipaddr = n.ipaddr;
this.wemoSwitch = new WeMo(n.ipaddr);
this.wemoSwitch.state = 0;
var node = this;
var tick = setInterval(function() {
wemoSwitch.getBinaryState(function(err, result) {
if (err) node.warn(err);
if (parseInt(result) != wemoSwitch.state) {
wemoSwitch.state = parseInt(result);
node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr});
} }
}); });
}, 2000); }
RED.nodes.registerType("wemo out",WeMoOut);
this.on("close", function() { function WeMoIn(n) {
clearInterval(tick); RED.nodes.createNode(this,n);
}); this.ipaddr = n.ipaddr;
this.wemoSwitch = new WeMo(n.ipaddr);
this.wemoSwitch.state = 0;
var node = this;
var tick = setInterval(function() {
wemoSwitch.getBinaryState(function(err, result) {
if (err) { node.warn(err); }
if (parseInt(result) != wemoSwitch.state) {
wemoSwitch.state = parseInt(result);
node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr});
}
});
}, 2000);
this.on("close", function() {
clearInterval(tick);
});
}
RED.nodes.registerType("wemo in",WeMoOut);
} }
RED.nodes.registerType("wemo in",WeMoOut);