Skip to content

Commit e7c16f8

Browse files
committed
consolidate similar URI endpoints, add blink1 disconnect guards to new /blink1 currentColor functionality, expand "homepage" a bit
1 parent 7482b63 commit e7c16f8

File tree

1 file changed

+115
-107
lines changed

1 file changed

+115
-107
lines changed

main.js

Lines changed: 115 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
/**
3-
* blink1-server
3+
* node-blink1-server
44
*
55
*
66
* @author Tod E. Kurt, http://todbot.com/blog
@@ -9,25 +9,27 @@
99

1010
"use strict";
1111

12+
var debug = require('debug')('http');
1213
var Blink1 = require('node-blink1');
1314
var parsecolor = require('parse-color');
1415
var express = require('express');
16+
1517
var app = express();
1618
app.set('json spaces', 4);
17-
18-
var port = 8080;
19-
19+
20+
var port = 8080; // default, can be set as an argument
21+
2022
var devices = Blink1.devices(); // returns array of serial numbers
2123
var blink1 = null;
2224
if( devices.length ) {
23-
blink1 = new Blink1();
25+
blink1 = new Blink1(); // gets first device found
2426
}
25-
27+
2628
var lastColor = '#000000';
2729
var lastTime = 0;
2830
var lastLedn = 0;
2931
var lastRepeats = 0;
30-
32+
3133
// rescan if we know we have no blink1
3234
var blink1TryConnect = function() {
3335
if( blink1 ) { return false; }
@@ -37,7 +39,7 @@
3739
}
3840
return true;
3941
};
40-
42+
4143
// Call blink1.setRGB while dealing with disconnect / reconnect of blink1
4244
var blink1Set = function( r, g, b ){
4345
blink1TryConnect();
@@ -50,7 +52,7 @@
5052
}
5153
return "success";
5254
};
53-
55+
5456
// Call blink1.fadeToRGB while dealing with disconnect / reconnect of blink1
5557
var blink1Fade = function( millis, r, g, b, ledn ){
5658
blink1TryConnect();
@@ -63,7 +65,7 @@
6365
}
6466
return "success";
6567
};
66-
68+
6769
var blink1Blink = function( onoff, repeats, millis, r, g, b, ledn ) {
6870
// console.log("blink1Blink:", onoff, repeats, millis, r, g, b, ledn );
6971
if( onoff ) {
@@ -79,37 +81,96 @@
7981
}, millis );
8082
}
8183
};
82-
84+
8385
var blink1Pattern = function(time, rgb, position) {
8486
blink1.writePatternLine(time * 1000, rgb[0], rgb[1], rgb[2], position);
8587
};
86-
88+
89+
// parse the standard args into a data struct
90+
var parseQueryArgs = function(query) {
91+
var args = {};
92+
args.color = parsecolor(query.rgb);
93+
args.time = Number(query.time) || 0.1;
94+
args.ledn = Number(query.ledn) || 0;
95+
args.repeats = Number(query.repeats) || 3;
96+
args.blink1_id = query.blink1_id;
97+
return args;
98+
};
99+
87100
app.get('/blink1', function(req, res) {
88-
blink1TryConnect();
89-
blink1.rgb(function(r, g, b) {
90-
var color = parsecolor("rgb("+r+","+g+","+b+")");
91-
var response = {
92-
blink1Connected: blink1 !== null,
93-
blink1Serials: devices,
94-
currentColor: color.hex,
95-
lastColor: lastColor,
96-
lastTime: lastTime,
97-
lastLedn: lastLedn,
98-
lastRepeats: lastRepeats,
99-
cmd: "info",
100-
status: "success"
101-
};
102-
res.json( response );
103-
});
101+
blink1TryConnect();
102+
var response = {
103+
blink1Connected: blink1 !== null,
104+
blink1Serials: devices,
105+
currentColor: '#000000',
106+
lastColor: lastColor,
107+
lastTime: lastTime,
108+
lastLedn: lastLedn,
109+
lastRepeats: lastRepeats,
110+
cmd: "info",
111+
status: "success"
112+
};
113+
if( blink1 == null ) { // in case no blink1 plugged in
114+
res.json(response);
115+
return;
116+
}
117+
try {
118+
blink1.rgb(function(r, g, b) {
119+
var color = parsecolor("rgb("+r+","+g+","+b+")");
120+
response.blink1Connected = true;
121+
response.currentColor = color.hex;
122+
res.json( response );
123+
});
124+
} catch(err) {
125+
blink1 = null;
126+
res.json( response );
127+
}
104128
});
105-
129+
130+
app.get('/blink1/:type(fadeToRGB|on|off|red|green|blue|yellow|cyan|magenta)', function(req, res) {
131+
if( req.params.type == 'on' ) { req.query.rgb = '#FFFFFF'; }
132+
else if( req.params.type == 'off' ) { req.query.rgb = '#000000'; }
133+
else if( req.params.type == 'red' ) { req.query.rgb = '#FF0000'; }
134+
else if( req.params.type == 'green' ) { req.query.rgb = '#00FF00'; }
135+
else if( req.params.type == 'blue' ) { req.query.rgb = '#0000FF'; }
136+
else if( req.params.type == 'yellow' ) { req.query.rgb = '#FFFF00'; }
137+
else if( req.params.type == 'cyan' ) { req.query.rgb = '#00FFFF'; }
138+
else if( req.params.type == 'magenta') { req.query.rgb = '#FF00FF'; }
139+
var args = parseQueryArgs(req.query);
140+
var status = req.params.type;
141+
142+
if( typeof(args.color.rgb) != 'undefined' ) {
143+
lastColor = args.color.hex;
144+
lastTime = args.time;
145+
lastLedn = args.ledn;
146+
var rgb = args.color.rgb;
147+
status = blink1Fade( args.time*1000, rgb[0], rgb[1], rgb[2], args.ledn );
148+
}
149+
else {
150+
status = "bad hex color specified '" + req.query.rgb + "'";
151+
}
152+
153+
var response = {
154+
blink1Connected: blink1 !== null,
155+
blink1Serials: devices,
156+
currentColor: lastColor,
157+
lastColor: lastColor,
158+
lastTime: lastTime,
159+
lastLedn: lastLedn,
160+
lastRepeats: lastRepeats,
161+
cmd: "fadeToRGB",
162+
status: status
163+
};
164+
res.json( response );
165+
});
166+
106167
app.get('/blink1/setRGB', function(req, res) {
107168
var color = parsecolor(req.query.rgb);
108169
var time = Number(req.query.time) || 0.1;
109170
var ledn = Number(req.query.ledn) || 0;
110171
var status = "success";
111172
var rgb = color.rgb;
112-
173+
113174
if( rgb ) {
114175
lastColor = color.hex;
115176
lastTime = time;
@@ -132,71 +193,7 @@
132193
};
133194
res.json( response );
134195
});
135-
136-
app.get('/blink1/fadeToRGB', function(req, res) {
137-
var color = parsecolor(req.query.rgb);
138-
var time = Number(req.query.time) || 0.1;
139-
var ledn = Number(req.query.ledn) || 0;
140-
var status = "success";
141-
var rgb = color.rgb;
142-
143-
if( rgb ) {
144-
lastColor = color.hex;
145-
lastTime = time;
146-
lastLedn = ledn;
147-
status = blink1Fade( time*1000, rgb[0], rgb[1], rgb[2], ledn );
148-
}
149-
else {
150-
status = "bad hex color specified " + req.query.rgb;
151-
}
152-
var response = {
153-
blink1Connected: blink1 !== null,
154-
blink1Serials: devices,
155-
currentColor: lastColor,
156-
lastColor: lastColor,
157-
lastTime: lastTime,
158-
lastLedn: lastLedn,
159-
lastRepeats: lastRepeats,
160-
cmd: "fadeToRGB",
161-
status: status
162-
};
163-
res.json( response );
164-
});
165-
166-
app.get('/blink1/off', function(req, res) {
167-
lastColor = "#000000";
168-
blink1Fade( 100, 0,0,0, 0);
169-
var response = {
170-
blink1Connected: blink1 !== null,
171-
blink1Serials: devices,
172-
currentColor: lastColor,
173-
lastColor: lastColor,
174-
lastTime: lastTime,
175-
lastLedn: lastLedn,
176-
lastRepeats: lastRepeats,
177-
cmd: "off",
178-
status: "success"
179-
};
180-
res.json( response );
181-
});
182-
183-
app.get('/blink1/on', function(req, res) {
184-
lastColor = "#FFFFFF";
185-
blink1Fade( 100, 255,255,255, 0);
186-
var response = {
187-
blink1Connected: blink1 !== null,
188-
blink1Serials: devices,
189-
currentColor: lastColor,
190-
lastColor: lastColor,
191-
lastTime: lastTime,
192-
lastLedn: lastLedn,
193-
lastRepeats: lastRepeats,
194-
cmd: "on",
195-
status: "success"
196-
};
197-
res.json( response );
198-
});
199-
196+
200197
app.get('/blink1/blink', function(req, res) {
201198
var color = parsecolor(req.query.rgb);
202199
var time = Number(req.query.time) || 0.1;
@@ -227,32 +224,32 @@
227224
};
228225
res.json( response );
229226
});
230-
227+
231228
app.get('/blink1/pattern', function(req, res) {
232229
var colors = req.query.rgb.split(',');
233230
var time = Number(req.query.time) || 0.1;
234231
// var repeats = Number(req.query.repeats) || Number(req.query.count) || 3;
235232
var repeats = parseInt( req.query.repeats || req.query.count );
236233
repeats = (repeats == NaN ) ? 3 : repeats;
237234
var status = "success";
238-
235+
239236
blink1TryConnect();
240237
if( blink1 ) {
241238
for (var i=0, len=colors.length; i < len; i++) {
242239
var rgb = parsecolor(colors[i]).rgb;
243240
blink1Pattern(time, rgb, i);
244241
}
245-
242+
246243
blink1.playLoop(0, colors.length, repeats);
247-
244+
248245
if (colors.length > 16) {
249246
status = "can only display first 16 colors. " + colors.length + " colors specified"
250247
}
251248
}
252249
else {
253250
status = "no blink1 connected";
254251
}
255-
252+
256253
var response = {
257254
blink1Connected: blink1 !== null,
258255
blink1Serials: devices,
@@ -262,10 +259,10 @@
262259
cmd: "pattern",
263260
status: status
264261
};
265-
262+
266263
res.json( response );
267264
});
268-
265+
269266
// respond with "Hello World!" on the homepage
270267
app.get('/', function(req, res) {
271268
res.send("<html>" +
@@ -274,28 +271,39 @@
274271
"Supported URIs: <ul>\n" +
275272
"<li> <code> /blink1 </code> " +
276273
" -- status info\n" +
274+
"<li> <code> /blink1/on </code> -- #FFFFFF full white" +
275+
"<li> <code> /blink1/off </code> -- #000000 full dark" +
276+
"<li> <code> /blink1/red </code> -- #FF0000 red " +
277+
"<li> <code> /blink1/green </code> -- #00FF00 green " +
278+
"<li> <code> /blink1/blue </code> -- #0000FF blue " +
279+
"<li> <code> /blink1/yellow </code> -- #FFFF00 yellow " +
280+
"<li> <code> /blink1/cyan </code> -- #00FF00 cyan " +
281+
"<li> <code> /blink1/magenta </code> -- #FF00FF magenta " +
277282
"<li> <code> /blink1/fadeToRGB?rgb=%23FF00FF&time=1.5&ledn=2 </code> " +
278283
"-- fade to a RGB color over time for led\n" +
284+
"<li> <code> /blink1/setRGB?rgb=%23FF00FF&ledn=2 </code> " +
285+
"-- set a RGB color immediately for led\n" +
286+
"<li> <code> /blink1/blink?rgb=%23FF00FF&time=0.5&repeats=5 </code> " +
287+
"-- blink an RGB color over time for number of repeats\n" +
279288
"<li> <code> /blink1/pattern?rgb=%23ff0000,%23ffffff,%230000ff&time=.2&repeats=8 </code> " +
280289
"-- blink a sequence of colors\n" +
281290
"</ul></p>\n" +
282291
"When starting server, argument specified is port to run on, e.g.:" +
283292
"<code> blink1-server 8080 </code>\n" +
284293
"</html>");
285294
});
286-
287-
295+
296+
288297
// if we have args
289298
if( process.argv.length > 2 ) {
290299
var p = Number(process.argv[2]);
291300
port = (p) ? p : port;
292301
}
293-
302+
294303
var server = app.listen(port, function() {
295304
var host = server.address().address;
296305
var port = server.address().port;
297306
host = (host === '::' ) ? "localhost" : host;
298-
307+
299308
console.log('blink1-server listening at http://%s:%s/', host, port);
300309
});
301-

0 commit comments

Comments
 (0)