0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-22 09:17:56 +01:00

Removed separate client and ap mode state system

This commit is contained in:
peteruithoven 2014-02-04 15:26:43 +01:00
parent 4ef164fb17
commit 556fc0dd1e

View File

@ -11,22 +11,8 @@
NetworkPanel.prototype = new FormPanel(); NetworkPanel.prototype = new FormPanel();
function NetworkPanel() { function NetworkPanel() {
// network client mode states var NOT_CONNECTED = "not connected"; // used as first item in networks list
var CLIENT_MODE_STATE = {
NOT_CONNECTED: "not connected", // also used as first item in networks list
CONNECTED: "connected",
CONNECTING: "connecting",
CONNECTING_FAILED: "connecting failed"
};
var _clientModeState = CLIENT_MODE_STATE.NOT_CONNECTED;
// network access point mode states
var AP_MODE_STATE = {
NO_AP: "no ap", // also used as first item in networks list
AP: "ap",
CREATING_AP: "creating ap"
};
var _apModeState = AP_MODE_STATE.NO_AP;
// network mode // network mode
var NETWORK_MODE = { var NETWORK_MODE = {
NEITHER: "neither", NEITHER: "neither",
@ -40,8 +26,8 @@ function NetworkPanel() {
var _currentNetwork; // the ssid of the network the box is on var _currentNetwork; // the ssid of the network the box is on
var _selectedNetwork; // the ssid of the selected network in the client mode settings var _selectedNetwork; // the ssid of the selected network in the client mode settings
var _currentLocalIP = ""; var _currentLocalIP = "";
var _currentAP; var _currentAP;
var _currentNetworkStatus;
var _retryRetrieveStatusDelayTime = 1000; var _retryRetrieveStatusDelayTime = 1000;
var _retryRetrieveStatusDelay; var _retryRetrieveStatusDelay;
@ -98,7 +84,6 @@ function NetworkPanel() {
/* /*
* Handlers * Handlers
*/ */
function showAPSettings() { function showAPSettings() {
_apFieldSet.show(); _apFieldSet.show();
_clientFieldSet.hide(); _clientFieldSet.hide();
@ -132,7 +117,7 @@ function NetworkPanel() {
// fill network selector // fill network selector
_networkSelector.empty(); _networkSelector.empty();
_networkSelector.append( _networkSelector.append(
$("<option></option>").val(CLIENT_MODE_STATE.NOT_CONNECTED).html(CLIENT_MODE_STATE.NOT_CONNECTED) $("<option></option>").val(NOT_CONNECTED).html(NOT_CONNECTED)
); );
$.each(data.networks, function(index,element) { $.each(data.networks, function(index,element) {
if(element.ssid == _currentNetwork) { if(element.ssid == _currentNetwork) {
@ -184,8 +169,8 @@ function NetworkPanel() {
setNetworkMode(NETWORK_MODE.ACCESS_POINT); setNetworkMode(NETWORK_MODE.ACCESS_POINT);
_currentNetwork = undefined; _currentNetwork = undefined;
_self.selectNetwork(CLIENT_MODE_STATE.NOT_CONNECTED); _self.selectNetwork(NOT_CONNECTED);
_networkSelector.val(CLIENT_MODE_STATE.NOT_CONNECTED); _networkSelector.val(NOT_CONNECTED);
if(data.ssid && data.status == NetworkAPI.STATUS.CREATED) { if(data.ssid && data.status == NetworkAPI.STATUS.CREATED) {
_currentAP = data.ssid; _currentAP = data.ssid;
@ -193,32 +178,10 @@ function NetworkPanel() {
break; break;
} }
// update status message // update ui if status changed
switch(data.status) { if(data.status != _currentNetworkStatus) {
case NetworkAPI.STATUS.CONNECTING_FAILED: updateClientModeUI(data.status,data.statusMessage);
setClientModeState(CLIENT_MODE_STATE.CONNECTING_FAILED,data.statusMessage); updateAPModeUI(data.status,"");
setAPModeState(AP_MODE_STATE.NO_AP,"");
break;
case NetworkAPI.STATUS.NOT_CONNECTED:
setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,"");
setAPModeState(AP_MODE_STATE.NO_AP,"");
break;
case NetworkAPI.STATUS.CONNECTING:
setClientModeState(CLIENT_MODE_STATE.CONNECTING,"");
setAPModeState(AP_MODE_STATE.NO_AP,"");
break;
case NetworkAPI.STATUS.CONNECTED:
setClientModeState(CLIENT_MODE_STATE.CONNECTED,"");
setAPModeState(AP_MODE_STATE.NO_AP,"");
break;
case NetworkAPI.STATUS.CREATING:
setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,"");
setAPModeState(AP_MODE_STATE.CREATING_AP,"");
break;
case NetworkAPI.STATUS.CREATED:
setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,"");
setAPModeState(AP_MODE_STATE.AP,"");
break;
} }
// Keep checking for updates? // Keep checking for updates?
@ -231,6 +194,7 @@ function NetworkPanel() {
break; break;
} }
} }
_currentNetworkStatus = data.status;
}, function() { }, function() {
console.log("NetworkPanel:retrieveStatus failed"); console.log("NetworkPanel:retrieveStatus failed");
clearTimeout(_retryRetrieveStatusDelay); clearTimeout(_retryRetrieveStatusDelay);
@ -265,40 +229,29 @@ function NetworkPanel() {
console.log("NetworkPanel:selectNetwork: ",ssid); console.log("NetworkPanel:selectNetwork: ",ssid);
if(ssid == "") return; if(ssid == "") return;
_selectedNetwork = ssid; _selectedNetwork = ssid;
if(ssid == CLIENT_MODE_STATE.NOT_CONNECTED) {
hideWiFiPassword(); var network = _networks[ssid];
if(network === undefined || network.encryption == "none") {
_passwordLabel.hide();
_passwordField.hide();
} else { } else {
var network = _networks[ssid]; _passwordLabel.show();
if(network === undefined) return; _passwordField.show();
if(network.encryption == "none") {
hideWiFiPassword();
} else {
showWiFiPassword();
}
_passwordField.val("");
} }
}; _passwordField.val("");
function showWiFiPassword() {
_passwordLabel.show();
_passwordField.show();
};
function hideWiFiPassword() {
_passwordLabel.hide();
_passwordField.hide();
}; };
function setClientModeState(state,statusMessage) { function updateClientModeUI(state,statusMessage) {
console.log("NetworkPanel:setClientModeState ",state,statusMessage); //console.log("NetworkPanel:updateClientModeUI ",state,statusMessage);
//console.log(" CLIENT_MODE_STATE.CONNECTING: ",CLIENT_MODE_STATE.CONNECTING);
var msg = ""; var msg = "";
switch(state) { switch(state) {
case CLIENT_MODE_STATE.NOT_CONNECTED: case NetworkAPI.STATUS.NOT_CONNECTED:
//console.log("CLIENT_MODE_STATE.NOT_CONNECTED"); case NetworkAPI.STATUS.CREATING:
case NetworkAPI.STATUS.CREATED:
_btnConnect.removeAttr("disabled"); _btnConnect.removeAttr("disabled");
msg = "Not connected"; msg = "Not connected";
break; break;
case CLIENT_MODE_STATE.CONNECTED: case NetworkAPI.STATUS.CONNECTED:
//console.log("CLIENT_MODE_STATE.CONNECTED");
_btnConnect.removeAttr("disabled"); _btnConnect.removeAttr("disabled");
msg = "Connected to: <b>"+_currentNetwork+"</b>."; msg = "Connected to: <b>"+_currentNetwork+"</b>.";
@ -307,41 +260,39 @@ function NetworkPanel() {
msg += " (IP: "+a+")"; msg += " (IP: "+a+")";
} }
break; break;
case CLIENT_MODE_STATE.CONNECTING: case NetworkAPI.STATUS.CONNECTING:
//console.log("CLIENT_MODE_STATE.CONNECTING");
_btnConnect.attr("disabled", true); _btnConnect.attr("disabled", true);
msg = "Connecting... Reconnect by connecting your device to <b>"+_selectedNetwork+"</b> and going to <a href='http://connect.doodle3d.com'>connect.doodle3d.com</a>"; msg = "Connecting... Reconnect by connecting your device to <b>"+_selectedNetwork+"</b> and going to <a href='http://connect.doodle3d.com'>connect.doodle3d.com</a>";
break; break;
case CLIENT_MODE_STATE.CONNECTING_FAILED: case NetworkAPI.STATUS.CONNECTING_FAILED:
//console.log("CLIENT_MODE_STATE.CONNECTING_FAILED");
_btnConnect.removeAttr("disabled"); _btnConnect.removeAttr("disabled");
msg = statusMessage; msg = statusMessage;
break; break;
} }
console.log(" msg: ",msg); console.log(" client display msg: ",msg);
_clientStateDisplay.html(msg); _clientStateDisplay.html(msg);
_clientModeState = state;
}; };
function setAPModeState(state,statusMessage) { function updateAPModeUI(state,statusMessage) {
//console.log("NetworkPanel:setAPModeState ",state,statusMessage);
var msg = ""; var msg = "";
switch(state) { switch(state) {
case AP_MODE_STATE.NO_AP: case NetworkAPI.STATUS.CONNECTING_FAILED:
_btnCreate.removeAttr("disabled"); case NetworkAPI.STATUS.NOT_CONNECTED:
msg = "Not currently a access point"; case NetworkAPI.STATUS.CONNECTING:
break; case NetworkAPI.STATUS.CONNECTED:
case AP_MODE_STATE.AP: _btnCreate.removeAttr("disabled");
_btnCreate.removeAttr("disabled"); msg = "Not currently a access point";
msg = "Is access point: <b>"+_currentAP+"</b>"; break;
break; case NetworkAPI.STATUS.CREATED:
case AP_MODE_STATE.CREATING_AP: _btnCreate.removeAttr("disabled");
_btnCreate.attr("disabled", true); msg = "Is access point: <b>"+_currentAP+"</b>";
msg = "Creating access point... Reconnect by connecting your device to <b>"+settings.substituted_ssid+"</b> and going to <a href='http://draw.doodle3d.com'>draw.doodle3d.com</a>"; break;
break; case NetworkAPI.STATUS.CREATING:
_btnCreate.attr("disabled", true);
msg = "Creating access point... Reconnect by connecting your device to <b>"+settings.substituted_ssid+"</b> and going to <a href='http://draw.doodle3d.com'>draw.doodle3d.com</a>";
break;
} }
//console.log(" msg: ",msg); console.log(" ap display msg: ",msg);
_apModeStateDisplay.html(msg); _apModeStateDisplay.html(msg);
_apModeState = state;
}; };
this.connectToNetwork = function() { this.connectToNetwork = function() {
@ -350,7 +301,7 @@ function NetworkPanel() {
// save network related settings and on complete, connect to network // save network related settings and on complete, connect to network
_self.saveSettings(_self.readForm(),function(validated) { _self.saveSettings(_self.readForm(),function(validated) {
if(!validated) return; if(!validated) return;
setClientModeState(CLIENT_MODE_STATE.CONNECTING,""); updateClientModeUI(NetworkAPI.STATUS.CONNECTING,"");
_api.associate(_selectedNetwork,_passwordField.val(),true); _api.associate(_selectedNetwork,_passwordField.val(),true);
// after switching wifi network or creating a access point we delay the status retrieval // after switching wifi network or creating a access point we delay the status retrieval
@ -365,7 +316,7 @@ function NetworkPanel() {
// save network related settings and on complete, create access point // save network related settings and on complete, create access point
_self.saveSettings(_self.readForm(),function(success) { _self.saveSettings(_self.readForm(),function(success) {
if(!success) return; if(!success) return;
setAPModeState(AP_MODE_STATE.CREATING_AP,""); updateAPModeUI(NetworkAPI.STATUS.CREATING,"");
_api.openAP(); _api.openAP();
// after switching wifi network or creating a access point we delay the status retrieval // after switching wifi network or creating a access point we delay the status retrieval