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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require("util");
var exec = require('child_process').exec;
var fs = require('fs');
module.exports = function(RED) {
"use strict";
var util = require("util");
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.";
}
}
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.";
}
}
// Map names of pins to Gordon's gpio PiFace pin numbers
var pintable = {
// Physical : WiringPi
// Map names of pins to Gordon's gpio PiFace pin numbers
var pintable = {
// Physical : WiringPi
"Button S1":"200",
"Button S2":"201",
"Button S3":"202",
@ -54,9 +55,9 @@ var pintable = {
"LED 5":"205",
"LED 6":"206",
"LED 7":"207"
}
var tablepin = {
// WiringPi : Physical
}
var tablepin = {
// WiringPi : Physical
"200":"S1",
"201":"S2",
"202":"S3",
@ -81,9 +82,9 @@ var tablepin = {
"205":"L5",
"206":"L6",
"207":"L7"
}
}
function PiFACEInNode(n) {
function PiFACEInNode(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
@ -91,11 +92,11 @@ function PiFACEInNode(n) {
var node = this;
if (node.pin) {
exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) {
if (err) node.error(err);
if (err) { node.error(err); }
else {
node._interval = setInterval( function() {
exec("gpio -p read "+node.pin, function(err,stdout,stderr) {
if (err) node.error(err);
if (err) { node.error(err); }
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
@ -117,35 +118,36 @@ function PiFACEInNode(n) {
node.on("close", function() {
clearInterval(node._interval);
});
}
}
function PiFACEOutNode(n) {
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;
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
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) {
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 {
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) {
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,22 +14,23 @@
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require("util");
var exec = require('child_process').exec;
var fs = require('fs');
module.exports = function(RED) {
"use strict";
var util = require("util");
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.";
}
}
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.";
}
}
// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant)
var pintable = {
// Physical : WiringPi
// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant)
var pintable = {
// Physical : WiringPi
"Amber LED":"0",
"Buzzer ":"1",
"Red LED":"2",
@ -43,9 +44,9 @@ var pintable = {
"In D":"12",
"In A":"13",
"Red Button":"14",
}
var tablepin = {
// WiringPi : Physical
}
var tablepin = {
// WiringPi : Physical
"0":"Amber",
"1":"Buzzer",
"2":"Red",
@ -59,9 +60,9 @@ var tablepin = {
"12":"D",
"13":"A",
"14":"R",
}
}
function PibrellaIn(n) {
function PibrellaIn(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
@ -69,11 +70,11 @@ function PibrellaIn(n) {
if (node.pin) {
exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) {
if (err) node.error(err);
if (err) { node.error(err); }
else {
node._interval = setInterval( function() {
exec("gpio read "+node.pin, function(err,stdout,stderr) {
if (err) node.error(err);
if (err) { node.error(err); }
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
@ -96,9 +97,9 @@ function PibrellaIn(n) {
node.on("close", function() {
clearInterval(node._interval);
});
}
}
function PibrellaOut(n) {
function PibrellaOut(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
var node = this;
@ -124,18 +125,18 @@ function PibrellaOut(n) {
else if (node.pin) {
process.nextTick(function() {
exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) node.error(err);
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;
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
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) {
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() {
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) {
//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 13 in");
//exec("gpio mode 14 in");
//});
//});
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
}

View File

@ -14,16 +14,17 @@
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require('util');
var fs = require('fs');
module.exports = function(RED) {
"use strict";
var util = require('util');
var fs = require('fs');
// check if /dev/ledborg exists - if not then don't even show the node.
if (!fs.existsSync("/dev/ledborg")) {
// check if /dev/ledborg exists - if not then don't even show the node.
if (!fs.existsSync("/dev/ledborg")) {
throw "Info : PiBorg hardware : LedBorg not found";
}
}
function LedBorgNode(n) {
function LedBorgNode(n) {
RED.nodes.createNode(this,n);
var p1 = /^[0-2][0-2][0-2]$/
var p2 = /^\#[A-Fa-f0-9]{6}$/
@ -32,7 +33,7 @@ function LedBorgNode(n) {
this.on("input", function(msg) {
if (p1.test(msg.payload)) {
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)) {
@ -40,7 +41,7 @@ function LedBorgNode(n) {
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();
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 {
@ -51,7 +52,7 @@ function LedBorgNode(n) {
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");
if (err) { node.warn(msg.payload+" : No LedBorg found"); }
});
}
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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var blinkstick = require("blinkstick");
module.exports = function(RED) {
"use strict";
var blinkstick = require("blinkstick");
Object.size = function(obj) {
var size = 0, key;
for (key in obj) { if (obj.hasOwnProperty(key)) size++; }
Object.size = function(obj) {
var size = 0;
for (var key in obj) { if (obj.hasOwnProperty(key)) { size++; } }
return size;
};
};
function BlinkStick(n) {
function BlinkStick(n) {
RED.nodes.createNode(this,n);
var p1 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
@ -57,6 +58,7 @@ function BlinkStick(n) {
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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var Blink1 = require("node-blink1");
// create a single global blink1 object
// all blink1 nodes affect the same (single) led
var blink1 = null;
module.exports = function(RED) {
"use strict";
var Blink1 = require("node-blink1");
// create a single global blink1 object
// all blink1 nodes affect the same (single) led
var blink1 = null;
function Blink1Node(n) {
function Blink1Node(n) {
RED.nodes.createNode(this,n);
this.fade = Number(n.fade) || 0;
var node = this;
@ -31,19 +32,20 @@ function Blink1Node(n) {
this.on("input", function(msg) {
blink1 = blink1 || new Blink1.Blink1();
if (blink1) {
var r,g,b;
try {
if (p1.test(msg.payload)) {
// if it is a hex colour string
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);
if (node.fade == 0) { blink1.setRGB( r, g, b ); }
r = parseInt(msg.payload.slice(1,3),16);
g = parseInt(msg.payload.slice(3,5),16);
b = parseInt(msg.payload.slice(5),16);
if (node.fade === 0) { blink1.setRGB( r, g, b ); }
else { blink1.fadeToRGB(node.fade, 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); }
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 {
@ -53,10 +55,10 @@ function Blink1Node(n) {
"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 ); }
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 {
@ -79,5 +81,6 @@ function Blink1Node(n) {
catch(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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var HID = require('node-hid');
var device;
var node;
module.exports = function(RED) {
"use strict";
var HID = require('node-hid');
var device;
var node;
function DigiRGBNode(n) {
function DigiRGBNode(n) {
RED.nodes.createNode(this,n);
node=this;
@ -65,5 +66,6 @@ function DigiRGBNode(n) {
this.on('close', function() {
if (device) { device.close(); }
});
}
RED.nodes.registerType("digiRGB",DigiRGBNode);
}
RED.nodes.registerType("digiRGB",DigiRGBNode);

View File

@ -14,10 +14,11 @@
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var WeMo = new require('wemo');
module.exports = function(RED) {
"use strict";
var WeMo = new require('wemo');
function WeMoOut(n) {
function WeMoOut(n) {
RED.nodes.createNode(this,n);
this.ipaddr = n.ipaddr;
this.wemoSwitch = new WeMo(n.ipaddr);
@ -26,17 +27,17 @@ function WeMoOut(n) {
this.on("input", function(msg) {
if (msg != null) {
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) {
if (err) node.warn(err);
if (err) { node.warn(err); }
//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);
this.ipaddr = n.ipaddr;
this.wemoSwitch = new WeMo(n.ipaddr);
@ -45,7 +46,7 @@ function WeMoIn(n) {
var tick = setInterval(function() {
wemoSwitch.getBinaryState(function(err, result) {
if (err) node.warn(err);
if (err) { node.warn(err); }
if (parseInt(result) != wemoSwitch.state) {
wemoSwitch.state = parseInt(result);
node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr});
@ -56,5 +57,6 @@ function WeMoIn(n) {
this.on("close", function() {
clearInterval(tick);
});
}
RED.nodes.registerType("wemo in",WeMoOut);
}
RED.nodes.registerType("wemo in",WeMoOut);