mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2024-11-05 04:33:24 +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:
parent
6af01a0f27
commit
f3cadff7d5
28
README.md
28
README.md
@ -2,18 +2,26 @@
|
||||
Doodle3D API for communication with the Doodle3D WiFi-Box
|
||||
|
||||
```javascript
|
||||
import Doodle3DAPI from 'doodle3dapi';
|
||||
import Doodle3DManager from 'src/doodle3dmanager.js';
|
||||
|
||||
var localIP = "192.168.5.1";
|
||||
var doodle3DAPI = new Doodle3DAPI(localIP);
|
||||
doodle3DAPI.onconnect = function () {
|
||||
var doodle3DManager = new Doodle3DManager();
|
||||
|
||||
};
|
||||
doodle3DAPI.ondisconnect = function () {
|
||||
doodle3DManager.addEventListener('boxappeared', (event) => {
|
||||
var box = event.box;
|
||||
|
||||
};
|
||||
doodle3DAPI.onupdate = function (data) {
|
||||
box.addEventListener('connect', (event) => {
|
||||
});
|
||||
|
||||
box.addEventListener('disconnect', (event) => {
|
||||
});
|
||||
|
||||
box.addEventListener('update', (event) => {
|
||||
var status = event.state;
|
||||
});
|
||||
|
||||
box.setAutoUpdate(true);
|
||||
});
|
||||
|
||||
doodle3DManager.setAutoUpdate(true);
|
||||
|
||||
};
|
||||
doodle3DAPI.startUpdateLoop();
|
||||
```
|
||||
|
@ -17,6 +17,7 @@ System.config({
|
||||
"map": {
|
||||
"babel": "npm:babel-core@5.7.4",
|
||||
"babel-runtime": "npm:babel-runtime@5.7.0",
|
||||
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
|
||||
"core-js": "npm:core-js@0.9.18",
|
||||
"github/fetch": "github:github/fetch@0.9.0",
|
||||
"traceur": "github:jmcriffey/bower-traceur@0.0.90",
|
||||
|
155
example/app.js
155
example/app.js
@ -1,106 +1,79 @@
|
||||
import Doodle3DAPI from 'src/index.js';
|
||||
import * as rest from 'src/restapi.js';
|
||||
import Doodle3DManager from 'src/doodle3dmanager.js';
|
||||
|
||||
var api = 'http://connect.doodle3d.com/api/';
|
||||
var doodle3DManager = new Doodle3DManager();
|
||||
|
||||
var addBox = (function () {
|
||||
var known = [];
|
||||
doodle3DManager.addEventListener('boxappeared', (event) => {
|
||||
var box = event.box;
|
||||
|
||||
return function (boxData) {
|
||||
if (known.indexOf(boxData.localip) === -1) {
|
||||
known.push(boxData.localip);
|
||||
var row = document.createElement('tr');
|
||||
row.style.color = 'gray';
|
||||
|
||||
var row = document.createElement('tr');
|
||||
row.style.color = 'gray';
|
||||
var id = document.createElement('td');
|
||||
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');
|
||||
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');
|
||||
row.appendChild(id);
|
||||
row.appendChild(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);
|
||||
|
||||
row.appendChild(id);
|
||||
row.appendChild(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 = box.boxData.wifiboxid;
|
||||
localIP.innerHTML = box.boxData.localip;
|
||||
|
||||
id.innerHTML = boxData.wifiboxid;
|
||||
localIP.innerHTML = boxData.localip;
|
||||
document.getElementById('table').appendChild(row);
|
||||
|
||||
document.getElementById('table').appendChild(row);
|
||||
box.addEventListener('connect', (event) => {
|
||||
row.style.color = 'black';
|
||||
});
|
||||
|
||||
var doodle3DAPI = new Doodle3DAPI(boxData.localip);
|
||||
doodle3DAPI.onconnect = function () {
|
||||
row.style.color = 'black';
|
||||
};
|
||||
doodle3DAPI.ondisconnect = function () {
|
||||
row.style.color = 'gray';
|
||||
};
|
||||
doodle3DAPI.onupdate = function (data) {
|
||||
state.innerHTML = data.state;
|
||||
if (data.state !== 'disconnected' && data.state !== 'connecting' && data.state !== 'unknown') {
|
||||
bed.innerHTML = data.bed;
|
||||
bedTarget.innerHTML = data.bed_target;
|
||||
bufferedLines.innerHTML = data.buffered_lines;
|
||||
currentLine.innerHTML = data.current_line;
|
||||
hasControl.innerHTML = data.has_control;
|
||||
hotend.innerHTML = data.hotend;
|
||||
hotendTarget.innerHTML = data.hotend_target;
|
||||
state.innerHTML = data.state;
|
||||
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();
|
||||
box.addEventListener('disconnect', (event) => {
|
||||
row.style.color = 'gray';
|
||||
});
|
||||
|
||||
box.addEventListener('update', (event) => {
|
||||
var status = event.state;
|
||||
|
||||
state.innerHTML = status.state;
|
||||
if (status.state !== 'disconnected' && status.state !== 'connecting' && status.state !== 'unknown') {
|
||||
bed.innerHTML = status.bed;
|
||||
bedTarget.innerHTML = status.bed_target;
|
||||
bufferedLines.innerHTML = status.buffered_lines;
|
||||
currentLine.innerHTML = status.current_line;
|
||||
hasControl.innerHTML = status.has_control;
|
||||
hotend.innerHTML = status.hotend;
|
||||
hotendTarget.innerHTML = status.hotend_target;
|
||||
state.innerHTML = status.state;
|
||||
totalLines.innerHTML = status.total_lines;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
function searchBoxes () {
|
||||
rest.get(api + 'list.php').then((boxes) => {
|
||||
for (var i = 0; i < boxes.length; i ++) {
|
||||
var box = boxes[i];
|
||||
|
||||
addBox(box);
|
||||
else {
|
||||
bed.innerHTML = '';
|
||||
bedTarget.innerHTML = '';
|
||||
bufferedLines.innerHTML = '';
|
||||
currentLine.innerHTML = '';
|
||||
hasControl.innerHTML = '';
|
||||
hotend.innerHTML = '';
|
||||
hotendTarget.innerHTML = '';
|
||||
state.innerHTML = '';
|
||||
totalLines.innerHTML = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
setInterval(searchBoxes, 5000);
|
||||
searchBoxes();
|
||||
|
||||
/*
|
||||
addBox({
|
||||
localip: '127.0.0.1:3000',
|
||||
wifiboxid: 'Node Server'
|
||||
box.setAutoUpdate(true);
|
||||
});
|
||||
|
||||
|
||||
addBox({
|
||||
localip: '192.168.5.1',
|
||||
wifiboxid: 'Wired Printer'
|
||||
});
|
||||
|
||||
*/
|
||||
doodle3DManager.setAutoUpdate(true);
|
||||
|
755849
example/test.gcode
Normal file
755849
example/test.gcode
Normal file
File diff suppressed because it is too large
Load Diff
@ -5,6 +5,7 @@
|
||||
"lib": "src"
|
||||
},
|
||||
"dependencies": {
|
||||
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
|
||||
"github/fetch": "github:github/fetch@^0.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = 'http://' + localIP + '/d3dapi/';
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
get (keys) {
|
||||
@ -19,4 +18,4 @@ export default class {
|
||||
|
||||
return rest.post(this.api + 'config', data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import EventDispatcher from 'casperlamboo/EventDispatcher';
|
||||
import * as rest from './restapi.js';
|
||||
import ConfigAPI from './configapi.js';
|
||||
import InfoAPI from './infoapi.js';
|
||||
@ -7,20 +8,26 @@ import SketchAPI from './sketchapi.js';
|
||||
import SystemAPI from './systemapi.js';
|
||||
import UpdateAPI from './updateapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.config = new ConfigAPI(localIP);
|
||||
this.info = new InfoAPI(localIP);
|
||||
this.network = new NetworkAPI(localIP);
|
||||
this.printer = new PrinterAPI(localIP);
|
||||
this.sketch = new SketchAPI(localIP);
|
||||
this.system = new SystemAPI(localIP);
|
||||
this.update = new UpdateAPI(localIP);
|
||||
export default class extends EventDispatcher {
|
||||
constructor (boxData) {
|
||||
super();
|
||||
|
||||
this.boxData = boxData;
|
||||
|
||||
this.api = `http://${boxData.localip}/d3dapi/`;
|
||||
|
||||
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.batchSize = 512;
|
||||
this.maxBufferedLines = 4096;
|
||||
this.maxBatchSize = 10*1024;
|
||||
this.maxBufferedLines = 1024*1024;
|
||||
|
||||
this.state = {};
|
||||
|
||||
@ -30,14 +37,14 @@ export default class {
|
||||
this.loaded = false;
|
||||
}
|
||||
|
||||
startUpdateLoop () {
|
||||
|
||||
setAutoUpdate (autoUpdate) {
|
||||
this.network.alive().then(() => {
|
||||
|
||||
this.alive = true;
|
||||
if (this.onconnect !== undefined) {
|
||||
this.onconnect();
|
||||
}
|
||||
|
||||
this.dispatchEvent({
|
||||
type: 'connect'
|
||||
});
|
||||
|
||||
if (!this.loaded) {
|
||||
this.loaded = true;
|
||||
@ -45,19 +52,17 @@ export default class {
|
||||
|
||||
this._updateState();
|
||||
|
||||
}).catch(() => {
|
||||
|
||||
}).catch((error) => {
|
||||
if (this.alive) {
|
||||
this.alive = false;
|
||||
|
||||
if (this.ondisconnect !== undefined) {
|
||||
this.ondisconnect();
|
||||
}
|
||||
this.dispatchEvent({
|
||||
type: 'disconnect'
|
||||
});
|
||||
}
|
||||
console.warn(error);
|
||||
|
||||
setTimeout(() => {
|
||||
this.startUpdateLoop();
|
||||
this.setAutoUpdate();
|
||||
}, 1000);
|
||||
|
||||
});
|
||||
@ -68,30 +73,27 @@ export default class {
|
||||
print (gcode) {
|
||||
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
|
||||
while (gcode.length > 0) {
|
||||
var gcodeBatch = gcode.splice(0, Math.min(this.batchSize, gcode.length));
|
||||
this._printBatches.push(gcodeBatch.join('\n'));
|
||||
this.printBatches.push(batch);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
stopPrint (settings) {
|
||||
stopPrint (endCode = '') {
|
||||
|
||||
this._printBatches = [];
|
||||
this._currentBatch = 0;
|
||||
|
||||
this.printer.stop({
|
||||
'gcode': settings.endCode()
|
||||
}).then((data) => {
|
||||
|
||||
this.printer.stop(endCode).then((data) => {
|
||||
|
||||
console.log('Printer stop command sent');
|
||||
}).catch((error) => {
|
||||
|
||||
console.warn(error);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
@ -112,31 +114,31 @@ export default class {
|
||||
_updateState () {
|
||||
//que api calls so they don't overload the d3d box
|
||||
|
||||
this.info.status().then((data) => {
|
||||
this.state = data;
|
||||
this.info.status().then((state) => {
|
||||
|
||||
if (this.onupdate !== undefined) {
|
||||
this.onupdate(data);
|
||||
}
|
||||
this.state = state;
|
||||
|
||||
this.dispatchEvent({
|
||||
type: 'update',
|
||||
state
|
||||
});
|
||||
|
||||
this._updateLoop();
|
||||
}).catch((error) => {
|
||||
|
||||
console.warn(error);
|
||||
this.startUpdateLoop();
|
||||
this.setAutoUpdate();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
_printBatch () {
|
||||
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({
|
||||
'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) => {
|
||||
|
||||
this.printer.print(gcode, start, first, last).then((data) => {
|
||||
console.log('batch sent: ' + this._currentBatch, data);
|
||||
|
||||
if (this._printBatches.length > 0) {
|
||||
@ -148,11 +150,11 @@ export default class {
|
||||
|
||||
this._updateState();
|
||||
}).catch((error) => {
|
||||
console.warn(error);
|
||||
|
||||
this._printBatches.unshift(gcode);
|
||||
|
||||
console.warn(error);
|
||||
this.startUpdateLoop();
|
||||
this.setAutoUpdate();
|
||||
});
|
||||
}
|
||||
}
|
57
src/doodle3dmanager.js
Normal file
57
src/doodle3dmanager.js
Normal 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
get () {
|
||||
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
scan () {
|
||||
@ -19,9 +18,8 @@ export default class {
|
||||
}
|
||||
|
||||
assosiate (ssid, phrase = null, recreate = false) {
|
||||
|
||||
var data = {ssid, recreate};
|
||||
if (phrase) phrase = phrase;
|
||||
if (phrase) data.phrase = phrase;
|
||||
|
||||
return rest.post(this.api + 'network/associate', data);
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
temperature () {
|
||||
@ -26,11 +25,14 @@ export default class {
|
||||
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);
|
||||
}
|
||||
|
||||
stop (data) {
|
||||
return rest.post(this.api + 'printer/stop', data);
|
||||
stop (gcode = '') {
|
||||
return rest.post(this.api + 'printer/stop', {gcode});
|
||||
}
|
||||
}
|
@ -1,18 +1,17 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
getSketch (id) {
|
||||
return rest.get(this.api + 'sketch/?id=' + id);
|
||||
}
|
||||
|
||||
set (data) {
|
||||
set (data = '') {
|
||||
return rest.post(this.api + 'sketch', {
|
||||
'data': data
|
||||
data
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
versions () {
|
||||
|
@ -1,9 +1,8 @@
|
||||
import * as rest from './restapi.js';
|
||||
|
||||
export default class {
|
||||
constructor (localIP) {
|
||||
this.localIP = localIP;
|
||||
this.api = `http://${localIP}/d3dapi/`;
|
||||
constructor (api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
status () {
|
||||
|
Loading…
Reference in New Issue
Block a user