Doodle3D-API/src/doodle3dapi.js

160 lines
3.2 KiB
JavaScript
Raw Normal View History

import EventDispatcher from 'casperlamboo/EventDispatcher';
import * as rest from './restapi.js';
2015-07-23 16:12:11 +02:00
import ConfigAPI from './configapi.js';
import InfoAPI from './infoapi.js';
import NetworkAPI from './networkapi.js';
import PrinterAPI from './printerapi.js';
import SketchAPI from './sketchapi.js';
import SystemAPI from './systemapi.js';
import UpdateAPI from './updateapi.js';
2015-07-15 15:06:18 +02:00
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);
2015-07-15 15:06:18 +02:00
this.alive = false;
this.maxBatchSize = 10*1024;
this.maxBufferedLines = 1024*1024;
2015-07-15 15:06:18 +02:00
this.state = {};
this._printBatches = [];
this._currentBatch = 0;
this.loaded = false;
}
setAutoUpdate (autoUpdate) {
this.network.alive().then(() => {
2015-07-15 15:06:18 +02:00
this.alive = true;
this.dispatchEvent({
type: 'connect'
});
2015-07-15 15:06:18 +02:00
if (!this.loaded) {
this.loaded = true;
2015-07-15 15:06:18 +02:00
}
this._updateState();
}).catch((error) => {
if (this.alive) {
this.alive = false;
this.dispatchEvent({
type: 'disconnect'
});
2015-07-15 15:06:18 +02:00
}
setTimeout(() => {
this.setAutoUpdate();
}, 1000);
2015-07-15 15:06:18 +02:00
});
return this;
}
print (gcode) {
this._currentBatch = 0;
var lastIndex = 0;
while (lastIndex !== (gcode.length - 1)) {
var index = gcode.lastIndexOf('\n', lastIndex + maxBatchSize);
var batch = gcode.substring(lastIndex, index);
lastIndex = index;
2015-07-15 15:06:18 +02:00
this.printBatches.push(batch);
2015-07-15 15:06:18 +02:00
}
return this;
}
stopPrint (endCode = '') {
2015-07-15 15:06:18 +02:00
this._printBatches = [];
this._currentBatch = 0;
this.printer.stop(endCode).then((data) => {
2015-07-15 15:06:18 +02:00
console.log('Printer stop command sent');
2015-07-15 15:06:18 +02:00
});
return this;
}
_updateLoop () {
if (this._printBatches.length > 0 && (this.state['buffered_lines'] + this._printBatches[0].length) <= this.maxBufferedLines) {
//if (this._printBatches.length > 0 ) {
this._printBatch();
}
else {
setTimeout(() => {
this._updateState();
2015-07-15 15:06:18 +02:00
}, 1000);
}
}
_updateState () {
//que api calls so they don't overload the d3d box
this.info.status().then((state) => {
2015-07-15 15:06:18 +02:00
this.state = state;
this.dispatchEvent({
type: 'update',
state
});
2015-07-15 15:06:18 +02:00
this._updateLoop();
}).catch((error) => {
2015-07-15 15:06:18 +02:00
console.warn(error);
this.setAutoUpdate();
2015-07-15 15:06:18 +02:00
});
}
_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
2015-07-15 15:06:18 +02:00
this.printer.print(gcode, start, first, last).then((data) => {
console.log('batch sent: ' + this._currentBatch, data);
2015-07-15 15:06:18 +02:00
if (this._printBatches.length > 0) {
this._currentBatch ++;
2015-07-15 15:06:18 +02:00
}
else {
console.log('Finish sending gcode to printer');
}
this._updateState();
}).catch((error) => {
console.warn(error);
this._printBatches.unshift(gcode);
this.setAutoUpdate();
2015-07-15 15:06:18 +02:00
});
}
}