var five = require("johnny-five");
var request = require("request");
var parseString = require('xml2js').parseString;
var board = new five.Board({
port: "COM14"
});
var url = "http://beermapping.com/webservice/loccity/3c22855e0a2202cac82d92bd2ea2b149/pittsburgh";
var phoneList = [];
request(url, function(error, response, body) {
parseString(body, function (err, result) {
json = JSON.parse(JSON.stringify(result));
locations = json["bmp_locations"]["location"];
for (i = 0; i < locations.length; i++) {
phone = locations[i]["phone"];
if (phone != "") {
phoneList.push(phone[0].replace(/\D/g,''));
}
}
});
})
function playPhone() {
var led = new five.Led(5);
var led1 = new five.Led(6);
var led2 = new five.Led(7);
var led3 = new five.Led(8);
selected = phoneList[Math.floor(Math.random() * phoneList.length)];
for (var i = 0, len = selected.length; i < len; i++) {
(function(ind) {
setTimeout(function() {
if (selected[ind] == "0" || selected[ind] == "1") {
led.on();
setTimeout(function() {led.off()}, 100);
} else if (selected[ind] == "2" || selected[ind] == "3" || selected[ind] == "4") {
led1.on();
setTimeout(function() {led1.off()}, 100);
} else if (selected[ind] == "5" || selected[ind] == "6") {
led2.on();
setTimeout(function() {led2.off()}, 100);
} else {
led3.on();
setTimeout(function() {led3.off()}, 100);
}
console.log(selected[ind]);
}, (1000 * ind));
})(i);
}
}
// The board's pins will not be accessible until
// the board has reported that it is ready
board.on("ready", function() {
console.log("Ready!");
playPhone();
setInterval(playPhone, 15000);
setTimeout(function() {
var led4 = new five.Led(13);
led4.on();
setTimeout(function() {led4.off()}, 3000);
},11000);
setTimeout(function() {
setInterval(function() {
var led4 = new five.Led(13);
led4.on();
setTimeout(function() {led4.off()}, 3000);
}, 15000);
},10000);
});