2015-07-24 13:15:00 +02:00
|
|
|
import EventDispatcher from 'casperlamboo/EventDispatcher';
|
2015-07-23 19:12:50 +02:00
|
|
|
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
|
|
|
|
2015-07-24 13:15:00 +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;
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
setAutoUpdate (autoUpdate) {
|
2015-07-23 19:12:50 +02:00
|
|
|
this.network.alive().then(() => {
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
this.alive = true;
|
2015-07-24 13:15:00 +02:00
|
|
|
|
|
|
|
this.dispatchEvent({
|
|
|
|
type: 'connect'
|
|
|
|
});
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
if (!this.loaded) {
|
|
|
|
this.loaded = true;
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
this._updateState();
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
}).catch((error) => {
|
2015-07-23 19:12:50 +02:00
|
|
|
if (this.alive) {
|
|
|
|
this.alive = false;
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.dispatchEvent({
|
|
|
|
type: 'disconnect'
|
|
|
|
});
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
2015-07-23 19:12:50 +02:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2015-07-24 13:15:00 +02:00
|
|
|
this.setAutoUpdate();
|
2015-07-23 19:12:50 +02:00
|
|
|
}, 1000);
|
2015-07-15 15:06:18 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
print (gcode) {
|
|
|
|
this._currentBatch = 0;
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
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
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.printBatches.push(batch);
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
stopPrint (endCode = '') {
|
2015-07-15 15:06:18 +02:00
|
|
|
|
|
|
|
this._printBatches = [];
|
|
|
|
this._currentBatch = 0;
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.printer.stop(endCode).then((data) => {
|
|
|
|
|
2015-07-15 15:06:18 +02:00
|
|
|
console.log('Printer stop command sent');
|
2015-07-24 13:15:00 +02:00
|
|
|
|
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 {
|
2015-07-23 19:12:50 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
this._updateState();
|
2015-07-15 15:06:18 +02:00
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateState () {
|
|
|
|
//que api calls so they don't overload the d3d box
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.info.status().then((state) => {
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.state = state;
|
|
|
|
|
|
|
|
this.dispatchEvent({
|
|
|
|
type: 'update',
|
|
|
|
state
|
|
|
|
});
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
this._updateLoop();
|
|
|
|
}).catch((error) => {
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
console.warn(error);
|
2015-07-24 13:15:00 +02:00
|
|
|
this.setAutoUpdate();
|
|
|
|
|
2015-07-15 15:06:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_printBatch () {
|
|
|
|
var gcode = this._printBatches.shift();
|
2015-07-24 13:15:00 +02:00
|
|
|
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
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.printer.print(gcode, start, first, last).then((data) => {
|
2015-07-23 19:12:50 +02:00
|
|
|
console.log('batch sent: ' + this._currentBatch, data);
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-23 19:12:50 +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');
|
|
|
|
}
|
|
|
|
|
2015-07-23 19:12:50 +02:00
|
|
|
this._updateState();
|
|
|
|
}).catch((error) => {
|
2015-07-24 13:15:00 +02:00
|
|
|
console.warn(error);
|
2015-07-23 19:12:50 +02:00
|
|
|
|
|
|
|
this._printBatches.unshift(gcode);
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.setAutoUpdate();
|
2015-07-15 15:06:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|