From d147ccceab855a0fcd4a4cc8507d6aad9577144f Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Fri, 25 Apr 2014 16:23:43 +0200 Subject: [PATCH] Extended NetworkAPI Including usability improving hacks in this library --- js/api/NetworkAPI.js | 100 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 84 insertions(+), 16 deletions(-) diff --git a/js/api/NetworkAPI.js b/js/api/NetworkAPI.js index d029c21..0337cbd 100644 --- a/js/api/NetworkAPI.js +++ b/js/api/NetworkAPI.js @@ -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() {