0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-12-25 18:13:48 +01:00

Extended NetworkAPI

Including usability improving hacks in this library
This commit is contained in:
peteruithoven 2014-04-25 16:23:43 +02:00
parent e8d0283447
commit d147ccceab

View File

@ -23,6 +23,18 @@ function NetworkAPI() {
var _wifiboxCGIBinURL; var _wifiboxCGIBinURL;
var _timeoutTime = 3000; var _timeoutTime = 3000;
var _networkStatus;
var _networkSSID;
var _associateTime;
var _retrieveStatusDelayTime = 1000;
var _autoRefreshing = false;
var _refreshDelay;
this.refreshDelayTime = 2000;
//callbacks
this.refreshing; // I'm refreshing
this.updated; // New network status info
var _self = this; var _self = this;
this.init = function(wifiboxURL) { this.init = function(wifiboxURL) {
@ -55,26 +67,81 @@ function NetworkAPI() {
}; };
this.status = function(completeHandler,failedHandler) { this.status = function(completeHandler,failedHandler) {
//console.log("NetworkAPI:status"); //console.log("NetworkAPI:status");
$.ajax({ // After switching wifi network or creating a access point we delay the actual status
url: _wifiboxURL + "/network/status", // retrieval because the webserver needs time to switch it's status
type: "GET", var now = new Date().getTime();
dataType: 'json', if(now < _associateTime+_retrieveStatusDelayTime) {
timeout: _timeoutTime, var data = {status: NetworkAPI.STATUS.CONNECTING};
success: function(response){ completeHandler(data);
//console.log("NetworkAPI:status response: ",response); } else {
if(response.status == "error" || response.status == "fail") { $.ajax({
if(failedHandler) failedHandler(response); url: _wifiboxURL + "/network/status",
} else { type: "GET",
completeHandler(response.data); dataType: 'json',
timeout: _timeoutTime,
success: function(response){
//console.log("NetworkAPI:status response: ",response);
if(response.status == "error" || response.status == "fail") {
if(failedHandler) failedHandler(response);
} else {
var data = response.data;
// The WiFi-Box won't create a access point when it already is when booting,
// so when the status is empty it actually is a access point.
if(data.status === "") {
data.status = NetworkAPI.STATUS.CREATED.toString();
}
completeHandler(response.data);
}
} }
} }).fail(function() {
}).fail(function() { if(failedHandler) failedHandler();
if(failedHandler) failedHandler(); });
}); }
}; };
this.startAutoRefresh = function(delay,refreshingHandler,updatedHandler) {
if(delay !== undefined) { _self.refreshDelayTime = delay; }
if(refreshingHandler !== undefined) { _self.refreshing = refreshingHandler; }
if(updatedHandler !== undefined) { _self.updated = updatedHandler; }
_autoRefreshing = true;
_self.refresh();
}
this.stopAutoRefresh = function() {
_autoRefreshing = false;
clearTimeout(_refreshDelay);
}
this.refresh = function() {
//console.log("NetworkAPI:refresh");
if(_self.refreshing) { _self.refreshing(); }
_self.status(function(data) { // completed
//console.log("NetworkAPI:refresh:completed");
//console.log(" data: ",data);
if(_self.updated !== undefined &&
(_networkStatus !== data.status || _networkSSID !== data.ssid)) {
_networkStatus = data.status;
_networkSSID = data.ssid;
_self.updated(data);
}
if(_autoRefreshing) {
// keep refreshing
clearTimeout(_refreshDelay);
_refreshDelay = setTimeout(_self.refresh, _self.refreshDelayTime);
}
},function() { // failed
if(_autoRefreshing) {
// retry
clearTimeout(_refreshDelay);
_refreshDelay = setTimeout(_self.refresh, _self.refreshDelayTime);
}
});
}
this.associate = function(ssid,phrase,recreate) { this.associate = function(ssid,phrase,recreate) {
//console.log("NetworkAPI:associate"); console.log("NetworkAPI:associate");
console.log(" ssid: ",ssid);
console.log(" recreate: ",recreate);
if(phrase === undefined) { phrase = ""; }
console.log(" phrase: ",phrase);
var postData = { var postData = {
ssid:ssid, ssid:ssid,
phrase:phrase, phrase:phrase,
@ -92,6 +159,7 @@ function NetworkAPI() {
}).fail(function() { }).fail(function() {
//console.log("NetworkAPI:associate: timeout (normal behavior)"); //console.log("NetworkAPI:associate: timeout (normal behavior)");
}); });
_associateTime = new Date().getTime();
}; };
this.openAP = function() { this.openAP = function() {