0
0
mirror of https://github.com/Doodle3D/Doodle3D-API synced 2024-11-16 00:37:55 +01:00

added DoodleBox Manager

Where plausible used parameter as arguments
(https://github.com/Doodle3D/Doodle3D-API/issues/2)

added doodle box manager for a cleaner structure
(https://github.com/Doodle3D/Doodle3D-API/issues/9)
This commit is contained in:
casperlamboo 2015-07-24 13:15:00 +02:00
parent 6af01a0f27
commit f3cadff7d5
14 changed files with 756067 additions and 181 deletions

View File

@ -2,18 +2,26 @@
Doodle3D API for communication with the Doodle3D WiFi-Box Doodle3D API for communication with the Doodle3D WiFi-Box
```javascript ```javascript
import Doodle3DAPI from 'doodle3dapi'; import Doodle3DManager from 'src/doodle3dmanager.js';
var localIP = "192.168.5.1"; var doodle3DManager = new Doodle3DManager();
var doodle3DAPI = new Doodle3DAPI(localIP);
doodle3DAPI.onconnect = function () {
}; doodle3DManager.addEventListener('boxappeared', (event) => {
doodle3DAPI.ondisconnect = function () { var box = event.box;
}; box.addEventListener('connect', (event) => {
doodle3DAPI.onupdate = function (data) { });
box.addEventListener('disconnect', (event) => {
});
box.addEventListener('update', (event) => {
var status = event.state;
});
box.setAutoUpdate(true);
});
doodle3DManager.setAutoUpdate(true);
};
doodle3DAPI.startUpdateLoop();
``` ```

View File

@ -17,6 +17,7 @@ System.config({
"map": { "map": {
"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",
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"core-js": "npm:core-js@0.9.18", "core-js": "npm:core-js@0.9.18",
"github/fetch": "github:github/fetch@0.9.0", "github/fetch": "github:github/fetch@0.9.0",
"traceur": "github:jmcriffey/bower-traceur@0.0.90", "traceur": "github:jmcriffey/bower-traceur@0.0.90",

View File

@ -1,106 +1,79 @@
import Doodle3DAPI from 'src/index.js'; import Doodle3DManager from 'src/doodle3dmanager.js';
import * as rest from 'src/restapi.js';
var api = 'http://connect.doodle3d.com/api/'; var doodle3DManager = new Doodle3DManager();
var addBox = (function () { doodle3DManager.addEventListener('boxappeared', (event) => {
var known = []; var box = event.box;
return function (boxData) { var row = document.createElement('tr');
if (known.indexOf(boxData.localip) === -1) { row.style.color = 'gray';
known.push(boxData.localip);
var row = document.createElement('tr'); var id = document.createElement('td');
row.style.color = 'gray'; var state = document.createElement('td');
var localIP = document.createElement('td');
var bed = document.createElement('td');
var bedTarget = document.createElement('td');
var bufferedLines = document.createElement('td');
var currentLine = document.createElement('td');
var hasControl = document.createElement('td');
var hotend = document.createElement('td');
var hotendTarget = document.createElement('td');
var totalLines = document.createElement('td');
var id = document.createElement('td'); row.appendChild(id);
var state = document.createElement('td'); row.appendChild(localIP);
var localIP = document.createElement('td'); row.appendChild(state);
var bed = document.createElement('td'); row.appendChild(currentLine);
var bedTarget = document.createElement('td'); row.appendChild(bufferedLines);
var bufferedLines = document.createElement('td'); row.appendChild(totalLines);
var currentLine = document.createElement('td'); row.appendChild(hotend);
var hasControl = document.createElement('td'); row.appendChild(hotendTarget);
var hotend = document.createElement('td'); row.appendChild(bed);
var hotendTarget = document.createElement('td'); row.appendChild(bedTarget);
var totalLines = document.createElement('td'); row.appendChild(hasControl);
row.appendChild(id); id.innerHTML = box.boxData.wifiboxid;
row.appendChild(localIP); localIP.innerHTML = box.boxData.localip;
row.appendChild(state);
row.appendChild(currentLine);
row.appendChild(bufferedLines);
row.appendChild(totalLines);
row.appendChild(hotend);
row.appendChild(hotendTarget);
row.appendChild(bed);
row.appendChild(bedTarget);
row.appendChild(hasControl);
id.innerHTML = boxData.wifiboxid; document.getElementById('table').appendChild(row);
localIP.innerHTML = boxData.localip;
document.getElementById('table').appendChild(row); box.addEventListener('connect', (event) => {
row.style.color = 'black';
});
var doodle3DAPI = new Doodle3DAPI(boxData.localip); box.addEventListener('disconnect', (event) => {
doodle3DAPI.onconnect = function () { row.style.color = 'gray';
row.style.color = 'black'; });
};
doodle3DAPI.ondisconnect = function () { box.addEventListener('update', (event) => {
row.style.color = 'gray'; var status = event.state;
};
doodle3DAPI.onupdate = function (data) { state.innerHTML = status.state;
state.innerHTML = data.state; if (status.state !== 'disconnected' && status.state !== 'connecting' && status.state !== 'unknown') {
if (data.state !== 'disconnected' && data.state !== 'connecting' && data.state !== 'unknown') { bed.innerHTML = status.bed;
bed.innerHTML = data.bed; bedTarget.innerHTML = status.bed_target;
bedTarget.innerHTML = data.bed_target; bufferedLines.innerHTML = status.buffered_lines;
bufferedLines.innerHTML = data.buffered_lines; currentLine.innerHTML = status.current_line;
currentLine.innerHTML = data.current_line; hasControl.innerHTML = status.has_control;
hasControl.innerHTML = data.has_control; hotend.innerHTML = status.hotend;
hotend.innerHTML = data.hotend; hotendTarget.innerHTML = status.hotend_target;
hotendTarget.innerHTML = data.hotend_target; state.innerHTML = status.state;
state.innerHTML = data.state; totalLines.innerHTML = status.total_lines;
totalLines.innerHTML = data.total_lines;
}
else {
bed.innerHTML = '';
bedTarget.innerHTML = '';
bufferedLines.innerHTML = '';
currentLine.innerHTML = '';
hasControl.innerHTML = '';
hotend.innerHTML = '';
hotendTarget.innerHTML = '';
state.innerHTML = '';
totalLines.innerHTML = '';
}
};
doodle3DAPI.startUpdateLoop();
} }
} else {
})(); bed.innerHTML = '';
bedTarget.innerHTML = '';
function searchBoxes () { bufferedLines.innerHTML = '';
rest.get(api + 'list.php').then((boxes) => { currentLine.innerHTML = '';
for (var i = 0; i < boxes.length; i ++) { hasControl.innerHTML = '';
var box = boxes[i]; hotend.innerHTML = '';
hotendTarget.innerHTML = '';
addBox(box); state.innerHTML = '';
totalLines.innerHTML = '';
} }
}); });
}
setInterval(searchBoxes, 5000);
searchBoxes();
/* box.setAutoUpdate(true);
addBox({
localip: '127.0.0.1:3000',
wifiboxid: 'Node Server'
}); });
doodle3DManager.setAutoUpdate(true);
addBox({
localip: '192.168.5.1',
wifiboxid: 'Wired Printer'
});
*/

755849
example/test.gcode Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
"lib": "src" "lib": "src"
}, },
"dependencies": { "dependencies": {
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"github/fetch": "github:github/fetch@^0.9.0" "github/fetch": "github:github/fetch@^0.9.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = 'http://' + localIP + '/d3dapi/';
} }
get (keys) { get (keys) {

View File

@ -1,3 +1,4 @@
import EventDispatcher from 'casperlamboo/EventDispatcher';
import * as 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';
@ -7,20 +8,26 @@ import SketchAPI from './sketchapi.js';
import SystemAPI from './systemapi.js'; import SystemAPI from './systemapi.js';
import UpdateAPI from './updateapi.js'; import UpdateAPI from './updateapi.js';
export default class { export default class extends EventDispatcher {
constructor (localIP) { constructor (boxData) {
this.config = new ConfigAPI(localIP); super();
this.info = new InfoAPI(localIP);
this.network = new NetworkAPI(localIP); this.boxData = boxData;
this.printer = new PrinterAPI(localIP);
this.sketch = new SketchAPI(localIP); this.api = `http://${boxData.localip}/d3dapi/`;
this.system = new SystemAPI(localIP);
this.update = new UpdateAPI(localIP); this.config = new ConfigAPI(this.api);
this.info = new InfoAPI(this.api);
this.network = new NetworkAPI(this.api);
this.printer = new PrinterAPI(this.api);
this.sketch = new SketchAPI(this.api);
this.system = new SystemAPI(this.api);
this.update = new UpdateAPI(this.api);
this.alive = false; this.alive = false;
this.batchSize = 512; this.maxBatchSize = 10*1024;
this.maxBufferedLines = 4096; this.maxBufferedLines = 1024*1024;
this.state = {}; this.state = {};
@ -30,14 +37,14 @@ export default class {
this.loaded = false; this.loaded = false;
} }
startUpdateLoop () { setAutoUpdate (autoUpdate) {
this.network.alive().then(() => { this.network.alive().then(() => {
this.alive = true; this.alive = true;
if (this.onconnect !== undefined) {
this.onconnect(); this.dispatchEvent({
} type: 'connect'
});
if (!this.loaded) { if (!this.loaded) {
this.loaded = true; this.loaded = true;
@ -45,19 +52,17 @@ export default class {
this._updateState(); this._updateState();
}).catch(() => { }).catch((error) => {
if (this.alive) { if (this.alive) {
this.alive = false; this.alive = false;
if (this.ondisconnect !== undefined) { this.dispatchEvent({
this.ondisconnect(); type: 'disconnect'
} });
} }
console.warn(error);
setTimeout(() => { setTimeout(() => {
this.startUpdateLoop(); this.setAutoUpdate();
}, 1000); }, 1000);
}); });
@ -68,30 +73,27 @@ export default class {
print (gcode) { print (gcode) {
this._currentBatch = 0; this._currentBatch = 0;
gcode = gcode.split('\n'); var lastIndex = 0;
while (lastIndex !== (gcode.length - 1)) {
var index = gcode.lastIndexOf('\n', lastIndex + maxBatchSize);
var batch = gcode.substring(lastIndex, index);
lastIndex = index;
//split gcode in batches this.printBatches.push(batch);
while (gcode.length > 0) {
var gcodeBatch = gcode.splice(0, Math.min(this.batchSize, gcode.length));
this._printBatches.push(gcodeBatch.join('\n'));
} }
return this; return this;
} }
stopPrint (settings) { stopPrint (endCode = '') {
this._printBatches = []; this._printBatches = [];
this._currentBatch = 0; this._currentBatch = 0;
this.printer.stop({ this.printer.stop(endCode).then((data) => {
'gcode': settings.endCode()
}).then((data) => {
console.log('Printer stop command sent'); console.log('Printer stop command sent');
}).catch((error) => {
console.warn(error);
}); });
return this; return this;
@ -112,31 +114,31 @@ export default class {
_updateState () { _updateState () {
//que api calls so they don't overload the d3d box //que api calls so they don't overload the d3d box
this.info.status().then((data) => { this.info.status().then((state) => {
this.state = data;
if (this.onupdate !== undefined) { this.state = state;
this.onupdate(data);
} this.dispatchEvent({
type: 'update',
state
});
this._updateLoop(); this._updateLoop();
}).catch((error) => { }).catch((error) => {
console.warn(error); console.warn(error);
this.startUpdateLoop(); this.setAutoUpdate();
}); });
} }
_printBatch () { _printBatch () {
var gcode = this._printBatches.shift(); var gcode = this._printBatches.shift();
var start = (this._currentBatch === 0) ? true : false;
var first = (this._currentBatch === 0) ? true : false;
var last = (this._printBatches.length === 0) ? true : false; //only for the node js server
this.printer.print({ this.printer.print(gcode, start, first, last).then((data) => {
'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
}).then((data) => {
console.log('batch sent: ' + this._currentBatch, data); console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) { if (this._printBatches.length > 0) {
@ -148,11 +150,11 @@ export default class {
this._updateState(); this._updateState();
}).catch((error) => { }).catch((error) => {
console.warn(error);
this._printBatches.unshift(gcode); this._printBatches.unshift(gcode);
console.warn(error); this.setAutoUpdate();
this.startUpdateLoop();
}); });
} }
} }

57
src/doodle3dmanager.js Normal file
View File

@ -0,0 +1,57 @@
import * as rest from './restapi.js';
import Doodle3DAPI from './doodle3dapi.js';
import EventDispatcher from 'casperlamboo/EventDispatcher';
export default class extends EventDispatcher {
constructor () {
super();
this.boxes = [];
this.api = 'http://connect.doodle3d.com/api/';
}
setAutoUpdate (autoUpdate = true, rate = 5000) {
if (autoUpdate) {
this._checkNew();
this.interval = setInterval(() => {
this._checkNew();
}, rate);
}
else if (this.interval !== undefined) {
clearInterval(this.interval);
delete this.interval;
}
return this;
}
addBox (boxData) {
var box = new Doodle3DAPI(boxData);
this.boxes.push(box);
this.dispatchEvent({
type: 'boxappeared',
box: box
});
}
_checkNew () {
rest.get(this.api + 'list.php').then((boxes) => {
var knownIPs = [];
for (var i = 0; i < this.boxes.length; i ++) {
var boxData = this.boxes[i].boxData;
knownIPs.push(boxData.localip);
}
for (var i = 0; i < boxes.length; i ++) {
var boxData = boxes[i];
if (knownIPs.indexOf(boxData.localip) === -1) {
this.addBox(boxData);
}
}
});
}
}

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = `http://${localIP}/d3dapi/`;
} }
get () { get () {

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = `http://${localIP}/d3dapi/`;
} }
scan () { scan () {
@ -19,9 +18,8 @@ export default class {
} }
assosiate (ssid, phrase = null, recreate = false) { assosiate (ssid, phrase = null, recreate = false) {
var data = {ssid, recreate}; var data = {ssid, recreate};
if (phrase) phrase = phrase; if (phrase) data.phrase = phrase;
return rest.post(this.api + 'network/associate', data); return rest.post(this.api + 'network/associate', data);
} }

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = `http://${localIP}/d3dapi/`;
} }
temperature () { temperature () {
@ -26,11 +25,14 @@ export default class {
return rest.post(this.api + 'printer/heatup', {}); return rest.post(this.api + 'printer/heatup', {});
} }
print (data) { print (gcode = '', first = false, start = false, last = false) {
var data = {gcode, first, start}
if (last) data.last = last;
return rest.post(this.api + 'printer/print', data); return rest.post(this.api + 'printer/print', data);
} }
stop (data) { stop (gcode = '') {
return rest.post(this.api + 'printer/stop', data); return rest.post(this.api + 'printer/stop', {gcode});
} }
} }

View File

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

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = `http://${localIP}/d3dapi/`;
} }
versions () { versions () {

View File

@ -1,9 +1,8 @@
import * as rest from './restapi.js'; import * as rest from './restapi.js';
export default class { export default class {
constructor (localIP) { constructor (api) {
this.localIP = localIP; this.api = api;
this.api = `http://${localIP}/d3dapi/`;
} }
status () { status () {