From 9bf4b90b2fc8240ea2b57ab1bca916dd51d29b50 Mon Sep 17 00:00:00 2001 From: Rick Companje Date: Tue, 20 Jun 2017 21:42:18 +0200 Subject: [PATCH] added API wrappers --- js/api/ConfigAPI.js | 8 +++--- js/api/InfoAPI.js | 20 +++++++++++++ js/api/PrinterAPI.js | 68 ++++++++++++++++++++++++++++++++++++++++++++ js/api/ServerAPI.js | 38 +++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 js/api/ServerAPI.js diff --git a/js/api/ConfigAPI.js b/js/api/ConfigAPI.js index bb78559..e919ca5 100644 --- a/js/api/ConfigAPI.js +++ b/js/api/ConfigAPI.js @@ -13,7 +13,7 @@ function ConfigAPI() { var _wifiboxURL; var _wifiboxCGIBinURL; - var _timeoutTime = 3000; + var _timeoutTime = 3000; var _saveSettingsTimeoutTime = 8000; var _self = this; @@ -33,13 +33,13 @@ function ConfigAPI() { timeout: _timeoutTime, success: function(response){ if(response.status == "error" || response.status == "fail") { - if(failedHandler) failedHandler(response); + if (failedHandler) failedHandler(response); } else { - completeHandler(response.data); + if (completeHandler) completeHandler(response.data); } } }).fail(function() { - if(failedHandler) failedHandler(); + if (failedHandler) failedHandler(); }); }; this.load = function(targetSettings,completeHandler,failedHandler) { diff --git a/js/api/InfoAPI.js b/js/api/InfoAPI.js index 474b764..01627e2 100644 --- a/js/api/InfoAPI.js +++ b/js/api/InfoAPI.js @@ -58,4 +58,24 @@ function InfoAPI() { if(failedHandler) failedHandler(); }); }; + + this.getStatus = function(completeHandler,failedHandler) { + $.ajax({ + url: _wifiboxURL + "/info/status", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if (response.status == "error" || response.status == "fail") { + if (failedHandler) failedHandler(response); + } else { + if (completeHandler) completeHandler(response.data); + } + } + }).fail(function() { + if (failedHandler) failedHandler(); + }); + }; + + } \ No newline at end of file diff --git a/js/api/PrinterAPI.js b/js/api/PrinterAPI.js index cd044ce..71480f9 100644 --- a/js/api/PrinterAPI.js +++ b/js/api/PrinterAPI.js @@ -46,4 +46,72 @@ function PrinterAPI() { if(failedHandler) failedHandler(); }); }; + + this.fetch = function(data, completeHandler,failedHandler) { + console.log("fetch",_wifiboxURL,data); + + $.ajax({ + url: _wifiboxURL + "/printer/fetch", + type: "POST", + dataType: 'json', + data: data, + timeout: _timeoutTime, + success: function(response){ + console.log("printerAPI.fetch response=",response); + if(response.status == "error" || response.status == "fail") { + if (failedHandler) failedHandler(response); + } else { + if (completeHandler) completeHandler(response.data); + } + } + }).fail(function() { + if(failedHandler) failedHandler(); + }); + }; + + this.print = function(data, completeHandler,failedHandler) { + // data = { "gcode": gcode, "first": first, "start": start }, + console.log("print",_wifiboxURL,data); + + $.ajax({ + url: _wifiboxURL + "/printer/print", + type: "POST", + dataType: 'json', + data: data, + timeout: _timeoutTime, + success: function(response){ + console.log("printerAPI.print response=",response); + if(response.status == "error" || response.status == "fail") { + if (failedHandler) failedHandler(response); + } else { + if (completeHandler) completeHandler(response.data); + } + } + }).fail(function() { + if (failedHandler) failedHandler(); + }); + }; + + this.stop = function(data, completeHandler, failedHandler) { + console.log("stop",_wifiboxURL,data); + + $.ajax({ + url: _wifiboxURL + "/printer/stop", + type: "POST", + dataType: 'json', + data: data, + timeout: _timeoutTime, + success: function(response){ + console.log("printerAPI.stop response=",response); + if(response.status == "error" || response.status == "fail") { + if (failedHandler) failedHandler(response); + } else { + if (completeHandler) completeHandler(response.data); + } + } + }).fail(function() { + if (failedHandler) failedHandler(); + }); + }; + } \ No newline at end of file diff --git a/js/api/ServerAPI.js b/js/api/ServerAPI.js new file mode 100644 index 0000000..00ac9dd --- /dev/null +++ b/js/api/ServerAPI.js @@ -0,0 +1,38 @@ +/* + * 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 ServerAPI() { + var _apiURL = ""; + var _timeoutTime = 6000; + + this.init = function(url) { + _apiURL = url; + } + + this.getInfo = function(uuid, completeHandler, failedHandler) { + console.log("ServerAPI:getInfo"); + + $.ajax({ + url: _apiURL + "/info/" + uuid, + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + cache: false, + success: function(response) { + if (completeHandler) completeHandler(response); + } + }).fail(function(response) { + console.log("fail"); + if (failedHandler) failedHandler(); + }); + }; + + // '/fetch/'+uid.value+'/'+line.value; + + +}