mirror of
https://github.com/Doodle3D/doodle3d-connect.git
synced 2024-11-04 22:53:23 +01:00
Extended NetworkAPI
Including usability improving hacks in this library
This commit is contained in:
parent
e8d0283447
commit
d147ccceab
@ -23,6 +23,18 @@ function NetworkAPI() {
|
||||
var _wifiboxCGIBinURL;
|
||||
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;
|
||||
|
||||
this.init = function(wifiboxURL) {
|
||||
@ -55,26 +67,81 @@ function NetworkAPI() {
|
||||
};
|
||||
this.status = function(completeHandler,failedHandler) {
|
||||
//console.log("NetworkAPI:status");
|
||||
$.ajax({
|
||||
url: _wifiboxURL + "/network/status",
|
||||
type: "GET",
|
||||
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 {
|
||||
completeHandler(response.data);
|
||||
// After switching wifi network or creating a access point we delay the actual status
|
||||
// retrieval because the webserver needs time to switch it's status
|
||||
var now = new Date().getTime();
|
||||
if(now < _associateTime+_retrieveStatusDelayTime) {
|
||||
var data = {status: NetworkAPI.STATUS.CONNECTING};
|
||||
completeHandler(data);
|
||||
} else {
|
||||
$.ajax({
|
||||
url: _wifiboxURL + "/network/status",
|
||||
type: "GET",
|
||||
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() {
|
||||
if(failedHandler) failedHandler();
|
||||
});
|
||||
}).fail(function() {
|
||||
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) {
|
||||
//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 = {
|
||||
ssid:ssid,
|
||||
phrase:phrase,
|
||||
@ -92,6 +159,7 @@ function NetworkAPI() {
|
||||
}).fail(function() {
|
||||
//console.log("NetworkAPI:associate: timeout (normal behavior)");
|
||||
});
|
||||
_associateTime = new Date().getTime();
|
||||
};
|
||||
|
||||
this.openAP = function() {
|
||||
|
Loading…
Reference in New Issue
Block a user