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-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": {

View File

@ -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'
});
});
*/

View File

@ -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",

View File

@ -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);
}
}

View File

@ -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();
});
}
}

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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);
}
}

View File

@ -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);
});
}

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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', {});
}
}