removed jquery and installed fetch

solves https://github.com/Doodle3D/Doodle3D-API/issues/1
This commit is contained in:
casperlamboo 2015-07-23 19:12:50 +02:00
parent 67cde0d2cb
commit 6af01a0f27
12 changed files with 181 additions and 194 deletions

View File

@ -18,7 +18,7 @@ System.config({
"babel": "npm:babel-core@5.7.4", "babel": "npm:babel-core@5.7.4",
"babel-runtime": "npm:babel-runtime@5.7.0", "babel-runtime": "npm:babel-runtime@5.7.0",
"core-js": "npm:core-js@0.9.18", "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": "github:jmcriffey/bower-traceur@0.0.90",
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90", "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90",
"github:jspm/nodelibs-process@0.1.1": { "github:jspm/nodelibs-process@0.1.1": {

View File

@ -1,5 +1,5 @@
import Doodle3DAPI from 'src/index.js'; 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/'; var api = 'http://connect.doodle3d.com/api/';
@ -80,12 +80,7 @@ var addBox = (function () {
})(); })();
function searchBoxes () { function searchBoxes () {
rest.get(api + 'list.php', function (error, boxes) { rest.get(api + 'list.php').then((boxes) => {
if (error) {
return;
console.warn(error);
}
for (var i = 0; i < boxes.length; i ++) { for (var i = 0; i < boxes.length; i ++) {
var box = boxes[i]; var box = boxes[i];
@ -96,12 +91,16 @@ function searchBoxes () {
setInterval(searchBoxes, 5000); setInterval(searchBoxes, 5000);
searchBoxes(); searchBoxes();
/*
addBox({ addBox({
localip: '127.0.0.1:3000', localip: '127.0.0.1:3000',
wifiboxid: 'Node Server' wifiboxid: 'Node Server'
}); });
addBox({ addBox({
localip: '192.168.5.1', localip: '192.168.5.1',
wifiboxid: 'Wired Printer' wifiboxid: 'Wired Printer'
}); });
*/

View File

@ -5,7 +5,7 @@
"lib": "src" "lib": "src"
}, },
"dependencies": { "dependencies": {
"jquery": "github:components/jquery@^2.1.4" "github/fetch": "github:github/fetch@^0.9.0"
}, },
"devDependencies": { "devDependencies": {
"babel": "npm:babel-core@^5.1.13", "babel": "npm:babel-core@^5.1.13",

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,22 +6,17 @@ export default class {
this.api = 'http://' + localIP + '/d3dapi/'; this.api = 'http://' + localIP + '/d3dapi/';
} }
get (keys, callback) { get (keys) {
rest.get(this.api + 'config/?' + keys.join('=&') + '=', callback); return rest.get(this.api + 'config/?' + keys.join('=&') + '=');
} }
getAll (callback) { getAll () {
rest.get(this.api + 'config/all', callback); return rest.get(this.api + 'config/all');
} }
set (data, callback) { set (data) {
var scope = this; var scope = this;
rest.post(this.api + 'config', data, function (response) { return rest.post(this.api + 'config', data);
if (callback !== undefined) {
callback(response);
}
});
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
import ConfigAPI from './configapi.js'; import ConfigAPI from './configapi.js';
import InfoAPI from './infoapi.js'; import InfoAPI from './infoapi.js';
import NetworkAPI from './networkapi.js'; import NetworkAPI from './networkapi.js';
@ -31,36 +31,35 @@ export default class {
} }
startUpdateLoop () { startUpdateLoop () {
var scope = this;
this.network.alive(function (error, data) { this.network.alive().then(() => {
if (error) {
if (scope.alive) {
scope.alive = false;
if (scope.ondisconnect !== undefined) { this.alive = true;
scope.ondisconnect(); 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; return this;
@ -81,66 +80,54 @@ export default class {
} }
stopPrint (settings) { stopPrint (settings) {
var scope = this;
this._printBatches = []; this._printBatches = [];
this._currentBatch = 0; this._currentBatch = 0;
this.printer.stop({ this.printer.stop({
'gcode': settings.endCode() 'gcode': settings.endCode()
}, function (error, data) { }).then((data) => {
if (error) {
console.warn(error);
scope.startUpdateLoop();
return;
}
console.log('Printer stop command sent'); console.log('Printer stop command sent');
}).catch((error) => {
console.warn(error);
}); });
return this; return this;
} }
_updateLoop () { _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.state['buffered_lines'] + this._printBatches[0].length) <= this.maxBufferedLines) {
//if (this._printBatches.length > 0 ) { //if (this._printBatches.length > 0 ) {
this._printBatch(); this._printBatch();
} }
else { else {
setTimeout(function () { setTimeout(() => {
scope._updateState(); this._updateState();
}, 1000); }, 1000);
} }
} }
_updateState () { _updateState () {
//que api calls so they don't overload the d3d box //que api calls so they don't overload the d3d box
var scope = this;
this.info.status(function (error, data) { this.info.status().then((data) => {
if (error) { this.state = data;
console.warn(error);
scope.startUpdateLoop();
return; if (this.onupdate !== undefined) {
this.onupdate(data);
} }
scope.state = data; this._updateLoop();
}).catch((error) => {
if (scope.onupdate !== undefined) { console.warn(error);
scope.onupdate(data); this.startUpdateLoop();
}
scope._updateLoop();
}); });
} }
_printBatch () { _printBatch () {
var scope = this;
var gcode = this._printBatches.shift(); var gcode = this._printBatches.shift();
this.printer.print({ this.printer.print({
@ -148,26 +135,24 @@ export default class {
'first': ((this._currentBatch === 0) ? true : false), 'first': ((this._currentBatch === 0) ? true : false),
'gcode': gcode, 'gcode': gcode,
'last': ((this._printBatches.length === 0) ? true : false) //only for debug purposes 'last': ((this._printBatches.length === 0) ? true : false) //only for debug purposes
}, function (error, data) { }).then((data) => {
if (error) {
scope._printBatches.unshift(gcode);
console.warn(error);
scope.startUpdateLoop();
return; console.log('batch sent: ' + this._currentBatch, data);
}
console.log('batch sent: ' + scope._currentBatch, data); if (this._printBatches.length > 0) {
this._currentBatch ++;
if (scope._printBatches.length > 0) {
scope._currentBatch ++;
} }
else { else {
console.log('Finish sending gcode to printer'); console.log('Finish sending gcode to printer');
} }
scope._updateState(); this._updateState();
}).catch((error) => {
this._printBatches.unshift(gcode);
console.warn(error);
this.startUpdateLoop();
}); });
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,19 +6,19 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
get (callback) { get () {
rest.get(this.api + 'info', callback); return rest.get(this.api + 'info');
} }
status (callback) { status () {
rest.get(this.api + 'info/status', callback); return rest.get(this.api + 'info/status');
} }
downloadLogFiles () { downloadLogFiles () {
window.location = this.api + 'info/logfiles'; window.location = this.api + 'info/logfiles';
} }
acces (callback) { acces () {
rest.get(this.api + 'info/access', callback); return rest.get(this.api + 'info/access');
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,45 +6,49 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
scan (callback) { scan () {
rest.get(this.api + 'network/scan', callback); return rest.get(this.api + 'network/scan');
} }
known (callback) { known () {
rest.get(this.api + 'network/known', callback); return rest.get(this.api + 'network/known');
} }
status (callback) { status () {
rest.get(this.api + 'network/status', callback); return rest.get(this.api + 'network/status');
} }
assosiate (data, callback) { assosiate (ssid, phrase = null, recreate = false) {
rest.post(this.api + 'network/associate', data, callback);
var data = {ssid, recreate};
if (phrase) phrase = phrase;
return rest.post(this.api + 'network/associate', data);
} }
disassosiate (callback) { disassosiate () {
//not tested //not tested
rest.post(this.api + 'network/disassociate', {}, callback); return rest.post(this.api + 'network/disassociate', {});
} }
openAccesPoint (callback) { openAccesPoint () {
//not tested //not tested
rest.post(this.api + 'network/openap', {}, callback); return rest.post(this.api + 'network/openap', {});
} }
remove (ssid, callback) { remove (ssid) {
rest.post(this.api + 'network/remove', { return rest.post(this.api + 'network/remove', {
'ssid': ssid 'ssid': ssid
}, callback); });
} }
signin (callback) { signin () {
rest.get(this.api + 'network/signin', callback); return rest.get(this.api + 'network/signin');
} }
alive (callback) { alive () {
rest.get(this.api + 'network/alive', callback); return rest.get(this.api + 'network/alive');
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,31 +6,31 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
temperature (callback) { temperature () {
rest.get(this.api + 'printer/temperature', callback); return rest.get(this.api + 'printer/temperature');
} }
progress (callback) { progress () {
rest.get(this.api + 'printer/progress', callback); return rest.get(this.api + 'printer/progress');
} }
state (callback) { state () {
rest.get(this.api + 'printer/state', callback); return rest.get(this.api + 'printer/state');
} }
listAll (callback) { listAll () {
rest.get(this.api + 'printer/listall', callback); return rest.get(this.api + 'printer/listall');
} }
heatup (callback) { heatup () {
rest.post(this.api + 'printer/heatup', {}, callback); return rest.post(this.api + 'printer/heatup', {});
} }
print (data, callback) { print (data) {
rest.post(this.api + 'printer/print', data, callback); return rest.post(this.api + 'printer/print', data);
} }
stop (data, callback) { stop (data) {
rest.post(this.api + 'printer/stop', data, callback); return rest.post(this.api + 'printer/stop', data);
} }
} }

View File

@ -1,46 +1,50 @@
import $ from 'jquery'; import 'github/fetch';
//in future remove jquery and use framework specificly for ajax calls or write own ajax calls
export default { export function get (url) {
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);
});
},
get (url, callback) { return new Promise((resolve, reject) => {
$.ajax({
url: url, fetch(url).then((response) => {
dataType: 'json',
timeout: 5000, return response.json();
success: function (response) {
if (response.status === 'success') { }).then((json) => {
if (callback !== undefined) {
callback(null, response.data); if (json.status === 'success') {
} resolve(json.data);
}
else {
callback(response.msg);
}
} }
}).fail(function () { else {
callback('Failed connecting to ' + url); 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);
});
}

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,21 +6,21 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
getSketch (id, callback) { getSketch (id) {
rest.get(this.api + 'sketch/?id=' + id, callback); return rest.get(this.api + 'sketch/?id=' + id);
} }
set (data, callback) { set (data) {
rest.post(this.api + 'sketch', { return rest.post(this.api + 'sketch', {
'data': data 'data': data
}, callback); });
} }
status (callback) { status () {
rest.get(this.api + 'sketch/status', callback); return rest.get(this.api + 'sketch/status');
} }
clear (callback) { clear () {
rest.post(this.api + 'sketch/clear', callback); return rest.post(this.api + 'sketch/clear');
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,7 +6,7 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
versions (callback) { versions () {
rest.get(this.api + 'system/fwversions', callback); return rest.get(this.api + 'system/fwversions');
} }
} }

View File

@ -1,4 +1,4 @@
import rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (localIP) {
@ -6,25 +6,25 @@ export default class {
this.api = `http://${localIP}/d3dapi/`; this.api = `http://${localIP}/d3dapi/`;
} }
status (callback) { status () {
rest.get(this.api + 'update/status', callback); return rest.get(this.api + 'update/status');
} }
download (callback) { download () {
//not tested //not tested
rest.post(this.api + 'update/download', {}, callback); return rest.post(this.api + 'update/download', {});
} }
install (callback) { install () {
//not tested //not tested
rest.post(this.api + 'update/install', {}, callback); return rest.post(this.api + 'update/install', {});
} }
clear (callback) { clear () {
//not tested //not tested
rest.post(this.api + 'update/clear', {}, callback); return rest.post(this.api + 'update/clear', {});
} }
} }