mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2024-11-05 04:33:24 +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;
|
||||
}
|
||||
|
||||
checkAlive () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let alive;
|
||||
try {
|
||||
await this.network.alive();
|
||||
alive = true;
|
||||
}
|
||||
catch (error) {
|
||||
alive = false;
|
||||
}
|
||||
async checkAlive () {
|
||||
const alive = await this.network.alive();
|
||||
|
||||
if (alive !== this.alive) {
|
||||
this.dispatchEvent({
|
||||
type: alive ? 'connect' : 'disconnect'
|
||||
});
|
||||
}
|
||||
if (alive !== this.alive) {
|
||||
const type = alive ? 'connect' : 'disconnect';
|
||||
this.dispatchEvent({ type });
|
||||
}
|
||||
|
||||
this.alive = alive;
|
||||
resolve(alive);
|
||||
});
|
||||
this.alive = alive;
|
||||
return alive;
|
||||
}
|
||||
|
||||
sendGCode (gcode) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let printerState = await this.printer.state();
|
||||
if (printerState.state !== 'idle') {
|
||||
reject(`Cannot print, print state is ${printerState.state}`);
|
||||
return;
|
||||
}
|
||||
async sendGCode (gcode) {
|
||||
const printerState = await this.printer.state();
|
||||
if (printerState.state !== 'idle') {
|
||||
throw `Cannot print, print state is ${printerState.state}`;
|
||||
}
|
||||
|
||||
let autoUpdateState = this.autoUpdate;
|
||||
this.autoUpdate = false;
|
||||
const autoUpdateState = this.autoUpdate;
|
||||
this.autoUpdate = false;
|
||||
|
||||
if (!gcode.endsWith('\n')) {
|
||||
gcode += '\n';
|
||||
}
|
||||
if (!gcode.endsWith('\n')) {
|
||||
gcode += '\n';
|
||||
}
|
||||
|
||||
this._currentBatch = 0;
|
||||
this._currentBatch = 0;
|
||||
|
||||
let lastIndex = 0;
|
||||
let start = true;
|
||||
while (lastIndex !== gcode.length) {
|
||||
let lastIndex = lastIndex + this.maxBatchSize;
|
||||
let index = gcode.lastIndexOf('\n', lastIndex);
|
||||
let batch = gcode.substring(lastIndex, index);
|
||||
let lastIndex = 0;
|
||||
let start = true;
|
||||
while (lastIndex !== gcode.length) {
|
||||
const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
|
||||
const 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;
|
||||
lastIndex = index + 1; //skip next \n
|
||||
}
|
||||
start = false;
|
||||
lastIndex = index + 1; //skip next \n
|
||||
}
|
||||
|
||||
this.autoUpdate = autoUpdateState;
|
||||
|
||||
resolve();
|
||||
});
|
||||
this.autoUpdate = autoUpdateState;
|
||||
}
|
||||
|
||||
async _update () {
|
||||
@ -113,12 +97,8 @@ export default class extends EventDispatcher {
|
||||
try {
|
||||
this.state = await this.info.status();
|
||||
|
||||
this.dispatchEvent({
|
||||
type: 'update',
|
||||
state: this.state
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
this.dispatchEvent({ type: 'update', state: this.state });
|
||||
} catch(error) {
|
||||
await this.checkAlive();
|
||||
}
|
||||
|
||||
@ -126,21 +106,16 @@ export default class extends EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
_sendBatch (gcode, start) {
|
||||
return new Promise (async (resolve, reject) => {
|
||||
try {
|
||||
let response = await this.printer.print(gcode, start, start);
|
||||
// maybe do something with failing response
|
||||
async _sendBatch (gcode, start) {
|
||||
try {
|
||||
const response = await this.printer.print(gcode, start, start);
|
||||
// maybe do something with failing response
|
||||
|
||||
console.log(`batch sent: ${index}`, printRequest);
|
||||
}
|
||||
catch (error) {
|
||||
await sleep(1000);
|
||||
console.log(`batch sent: ${index}`, printRequest);
|
||||
} catch(error) {
|
||||
await sleep(1000);
|
||||
|
||||
await this._sendBatch(gcode, index);
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
await this._sendBatch(gcode, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,55 +48,39 @@ export default class Doodle3DManager extends EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
_checkAlive () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
async _checkAlive () {
|
||||
for (const box of boxes) {
|
||||
const alive = await box.checkAlive();
|
||||
|
||||
await Promise.all(this.boxes.map((box) => {
|
||||
let promise = box.checkAlive();
|
||||
promise.then((alive) => {
|
||||
if (!alive) {
|
||||
this._removeBox(box);
|
||||
}
|
||||
})
|
||||
|
||||
return promise;
|
||||
}));
|
||||
|
||||
resolve();
|
||||
});
|
||||
if (!alive) {
|
||||
this._removeBox(box);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_checkNew () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let boxes;
|
||||
try {
|
||||
boxes = await rest.get(`${this.api}list.php`);
|
||||
async _checkNew () {
|
||||
let boxes;
|
||||
try {
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user