Skip to content

Instantly share code, notes, and snippets.

@toddanglin
Last active December 13, 2015 20:29
Show Gist options
  • Save toddanglin/4970717 to your computer and use it in GitHub Desktop.
Save toddanglin/4970717 to your computer and use it in GitHub Desktop.
A simple NodeJS timer script for the blink(1) USB status light. The script accepts a duration (in minutes) + a warning time (in minutes or seconds) + a "flash" time (in minutes or seconds) via the command line. For example: node blinkTimer.js duration=10,warn=2,flash=1 This command will display a green light first. At 5 minutes (automatic half-w…
//////////////////////////////////////////////////////////////
// blink(1) timer //
// by: @toddanglin //
//////////////////////////////////////////////////////////////
var http = require("http");
console.log("blink(1) Timer Started");
//PROTOTYPE Customizations
Date.prototype.addMs = function(m) {
this.setTime(this.getTime() + m);
return this;
}
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
//GLOBAL EVENTS
process.on( 'SIGINT', function() {
console.log( "\nShutting down blink timer..." )
setOff(function(){
process.exit();
}); //Turn off the light
})
//VARIABLES
var durationMs = 60 * 60 * 1000, //DEFAULT Duration (60 min)
endTime, //SET AFTER ARGS are processed
warnTimeMs = 5 * 60 * 1000, //DEFAULT Warn Time (5min)
flashTimeMs = 1 * 60 * 1000, //DEFAULT Flash Time (1min)
startColor = "#09FF34",
midColor = "#FFBA07",
endColor = "#FF211E",
half = false, //Status flags to track light actions
warn = false,
flash = false,
blinkHost = "localhost",
blinkPort = 8934, //DEFAULT blink port
arguments = process.argv.splice(2);
//ARGUMENTS
//Process configuration values
if(arguments != null){
var config = String(arguments).split(",");
config.forEach(function(val){
var pair = val.split("=");
switch(pair[0]){
case 'length':
console.log("Length (min): "+ pair[1]);
durationMs = pair[1] * 60 * 1000;
break;
case 'warn':
console.log("Warn Time (time index): "+ pair[1]);
if(pair[1].endsWith("s")){
warnTimeMs = pair[1].replace("s","") * 1000; //Time provided in seconds
} else {
warnTimeMs = pair[1] * 60 * 1000; //Assume time provided in minutes
}
break;
case 'flash':
console.log("Flash Time (time index): "+ pair[1]);
if(pair[1].endsWith("s")){
flashTimeMs = pair[1].replace("s","") * 1000; //Time provided in seconds
} else {
flashTimeMs = pair[1] * 60 * 1000; //Assume time provided in minutes
}
break;
case '-h' || 'help':
console.log("Usage: BLINK timer allows you to specify a duration, warning time, and flash time");
return;
default:
console.log("Sorry. Don't understand that configuration option ("+ val +")");
return;
}
});
}
//SET END TIME
endTime = new Date().addMs(durationMs);
//====== APPLICATION BEGIN ========
//To start set BLINK to Green
setColor(startColor);
//START timer to check for color change condition
var timer = setInterval (checkColor, 500);
function checkColor(){
var now = new Date(),
timeLeft = endTime - now;
//console.log("CHECK COLOR (Time left: "+ timeLeft +")");
if(timeLeft > (durationMs/2)) return;
if(timeLeft > warnTimeMs && !half){
console.log("HALF TIME elapsed. Showing mid color.");
//IF HERE, Half of time has elapsed. Set BLINK to Yellow
setColor(midColor);
half = true; //Flip flag so we don't set light twice
return;
}else if(timeLeft <= warnTimeMs && timeLeft > flashTimeMs && !warn){
console.log("WARNING TIME reached. Showing warn color.");
//IF HERE, Only 5 minutes remain. Set BLINK to Red
setColor(endColor);
warn = true; //Flip flag so we don't set warn light twice
return;
}else if(timeLeft <= flashTimeMs && timeLeft > 0 && !flash){
console.log("FLASH TIME reached. Flashing the warn color.");
//IF HERE, Only 1 minute remains. Flash BLINK.
setColor(endColor, true);
flash = true; //Flip flag
return;
}else if(timeLeft <= 0){
shutDown();
}
}
function shutDown(){
console.log("TIME'S UP! Turning-off light");
if(timer !== undefined)
clearTimeout(timer);
destroyFlashPattern();
setOff();
return;
}
function setColor(hexColor, flashColor){
if(flashColor === false || flashColor === undefined){
sendCommand("/blink1/fadeToRGB?rgb="+ encodeURIComponent(hexColor) +"&time="+ 0.3)
} else {
createFlashPattern(hexColor, function(){
sendCommand("/blink1/pattern/play?pname=blinker");
});
}
}
function setOff(cb){
sendCommand("/blink1/off", cb);
}
function playFlash(){
sendCommand();
}
function createFlashPattern(hexColor, cb)
{
sendCommand("/blink1/pattern/add?pname=blinker&pattern=0,"+ encodeURIComponent(hexColor) +",0.8,%23000000,0.3", cb);
}
function destroyFlashPattern()
{
sendCommand("/blink1/pattern/del?pname=blinker");
}
function sendCommand(path, cb){
console.log("SEND BLINK Command: "+ path);
var options = {
host: blinkHost,
port: blinkPort, //Default BLINK port
path: path,
method: "GET"
};
http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log('BLINK Response: ' + chunk);
if(cb !== undefined)
cb(chunk);
});
}).end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment