mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2024-11-05 04:33:24 +01:00
Updated autoupdate loop
This commit is contained in:
parent
528ddb7bc8
commit
5584a1f71e
@ -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);
|
||||
});
|
||||
|
||||
|
@ -24,19 +24,19 @@
|
||||
|
||||
<table style="width:100%">
|
||||
<tbody id="table">
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Local IP</th>
|
||||
<th>State</th>
|
||||
<th>Current Line</th>
|
||||
<th>Buffered Lines</th>
|
||||
<th>Total Lines</th>
|
||||
<th>Hotend</th>
|
||||
<th>Hotend Target</th>
|
||||
<th>Bed</th>
|
||||
<th>Bed Target</th>
|
||||
<th>Has Control</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Local IP</th>
|
||||
<th>State</th>
|
||||
<th>Current Line</th>
|
||||
<th>Buffered Lines</th>
|
||||
<th>Total Lines</th>
|
||||
<th>Hotend</th>
|
||||
<th>Hotend Target</th>
|
||||
<th>Bed</th>
|
||||
<th>Bed Target</th>
|
||||
<th>Has Control</th>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
@ -14,8 +14,6 @@ export default class {
|
||||
}
|
||||
|
||||
set (data) {
|
||||
var scope = this;
|
||||
|
||||
return rest.post(`${this.api}config`, data);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user