Doodle3D-API/src/api/printer.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-11-30 17:23:13 +01:00
import { parseFetch, sleep } from '../utils.js';
2015-07-15 15:06:18 +02:00
2016-04-21 15:44:20 +02:00
export default class Printer {
2016-04-21 15:52:23 +02:00
constructor(api) {
this.api = api;
}
temperature() {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/temperature`, { method: 'GET' }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
progress() {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/progress`, { method: 'GET' }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
state() {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/state`, { method: 'GET' }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
listAll() {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/listall`, { method: 'GET' }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
heatup() {
2018-01-25 17:29:51 +01:00
const body = new URLSearchParams();
return fetch(`${this.api}printer/heatup`, { method: 'POST', body }).then(parseFetch);
}
fetch(id, startCode = 'g28', endCode = 'g28') {
const body = new URLSearchParams();
body.append('id', id);
body.append('start_code', startCode);
body.append('end_code', endCode);
return fetch(`${this.api}printer/fetch`, { method: 'POST', body }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
print(gcode = '', first = false, start = false, last) {
2018-01-25 17:29:51 +01:00
const body = new URLSearchParams();
body.append('gcode', gcode);
body.append('first', first);
body.append('start', start);
body.append('last', last);
return fetch(`${this.api}printer/print`, { method: 'POST', body }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
stop(gcode = '') {
2018-01-25 17:29:51 +01:00
const body = new URLSearchParams();
body.append('gcode', gcode);
return fetch(`${this.api}printer/stop`, { method: 'POST', body }).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
2016-08-02 17:59:39 +02:00
async _sendBatch(gcode, start, index) {
try {
const response = await this.print(gcode, start, start);
2017-11-30 17:23:13 +01:00
console.log(`batch sent: ${index}`);
2016-08-02 17:59:39 +02:00
} catch(error) {
2017-11-30 17:23:13 +01:00
console.log(`failed sending batch: ${index}`);
2016-08-02 17:59:39 +02:00
await sleep(1000);
await this._sendBatch(gcode, index);
}
}
}