diff --git a/example/app.js b/example/app.js index 44fca96..59f630f 100644 --- a/example/app.js +++ b/example/app.js @@ -1,4 +1,4 @@ -import Doodle3DManager from 'src/doodle3dmanager.js'; +import {Doodle3DManager} from 'src/index.js'; const doodle3DManager = new Doodle3DManager(); const TABLE = document.getElementById('table'); @@ -69,13 +69,13 @@ doodle3DManager.addEventListener('boxappeared', ({box}) => { box.addEventListener('connect', (event) => { row.style.color = 'black'; - + box.addEventListener('update', update); }); box.addEventListener('disconnect', (event) => { row.style.color = 'gray'; - + box.removeEventListener('update', update); }); diff --git a/example/index.html b/example/index.html index c89c8a2..45dbff1 100644 --- a/example/index.html +++ b/example/index.html @@ -24,19 +24,19 @@ - - - - - - - - - - - - - + + + + + + + + + + + + +
IDLocal IPStateCurrent LineBuffered LinesTotal LinesHotendHotend TargetBedBed TargetHas Control
IDLocal IPStateCurrent LineBuffered LinesTotal LinesHotendHotend TargetBedBed TargetHas Control
diff --git a/src/configapi.js b/src/configapi.js index a7b5071..25fa330 100644 --- a/src/configapi.js +++ b/src/configapi.js @@ -14,8 +14,6 @@ export default class { } set (data) { - var scope = this; - return rest.post(`${this.api}config`, data); } } diff --git a/src/doodle3dapi.js b/src/doodle3dapi.js index eb3c6db..e0ddc8e 100644 --- a/src/doodle3dapi.js +++ b/src/doodle3dapi.js @@ -1,5 +1,4 @@ import EventDispatcher from 'casperlamboo/EventDispatcher'; -import * as rest from './restapi.js'; import ConfigAPI from './configapi.js'; import InfoAPI from './infoapi.js'; import NetworkAPI from './networkapi.js'; @@ -22,7 +21,6 @@ export default class extends EventDispatcher { this.state = {}; this.maxBatchSize = 10*1024; - this.maxBufferSize = 1024*1024; this.fullBufferTimeout = 10000; this.config = new ConfigAPI(this.api); @@ -92,27 +90,16 @@ export default class extends EventDispatcher { let lastIndex = 0; let start = true; while (lastIndex !== gcode.length) { - let index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize); + let lastIndex = lastIndex + this.maxBatchSize; + let index = gcode.lastIndexOf('\n', lastIndex); let batch = gcode.substring(lastIndex, index); let progress = await this.printer.progress(); - if (progress['buffered_lines'] + batch.length < this.maxBufferSize) { - try { - await this._sendBatch(batch, start); + await this._sendBatch(batch, start); - start = false; - lastIndex = index + 1; //skip next \n - } - catch (error) { - console.log('error while sending gcode', error); - - await sleep(this.fullBufferTimeout); - } - } - else { - await sleep(this.fullBufferTimeout); - } + start = false; + lastIndex = index + 1; //skip next \n } this.autoUpdate = autoUpdateState; @@ -139,12 +126,11 @@ export default class extends EventDispatcher { } } - _sendBatch (gcode, index) { + _sendBatch (gcode, start) { return new Promise (async (resolve, reject) => { try { - let first = index === 0; - let start = first; - let printRequest = await this.printer.print(gcode, first, start); + let response = await this.printer.print(gcode, start, start); + // maybe do something with failing response console.log(`batch sent: ${index}`, printRequest); } diff --git a/src/doodle3dmanager.js b/src/doodle3dmanager.js index 5d8fe1b..7790917 100644 --- a/src/doodle3dmanager.js +++ b/src/doodle3dmanager.js @@ -3,7 +3,7 @@ import Doodle3DAPI from './doodle3dapi.js'; import EventDispatcher from 'casperlamboo/EventDispatcher'; import {sleep} from './utils.js'; -export default class extends EventDispatcher { +export default class Doodle3DManager extends EventDispatcher { constructor () { super(); @@ -23,7 +23,7 @@ export default class extends EventDispatcher { this.autoUpdate = false; } - setAutoUpdate (autoUpdate = true, updateInterval = 1000) { + setAutoUpdate (autoUpdate = true, updateInterval = 1000) { this.updateInterval = updateInterval; if (this.autoUpdate === autoUpdate) { @@ -50,45 +50,52 @@ export default class extends EventDispatcher { _checkAlive () { return new Promise(async (resolve, reject) => { - for (let box of this.boxes) { - let alive = await box.checkAlive(); - if (!alive) { - this._removeBox(box); - } - } + await Promise.all(this.boxes.map((box) => { + let promise = box.checkAlive(); + promise.then((alive) => { + if (!alive) { + this._removeBox(box); + } + }) + + return promise; + })); + resolve(); }); } _checkNew () { - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve, reject) => { + let boxes; try { - let boxes = await rest.get(`${this.api}list.php`); - - if (this.checkNonServerBoxes) { - boxes = boxes.concat(this.nonServerBoxes); - } - - let knownIPs = this.boxes.map((box) => box.boxData.localip); - - for (let boxData of boxes) { - if (knownIPs.indexOf(boxData.localip) === -1) { - let box = new Doodle3DAPI(boxData); - - let alive = await box.checkAlive(); - - if (alive) { - this._addBox(box); - } - } - } - - resolve(); + boxes = await rest.get(`${this.api}list.php`); } catch (error) { - console.warn('fail connecting to Doodle3D server'); + console.warn('fail connecting to Doodle3D server', error); + return; } + + if (this.checkNonServerBoxes) { + boxes = [...boxes, ...this.nonServerBoxes]; + } + + let knownIPs = this.boxes.map((box) => box.boxData.localip); + + for (let boxData of boxes) { + if (knownIPs.indexOf(boxData.localip) === -1) { + let box = new Doodle3DAPI(boxData); + + let alive = await box.checkAlive(); + + if (alive) { + this._addBox(box); + } + } + } + + resolve(); }); }