2014-04-25 16:24:16 +02: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.
|
|
|
|
*/
|
|
|
|
|
2014-05-12 14:44:21 +02:00
|
|
|
(function (w) {
|
2014-04-25 16:24:16 +02:00
|
|
|
|
|
|
|
var _page;
|
|
|
|
var _statusField;
|
|
|
|
var _actionField;
|
|
|
|
var _networkAPI = new NetworkAPI();
|
2014-04-28 16:58:21 +02:00
|
|
|
var _connectAPI = new ConnectAPI();
|
2014-05-12 14:43:59 +02:00
|
|
|
var _connectedBoxNetworkAPI = new NetworkAPI();
|
2014-04-28 16:58:21 +02:00
|
|
|
var _infoAPI = new InfoAPI();
|
2014-04-25 16:24:16 +02:00
|
|
|
var _pageData = {};
|
2014-05-01 22:53:54 +02:00
|
|
|
var _formData;
|
2014-04-28 21:38:56 +02:00
|
|
|
var _wifiboxid;
|
2014-05-07 23:36:12 +02:00
|
|
|
var _wifiboxSSID;
|
2014-04-28 16:58:21 +02:00
|
|
|
var _connectedChecking = false;
|
2014-04-25 16:24:16 +02:00
|
|
|
|
2014-05-07 15:36:35 +02:00
|
|
|
var CONNECTED_REDIRECT_DELAY = 5000;
|
2014-05-12 14:43:59 +02:00
|
|
|
var _connectedRedirectDelay;
|
2014-05-07 23:36:12 +02:00
|
|
|
var BACKUP_REDIRECT_DELAY = 10*1000; // when the wifiboxid isn't retrievable we want to redirect anyway
|
2014-05-12 14:43:59 +02:00
|
|
|
var _backupRedirectDelay;
|
2014-04-25 16:24:16 +02:00
|
|
|
var PAGE_ID = "#connecting_to_network";
|
|
|
|
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
$.mobile.document.on( "pageinit", PAGE_ID, function( event, data ) {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(PAGE_ID+": pageinit");
|
2014-04-25 16:24:16 +02:00
|
|
|
_page = $(this);
|
|
|
|
_statusField = _page.find("#status");
|
|
|
|
_actionField = _page.find("#action");
|
|
|
|
});
|
|
|
|
$.mobile.document.on( "pagebeforeshow", PAGE_ID, function( event, data ) {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(PAGE_ID+": pagebeforeshow");
|
2014-04-25 16:24:16 +02:00
|
|
|
_pageData = d3d.util.getPageParams(PAGE_ID);
|
2014-05-14 12:12:59 +02:00
|
|
|
var form = data.prevPage.find("form");
|
2014-05-22 11:26:52 +02:00
|
|
|
// check if there are url params and
|
|
|
|
// when encrypted if there was a form from a prev page
|
|
|
|
if(_pageData.encryption === "") {
|
|
|
|
_pageData.encryption = "none";
|
|
|
|
}
|
|
|
|
if(_pageData === undefined ||
|
|
|
|
(_pageData.encryption !== "none" && form.length === 0)) {
|
2014-05-14 12:00:02 +02:00
|
|
|
$.mobile.changePage("#boxes");
|
|
|
|
return;
|
|
|
|
}
|
2014-04-25 16:24:16 +02:00
|
|
|
var boxURL = "http://"+_pageData.localip;
|
2014-05-01 22:53:54 +02:00
|
|
|
_formData = d3d.util.getFormData(form);
|
2014-04-28 16:58:21 +02:00
|
|
|
_infoAPI.init(boxURL);
|
2014-04-25 16:24:16 +02:00
|
|
|
_networkAPI.init(boxURL);
|
2014-04-28 16:58:21 +02:00
|
|
|
retrieveWiFiBoxID(function() {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(" _wifiboxid: ",_wifiboxid);
|
|
|
|
console.log(" _wifiboxSSID: ",_wifiboxSSID);
|
2014-04-28 16:58:21 +02:00
|
|
|
joinNetwork();
|
|
|
|
_networkAPI.refreshing = onRefreshing;
|
|
|
|
_networkAPI.updated = onStatusUpdated;
|
|
|
|
_networkAPI.startAutoRefresh();
|
|
|
|
});
|
2014-04-25 16:24:16 +02:00
|
|
|
});
|
2014-05-12 14:53:01 +02:00
|
|
|
$.mobile.document.on( "pagebeforehide", PAGE_ID, function( event, data ) {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(PAGE_ID+": pagebeforehide");
|
2014-04-25 16:24:16 +02:00
|
|
|
_networkAPI.stopAutoRefresh();
|
2014-04-28 16:58:21 +02:00
|
|
|
_connectAPI.stop();
|
2014-05-12 14:43:59 +02:00
|
|
|
_connectedBoxNetworkAPI.stopAutoRefresh();
|
|
|
|
clearTimeout(_connectedRedirectDelay);
|
|
|
|
clearTimeout(_backupRedirectDelay);
|
2014-04-25 16:24:16 +02:00
|
|
|
});
|
2014-04-28 16:58:21 +02:00
|
|
|
function retrieveWiFiBoxID(completeHandler) {
|
|
|
|
console.log(PAGE_ID+":retrieveWiFiBoxID");
|
|
|
|
_infoAPI.getInfo(function(infoData) {
|
|
|
|
_wifiboxid = infoData.wifiboxid;
|
2014-05-07 23:36:12 +02:00
|
|
|
_wifiboxSSID = infoData.substituted_ssid;
|
2014-04-28 16:58:21 +02:00
|
|
|
completeHandler();
|
|
|
|
},function() {
|
2014-05-07 23:36:12 +02:00
|
|
|
_wifiboxid = undefined;
|
|
|
|
_wifiboxSSID = undefined;
|
2014-05-07 17:17:42 +02:00
|
|
|
// try connecting anyway (making sure wifiboxid retrieval isn't blocking)
|
2014-04-28 16:58:21 +02:00
|
|
|
completeHandler();
|
|
|
|
});
|
|
|
|
}
|
2014-04-25 16:24:16 +02:00
|
|
|
function joinNetwork() {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(PAGE_ID+":joinNetwork");
|
2014-05-01 22:53:54 +02:00
|
|
|
_networkAPI.associate(_pageData.ssid,_formData.password,true);
|
2014-04-28 16:58:21 +02:00
|
|
|
_connectedChecking = false;
|
2014-04-25 16:24:16 +02:00
|
|
|
}
|
|
|
|
function onRefreshing() {
|
|
|
|
//console.log("ConnectingToNetworkPage:onRefreshing");
|
|
|
|
d3d.util.showLoader(true);
|
|
|
|
}
|
|
|
|
function onStatusUpdated(data) {
|
|
|
|
console.log("ConnectingToNetworkPage:onStatusUpdated");
|
2014-05-07 23:36:12 +02:00
|
|
|
//console.log(" data: ",data);
|
2014-04-25 16:24:16 +02:00
|
|
|
data.status = parseInt(data.status,10);
|
|
|
|
console.log(" data.status: ",data.status);
|
|
|
|
|
|
|
|
// update texts
|
|
|
|
var statusText = "";
|
|
|
|
var actionText = "";
|
|
|
|
switch(data.status) {
|
|
|
|
case NetworkAPI.STATUS.CONNECTING:
|
2014-05-01 11:37:25 +02:00
|
|
|
statusText = "Connecting to "+_pageData.ssid+"...";
|
2014-04-28 16:58:21 +02:00
|
|
|
//actionText = "Please reconnect yourself to <b>"+_pageData.ssid+"</b>. Once you are connected return to this page.";
|
2014-05-01 11:37:25 +02:00
|
|
|
actionText = "Please reconnect yourself to <b>"+_pageData.ssid+"</b>. Once you are connected return to this page.";
|
2014-04-28 16:58:21 +02:00
|
|
|
_actionField.attr("class","notice");
|
2014-04-25 16:24:16 +02:00
|
|
|
break;
|
|
|
|
case NetworkAPI.STATUS.CONNECTING_FAILED:
|
|
|
|
statusText = "Could not connect...";
|
|
|
|
actionText = "Please check password and try again";
|
|
|
|
_actionField.attr("class","error");
|
|
|
|
break;
|
2014-05-01 11:37:25 +02:00
|
|
|
case NetworkAPI.STATUS.CONNECTED:
|
|
|
|
statusText = "Connected to "+_pageData.ssid;
|
|
|
|
actionText = "Please reconnect yourself to <b>"+_pageData.ssid+"</b>. Once you are connected return to this page.";
|
|
|
|
_actionField.attr("class","notice");
|
|
|
|
break;
|
2014-04-25 16:24:16 +02:00
|
|
|
default:
|
|
|
|
actionText = "Something went wrong, please try again";
|
|
|
|
_actionField.attr("class","error");
|
|
|
|
break;
|
|
|
|
}
|
2014-05-07 17:17:42 +02:00
|
|
|
// TODO ignore connected?
|
2014-04-25 16:24:16 +02:00
|
|
|
_statusField.html(statusText);
|
|
|
|
_actionField.html(actionText);
|
|
|
|
|
2014-04-28 16:58:21 +02:00
|
|
|
// When the box is connecting we start checking connect.doodle3d.com
|
|
|
|
// for a box with the same wifiboxid
|
2014-05-07 23:36:12 +02:00
|
|
|
if(data.status === NetworkAPI.STATUS.CONNECTING && !_connectedChecking) {
|
|
|
|
if(_wifiboxid !== undefined || _wifiboxSSID !== undefined) {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(" start checking for same box");
|
2014-05-07 23:36:12 +02:00
|
|
|
_connectAPI.boxAppeared = onBoxAppeared;
|
|
|
|
} else {
|
|
|
|
// if there is no wifiboxid or ssid available we'll check if we're online
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(" start checking for internet");
|
2014-05-07 23:36:12 +02:00
|
|
|
_connectAPI.listSuccess = onListSuccess;
|
|
|
|
}
|
2014-05-07 17:17:42 +02:00
|
|
|
_connectAPI.checkLocal = false;
|
2014-04-28 16:58:21 +02:00
|
|
|
_connectAPI.start();
|
|
|
|
_connectedChecking = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function onBoxAppeared(boxData) {
|
|
|
|
console.log(PAGE_ID+":onBoxAppeared: ",boxData.localip,boxData.wifiboxid);
|
|
|
|
// if same box is found...
|
2014-05-07 23:36:12 +02:00
|
|
|
if(_wifiboxid !== undefined && boxData.wifiboxid === _wifiboxid) {
|
|
|
|
console.log("found _wifiboxid");
|
|
|
|
checkBox(boxData);
|
|
|
|
// wifiboxid of older firmware isn't available, fallback to ssid
|
|
|
|
} else if(_wifiboxSSID !== undefined){
|
|
|
|
console.log("no _wifiboxid, falling back to _wifiboxSSID comparison");
|
|
|
|
var connectedBoxConfigAPI = new ConfigAPI();
|
|
|
|
connectedBoxConfigAPI.init("http://"+boxData.localip);
|
|
|
|
connectedBoxConfigAPI.save({},function(saveResponseData) {
|
|
|
|
if(saveResponseData.substituted_ssid === _wifiboxSSID) {
|
|
|
|
checkBox(boxData);
|
2014-04-28 16:58:21 +02:00
|
|
|
}
|
2014-05-07 23:36:12 +02:00
|
|
|
});
|
2014-04-25 16:24:16 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 23:36:12 +02:00
|
|
|
function checkBox(boxData) {
|
|
|
|
// check if it finished connecting
|
|
|
|
var boxURL = "http://"+boxData.localip;
|
2014-05-12 14:43:59 +02:00
|
|
|
_connectedBoxNetworkAPI = new NetworkAPI();
|
|
|
|
_connectedBoxNetworkAPI.init(boxURL);
|
|
|
|
_connectedBoxNetworkAPI.updated = function(data) {
|
2014-05-07 23:36:12 +02:00
|
|
|
data.status = parseInt(data.status,10);
|
|
|
|
console.log(PAGE_ID+":connectedBoxNetworkAPI:onStatusUpdated: ",data.status);
|
|
|
|
// if box finished connecting
|
|
|
|
if(data.status === NetworkAPI.STATUS.CONNECTED) {
|
|
|
|
console.log(" found connected box");
|
|
|
|
_statusField.html("Connected to "+_pageData.ssid);
|
|
|
|
_actionField.html("Congratulations the box is connected to <b>"+_pageData.ssid+"</b>. You will be redirected in a moment...");
|
|
|
|
_actionField.attr("class","info");
|
|
|
|
// prevent status changes by wired box
|
|
|
|
_networkAPI.stopAutoRefresh();
|
|
|
|
|
2014-05-12 14:43:59 +02:00
|
|
|
_connectedRedirectDelay = setTimeout(function () {
|
2014-05-07 23:36:12 +02:00
|
|
|
// redirect to it's box page
|
|
|
|
console.log(" redirect to box");
|
2014-05-12 18:22:52 +02:00
|
|
|
// replace this page with boxes page in history
|
|
|
|
window.history.replaceState(null, "", "#boxes");
|
2014-05-07 23:36:12 +02:00
|
|
|
var linkParams = {localip: boxData.localip,wifiboxid: boxData.wifiboxid};
|
|
|
|
var link = "#box";
|
|
|
|
link = d3d.util.replaceURLParameters(link,linkParams);
|
|
|
|
$.mobile.changePage(link);
|
2014-05-12 14:43:59 +02:00
|
|
|
_connectedBoxNetworkAPI.stopAutoRefresh();
|
2014-05-07 23:36:12 +02:00
|
|
|
|
|
|
|
// disable warnings that are enabled on boxes page
|
|
|
|
d3d.util.disableRefreshPrevention();
|
|
|
|
d3d.util.disableLeaveWarning();
|
|
|
|
},CONNECTED_REDIRECT_DELAY);
|
|
|
|
}
|
|
|
|
};
|
2014-05-12 14:43:59 +02:00
|
|
|
_connectedBoxNetworkAPI.startAutoRefresh();
|
2014-05-07 23:36:12 +02:00
|
|
|
}
|
2014-05-12 14:44:21 +02:00
|
|
|
// when no wifiboxid or wifiboxSSID is available but we are online, we redirect to the boxes page
|
2014-05-07 23:36:12 +02:00
|
|
|
function onListSuccess() {
|
2014-05-14 16:35:23 +02:00
|
|
|
console.log(PAGE_ID+":onListSuccess");
|
2014-05-12 14:43:59 +02:00
|
|
|
_backupRedirectDelay = setTimeout(function () {
|
2014-05-07 23:36:12 +02:00
|
|
|
$.mobile.changePage("#boxes");
|
|
|
|
},BACKUP_REDIRECT_DELAY);
|
|
|
|
}
|
2014-04-25 18:38:41 +02:00
|
|
|
})(window);
|