From a5adbc220768cb1c51570145770b812789177de8 Mon Sep 17 00:00:00 2001 From: dceejay Date: Tue, 24 Feb 2015 17:29:51 +0000 Subject: [PATCH] Catch blink1 missing device error --- hardware/blink1/77-blink1.js | 72 ++++++++++++++++++------------------ hardware/blink1/package.json | 2 +- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/hardware/blink1/77-blink1.js b/hardware/blink1/77-blink1.js index dd1134c0..e6886f69 100644 --- a/hardware/blink1/77-blink1.js +++ b/hardware/blink1/77-blink1.js @@ -30,46 +30,48 @@ module.exports = function(RED) { var p1 = /^\#[A-Fa-f0-9]{6}$/ var p2 = /[0-9]+,[0-9]+,[0-9]+/ 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 - 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); } - 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); + try { + blink1 = blink1 || new Blink1.Blink1(); + if (blink1) { + var r,g,b; + try { + if (p1.test(msg.payload)) { + // if it is a hex colour string + 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 { - node.warn("Blink1 : invalid msg : "+msg.payload); + 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 ); } + 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() { if (blink1 && typeof blink1.close == "function") { diff --git a/hardware/blink1/package.json b/hardware/blink1/package.json index 0e90af44..1fd2d1e5 100644 --- a/hardware/blink1/package.json +++ b/hardware/blink1/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-blink1", - "version" : "0.0.2", + "version" : "0.0.3", "description" : "A Node-RED node to control a Thingm Blink(1)", "dependencies" : { "node-blink1" : "0.1.2"