From 6af01a0f27d479ce02e3354e1b976b30a74079e1 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 23 Jul 2015 19:12:50 +0200 Subject: [PATCH] removed jquery and installed fetch solves https://github.com/Doodle3D/Doodle3D-API/issues/1 --- config.js | 2 +- example/app.js | 15 +++--- package.json | 2 +- src/configapi.js | 19 +++----- src/index.js | 115 ++++++++++++++++++++-------------------------- src/infoapi.js | 14 +++--- src/networkapi.js | 44 ++++++++++-------- src/printerapi.js | 30 ++++++------ src/restapi.js | 90 +++++++++++++++++++----------------- src/sketchapi.js | 20 ++++---- src/systemapi.js | 6 +-- src/updateapi.js | 18 ++++---- 12 files changed, 181 insertions(+), 194 deletions(-) diff --git a/config.js b/config.js index 85d708c..e32f34d 100644 --- a/config.js +++ b/config.js @@ -18,7 +18,7 @@ System.config({ "babel": "npm:babel-core@5.7.4", "babel-runtime": "npm:babel-runtime@5.7.0", "core-js": "npm:core-js@0.9.18", - "jquery": "github:components/jquery@2.1.4", + "github/fetch": "github:github/fetch@0.9.0", "traceur": "github:jmcriffey/bower-traceur@0.0.90", "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90", "github:jspm/nodelibs-process@0.1.1": { diff --git a/example/app.js b/example/app.js index 129d166..eb130bf 100644 --- a/example/app.js +++ b/example/app.js @@ -1,5 +1,5 @@ import Doodle3DAPI from 'src/index.js'; -import rest from 'src/restapi.js'; +import * as rest from 'src/restapi.js'; var api = 'http://connect.doodle3d.com/api/'; @@ -80,12 +80,7 @@ var addBox = (function () { })(); function searchBoxes () { - rest.get(api + 'list.php', function (error, boxes) { - if (error) { - return; - console.warn(error); - } - + rest.get(api + 'list.php').then((boxes) => { for (var i = 0; i < boxes.length; i ++) { var box = boxes[i]; @@ -96,12 +91,16 @@ function searchBoxes () { setInterval(searchBoxes, 5000); searchBoxes(); +/* addBox({ localip: '127.0.0.1:3000', wifiboxid: 'Node Server' }); + addBox({ localip: '192.168.5.1', wifiboxid: 'Wired Printer' -}); \ No newline at end of file +}); + +*/ \ No newline at end of file diff --git a/package.json b/package.json index 8021b90..2340d62 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lib": "src" }, "dependencies": { - "jquery": "github:components/jquery@^2.1.4" + "github/fetch": "github:github/fetch@^0.9.0" }, "devDependencies": { "babel": "npm:babel-core@^5.1.13", diff --git a/src/configapi.js b/src/configapi.js index 9f60b7c..b72ce03 100644 --- a/src/configapi.js +++ b/src/configapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,22 +6,17 @@ export default class { this.api = 'http://' + localIP + '/d3dapi/'; } - get (keys, callback) { - rest.get(this.api + 'config/?' + keys.join('=&') + '=', callback); + get (keys) { + return rest.get(this.api + 'config/?' + keys.join('=&') + '='); } - getAll (callback) { - rest.get(this.api + 'config/all', callback); + getAll () { + return rest.get(this.api + 'config/all'); } - set (data, callback) { + set (data) { var scope = this; - rest.post(this.api + 'config', data, function (response) { - - if (callback !== undefined) { - callback(response); - } - }); + return rest.post(this.api + 'config', data); } } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1d95343..79232a7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; import ConfigAPI from './configapi.js'; import InfoAPI from './infoapi.js'; import NetworkAPI from './networkapi.js'; @@ -31,36 +31,35 @@ export default class { } startUpdateLoop () { - var scope = this; - this.network.alive(function (error, data) { - if (error) { - if (scope.alive) { - scope.alive = false; + this.network.alive().then(() => { - if (scope.ondisconnect !== undefined) { - scope.ondisconnect(); - } + this.alive = true; + if (this.onconnect !== undefined) { + this.onconnect(); + } + + if (!this.loaded) { + this.loaded = true; + } + + this._updateState(); + + }).catch(() => { + + if (this.alive) { + this.alive = false; + + if (this.ondisconnect !== undefined) { + this.ondisconnect(); } - console.warn(error); - - setTimeout(function () { - scope.startUpdateLoop(); - }, 1000); - - return; } + console.warn(error); + + setTimeout(() => { + this.startUpdateLoop(); + }, 1000); - scope.alive = true; - if (scope.onconnect !== undefined) { - scope.onconnect(); - } - - if (!scope.loaded) { - scope.loaded = true; - } - - scope._updateState(); }); return this; @@ -81,66 +80,54 @@ export default class { } stopPrint (settings) { - var scope = this; this._printBatches = []; this._currentBatch = 0; this.printer.stop({ 'gcode': settings.endCode() - }, function (error, data) { - if (error) { - console.warn(error); - scope.startUpdateLoop(); - - return; - } + }).then((data) => { console.log('Printer stop command sent'); + }).catch((error) => { + + console.warn(error); }); return this; } _updateLoop () { - var scope = this; - if (this._printBatches.length > 0 && (this.state['buffered_lines'] + this._printBatches[0].length) <= this.maxBufferedLines) { //if (this._printBatches.length > 0 ) { this._printBatch(); } else { - setTimeout(function () { - scope._updateState(); + setTimeout(() => { + this._updateState(); }, 1000); } } _updateState () { //que api calls so they don't overload the d3d box - var scope = this; - this.info.status(function (error, data) { - if (error) { - console.warn(error); - scope.startUpdateLoop(); + this.info.status().then((data) => { + this.state = data; - return; + if (this.onupdate !== undefined) { + this.onupdate(data); } - scope.state = data; + this._updateLoop(); + }).catch((error) => { - if (scope.onupdate !== undefined) { - scope.onupdate(data); - } - - scope._updateLoop(); + console.warn(error); + this.startUpdateLoop(); }); } _printBatch () { - var scope = this; - var gcode = this._printBatches.shift(); this.printer.print({ @@ -148,26 +135,24 @@ export default class { 'first': ((this._currentBatch === 0) ? true : false), 'gcode': gcode, 'last': ((this._printBatches.length === 0) ? true : false) //only for debug purposes - }, function (error, data) { - if (error) { - scope._printBatches.unshift(gcode); - - console.warn(error); - scope.startUpdateLoop(); + }).then((data) => { - return; - } + console.log('batch sent: ' + this._currentBatch, data); - console.log('batch sent: ' + scope._currentBatch, data); - - if (scope._printBatches.length > 0) { - scope._currentBatch ++; + if (this._printBatches.length > 0) { + this._currentBatch ++; } else { console.log('Finish sending gcode to printer'); } - scope._updateState(); + this._updateState(); + }).catch((error) => { + + this._printBatches.unshift(gcode); + + console.warn(error); + this.startUpdateLoop(); }); } } \ No newline at end of file diff --git a/src/infoapi.js b/src/infoapi.js index fa28e89..80e7e27 100644 --- a/src/infoapi.js +++ b/src/infoapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,19 +6,19 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - get (callback) { - rest.get(this.api + 'info', callback); + get () { + return rest.get(this.api + 'info'); } - status (callback) { - rest.get(this.api + 'info/status', callback); + status () { + return rest.get(this.api + 'info/status'); } downloadLogFiles () { window.location = this.api + 'info/logfiles'; } - acces (callback) { - rest.get(this.api + 'info/access', callback); + acces () { + return rest.get(this.api + 'info/access'); } } \ No newline at end of file diff --git a/src/networkapi.js b/src/networkapi.js index 5859e31..0342a4a 100644 --- a/src/networkapi.js +++ b/src/networkapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,45 +6,49 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - scan (callback) { - rest.get(this.api + 'network/scan', callback); + scan () { + return rest.get(this.api + 'network/scan'); } - known (callback) { - rest.get(this.api + 'network/known', callback); + known () { + return rest.get(this.api + 'network/known'); } - status (callback) { - rest.get(this.api + 'network/status', callback); + status () { + return rest.get(this.api + 'network/status'); } - assosiate (data, callback) { - rest.post(this.api + 'network/associate', data, callback); + assosiate (ssid, phrase = null, recreate = false) { + + var data = {ssid, recreate}; + if (phrase) phrase = phrase; + + return rest.post(this.api + 'network/associate', data); } - disassosiate (callback) { + disassosiate () { //not tested - rest.post(this.api + 'network/disassociate', {}, callback); + return rest.post(this.api + 'network/disassociate', {}); } - openAccesPoint (callback) { + openAccesPoint () { //not tested - rest.post(this.api + 'network/openap', {}, callback); + return rest.post(this.api + 'network/openap', {}); } - remove (ssid, callback) { - rest.post(this.api + 'network/remove', { + remove (ssid) { + return rest.post(this.api + 'network/remove', { 'ssid': ssid - }, callback); + }); } - signin (callback) { - rest.get(this.api + 'network/signin', callback); + signin () { + return rest.get(this.api + 'network/signin'); } - alive (callback) { - rest.get(this.api + 'network/alive', callback); + alive () { + return rest.get(this.api + 'network/alive'); } } \ No newline at end of file diff --git a/src/printerapi.js b/src/printerapi.js index 2e96ac5..11955cd 100644 --- a/src/printerapi.js +++ b/src/printerapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,31 +6,31 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - temperature (callback) { - rest.get(this.api + 'printer/temperature', callback); + temperature () { + return rest.get(this.api + 'printer/temperature'); } - progress (callback) { - rest.get(this.api + 'printer/progress', callback); + progress () { + return rest.get(this.api + 'printer/progress'); } - state (callback) { - rest.get(this.api + 'printer/state', callback); + state () { + return rest.get(this.api + 'printer/state'); } - listAll (callback) { - rest.get(this.api + 'printer/listall', callback); + listAll () { + return rest.get(this.api + 'printer/listall'); } - heatup (callback) { - rest.post(this.api + 'printer/heatup', {}, callback); + heatup () { + return rest.post(this.api + 'printer/heatup', {}); } - print (data, callback) { - rest.post(this.api + 'printer/print', data, callback); + print (data) { + return rest.post(this.api + 'printer/print', data); } - stop (data, callback) { - rest.post(this.api + 'printer/stop', data, callback); + stop (data) { + return rest.post(this.api + 'printer/stop', data); } } \ No newline at end of file diff --git a/src/restapi.js b/src/restapi.js index 6820b92..250a366 100644 --- a/src/restapi.js +++ b/src/restapi.js @@ -1,46 +1,50 @@ -import $ from 'jquery'; -//in future remove jquery and use framework specificly for ajax calls or write own ajax calls +import 'github/fetch'; -export default { - post (url, data, callback) { - $.ajax({ - url: url, - type: 'POST', - data: data, - dataType: 'json', - timeout: 10000, - success: function (response) { - if (response.status === 'success') { - if (callback !== undefined) { - callback(null, response); - } - } - else { - callback(response.msg); - } - } - }).fail(function () { - callback('Failed connecting to ' + url); - }); - }, +export function get (url) { - get (url, callback) { - $.ajax({ - url: url, - dataType: 'json', - timeout: 5000, - success: function (response) { - if (response.status === 'success') { - if (callback !== undefined) { - callback(null, response.data); - } - } - else { - callback(response.msg); - } + return new Promise((resolve, reject) => { + + fetch(url).then((response) => { + + return response.json(); + + }).then((json) => { + + if (json.status === 'success') { + resolve(json.data); } - }).fail(function () { - callback('Failed connecting to ' + url); - }); - } -}; + else { + reject(json.msg); + } + + }).catch(reject); + }); +} + +export function post (url, data) { + + return new Promise((resolve, reject) => { + + fetch(url, { + method: 'post', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }).then((response) => { + + return response.json(); + + }).then((json) => { + + if (json.status === 'success') { + resolve(json.data); + } + else { + reject(json.msg); + } + + }).catch(reject); + }); +} \ No newline at end of file diff --git a/src/sketchapi.js b/src/sketchapi.js index 49d4c0a..c93a9c2 100644 --- a/src/sketchapi.js +++ b/src/sketchapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,21 +6,21 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - getSketch (id, callback) { - rest.get(this.api + 'sketch/?id=' + id, callback); + getSketch (id) { + return rest.get(this.api + 'sketch/?id=' + id); } - set (data, callback) { - rest.post(this.api + 'sketch', { + set (data) { + return rest.post(this.api + 'sketch', { 'data': data - }, callback); + }); } - status (callback) { - rest.get(this.api + 'sketch/status', callback); + status () { + return rest.get(this.api + 'sketch/status'); } - clear (callback) { - rest.post(this.api + 'sketch/clear', callback); + clear () { + return rest.post(this.api + 'sketch/clear'); } } \ No newline at end of file diff --git a/src/systemapi.js b/src/systemapi.js index 4e65758..d011fba 100644 --- a/src/systemapi.js +++ b/src/systemapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,7 +6,7 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - versions (callback) { - rest.get(this.api + 'system/fwversions', callback); + versions () { + return rest.get(this.api + 'system/fwversions'); } } \ No newline at end of file diff --git a/src/updateapi.js b/src/updateapi.js index d769bf9..7695fa5 100644 --- a/src/updateapi.js +++ b/src/updateapi.js @@ -1,4 +1,4 @@ -import rest from './restapi.js'; +import * as rest from './restapi.js'; export default class { constructor (localIP) { @@ -6,25 +6,25 @@ export default class { this.api = `http://${localIP}/d3dapi/`; } - status (callback) { - rest.get(this.api + 'update/status', callback); + status () { + return rest.get(this.api + 'update/status'); } - download (callback) { + download () { //not tested - rest.post(this.api + 'update/download', {}, callback); + return rest.post(this.api + 'update/download', {}); } - install (callback) { + install () { //not tested - rest.post(this.api + 'update/install', {}, callback); + return rest.post(this.api + 'update/install', {}); } - clear (callback) { + clear () { //not tested - rest.post(this.api + 'update/clear', {}, callback); + return rest.post(this.api + 'update/clear', {}); } } \ No newline at end of file