From c10eeb4fe445f1ba7daf94244168f62a5331946a Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Mon, 28 Apr 2014 16:54:57 +0200 Subject: [PATCH] Added Config and Info api libraries --- js/api/ConfigAPI.js | 102 ++++++++++++++++++++++++++++++++++++++++++++ js/api/InfoAPI.js | 60 ++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 js/api/ConfigAPI.js create mode 100644 js/api/InfoAPI.js diff --git a/js/api/ConfigAPI.js b/js/api/ConfigAPI.js new file mode 100644 index 0000000..3d93a15 --- /dev/null +++ b/js/api/ConfigAPI.js @@ -0,0 +1,102 @@ +/* + * This file is part of the Doodle3D project (http://doodle3d.com). + * + * Copyright (c) 2013, Doodle3D + * This software is licensed under the terms of the GNU GPL v2 or later. + * See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. + */ + +function ConfigAPI() { + + var _apiPath = "/d3dapi"; + var _apiCGIPath = "/cgi-bin"+_apiPath; + var _wifiboxURL; + var _wifiboxCGIBinURL; + + var _timeoutTime = 3000; + var _saveSettingsTimeoutTime = 8000; + + var _self = this; + + this.init = function(wifiboxURL) { + //console.log("ConfigAPI:init"); + + _wifiboxURL = wifiboxURL+_apiPath; + _wifiboxCGIBinURL = wifiboxURL+_apiCGIPath; + } + this.loadAll = function(completeHandler,failedHandler) { + //console.log("ConfigAPI:loadAll"); + $.ajax({ + url: _wifiboxURL + "/config/all", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + if(failedHandler) failedHandler(response); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; + this.load = function(targetSettings,completeHandler,failedHandler) { + //console.log("ConfigAPI:load"); + $.ajax({ + url: _wifiboxURL + "/config/", + type: "GET", + dataType: 'json', + data: targetSettings, + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + if(failedHandler) failedHandler(response); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; + this.save = function(newSettings,completeHandler,failedHandler) { + //console.log("ConfigAPI:save"); + $.ajax({ + url: _wifiboxCGIBinURL + "/config", + type: "POST", + data: newSettings, + dataType: 'json', + timeout: _saveSettingsTimeoutTime, + success: function(response){ + //console.log("ConfigAPI:save response: ",response); + if(response.status == "error" || response.status == "fail") { + if(failedHandler) failedHandler(response); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; + this.resetAll = function(completeHandler,failedHandler) { + //console.log("ConfigAPI:resetAll"); + $.ajax({ + url: _wifiboxCGIBinURL + "/config/resetall", + type: "POST", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + if(failedHandler) failedHandler(response); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; +} \ No newline at end of file diff --git a/js/api/InfoAPI.js b/js/api/InfoAPI.js new file mode 100644 index 0000000..75dbc8a --- /dev/null +++ b/js/api/InfoAPI.js @@ -0,0 +1,60 @@ +/* + * This file is part of the Doodle3D project (http://doodle3d.com). + * + * Copyright (c) 2013, Doodle3D + * This software is licensed under the terms of the GNU GPL v2 or later. + * See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. + */ + +function InfoAPI() { + + var _apiPath = "/d3dapi"; + var _apiCGIPath = "/cgi-bin"+_apiPath; + var _wifiboxURL; + var _wifiboxCGIBinURL; + + var _timeoutTime = 3000; + + var _configAPI = new ConfigAPI(); // needed for wifiboxid workaround + + var _self = this; + + this.init = function(wifiboxURL) { + console.log("InfoAPI:init"); + + _wifiboxURL = wifiboxURL+_apiPath; + _wifiboxCGIBinURL = wifiboxURL+_apiCGIPath; + _configAPI.init(wifiboxURL); + } + this.getInfo = function(completeHandler,failedHandler) { + $.ajax({ + url: _wifiboxURL + "/info", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + if(failedHandler) failedHandler(response); + } else { + var infoData = response.data; + + // Versions older than 0.10.2 don't include wifiboxid in info response + // so we use a workaround (saving to config) + if(infoData.wifiboxid === undefined) { + _configAPI.save({},function(saveResponseData) { + infoData.wifiboxid = saveResponseData.substituted_wifiboxid; + completeHandler(infoData); + },function() { + failedHandler(); + }); + } else { + completeHandler(infoData); + } + + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; +} \ No newline at end of file