2015-07-24 13:15:00 +02:00
|
|
|
import EventDispatcher from 'casperlamboo/EventDispatcher';
|
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';
|
2016-04-21 15:09:49 +02:00
|
|
|
import { sleep } from './utils.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;
|
|
|
|
|
2016-04-21 15:09:49 +02:00
|
|
|
this.api = `http://${ boxData.localip }/d3dapi/`;
|
2016-04-16 18:06:06 +02:00
|
|
|
|
2015-07-15 15:06:18 +02:00
|
|
|
this.alive = false;
|
2015-07-24 17:37:01 +02:00
|
|
|
this.autoUpdate = false;
|
2015-07-15 15:06:18 +02:00
|
|
|
this.state = {};
|
2015-10-12 12:33:33 +02:00
|
|
|
|
2016-04-21 15:09:49 +02:00
|
|
|
this.maxBatchSize = 10 * 1024;
|
2015-10-12 12:33:33 +02:00
|
|
|
this.fullBufferTimeout = 10000;
|
2015-10-12 13:06:20 +02:00
|
|
|
|
|
|
|
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-24 17:37:01 +02:00
|
|
|
}
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-07-24 17:37:01 +02:00
|
|
|
setAutoUpdate (autoUpdate = true, updateInterval = 1000) {
|
|
|
|
this.updateInterval = updateInterval;
|
2016-04-21 15:05:15 +02:00
|
|
|
if (this.autoUpdate === autoUpdate) return;
|
2015-07-24 17:37:01 +02:00
|
|
|
|
|
|
|
this.autoUpdate = autoUpdate;
|
2016-04-21 15:05:15 +02:00
|
|
|
if (autoUpdate) this._update();
|
2015-07-24 17:37:01 +02:00
|
|
|
|
|
|
|
return this;
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
async checkAlive () {
|
|
|
|
const alive = await this.network.alive();
|
2015-07-24 17:37:01 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
if (alive !== this.alive) {
|
|
|
|
const type = alive ? 'connect' : 'disconnect';
|
|
|
|
this.dispatchEvent({ type });
|
|
|
|
}
|
2015-07-27 16:32:11 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
this.alive = alive;
|
|
|
|
return alive;
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
async sendGCode (gcode) {
|
|
|
|
const printerState = await this.printer.state();
|
|
|
|
if (printerState.state !== 'idle') {
|
2016-04-21 15:09:49 +02:00
|
|
|
throw `Cannot print, print state is ${ printerState.state }`;
|
2016-04-16 18:07:11 +02:00
|
|
|
}
|
2015-10-13 11:03:18 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
if (!gcode.endsWith('\n')) {
|
|
|
|
gcode += '\n';
|
|
|
|
}
|
2015-07-23 19:12:50 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
let lastIndex = 0;
|
|
|
|
let start = true;
|
|
|
|
while (lastIndex !== gcode.length) {
|
|
|
|
const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
|
|
|
|
const batch = gcode.substring(lastIndex, index);
|
2015-07-24 17:37:01 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
// const progress = await this.printer.progress();
|
2015-10-15 10:56:01 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
await this._sendBatch(batch, start);
|
2015-10-12 12:33:33 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
start = false;
|
|
|
|
lastIndex = index + 1; //skip next \n
|
|
|
|
}
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
|
|
|
|
2015-10-12 12:33:33 +02:00
|
|
|
async _update () {
|
2015-10-12 13:06:20 +02:00
|
|
|
while (this.autoUpdate) {
|
|
|
|
try {
|
|
|
|
this.state = await this.info.status();
|
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
this.dispatchEvent({ type: 'update', state: this.state });
|
|
|
|
} catch(error) {
|
2015-10-12 13:06:20 +02:00
|
|
|
await this.checkAlive();
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2015-10-12 13:06:20 +02:00
|
|
|
await sleep(this.updateInterval);
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
async _sendBatch (gcode, start) {
|
|
|
|
try {
|
|
|
|
const response = await this.printer.print(gcode, start, start);
|
|
|
|
// maybe do something with failing response
|
2015-07-15 15:06:18 +02:00
|
|
|
|
2016-04-21 15:09:49 +02:00
|
|
|
console.log(`batch sent: ${ index }`, printRequest);
|
2016-04-16 18:07:11 +02:00
|
|
|
} catch(error) {
|
|
|
|
await sleep(1000);
|
2015-10-12 12:33:33 +02:00
|
|
|
|
2016-04-16 18:07:11 +02:00
|
|
|
await this._sendBatch(gcode, index);
|
|
|
|
}
|
2015-07-15 15:06:18 +02:00
|
|
|
}
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|