2013-10-02 17:24:40 +02:00
|
|
|
|
2013-10-18 11:58:26 +02:00
|
|
|
var retrieveListInterval = 3000;
|
2013-10-02 17:24:40 +02:00
|
|
|
var retrieveListDelay; // retry setTimout instance
|
2013-10-03 18:06:05 +02:00
|
|
|
var boxTimeoutTime = 500;
|
2013-10-02 17:24:40 +02:00
|
|
|
|
|
|
|
var numBoxesChecking = 0; // count how many boxes we are checking
|
|
|
|
var numBoxesFound = 0; // count how many boxes responded
|
|
|
|
|
2013-09-27 12:31:55 +02:00
|
|
|
var $list;
|
2013-10-02 17:24:40 +02:00
|
|
|
var $intro;
|
2013-10-18 11:58:26 +02:00
|
|
|
var $hint;
|
2013-10-02 17:24:40 +02:00
|
|
|
var $preloader;
|
|
|
|
var spinner;
|
2013-09-27 12:31:55 +02:00
|
|
|
|
|
|
|
$(function() {
|
|
|
|
//console.log("ready");
|
2013-10-02 17:24:40 +02:00
|
|
|
|
|
|
|
$intro = $("#intro");
|
|
|
|
$list = $("#list");
|
2013-10-18 11:58:26 +02:00
|
|
|
$hint = $("#hint");
|
2013-10-02 17:24:40 +02:00
|
|
|
$preloader = $("#preloader");
|
|
|
|
|
|
|
|
var spinnerSettings = {
|
2013-10-10 12:00:14 +02:00
|
|
|
lines: 7, // The number of lines to draw
|
|
|
|
length: 0, // The length of each line
|
|
|
|
width: 14, // The line thickness
|
|
|
|
radius: 15, // The radius of the inner circle
|
2013-10-02 17:24:40 +02:00
|
|
|
corners: 1, // Corner roundness (0..1)
|
|
|
|
rotate: 0, // The rotation offset
|
|
|
|
direction: 1, // 1: clockwise, -1: counterclockwise
|
2013-10-03 16:59:28 +02:00
|
|
|
color: '#57BF42', // #rgb or #rrggbb or array of colors
|
2013-10-10 12:00:14 +02:00
|
|
|
speed: 1.2, // Rounds per second
|
|
|
|
trail: 69, // Afterglow percentage
|
2013-10-02 17:24:40 +02:00
|
|
|
shadow: false, // Whether to render a shadow
|
|
|
|
hwaccel: false, // Whether to use hardware acceleration
|
|
|
|
className: 'spinner', // The CSS class to assign to the spinner
|
|
|
|
zIndex: 2e9, // The z-index (defaults to 2000000000)
|
|
|
|
top: 'auto', // Top position relative to parent in px
|
|
|
|
left: 'auto' // Left position relative to parent in px
|
|
|
|
};
|
|
|
|
spinner = new Spinner(spinnerSettings);
|
|
|
|
spinner.spin($preloader[0]);
|
|
|
|
|
2013-09-27 12:31:55 +02:00
|
|
|
retrieveList();
|
|
|
|
})
|
|
|
|
function retrieveList() {
|
2013-10-02 17:24:40 +02:00
|
|
|
$preloader.show();
|
|
|
|
//spinner.spin($preloader[0]);
|
|
|
|
|
|
|
|
$.ajax({
|
2013-10-04 10:53:48 +02:00
|
|
|
url: "api/list.php",
|
2013-10-02 17:24:40 +02:00
|
|
|
dataType: 'json',
|
|
|
|
success: function(response){
|
|
|
|
//console.log("retrieveList response: ",response);
|
|
|
|
if(response.status == "success") {
|
|
|
|
updateList(response.data);
|
|
|
|
}
|
2013-09-27 12:31:55 +02:00
|
|
|
clearTimeout(retrieveListDelay);
|
2013-10-02 17:24:40 +02:00
|
|
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval);
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
//console.log("retrieveList: failed");
|
|
|
|
clearTimeout(retrieveListDelay);
|
|
|
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval); // retry after delay
|
|
|
|
});
|
2013-09-27 12:31:55 +02:00
|
|
|
}
|
|
|
|
function updateList(boxes) {
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking = 0;
|
|
|
|
numBoxesFound = 0;
|
2013-10-03 19:08:42 +02:00
|
|
|
|
|
|
|
// remove displayed, but unlisted boxes
|
|
|
|
$list.find("a").each(function(index, element) {
|
|
|
|
var localip = $(element).attr("id");
|
|
|
|
var wifiboxid = $(element).text();
|
|
|
|
var found = false;
|
|
|
|
jQuery.each(boxes, function (index,box) {
|
|
|
|
if(box.localip == localip && box.wifiboxid == wifiboxid) found = true;
|
|
|
|
});
|
|
|
|
if(!found) $(element).parent().remove();
|
|
|
|
})
|
|
|
|
|
2013-09-27 12:31:55 +02:00
|
|
|
jQuery.each(boxes, function (index,box) {
|
2013-09-30 17:34:27 +02:00
|
|
|
checkBox(box);
|
2013-09-27 12:31:55 +02:00
|
|
|
});
|
2013-10-02 17:24:40 +02:00
|
|
|
updateIntro();
|
2013-09-30 17:34:27 +02:00
|
|
|
}
|
|
|
|
function checkBox(box) {
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking++;
|
2013-09-30 17:34:27 +02:00
|
|
|
$.ajax({
|
2013-10-03 18:06:05 +02:00
|
|
|
url: "http://"+box.localip+"/d3dapi/network/alive",
|
|
|
|
dataType: "json",
|
2013-10-02 17:24:40 +02:00
|
|
|
timeout: boxTimeoutTime,
|
2013-09-30 17:34:27 +02:00
|
|
|
success: function(response){
|
|
|
|
if(response.status == "success") {
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesFound++;
|
2013-10-03 19:08:42 +02:00
|
|
|
addBox(box);
|
|
|
|
} else {
|
|
|
|
removeBox(box);
|
2013-09-30 17:34:27 +02:00
|
|
|
}
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking--;
|
|
|
|
updateIntro();
|
2013-09-30 17:34:27 +02:00
|
|
|
}
|
2013-10-02 17:24:40 +02:00
|
|
|
}).fail(function() {
|
2013-10-10 00:15:50 +02:00
|
|
|
//console.log("box not alive: "+box.wifiboxid);
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking--;
|
2013-10-03 19:08:42 +02:00
|
|
|
removeBox(box);
|
2013-10-02 17:24:40 +02:00
|
|
|
updateIntro();
|
2013-09-30 17:34:27 +02:00
|
|
|
});
|
|
|
|
}
|
2013-10-03 19:08:42 +02:00
|
|
|
|
|
|
|
function addBox(box) {
|
|
|
|
if(boxExists(box.localip)) return;
|
|
|
|
var url = "http://"+box.localip;
|
|
|
|
var element = "<li><a href='"+url+"' id='"+box.localip+"'>"+box.wifiboxid+"</a></li>";
|
|
|
|
$(element).hide().appendTo($list).fadeIn(500);
|
|
|
|
}
|
|
|
|
function boxExists(localip){
|
|
|
|
return $list.find("a[id|='"+localip+"']").length > 0;
|
|
|
|
}
|
|
|
|
function removeBox(box) {
|
2013-10-10 00:15:50 +02:00
|
|
|
var $element = $list.find("a[id|='"+box.localip+"']");
|
|
|
|
$element.remove();
|
2013-10-02 17:24:40 +02:00
|
|
|
}
|
2013-10-03 19:08:42 +02:00
|
|
|
|
2013-10-02 17:24:40 +02:00
|
|
|
function updateIntro() {
|
2013-10-18 11:58:26 +02:00
|
|
|
$intro.fadeIn();
|
2013-10-02 17:24:40 +02:00
|
|
|
if(numBoxesChecking <= 0) {
|
|
|
|
if(numBoxesFound > 0) {
|
|
|
|
$intro.html("Found the following boxes near you:");
|
2013-10-18 11:58:26 +02:00
|
|
|
$hint.fadeOut();
|
2013-10-02 17:24:40 +02:00
|
|
|
} else {
|
|
|
|
$intro.html("No boxes found near you.");
|
2013-10-18 11:58:26 +02:00
|
|
|
$hint.fadeIn();
|
2013-10-02 17:24:40 +02:00
|
|
|
}
|
|
|
|
$preloader.fadeOut(1000);
|
|
|
|
}
|
2013-09-27 12:31:55 +02:00
|
|
|
}
|