0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-12-27 02:53:48 +01:00
doodle3d-connect/js/ConnectingToNetworkPage.js

208 lines
7.6 KiB
JavaScript
Raw Normal View History

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();
var _connectAPI = new ConnectAPI();
var _connectedBoxNetworkAPI = new NetworkAPI();
var _infoAPI = new InfoAPI();
2014-04-25 16:24:16 +02:00
var _pageData = {};
var _formData;
2014-04-28 21:38:56 +02:00
var _wifiboxid;
var _wifiboxSSID;
var _connectedChecking = false;
2014-04-25 16:24:16 +02:00
2014-05-07 15:36:35 +02:00
var CONNECTED_REDIRECT_DELAY = 5000;
var _connectedRedirectDelay;
var BACKUP_REDIRECT_DELAY = 10*1000; // when the wifiboxid isn't retrievable we want to redirect anyway
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");
// 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)) {
//RC: removed this check because it makes it more difficult to re-associate with a known network
$.mobile.changePage("#boxes");
return;
}
2014-04-25 16:24:16 +02:00
var boxURL = "http://"+_pageData.localip;
_formData = d3d.util.getFormData(form);
_infoAPI.init(boxURL);
2014-04-25 16:24:16 +02:00
_networkAPI.init(boxURL);
retrieveWiFiBoxID(function() {
2014-05-14 16:35:23 +02:00
console.log(" _wifiboxid: ",_wifiboxid);
console.log(" _wifiboxSSID: ",_wifiboxSSID);
joinNetwork();
_networkAPI.refreshing = onRefreshing;
_networkAPI.updated = onStatusUpdated;
_networkAPI.startAutoRefresh();
});
2014-04-25 16:24:16 +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();
_connectAPI.stop();
_connectedBoxNetworkAPI.stopAutoRefresh();
clearTimeout(_connectedRedirectDelay);
clearTimeout(_backupRedirectDelay);
2014-04-25 16:24:16 +02:00
});
function retrieveWiFiBoxID(completeHandler) {
console.log(PAGE_ID+":retrieveWiFiBoxID");
_infoAPI.getInfo(function(infoData) {
_wifiboxid = infoData.wifiboxid;
_wifiboxSSID = infoData.substituted_ssid;
completeHandler();
},function() {
_wifiboxid = undefined;
_wifiboxSSID = undefined;
// try connecting anyway (making sure wifiboxid retrieval isn't blocking)
completeHandler();
});
}
2014-04-25 16:24:16 +02:00
function joinNetwork() {
console.log(PAGE_ID+":joinNetwork",_formData.password);
//_networkAPI.associate(_pageData.ssid,_formData.password,true); //recreate=true
_networkAPI.associate(_pageData.ssid,_formData.password,false);
_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");
//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+"...";
//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.";
_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;
}
// TODO ignore connected?
2014-04-25 16:24:16 +02:00
_statusField.html(statusText);
_actionField.html(actionText);
// When the box is connecting we start checking connect.doodle3d.com
// for a box with the same wifiboxid
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");
_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");
_connectAPI.listSuccess = onListSuccess;
}
_connectAPI.checkLocal = false;
_connectAPI.start();
_connectedChecking = true;
}
}
function onBoxAppeared(boxData) {
console.log(PAGE_ID+":onBoxAppeared: ",boxData.localip,boxData.wifiboxid);
// if same box is found...
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-25 16:24:16 +02:00
}
}
function checkBox(boxData) {
// check if it finished connecting
var boxURL = "http://"+boxData.localip;
_connectedBoxNetworkAPI = new NetworkAPI();
_connectedBoxNetworkAPI.init(boxURL);
_connectedBoxNetworkAPI.updated = function(data) {
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();
_connectedRedirectDelay = setTimeout(function () {
// 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");
var linkParams = {localip: boxData.localip,wifiboxid: boxData.wifiboxid};
var link = "#box";
link = d3d.util.replaceURLParameters(link,linkParams);
$.mobile.changePage(link);
_connectedBoxNetworkAPI.stopAutoRefresh();
// disable warnings that are enabled on boxes page
d3d.util.disableRefreshPrevention();
d3d.util.disableLeaveWarning();
},CONNECTED_REDIRECT_DELAY);
}
};
_connectedBoxNetworkAPI.startAutoRefresh();
}
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
function onListSuccess() {
2014-05-14 16:35:23 +02:00
console.log(PAGE_ID+":onListSuccess");
_backupRedirectDelay = setTimeout(function () {
$.mobile.changePage("#boxes");
},BACKUP_REDIRECT_DELAY);
}
2014-04-25 18:38:41 +02:00
})(window);