0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-13 06:27:55 +01:00

API refactoring

This commit is contained in:
Rick Companje 2015-06-02 14:38:55 +02:00
parent 43a131acde
commit e82703420c
4 changed files with 46 additions and 19 deletions

View File

@ -7,7 +7,7 @@
*/
var API = function() {
var className = 'API';
var _wifiboxURL = 'http://192.168.5.1/d3dapi/';
var _wifiboxCGIBinURL = 'http://192.168.5.1/cgi-bin/d3dapi/';
var _timeoutTime = 10000;
@ -29,40 +29,41 @@ var API = function() {
success: function(response){
_isBusy = false;
if(response.status == "error" || response.status == "fail") {
console.log('API.post fail',cmd)
console.log(className,'post fail',cmd)
if (fail) fail(response);
} else {
if (success) success(response.data);
else console.log('API.post:',cmd,'success cb undefined')
else console.log(className,'post:',cmd,'success cb undefined')
}
}
}).fail(function(jqXHR, textStatus) {
_isBusy = false;
console.log('API.post fail',cmd,jqXHR,textStatus);
console.log(className,'post fail',cmd,jqXHR,textStatus);
if (fail) fail(jqXHR,textStatus);
});
}
function get(cmd,success,fail) {
function get(cmd,data,success,fail) {
_isBusy = true;
$.ajax({
url: _wifiboxURL + cmd,
type: "GET",
data: data,
dataType: 'json',
timeout: _timeoutTime,
success: function(response) {
_isBusy = false;
if (response.status == "error" || response.status == "fail") {
console.log('API.get fail',cmd,response);
console.log(className,'get fail',cmd,response);
if (fail) fail(response);
} else {
if (success) success(response.data);
else console.log('API.get:',cmd,'success cb undefined')
else console.log(className,'get:',cmd,'success cb undefined')
}
}
}).fail(function() {
_isBusy = false;
console.log('API.get fail',cmd);
console.log(className,'get fail',cmd);
if (fail) fail();
});
}

View File

@ -14,11 +14,15 @@ function ConfigAPI() {
}
function loadAll(success,fail) {
API.get('config/all',success,fail);
API.get('config/all',{},success,fail);
};
function load(key,success,fail) {
API.get('config/?'+key+'=',success,fail)
function load(dataObject,success,fail) {
// console.log(className,'load',dataObject);
API.get('config', dataObject, function(res) {
console.log(className,'load get config cb',dataObject,res);
if (success) success(res);
},fail);
};
function save(newSettings,success,fail) {
@ -31,7 +35,8 @@ function ConfigAPI() {
};
function getSetting(key,success,fail) {
API.get('config/?'+key+'=',function(response) {
console.log(className,'getSetting',key);
API.get('config/?'+key+'=',{},function(response) {
if (success) success(response[key]);
},fail);
}

View File

@ -17,19 +17,19 @@ function PrinterAPI() {
}
this.state = function(success,fail) {
API.get('printer/state',success,fail);
API.get('printer/state',{},success,fail);
};
this.listAll = function(success,fail) {
API.get('printer/listall',success,fail);
API.get('printer/listall',{},success,fail);
};
this.temperature = function(success,fail) {
API.get('printer/temperature',success,fail);
API.get('printer/temperature',{},success,fail);
};
this.progress = function(success,fail) {
API.get('printer/progress',success,fail);
API.get('printer/progress',{},success,fail);
}
function _printPartPost(lines,data,cb) {

View File

@ -9,13 +9,34 @@
function SketchAPI() {
var className = 'SketchAPI';
this.load = function(id,success,fail) {
API.get('sketch/?id='+id,success,fail);
function load(id,success,fail) {
API.get('sketch/?id='+id,{},success,fail);
}
this.save = function(data,success,fail) {
function list(success,fail) {
API.get('sketch/list',{},success,fail);
}
function save(data,success,fail) {
console.log(className,'saving sketch',data);
API.post('sketch',{data:data},success,fail);
}
function del(id,success,fail) {
console.log(className,'deleting sketch',id);
API.post('sketch/delete',{id:id},success,fail);
}
function status(success,fail) {
API.get('sketch/status',{},success,fail);
}
return {
load: load,
list: list,
save: save,
status: status,
del: del,
}
}