Updated autoupdate loop

This commit is contained in:
casperlamboo 2015-10-15 10:56:01 +02:00
parent 528ddb7bc8
commit 5584a1f71e
5 changed files with 62 additions and 71 deletions

View File

@ -1,4 +1,4 @@
import Doodle3DManager from 'src/doodle3dmanager.js'; import {Doodle3DManager} from 'src/index.js';
const doodle3DManager = new Doodle3DManager(); const doodle3DManager = new Doodle3DManager();
const TABLE = document.getElementById('table'); const TABLE = document.getElementById('table');
@ -69,13 +69,13 @@ doodle3DManager.addEventListener('boxappeared', ({box}) => {
box.addEventListener('connect', (event) => { box.addEventListener('connect', (event) => {
row.style.color = 'black'; row.style.color = 'black';
box.addEventListener('update', update); box.addEventListener('update', update);
}); });
box.addEventListener('disconnect', (event) => { box.addEventListener('disconnect', (event) => {
row.style.color = 'gray'; row.style.color = 'gray';
box.removeEventListener('update', update); box.removeEventListener('update', update);
}); });

View File

@ -24,19 +24,19 @@
<table style="width:100%"> <table style="width:100%">
<tbody id="table"> <tbody id="table">
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Local IP</th> <th>Local IP</th>
<th>State</th> <th>State</th>
<th>Current Line</th> <th>Current Line</th>
<th>Buffered Lines</th> <th>Buffered Lines</th>
<th>Total Lines</th> <th>Total Lines</th>
<th>Hotend</th> <th>Hotend</th>
<th>Hotend Target</th> <th>Hotend Target</th>
<th>Bed</th> <th>Bed</th>
<th>Bed Target</th> <th>Bed Target</th>
<th>Has Control</th> <th>Has Control</th>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -14,8 +14,6 @@ export default class {
} }
set (data) { set (data) {
var scope = this;
return rest.post(`${this.api}config`, data); return rest.post(`${this.api}config`, data);
} }
} }

View File

@ -1,5 +1,4 @@
import EventDispatcher from 'casperlamboo/EventDispatcher'; import EventDispatcher from 'casperlamboo/EventDispatcher';
import * as rest from './restapi.js';
import ConfigAPI from './configapi.js'; import ConfigAPI from './configapi.js';
import InfoAPI from './infoapi.js'; import InfoAPI from './infoapi.js';
import NetworkAPI from './networkapi.js'; import NetworkAPI from './networkapi.js';
@ -22,7 +21,6 @@ export default class extends EventDispatcher {
this.state = {}; this.state = {};
this.maxBatchSize = 10*1024; this.maxBatchSize = 10*1024;
this.maxBufferSize = 1024*1024;
this.fullBufferTimeout = 10000; this.fullBufferTimeout = 10000;
this.config = new ConfigAPI(this.api); this.config = new ConfigAPI(this.api);
@ -92,27 +90,16 @@ export default class extends EventDispatcher {
let lastIndex = 0; let lastIndex = 0;
let start = true; let start = true;
while (lastIndex !== gcode.length) { 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 batch = gcode.substring(lastIndex, index);
let progress = await this.printer.progress(); let progress = await this.printer.progress();
if (progress['buffered_lines'] + batch.length < this.maxBufferSize) { await this._sendBatch(batch, start);
try {
await this._sendBatch(batch, start);
start = false; start = false;
lastIndex = index + 1; //skip next \n lastIndex = index + 1; //skip next \n
}
catch (error) {
console.log('error while sending gcode', error);
await sleep(this.fullBufferTimeout);
}
}
else {
await sleep(this.fullBufferTimeout);
}
} }
this.autoUpdate = autoUpdateState; this.autoUpdate = autoUpdateState;
@ -139,12 +126,11 @@ export default class extends EventDispatcher {
} }
} }
_sendBatch (gcode, index) { _sendBatch (gcode, start) {
return new Promise (async (resolve, reject) => { return new Promise (async (resolve, reject) => {
try { try {
let first = index === 0; let response = await this.printer.print(gcode, start, start);
let start = first; // maybe do something with failing response
let printRequest = await this.printer.print(gcode, first, start);
console.log(`batch sent: ${index}`, printRequest); console.log(`batch sent: ${index}`, printRequest);
} }

View File

@ -3,7 +3,7 @@ import Doodle3DAPI from './doodle3dapi.js';
import EventDispatcher from 'casperlamboo/EventDispatcher'; import EventDispatcher from 'casperlamboo/EventDispatcher';
import {sleep} from './utils.js'; import {sleep} from './utils.js';
export default class extends EventDispatcher { export default class Doodle3DManager extends EventDispatcher {
constructor () { constructor () {
super(); super();
@ -23,7 +23,7 @@ export default class extends EventDispatcher {
this.autoUpdate = false; this.autoUpdate = false;
} }
setAutoUpdate (autoUpdate = true, updateInterval = 1000) { setAutoUpdate (autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval; this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) { if (this.autoUpdate === autoUpdate) {
@ -50,45 +50,52 @@ export default class extends EventDispatcher {
_checkAlive () { _checkAlive () {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
for (let box of this.boxes) {
let alive = await box.checkAlive();
if (!alive) { await Promise.all(this.boxes.map((box) => {
this._removeBox(box); let promise = box.checkAlive();
} promise.then((alive) => {
} if (!alive) {
this._removeBox(box);
}
})
return promise;
}));
resolve(); resolve();
}); });
} }
_checkNew () { _checkNew () {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let boxes;
try { try {
let boxes = await rest.get(`${this.api}list.php`); 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();
} }
catch (error) { 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();
}); });
} }