2014-02-24 11:26:30 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, Doodle3D
|
|
|
|
* This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
*/
|
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
|
|
|
|
|
2014-02-14 15:49:12 +01:00
|
|
|
var connectedBox = {localip:"192.168.5.1",wifiboxid:"Wired WiFi-Box"};
|
2014-02-24 11:26:30 +01:00
|
|
|
var apBox = {localip:"192.168.10.1",wifiboxid:"WiFi-Box",link:"http://draw.doodle3d.com"};
|
2014-02-14 15:49:12 +01:00
|
|
|
var connectAPI = "http://connect.doodle3d.com/api"
|
|
|
|
var boxAPI = "http://draw.doodle3d.com/d3dapi";
|
2013-10-18 13:19:36 +02:00
|
|
|
|
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
|
|
|
|
2014-02-24 11:26:30 +01:00
|
|
|
var boxes = {};
|
|
|
|
|
|
|
|
var networkAPI = new NetworkAPI();
|
|
|
|
var connectAPI = new ConnectAPI();
|
|
|
|
|
2013-09-27 12:31:55 +02:00
|
|
|
$(function() {
|
2014-02-14 15:49:12 +01:00
|
|
|
// console.log("ready");
|
2014-02-24 11:26:30 +01:00
|
|
|
|
|
|
|
networkAPI.init();
|
|
|
|
|
2014-02-14 15:49:12 +01:00
|
|
|
$intro = $("#intro");
|
2013-10-02 17:24:40 +02:00
|
|
|
$list = $("#list");
|
2014-02-24 11:26:30 +01:00
|
|
|
|
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();
|
2014-02-14 15:49:12 +01:00
|
|
|
});
|
|
|
|
|
2013-09-27 12:31:55 +02:00
|
|
|
function retrieveList() {
|
2013-10-02 17:24:40 +02:00
|
|
|
$preloader.show();
|
|
|
|
//spinner.spin($preloader[0]);
|
2014-02-24 11:26:30 +01:00
|
|
|
connectAPI.list(function(foundBoxes) {
|
|
|
|
//console.log(" foundBoxes: ",foundBoxes);
|
|
|
|
foundBoxes.push(connectedBox);
|
|
|
|
updateList(foundBoxes);
|
|
|
|
clearTimeout(retrieveListDelay);
|
|
|
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval);
|
|
|
|
removeBox(apBox.localip,true);
|
|
|
|
}, function() {
|
2014-02-16 01:48:40 +01:00
|
|
|
// if web is not accessible try to find the box as an accesspoint
|
|
|
|
// if not found, we look for a wired box
|
2014-02-24 11:26:30 +01:00
|
|
|
networkAPI.alive(apBox.localip,boxTimeoutTime,function() {
|
|
|
|
console.log("found apBox");
|
|
|
|
updateList([apBox]);
|
|
|
|
}, function() {
|
|
|
|
console.log("not found apBox");
|
|
|
|
updateList([connectedBox]);
|
|
|
|
});
|
2013-10-02 17:24:40 +02:00
|
|
|
clearTimeout(retrieveListDelay);
|
|
|
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval); // retry after delay
|
|
|
|
});
|
2013-09-27 12:31:55 +02:00
|
|
|
}
|
2014-02-14 15:49:12 +01:00
|
|
|
|
2014-02-24 11:26:30 +01:00
|
|
|
function updateList(foundBoxes) {
|
|
|
|
//console.log("updateList");
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking = 0;
|
|
|
|
numBoxesFound = 0;
|
2013-10-03 19:08:42 +02:00
|
|
|
|
2014-02-14 15:49:12 +01:00
|
|
|
if (boxes===undefined) boxes = [];
|
2014-02-16 01:48:40 +01:00
|
|
|
|
2014-02-24 11:26:30 +01:00
|
|
|
// remove displayed, but not found boxes
|
|
|
|
jQuery.each(boxes, function (index,box) {
|
2013-10-03 19:08:42 +02:00
|
|
|
var found = false;
|
2014-02-24 11:26:30 +01:00
|
|
|
jQuery.each(foundBoxes, function (index,foundBox) {
|
|
|
|
if(foundBox.localip == box.localip &&
|
|
|
|
foundBox.wifiboxid == box.wifiboxid) found = true;
|
2013-10-03 19:08:42 +02:00
|
|
|
});
|
2014-02-24 11:26:30 +01:00
|
|
|
if(!found) removeBox(box.localip);
|
2013-10-03 19:08:42 +02:00
|
|
|
})
|
|
|
|
|
2014-02-24 11:26:30 +01:00
|
|
|
// check if all found boxes are alive
|
|
|
|
jQuery.each(foundBoxes, function (index,foundBox) {
|
|
|
|
checkBox(foundBox);
|
2013-09-27 12:31:55 +02:00
|
|
|
});
|
2014-02-24 11:26:30 +01:00
|
|
|
|
2013-10-02 17:24:40 +02:00
|
|
|
updateIntro();
|
2013-09-30 17:34:27 +02:00
|
|
|
}
|
2014-02-14 15:49:12 +01:00
|
|
|
|
2014-02-24 11:26:30 +01:00
|
|
|
function checkBox(boxData) {
|
|
|
|
//console.log(" checkBox: ",boxData.localip);
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking++;
|
2014-02-24 11:26:30 +01:00
|
|
|
|
|
|
|
networkAPI.alive(boxData.localip,boxTimeoutTime,function() {
|
|
|
|
addBox(boxData);
|
|
|
|
numBoxesFound++;
|
|
|
|
numBoxesChecking--;
|
|
|
|
updateIntro();
|
|
|
|
}, function() {
|
|
|
|
removeBox(boxData.localip);
|
2013-10-02 17:24:40 +02:00
|
|
|
numBoxesChecking--;
|
|
|
|
updateIntro();
|
2013-09-30 17:34:27 +02:00
|
|
|
});
|
|
|
|
}
|
2014-02-24 11:26:30 +01:00
|
|
|
function getBox(localip) {
|
|
|
|
return boxes[localip];
|
|
|
|
}
|
|
|
|
function addBox(boxData) {
|
|
|
|
if(getBox(boxData.localip) !== undefined) return;
|
|
|
|
//console.log("addBox: ",boxData.localip);
|
|
|
|
var box = new Box();
|
|
|
|
box.init(boxData,$list);
|
|
|
|
box.destroyedHandler = boxDestroyedHandler;
|
|
|
|
boxes[box.localip] = box;
|
2013-10-03 19:08:42 +02:00
|
|
|
}
|
2014-02-24 11:26:30 +01:00
|
|
|
function removeBox(localip,force) {
|
|
|
|
var box = getBox(localip);
|
|
|
|
if(box === undefined) return;
|
|
|
|
//console.log("removeBox: ",localip," force: ",force);
|
|
|
|
if(!force && box.connecting) return;
|
|
|
|
//console.log(" calling destroyed");
|
|
|
|
box.destroy();
|
2013-10-03 19:08:42 +02:00
|
|
|
}
|
2014-02-24 11:26:30 +01:00
|
|
|
function boxDestroyedHandler(box) {
|
|
|
|
//console.log("boxDestroyedHandler");
|
|
|
|
delete boxes[box.localip];
|
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);
|
|
|
|
}
|
2014-02-25 11:30:20 +01:00
|
|
|
}
|