forked from todbot/blink1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlwatch.js
executable file
·71 lines (57 loc) · 1.63 KB
/
urlwatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
/*
*
*
*/
var Blink1 = require('node-blink1');
var http = require('http');
var testmode = 0;
var intervalSecs = 2;
var requrl = 'http://todbot.com/tst/color.txt';
// parse commandline args
process.argv.forEach(function (val, index, array) {
if( val.indexOf("http") == 0 ) {
requrl = val;
}
else if( val.indexOf("test") == 0 ) {
testmode = 1;
}
});
console.log("watching every "+intervalSecs+" the url: "+requrl);
// open a blink1 for use
var blink1;
try {
blink1 = new Blink1.Blink1();
} catch(err) {
console.log("no blink1 devices found");
if( !testmode ) process.exit(1);
}
reqcallback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
//console.log(":"+str+":");
var regex = /([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/;
var result = str.match(regex);
if( result ) {
var red = parseInt( result[1],16 );
var grn = parseInt( result[2],16 );
var blu = parseInt( result[3],16 );
console.log("found rgb: " + red +","+ grn +","+ blu +"");
if( blink1 == null ) return;
try {
blink1.fadeToRGB( 100, red,grn,blu );
} catch(err) {
console.log(err); // might get this if your USB port is weird/flaky
}
}
});
};
timercallback = function() {
var req = http.request(requrl, reqcallback);
req.end();
};
timercallback();
setInterval( timercallback, intervalSecs * 1000 );