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,22 +14,23 @@
* 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",
@ -54,9 +55,9 @@ var pintable = {
"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",
@ -81,9 +82,9 @@ var tablepin = {
"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];
@ -91,11 +92,11 @@ function PiFACEInNode(n) {
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;
@ -117,35 +118,36 @@ function PiFACEInNode(n) {
node.on("close", function() { node.on("close", function() {
clearInterval(node._interval); clearInterval(node._interval);
}); });
} }
function PiFACEOutNode(n) { function PiFACEOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
var node = this; var node = this;
if (node.pin) { if (node.pin) {
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg.payload === "true") msg.payload = true; if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") msg.payload = false; if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload); var out = Number(msg.payload);
if ((out == 0)|(out == 1)) { if ((out === 0)|(out === 1)) {
exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) { exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
}); });
} }
else node.warn("Invalid input - not 0 or 1"); else { node.warn("Invalid input - not 0 or 1"); }
}); });
} }
else { else {
node.error("Invalid PiFACE pin: "+node.pin); node.error("Invalid PiFACE pin: "+node.pin);
} }
} }
exec("gpio load spi",function(err,stdout,stderr) { exec("gpio load spi",function(err,stdout,stderr) {
if (err) { if (err) {
util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.'); 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 in",PiFACEInNode);
RED.nodes.registerType("rpi-piface out",PiFACEOutNode); RED.nodes.registerType("rpi-piface out",PiFACEOutNode);
}); });
}

View File

@ -14,22 +14,23 @@
* 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",
@ -43,9 +44,9 @@ var pintable = {
"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",
@ -59,9 +60,9 @@ var tablepin = {
"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];
@ -69,11 +70,11 @@ function PibrellaIn(n) {
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;
@ -96,9 +97,9 @@ function PibrellaIn(n) {
node.on("close", function() { node.on("close", function() {
clearInterval(node._interval); clearInterval(node._interval);
}); });
} }
function PibrellaOut(n) { function PibrellaOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
var node = this; var node = this;
@ -124,18 +125,18 @@ function PibrellaOut(n) {
else if (node.pin) { else if (node.pin) {
process.nextTick(function() { process.nextTick(function() {
exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) { exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
else { else {
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg.payload === "true") msg.payload = true; if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") msg.payload = false; if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload); var out = Number(msg.payload);
if ((out == 0)|(out == 1)) { if ((out === 0)|(out === 1)) {
exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) { exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) node.error(err); if (err) { node.error(err); }
}); });
} }
else node.warn("Invalid input - not 0 or 1"); else { node.warn("Invalid input - not 0 or 1"); }
}); });
} }
}); });
@ -148,9 +149,9 @@ function PibrellaOut(n) {
node.on("close", function() { node.on("close", function() {
exec("gpio mode "+node.pin+" in"); exec("gpio mode "+node.pin+" in");
}); });
} }
//exec("gpio mode 0 out",function(err,stdout,stderr) { //exec("gpio mode 0 out",function(err,stdout,stderr) {
//if (err) { //if (err) {
//util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.'); //util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.');
//} //}
@ -166,7 +167,8 @@ function PibrellaOut(n) {
//exec("gpio mode 12 in"); //exec("gpio mode 12 in");
//exec("gpio mode 13 in"); //exec("gpio mode 13 in");
//exec("gpio mode 14 in"); //exec("gpio mode 14 in");
//}); //});
RED.nodes.registerType("rpi-pibrella in",PibrellaIn); RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut); RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
}

View File

@ -14,16 +14,17 @@
* 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}$/
@ -32,7 +33,7 @@ function LedBorgNode(n) {
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)) {
@ -40,7 +41,7 @@ function LedBorgNode(n) {
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 { else {
@ -51,7 +52,7 @@ function LedBorgNode(n) {
if (msg.payload.toLowerCase() in colors) { if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()]; var c = colors[msg.payload.toLowerCase()];
fs.writeFile('/dev/ledborg', c, function (err) { fs.writeFile('/dev/ledborg', c, function (err) {
if (err) node.warn(msg.payload+" : No LedBorg found"); if (err) { node.warn(msg.payload+" : No LedBorg found"); }
}); });
} }
else { else {
@ -59,5 +60,6 @@ function LedBorgNode(n) {
} }
} }
}); });
}
RED.nodes.registerType("ledborg",LedBorgNode);
} }
RED.nodes.registerType("ledborg",LedBorgNode);

View File

@ -14,16 +14,17 @@
* 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]+/
@ -57,6 +58,7 @@ function BlinkStick(n) {
node.error("No BlinkStick found"); node.error("No BlinkStick found");
} }
} }
RED.nodes.registerType("blinkstick",BlinkStick); RED.nodes.registerType("blinkstick",BlinkStick);
}

View File

@ -14,13 +14,14 @@
* 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;
@ -31,19 +32,20 @@ function Blink1Node(n) {
this.on("input", function(msg) { this.on("input", function(msg) {
blink1 = blink1 || new Blink1.Blink1(); blink1 = blink1 || new Blink1.Blink1();
if (blink1) { if (blink1) {
var r,g,b;
try { try {
if (p1.test(msg.payload)) { if (p1.test(msg.payload)) {
// if it is a hex colour string // if it is a hex colour string
var r = parseInt(msg.payload.slice(1,3),16); r = parseInt(msg.payload.slice(1,3),16);
var g = parseInt(msg.payload.slice(3,5),16); g = parseInt(msg.payload.slice(3,5),16);
var b = parseInt(msg.payload.slice(5),16); b = parseInt(msg.payload.slice(5),16);
if (node.fade == 0) { blink1.setRGB( r, g, b ); } 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 if (p2.test(msg.payload)) { else if (p2.test(msg.payload)) {
// if it is a r,g,b triple // if it is a r,g,b triple
var rgb = msg.payload.split(','); var rgb = msg.payload.split(',');
if (node.fade == 0) { blink1.setRGB(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); } 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 { blink1.fadeToRGB(node.fade, parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
} }
else { else {
@ -53,10 +55,10 @@ function Blink1Node(n) {
"purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"} "purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"}
if (msg.payload.toLowerCase() in colors) { if (msg.payload.toLowerCase() in colors) {
var c = colors[msg.payload.toLowerCase()]; var c = colors[msg.payload.toLowerCase()];
var r = parseInt(c.slice(1,3),16); r = parseInt(c.slice(1,3),16);
var g = parseInt(c.slice(3,5),16); g = parseInt(c.slice(3,5),16);
var b = parseInt(c.slice(5),16); b = parseInt(c.slice(5),16);
if (node.fade == 0) { blink1.setRGB( r, g, b ); } 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 {
@ -79,5 +81,6 @@ function Blink1Node(n) {
catch(e) { catch(e) {
node.error("No Blink1 found (" + e + ")"); node.error("No Blink1 found (" + e + ")");
} }
}
RED.nodes.registerType("blink1",Blink1Node);
} }
RED.nodes.registerType("blink1",Blink1Node);

View File

@ -14,12 +14,13 @@
* 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;
@ -65,5 +66,6 @@ function DigiRGBNode(n) {
this.on('close', function() { this.on('close', function() {
if (device) { device.close(); } if (device) { device.close(); }
}); });
}
RED.nodes.registerType("digiRGB",DigiRGBNode);
} }
RED.nodes.registerType("digiRGB",DigiRGBNode);

View File

@ -14,10 +14,11 @@
* 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);
@ -26,17 +27,17 @@ function WeMoOut(n) {
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); RED.nodes.registerType("wemo out",WeMoOut);
function WeMoIn(n) { function WeMoIn(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);
@ -45,7 +46,7 @@ function WeMoIn(n) {
var tick = setInterval(function() { var tick = setInterval(function() {
wemoSwitch.getBinaryState(function(err, result) { wemoSwitch.getBinaryState(function(err, result) {
if (err) node.warn(err); if (err) { node.warn(err); }
if (parseInt(result) != wemoSwitch.state) { if (parseInt(result) != wemoSwitch.state) {
wemoSwitch.state = parseInt(result); wemoSwitch.state = parseInt(result);
node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr}); node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr});
@ -56,5 +57,6 @@ function WeMoIn(n) {
this.on("close", function() { this.on("close", function() {
clearInterval(tick); clearInterval(tick);
}); });
}
RED.nodes.registerType("wemo in",WeMoOut);
} }
RED.nodes.registerType("wemo in",WeMoOut);