0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-14 06:47:55 +01:00
doodle3d-client/js/api/API.js

84 lines
2.0 KiB
JavaScript
Raw Normal View History

/*
* 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';
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;
}
function post(cmd,data,success,fail) {
2015-04-27 23:38:01 +02:00
_isBusy = true;
$.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;
if(response.status == "error" || response.status == "fail") {
2015-06-02 14:38:55 +02:00
console.log(className,'post fail',cmd)
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')
}
}
}).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);
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;
$.ajax({
url: _wifiboxURL + cmd,
type: "GET",
2015-06-02 14:38:55 +02:00
data: data,
dataType: 'json',
timeout: _timeoutTime,
2015-04-27 23:38:01 +02:00
success: function(response) {
_isBusy = false;
if (response.status == "error" || response.status == "fail") {
2015-06-02 14:38:55 +02:00
console.log(className,'get fail',cmd,response);
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')
}
}
}).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);
if (fail) fail();
});
}
2015-04-27 23:38:01 +02:00
function getBusy() {
return _isBusy;
}
return {
get: get,
2015-04-27 23:38:01 +02:00
post: post,
getBusy: getBusy,
setURL: setURL,
}
}();