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.
**/
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
throw "Info : Ignoring Raspberry Pi specific node.";
}
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
throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
}
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
"Button S1":"200",
"Button S2":"201",
"Button S3":"202",
"Button S4":"203",
"Input 5":"204",
"Input 6":"205",
"Input 7":"206",
"Input 8":"207",
"Output0":"208",
"Output1":"209",
"Output2":"210",
"Output3":"211",
"Output4":"212",
"Output5":"213",
"Output6":"214",
"Output7":"215",
"LED 0 / Relay 0":"200",
"LED 1 / Relay 1":"201",
"LED 2":"202",
"LED 3":"203",
"LED 4":"204",
"LED 5":"205",
"LED 6":"206",
"LED 7":"207"
}
var tablepin = {
// WiringPi : Physical
"200":"S1",
"201":"S2",
"202":"S3",
"203":"S4",
"204":"I5",
"205":"I6",
"206":"I7",
"207":"I8",
"208":"O0",
"209":"O1",
"210":"O2",
"211":"O3",
"212":"O4",
"213":"O5",
"214":"O6",
"215":"O7",
"200":"L0",
"201":"L1",
"202":"L2",
"203":"L3",
"204":"L4",
"205":"L5",
"206":"L6",
"207":"L7"
}
// Map names of pins to Gordon's gpio PiFace pin numbers
var pintable = {
// Physical : WiringPi
"Button S1":"200",
"Button S2":"201",
"Button S3":"202",
"Button S4":"203",
"Input 5":"204",
"Input 6":"205",
"Input 7":"206",
"Input 8":"207",
"Output0":"208",
"Output1":"209",
"Output2":"210",
"Output3":"211",
"Output4":"212",
"Output5":"213",
"Output6":"214",
"Output7":"215",
"LED 0 / Relay 0":"200",
"LED 1 / Relay 1":"201",
"LED 2":"202",
"LED 3":"203",
"LED 4":"204",
"LED 5":"205",
"LED 6":"206",
"LED 7":"207"
}
var tablepin = {
// WiringPi : Physical
"200":"S1",
"201":"S2",
"202":"S3",
"203":"S4",
"204":"I5",
"205":"I6",
"206":"I7",
"207":"I8",
"208":"O0",
"209":"O1",
"210":"O2",
"211":"O3",
"212":"O4",
"213":"O5",
"214":"O6",
"215":"O7",
"200":"L0",
"201":"L1",
"202":"L2",
"203":"L3",
"204":"L4",
"205":"L5",
"206":"L6",
"207":"L7"
}
function PiFACEInNode(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
this.intype = n.intype;
var node = this;
if (node.pin) {
exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) {
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);
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
node.buttonState = Number(stdout);
if (previousState !== -1) {
var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState};
node.send(msg);
function PiFACEInNode(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
this.intype = n.intype;
var node = this;
if (node.pin) {
exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) {
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); }
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
node.buttonState = Number(stdout);
if (previousState !== -1) {
var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState};
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.
**/
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
throw "Info : Ignoring Raspberry Pi specific node.";
}
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
throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
}
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
"Amber LED":"0",
"Buzzer ":"1",
"Red LED":"2",
"Out E":"3",
"Out F":"4",
"Out G":"5",
"Out H":"6",
"Green LED":"7",
"In C":"10",
"In B":"11",
"In D":"12",
"In A":"13",
"Red Button":"14",
}
var tablepin = {
// WiringPi : Physical
"0":"Amber",
"1":"Buzzer",
"2":"Red",
"3":"E",
"4":"F",
"5":"G",
"6":"H",
"7":"Green",
"10":"C",
"11":"B",
"12":"D",
"13":"A",
"14":"R",
}
// 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",
"Out E":"3",
"Out F":"4",
"Out G":"5",
"Out H":"6",
"Green LED":"7",
"In C":"10",
"In B":"11",
"In D":"12",
"In A":"13",
"Red Button":"14",
}
var tablepin = {
// WiringPi : Physical
"0":"Amber",
"1":"Buzzer",
"2":"Red",
"3":"E",
"4":"F",
"5":"G",
"6":"H",
"7":"Green",
"10":"C",
"11":"B",
"12":"D",
"13":"A",
"14":"R",
}
function PibrellaIn(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
var node = this;
function PibrellaIn(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
var node = this;
if (node.pin) {
exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) {
if (err) node.error(err);
else {
node._interval = setInterval( function() {
exec("gpio read "+node.pin, function(err,stdout,stderr) {
if (err) node.error(err);
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
node.buttonState = Number(stdout);
if (previousState !== -1) {
var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState};
node.send(msg);
if (node.pin) {
exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
node._interval = setInterval( function() {
exec("gpio read "+node.pin, function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
node.buttonState = Number(stdout);
if (previousState !== -1) {
var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState};
node.send(msg);
}
}
}
}
});
}, 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");
});
});
}, 200);
}
});
}
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 "+node.pin+" in");
});
//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);
}
//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.
**/
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")) {
throw "Info : PiBorg hardware : LedBorg not found";
}
// 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) {
RED.nodes.createNode(this,n);
var p1 = /^[0-2][0-2][0-2]$/
var p2 = /^\#[A-Fa-f0-9]{6}$/
var node = this;
function LedBorgNode(n) {
RED.nodes.createNode(this,n);
var p1 = /^[0-2][0-2][0-2]$/
var p2 = /^\#[A-Fa-f0-9]{6}$/
var node = this;
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");
});
}
else if (p2.test(msg.payload)) {
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 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");
});
}
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");
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"); }
});
}
else if (p2.test(msg.payload)) {
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 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"); }
});
}
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);