2015-04-25 17:26:12 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var API = function() {
|
2015-06-02 14:38:55 +02:00
|
|
|
var className = 'API';
|
2015-04-25 17:26:12 +02:00
|
|
|
var _wifiboxURL = 'http://192.168.5.1/d3dapi/';
|
|
|
|
var _wifiboxCGIBinURL = 'http://192.168.5.1/cgi-bin/d3dapi/';
|
|
|
|
var _timeoutTime = 10000;
|
2015-04-27 23:38:01 +02:00
|
|
|
var _isBusy = false;
|
|
|
|
|
|
|
|
function setURL(url,cgiUrl) {
|
|
|
|
_wifiboxURL = url;
|
|
|
|
_wifiboxCGIBinURL = cgiUrl || url;
|
|
|
|
}
|
2015-04-25 17:26:12 +02:00
|
|
|
|
|
|
|
function post(cmd,data,success,fail) {
|
2015-04-27 23:38:01 +02:00
|
|
|
_isBusy = true;
|
2015-04-25 17:26:12 +02:00
|
|
|
$.ajax({
|
|
|
|
url: _wifiboxURL + cmd,
|
|
|
|
type: "POST",
|
|
|
|
data: data,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
|
|
|
success: function(response){
|
2015-04-27 23:38:01 +02:00
|
|
|
_isBusy = false;
|
2015-04-25 17:26:12 +02:00
|
|
|
if(response.status == "error" || response.status == "fail") {
|
2015-06-02 14:38:55 +02:00
|
|
|
console.log(className,'post fail',cmd)
|
2015-04-25 17:26:12 +02:00
|
|
|
if (fail) fail(response);
|
|
|
|
} else {
|
|
|
|
if (success) success(response.data);
|
2015-06-02 14:38:55 +02:00
|
|
|
else console.log(className,'post:',cmd,'success cb undefined')
|
2015-04-25 17:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function(jqXHR, textStatus) {
|
2015-04-27 23:38:01 +02:00
|
|
|
_isBusy = false;
|
2015-06-02 14:38:55 +02:00
|
|
|
console.log(className,'post fail',cmd,jqXHR,textStatus);
|
2015-04-25 17:26:12 +02:00
|
|
|
if (fail) fail(jqXHR,textStatus);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-02 14:38:55 +02:00
|
|
|
function get(cmd,data,success,fail) {
|
2015-04-27 23:38:01 +02:00
|
|
|
_isBusy = true;
|
2015-04-25 17:26:12 +02:00
|
|
|
$.ajax({
|
|
|
|
url: _wifiboxURL + cmd,
|
|
|
|
type: "GET",
|
2015-06-02 14:38:55 +02:00
|
|
|
data: data,
|
2015-04-25 17:26:12 +02:00
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
2015-04-27 23:38:01 +02:00
|
|
|
success: function(response) {
|
|
|
|
_isBusy = false;
|
2015-04-25 17:26:12 +02:00
|
|
|
if (response.status == "error" || response.status == "fail") {
|
2015-06-02 14:38:55 +02:00
|
|
|
console.log(className,'get fail',cmd,response);
|
2015-04-25 17:26:12 +02:00
|
|
|
if (fail) fail(response);
|
|
|
|
} else {
|
|
|
|
if (success) success(response.data);
|
2015-06-02 14:38:55 +02:00
|
|
|
else console.log(className,'get:',cmd,'success cb undefined')
|
2015-04-25 17:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
2015-04-27 23:38:01 +02:00
|
|
|
_isBusy = false;
|
2015-06-02 14:38:55 +02:00
|
|
|
console.log(className,'get fail',cmd);
|
2015-04-25 17:26:12 +02:00
|
|
|
if (fail) fail();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-27 23:38:01 +02:00
|
|
|
function getBusy() {
|
|
|
|
return _isBusy;
|
|
|
|
}
|
|
|
|
|
2015-04-25 17:26:12 +02:00
|
|
|
return {
|
|
|
|
get: get,
|
2015-04-27 23:38:01 +02:00
|
|
|
post: post,
|
|
|
|
getBusy: getBusy,
|
|
|
|
setURL: setURL,
|
2015-04-25 17:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}();
|
|
|
|
|