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:
parent
e8d0283447
commit
d147ccceab
@ -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,6 +67,13 @@ function NetworkAPI() {
|
|||||||
};
|
};
|
||||||
this.status = function(completeHandler,failedHandler) {
|
this.status = function(completeHandler,failedHandler) {
|
||||||
//console.log("NetworkAPI:status");
|
//console.log("NetworkAPI:status");
|
||||||
|
// 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({
|
$.ajax({
|
||||||
url: _wifiboxURL + "/network/status",
|
url: _wifiboxURL + "/network/status",
|
||||||
type: "GET",
|
type: "GET",
|
||||||
@ -65,16 +84,64 @@ function NetworkAPI() {
|
|||||||
if(response.status == "error" || response.status == "fail") {
|
if(response.status == "error" || response.status == "fail") {
|
||||||
if(failedHandler) failedHandler(response);
|
if(failedHandler) failedHandler(response);
|
||||||
} else {
|
} 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);
|
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() {
|
||||||
|
Loading…
Reference in New Issue
Block a user