mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2025-01-03 10:03:48 +01:00
use async and promises properly
This commit is contained in:
parent
7ed4183bb8
commit
1960d384d6
@ -48,64 +48,48 @@ export default class extends EventDispatcher {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkAlive () {
|
async checkAlive () {
|
||||||
return new Promise(async (resolve, reject) => {
|
const alive = await this.network.alive();
|
||||||
let alive;
|
|
||||||
try {
|
|
||||||
await this.network.alive();
|
|
||||||
alive = true;
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
alive = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (alive !== this.alive) {
|
if (alive !== this.alive) {
|
||||||
this.dispatchEvent({
|
const type = alive ? 'connect' : 'disconnect';
|
||||||
type: alive ? 'connect' : 'disconnect'
|
this.dispatchEvent({ type });
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.alive = alive;
|
this.alive = alive;
|
||||||
resolve(alive);
|
return alive;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendGCode (gcode) {
|
async sendGCode (gcode) {
|
||||||
return new Promise(async (resolve, reject) => {
|
const printerState = await this.printer.state();
|
||||||
let printerState = await this.printer.state();
|
if (printerState.state !== 'idle') {
|
||||||
if (printerState.state !== 'idle') {
|
throw `Cannot print, print state is ${printerState.state}`;
|
||||||
reject(`Cannot print, print state is ${printerState.state}`);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let autoUpdateState = this.autoUpdate;
|
const autoUpdateState = this.autoUpdate;
|
||||||
this.autoUpdate = false;
|
this.autoUpdate = false;
|
||||||
|
|
||||||
if (!gcode.endsWith('\n')) {
|
if (!gcode.endsWith('\n')) {
|
||||||
gcode += '\n';
|
gcode += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
this._currentBatch = 0;
|
this._currentBatch = 0;
|
||||||
|
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
let start = true;
|
let start = true;
|
||||||
while (lastIndex !== gcode.length) {
|
while (lastIndex !== gcode.length) {
|
||||||
let lastIndex = lastIndex + this.maxBatchSize;
|
const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
|
||||||
let index = gcode.lastIndexOf('\n', lastIndex);
|
const batch = gcode.substring(lastIndex, index);
|
||||||
let batch = gcode.substring(lastIndex, index);
|
|
||||||
|
|
||||||
let progress = await this.printer.progress();
|
// const progress = await this.printer.progress();
|
||||||
|
|
||||||
await this._sendBatch(batch, start);
|
await this._sendBatch(batch, start);
|
||||||
|
|
||||||
start = false;
|
start = false;
|
||||||
lastIndex = index + 1; //skip next \n
|
lastIndex = index + 1; //skip next \n
|
||||||
}
|
}
|
||||||
|
|
||||||
this.autoUpdate = autoUpdateState;
|
this.autoUpdate = autoUpdateState;
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _update () {
|
async _update () {
|
||||||
@ -113,12 +97,8 @@ export default class extends EventDispatcher {
|
|||||||
try {
|
try {
|
||||||
this.state = await this.info.status();
|
this.state = await this.info.status();
|
||||||
|
|
||||||
this.dispatchEvent({
|
this.dispatchEvent({ type: 'update', state: this.state });
|
||||||
type: 'update',
|
} catch(error) {
|
||||||
state: this.state
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
await this.checkAlive();
|
await this.checkAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,21 +106,16 @@ export default class extends EventDispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendBatch (gcode, start) {
|
async _sendBatch (gcode, start) {
|
||||||
return new Promise (async (resolve, reject) => {
|
try {
|
||||||
try {
|
const response = await this.printer.print(gcode, start, start);
|
||||||
let response = await this.printer.print(gcode, start, start);
|
// maybe do something with failing response
|
||||||
// maybe do something with failing response
|
|
||||||
|
|
||||||
console.log(`batch sent: ${index}`, printRequest);
|
console.log(`batch sent: ${index}`, printRequest);
|
||||||
}
|
} catch(error) {
|
||||||
catch (error) {
|
await sleep(1000);
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
await this._sendBatch(gcode, index);
|
await this._sendBatch(gcode, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,55 +48,39 @@ export default class Doodle3DManager extends EventDispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkAlive () {
|
async _checkAlive () {
|
||||||
return new Promise(async (resolve, reject) => {
|
for (const box of boxes) {
|
||||||
|
const alive = await box.checkAlive();
|
||||||
|
|
||||||
await Promise.all(this.boxes.map((box) => {
|
if (!alive) {
|
||||||
let promise = box.checkAlive();
|
this._removeBox(box);
|
||||||
promise.then((alive) => {
|
}
|
||||||
if (!alive) {
|
}
|
||||||
this._removeBox(box);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}));
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkNew () {
|
async _checkNew () {
|
||||||
return new Promise(async (resolve, reject) => {
|
let boxes;
|
||||||
let boxes;
|
try {
|
||||||
try {
|
boxes = await rest.get(`${this.api}list.php`);
|
||||||
boxes = await rest.get(`${this.api}list.php`);
|
} catch(error) {
|
||||||
|
throw 'fail connecting to Doodle3D server';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.checkNonServerBoxes) {
|
||||||
|
boxes = [...boxes, ...this.nonServerBoxes];
|
||||||
|
}
|
||||||
|
|
||||||
|
const knownIPs = this.boxes.map((box) => box.boxData.localip);
|
||||||
|
const boxes = boxes.filter(({ localip }) => knownIPs.indexOf(localip) === -1);
|
||||||
|
|
||||||
|
for (const boxData of boxes) {
|
||||||
|
const box = new Doodle3DAPI(boxData);
|
||||||
|
|
||||||
|
const alive = await box.checkAlive();
|
||||||
|
if (alive) {
|
||||||
|
this._addBox(box);
|
||||||
}
|
}
|
||||||
catch (error) {
|
}
|
||||||
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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_addBox (box) {
|
_addBox (box) {
|
||||||
|
Loading…
Reference in New Issue
Block a user