Doodle3D-API/src/api/printer.js

51 lines
1.5 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() {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/heatup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({})
}).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
print(gcode = '', first = false, start = false, last) {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ gcode, first, start, last })
}).then(parseFetch);
2016-04-21 15:52:23 +02:00
}
stop(gcode = '') {
2017-11-30 17:23:13 +01:00
return fetch(`${this.api}printer/stop`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ gcode, first, start, last })
}).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);
}
}
}