diff --git a/main.js b/main.js index 261da00..6e92c94 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,6 @@ #!/usr/bin/env node /** - * blink1-server + * node-blink1-server * * * @author Tod E. Kurt, http://todbot.com/blog @@ -9,25 +9,27 @@ "use strict"; + var debug = require('debug')('http'); var Blink1 = require('node-blink1'); var parsecolor = require('parse-color'); var express = require('express'); + var app = express(); app.set('json spaces', 4); - - var port = 8080; - + + var port = 8080; // default, can be set as an argument + var devices = Blink1.devices(); // returns array of serial numbers var blink1 = null; if( devices.length ) { - blink1 = new Blink1(); + blink1 = new Blink1(); // gets first device found } - + var lastColor = '#000000'; var lastTime = 0; var lastLedn = 0; var lastRepeats = 0; - + // rescan if we know we have no blink1 var blink1TryConnect = function() { if( blink1 ) { return false; } @@ -37,7 +39,7 @@ } return true; }; - + // Call blink1.setRGB while dealing with disconnect / reconnect of blink1 var blink1Set = function( r, g, b ){ blink1TryConnect(); @@ -50,7 +52,7 @@ } return "success"; }; - + // Call blink1.fadeToRGB while dealing with disconnect / reconnect of blink1 var blink1Fade = function( millis, r, g, b, ledn ){ blink1TryConnect(); @@ -63,7 +65,7 @@ } return "success"; }; - + var blink1Blink = function( onoff, repeats, millis, r, g, b, ledn ) { // console.log("blink1Blink:", onoff, repeats, millis, r, g, b, ledn ); if( onoff ) { @@ -79,37 +81,96 @@ }, millis ); } }; - + var blink1Pattern = function(time, rgb, position) { blink1.writePatternLine(time * 1000, rgb[0], rgb[1], rgb[2], position); }; - + + // parse the standard args into a data struct +var parseQueryArgs = function(query) { + var args = {}; + args.color = parsecolor(query.rgb); + args.time = Number(query.time) || 0.1; + args.ledn = Number(query.ledn) || 0; + args.repeats = Number(query.repeats) || 3; + args.blink1_id = query.blink1_id; + return args; + }; + app.get('/blink1', function(req, res) { - blink1TryConnect(); - blink1.rgb(function(r, g, b) { - var color = parsecolor("rgb("+r+","+g+","+b+")"); - var response = { - blink1Connected: blink1 !== null, - blink1Serials: devices, - currentColor: color.hex, - lastColor: lastColor, - lastTime: lastTime, - lastLedn: lastLedn, - lastRepeats: lastRepeats, - cmd: "info", - status: "success" - }; - res.json( response ); - }); + blink1TryConnect(); + var response = { + blink1Connected: blink1 !== null, + blink1Serials: devices, + currentColor: '#000000', + lastColor: lastColor, + lastTime: lastTime, + lastLedn: lastLedn, + lastRepeats: lastRepeats, + cmd: "info", + status: "success" + }; + if( blink1 == null ) { // in case no blink1 plugged in + res.json(response); + return; + } + try { + blink1.rgb(function(r, g, b) { + var color = parsecolor("rgb("+r+","+g+","+b+")"); + response.blink1Connected = true; + response.currentColor = color.hex; + res.json( response ); + }); + } catch(err) { + blink1 = null; + res.json( response ); + } }); - + + app.get('/blink1/:type(fadeToRGB|on|off|red|green|blue|yellow|cyan|magenta)', function(req, res) { + if( req.params.type == 'on' ) { req.query.rgb = '#FFFFFF'; } + else if( req.params.type == 'off' ) { req.query.rgb = '#000000'; } + else if( req.params.type == 'red' ) { req.query.rgb = '#FF0000'; } + else if( req.params.type == 'green' ) { req.query.rgb = '#00FF00'; } + else if( req.params.type == 'blue' ) { req.query.rgb = '#0000FF'; } + else if( req.params.type == 'yellow' ) { req.query.rgb = '#FFFF00'; } + else if( req.params.type == 'cyan' ) { req.query.rgb = '#00FFFF'; } + else if( req.params.type == 'magenta') { req.query.rgb = '#FF00FF'; } + var args = parseQueryArgs(req.query); + var status = req.params.type; + + if( typeof(args.color.rgb) != 'undefined' ) { + lastColor = args.color.hex; + lastTime = args.time; + lastLedn = args.ledn; + var rgb = args.color.rgb; + status = blink1Fade( args.time*1000, rgb[0], rgb[1], rgb[2], args.ledn ); + } + else { + status = "bad hex color specified '" + req.query.rgb + "'"; + } + + var response = { + blink1Connected: blink1 !== null, + blink1Serials: devices, + currentColor: lastColor, + lastColor: lastColor, + lastTime: lastTime, + lastLedn: lastLedn, + lastRepeats: lastRepeats, + cmd: "fadeToRGB", + status: status + }; + res.json( response ); +}); + app.get('/blink1/setRGB', function(req, res) { var color = parsecolor(req.query.rgb); var time = Number(req.query.time) || 0.1; var ledn = Number(req.query.ledn) || 0; var status = "success"; var rgb = color.rgb; - + if( rgb ) { lastColor = color.hex; lastTime = time; @@ -132,71 +193,7 @@ }; res.json( response ); }); - - app.get('/blink1/fadeToRGB', function(req, res) { - var color = parsecolor(req.query.rgb); - var time = Number(req.query.time) || 0.1; - var ledn = Number(req.query.ledn) || 0; - var status = "success"; - var rgb = color.rgb; - - if( rgb ) { - lastColor = color.hex; - lastTime = time; - lastLedn = ledn; - status = blink1Fade( time*1000, rgb[0], rgb[1], rgb[2], ledn ); - } - else { - status = "bad hex color specified " + req.query.rgb; - } - var response = { - blink1Connected: blink1 !== null, - blink1Serials: devices, - currentColor: lastColor, - lastColor: lastColor, - lastTime: lastTime, - lastLedn: lastLedn, - lastRepeats: lastRepeats, - cmd: "fadeToRGB", - status: status - }; - res.json( response ); - }); - - app.get('/blink1/off', function(req, res) { - lastColor = "#000000"; - blink1Fade( 100, 0,0,0, 0); - var response = { - blink1Connected: blink1 !== null, - blink1Serials: devices, - currentColor: lastColor, - lastColor: lastColor, - lastTime: lastTime, - lastLedn: lastLedn, - lastRepeats: lastRepeats, - cmd: "off", - status: "success" - }; - res.json( response ); - }); - - app.get('/blink1/on', function(req, res) { - lastColor = "#FFFFFF"; - blink1Fade( 100, 255,255,255, 0); - var response = { - blink1Connected: blink1 !== null, - blink1Serials: devices, - currentColor: lastColor, - lastColor: lastColor, - lastTime: lastTime, - lastLedn: lastLedn, - lastRepeats: lastRepeats, - cmd: "on", - status: "success" - }; - res.json( response ); - }); - + app.get('/blink1/blink', function(req, res) { var color = parsecolor(req.query.rgb); var time = Number(req.query.time) || 0.1; @@ -227,7 +224,7 @@ }; res.json( response ); }); - + app.get('/blink1/pattern', function(req, res) { var colors = req.query.rgb.split(','); var time = Number(req.query.time) || 0.1; @@ -235,16 +232,16 @@ var repeats = parseInt( req.query.repeats || req.query.count ); repeats = (repeats == NaN ) ? 3 : repeats; var status = "success"; - + blink1TryConnect(); if( blink1 ) { for (var i=0, len=colors.length; i < len; i++) { var rgb = parsecolor(colors[i]).rgb; blink1Pattern(time, rgb, i); } - + blink1.playLoop(0, colors.length, repeats); - + if (colors.length > 16) { status = "can only display first 16 colors. " + colors.length + " colors specified" } @@ -252,7 +249,7 @@ else { status = "no blink1 connected"; } - + var response = { blink1Connected: blink1 !== null, blink1Serials: devices, @@ -262,10 +259,10 @@ cmd: "pattern", status: status }; - + res.json( response ); }); - + // respond with "Hello World!" on the homepage app.get('/', function(req, res) { res.send("" + @@ -274,8 +271,20 @@ "Supported URIs:

\n" + @@ -283,19 +292,18 @@ " blink1-server 8080 \n" + ""); }); - - + + // if we have args if( process.argv.length > 2 ) { var p = Number(process.argv[2]); port = (p) ? p : port; } - + var server = app.listen(port, function() { var host = server.address().address; var port = server.address().port; host = (host === '::' ) ? "localhost" : host; - + console.log('blink1-server listening at http://%s:%s/', host, port); }); - \ No newline at end of file