Skip to content

Commit

Permalink
removed jquery and installed fetch
Browse files Browse the repository at this point in the history
solves #1
  • Loading branch information
casperlamboo committed Jul 23, 2015
1 parent 67cde0d commit 6af01a0
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 192 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 7 additions & 8 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -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/';

Expand Down Expand Up @@ -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];

Expand All @@ -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'
});
});
*/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 7 additions & 12 deletions src/configapi.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import rest from './restapi.js';
import * as rest from './restapi.js';

export default class {
constructor (localIP) {
this.localIP = localIP;
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);
}
}
109 changes: 47 additions & 62 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
}
}
console.warn(error);

setTimeout(function () {
scope.startUpdateLoop();
}, 1000);

return;
this.alive = true;
if (this.onconnect !== undefined) {
this.onconnect();
}

scope.alive = true;
if (scope.onconnect !== undefined) {
scope.onconnect();
if (!this.loaded) {
this.loaded = true;
}

if (!scope.loaded) {
scope.loaded = true;
this._updateState();

}).catch(() => {

if (this.alive) {
this.alive = false;

if (this.ondisconnect !== undefined) {
this.ondisconnect();
}
}
console.warn(error);

setTimeout(() => {
this.startUpdateLoop();
}, 1000);

scope._updateState();
});

return this;
Expand All @@ -81,93 +80,79 @@ 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({
'start': ((this._currentBatch === 0) ? true : false),
'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();

return;
}
}).then((data) => {

console.log('batch sent: ' + scope._currentBatch, data);
console.log('batch sent: ' + this._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();
});
}
}
14 changes: 7 additions & 7 deletions src/infoapi.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import rest from './restapi.js';
import * as rest from './restapi.js';

export default class {
constructor (localIP) {
this.localIP = localIP;
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');
}
}
44 changes: 24 additions & 20 deletions src/networkapi.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
import rest from './restapi.js';
import * as rest from './restapi.js';

export default class {
constructor (localIP) {
this.localIP = localIP;
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');
}
}
Loading

0 comments on commit 6af01a0

Please sign in to comment.