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

Catch blink1 missing device error

This commit is contained in:
dceejay 2015-02-24 17:29:51 +00:00
parent f550024bb2
commit a5adbc2207
2 changed files with 38 additions and 36 deletions

View File

@ -30,46 +30,48 @@ module.exports = function(RED) {
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(); try {
if (blink1) { blink1 = blink1 || new Blink1.Blink1();
var r,g,b; 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)) {
r = parseInt(msg.payload.slice(1,3),16); // if it is a hex colour string
g = parseInt(msg.payload.slice(3,5),16); r = parseInt(msg.payload.slice(1,3),16);
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 ); }
}
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":"#00FF00","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 ); } 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":"#00FF00","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"); } catch (e) { node.warn("Blink1 : device not found"); blink1 = null; }
}
}); });
this.on("close", function() { this.on("close", function() {
if (blink1 && typeof blink1.close == "function") { if (blink1 && typeof blink1.close == "function") {

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-blink1", "name" : "node-red-node-blink1",
"version" : "0.0.2", "version" : "0.0.3",
"description" : "A Node-RED node to control a Thingm Blink(1)", "description" : "A Node-RED node to control a Thingm Blink(1)",
"dependencies" : { "dependencies" : {
"node-blink1" : "0.1.2" "node-blink1" : "0.1.2"