Created
March 8, 2013 10:22
-
-
Save mauricesvay/5115546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/node | |
var http = require('http'); | |
var exec = require('child_process').exec; | |
var url = 'http://api.citybik.es/velib.json'; | |
// var lat = '48.8836694'; | |
// var lon = '2.3736453'; | |
var id = 19030; | |
command = '/home/pi/blink1-tool -m 100 --rgb 50,50,50'; | |
exec(command, function (error, stdout, stderr) { | |
if (error !== null) { | |
console.log(stdout); | |
process.exit(1); | |
} else { | |
getStatus(); | |
} | |
}); | |
function getStatus() { | |
var req = http.get(url, function(res){ | |
var responseText = ''; | |
res.on('data', function(data){ | |
responseText += data.toString(); | |
}); | |
res.on('end', function(){ | |
var data = JSON.parse(responseText); | |
var available = 0; | |
var color = ''; | |
var command = ''; | |
for (var i=0,l=data.length; i<l; i++) { | |
if (data[i].number == id) { | |
available = data[i].bikes; | |
} | |
} | |
console.log(available); | |
if (available < 3) { | |
color = "255,0,0"; | |
} if (available >= 3 && available < 6) { | |
color = "255,204,51"; | |
} if (available >= 6) { | |
color = "153,255,51"; | |
} | |
command = '/home/pi/blink1-tool -m 100 --rgb ' + color; | |
exec(command, function (error, stdout, stderr) { | |
if (error !== null) { | |
console.log(stdout); | |
} | |
}); | |
}); | |
}).on('error', function(e){ | |
console.log(e.message); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment