diff --git a/README.md b/README.md
index e117c64..1bf105f 100644
--- a/README.md
+++ b/README.md
@@ -2,24 +2,24 @@
Doodle3D API for communication with the Doodle3D WiFi-Box
```javascript
-import {Doodle3DManager} from 'Doodle3D/Doodle3D-API';
+import { Doodle3DManager } from 'Doodle3D/Doodle3D-API';
const doodle3DManager = new Doodle3DManager();
doodle3DManager.addEventListener('boxappeared', ({box}) => {
- box.addEventListener('connect', (event) => {
- });
+ box.addEventListener('connect', (event) => {
+ });
- box.addEventListener('disconnect', (event) => {
- });
+ box.addEventListener('disconnect', (event) => {
+ });
- box.addEventListener('update', (event) => {
- let status = event.state;
- });
+ box.addEventListener('update', (event) => {
+ const status = event.state;
+ });
- box.setAutoUpdate(true);
+ box.setAutoUpdate(true, 1000);
});
-doodle3DManager.setAutoUpdate(true);
+doodle3DManager.setAutoUpdate(true, 1000);
```
diff --git a/example/app.js b/example/app.js
index d304b25..c90aeec 100644
--- a/example/app.js
+++ b/example/app.js
@@ -4,23 +4,23 @@ const doodle3DManager = new Doodle3DManager();
doodle3DManager.setAutoUpdate(true, 1000);
doodle3DManager.addEventListener('boxappeared', ({ box }) => {
- box.setAutoUpdate(true, 1000);
+ box.setAutoUpdate(true, 1000);
- box.addEventListener('connect', () => {
- console.log('connect to box', box);
- });
+ box.addEventListener('connect', () => {
+ console.log('connect to box', box);
+ });
- box.addEventListener('disconnect', () => {
- console.log('disonnect to box', box);
- })
+ box.addEventListener('disconnect', () => {
+ console.log('disonnect to box', box);
+ })
- box.addEventListener('update', ({ state }) => {
- console.log(state);
- });
+ box.addEventListener('update', ({ state }) => {
+ console.log(state);
+ });
});
doodle3DManager.addEventListener('boxdisappeared', ({ box }) => {
- box.setAutoUpdate(false);
+ box.setAutoUpdate(false);
});
doodle3DManager.addEventListener('boxeschanged', ({ boxes }) => {
diff --git a/example/index.html b/example/index.html
index 119aa6c..75211be 100644
--- a/example/index.html
+++ b/example/index.html
@@ -1,14 +1,14 @@
- Doodle3D Box
+ Doodle3D Box
-
-
+
+
-
+
diff --git a/src/api/config.js b/src/api/config.js
index 781d8a3..8a9e010 100644
--- a/src/api/config.js
+++ b/src/api/config.js
@@ -1,16 +1,16 @@
import * as rest from '../rest.js';
export default class Config {
- constructor(api) {
- this.api = api;
- }
- get(...keys) {
- return rest.get(`${ this.api }config/?${ keys.join('=&') }=`);
- }
- getAll() {
- return rest.get(`${ this.api }config/all`);
- }
- set(data) {
- return rest.post(`${ this.api }config`, data);
- }
+ constructor(api) {
+ this.api = api;
+ }
+ get(...keys) {
+ return rest.get(`${ this.api }config/?${ keys.join('=&') }=`);
+ }
+ getAll() {
+ return rest.get(`${ this.api }config/all`);
+ }
+ set(data) {
+ return rest.post(`${ this.api }config`, data);
+ }
}
diff --git a/src/api/info.js b/src/api/info.js
index 29dffc0..b59c1fa 100644
--- a/src/api/info.js
+++ b/src/api/info.js
@@ -1,19 +1,19 @@
import * as rest from '../rest.js';
export default class Info {
- constructor(api) {
- this.api = api;
- }
- get() {
- return rest.get(`${ this.api }info`);
- }
- status() {
- return rest.get(`${ this.api }info/status`);
- }
- downloadLogFiles() {
- window.location = `${ this.api }info/logfiles`;
- }
- acces() {
- return rest.get(`${ this.api }info/access`);
- }
+ constructor(api) {
+ this.api = api;
+ }
+ get() {
+ return rest.get(`${ this.api }info`);
+ }
+ status() {
+ return rest.get(`${ this.api }info/status`);
+ }
+ downloadLogFiles() {
+ window.location = `${ this.api }info/logfiles`;
+ }
+ acces() {
+ return rest.get(`${ this.api }info/access`);
+ }
}
diff --git a/src/api/network.js b/src/api/network.js
index eca576d..0348bc2 100644
--- a/src/api/network.js
+++ b/src/api/network.js
@@ -1,46 +1,46 @@
import * as rest from '../rest.js';
export default class Network {
- constructor(api) {
- this.api = api;
- }
- scan() {
- return rest.get(`${ this.api }network/scan`);
- }
- known() {
- return rest.get(`${ this.api }network/known`);
- }
- status() {
- return rest.get(`${ this.api }network/status`);
- }
- assosiate(ssid, phrase, recreate = false) {
- const data = { ssid, recreate, phrase };
+ constructor(api) {
+ this.api = api;
+ }
+ scan() {
+ return rest.get(`${ this.api }network/scan`);
+ }
+ known() {
+ return rest.get(`${ this.api }network/known`);
+ }
+ status() {
+ return rest.get(`${ this.api }network/status`);
+ }
+ assosiate(ssid, phrase, recreate = false) {
+ const data = { ssid, recreate, phrase };
- return rest.post(`${ this.api }network/associate`, data);
- }
- disassociate() {
- //not tested
- return rest.post(`${ this.api }network/disassociate`, {});
- }
- openAccesPoint() {
- //not tested
- return rest.post(`${ this.api }network/openap`, {});
- }
- remove(ssid) {
- return rest.post(`${ this.api }network/remove`, {
- 'ssid': ssid
- });
- }
- signin() {
- return rest.get(`${ this.api }network/signin`);
- }
- async alive() {
- try {
- await rest.get(`${ this.api }network/alive`);
+ return rest.post(`${ this.api }network/associate`, data);
+ }
+ disassociate() {
+ //not tested
+ return rest.post(`${ this.api }network/disassociate`, {});
+ }
+ openAccesPoint() {
+ //not tested
+ return rest.post(`${ this.api }network/openap`, {});
+ }
+ remove(ssid) {
+ return rest.post(`${ this.api }network/remove`, {
+ 'ssid': ssid
+ });
+ }
+ signin() {
+ return rest.get(`${ this.api }network/signin`);
+ }
+ async alive() {
+ try {
+ await rest.get(`${ this.api }network/alive`);
- return true;
- } catch(error) {
- return false;
- }
- }
+ return true;
+ } catch(error) {
+ return false;
+ }
+ }
}
diff --git a/src/api/printer.js b/src/api/printer.js
index 052d226..1d1a53d 100644
--- a/src/api/printer.js
+++ b/src/api/printer.js
@@ -1,32 +1,32 @@
import * as rest from '../rest.js';
export default class Printer {
- constructor(api) {
- this.api = api;
- }
- temperature() {
- return rest.get(`${ this.api }printer/temperature`);
- }
- progress() {
- return rest.get(`${ this.api }printer/progress`);
- }
- state() {
- return rest.get(`${ this.api }printer/state`);
- }
- listAll() {
- return rest.get(`${ this.api }printer/listall`);
- }
- heatup() {
- return rest.post(`${ this.api }printer/heatup`, {});
- }
- print(gcode = '', first = false, start = false, last) {
- const data = { gcode, first, start, last };
+ constructor(api) {
+ this.api = api;
+ }
+ temperature() {
+ return rest.get(`${ this.api }printer/temperature`);
+ }
+ progress() {
+ return rest.get(`${ this.api }printer/progress`);
+ }
+ state() {
+ return rest.get(`${ this.api }printer/state`);
+ }
+ listAll() {
+ return rest.get(`${ this.api }printer/listall`);
+ }
+ heatup() {
+ return rest.post(`${ this.api }printer/heatup`, {});
+ }
+ print(gcode = '', first = false, start = false, last) {
+ const data = { gcode, first, start, last };
- return rest.post(`${ this.api }printer/print`, data);
- }
- stop(gcode = '') {
- const data = { gcode };
+ return rest.post(`${ this.api }printer/print`, data);
+ }
+ stop(gcode = '') {
+ const data = { gcode };
- return rest.post(`${ this.api }printer/stop`, data);
- }
+ return rest.post(`${ this.api }printer/stop`, data);
+ }
}
diff --git a/src/api/sketch.js b/src/api/sketch.js
index c974af9..8a0a08d 100644
--- a/src/api/sketch.js
+++ b/src/api/sketch.js
@@ -1,19 +1,19 @@
import * as rest from '../rest.js';
export default class Sketch {
- constructor(api) {
- this.api = api;
- }
- getSketch(id) {
- return rest.get(`${ this.api }sketch/?id=${ id }`);
- }
- set(data = '') {
- return rest.post(`${ this.api }sketch`, { data });
- }
- status() {
- return rest.get(`${ this.api }sketch/status`);
- }
- clear() {
- return rest.post(`${ this.api }sketch/clear`);
- }
+ constructor(api) {
+ this.api = api;
+ }
+ getSketch(id) {
+ return rest.get(`${ this.api }sketch/?id=${ id }`);
+ }
+ set(data = '') {
+ return rest.post(`${ this.api }sketch`, { data });
+ }
+ status() {
+ return rest.get(`${ this.api }sketch/status`);
+ }
+ clear() {
+ return rest.post(`${ this.api }sketch/clear`);
+ }
}
diff --git a/src/api/system.js b/src/api/system.js
index 3506b21..e811393 100644
--- a/src/api/system.js
+++ b/src/api/system.js
@@ -1,10 +1,10 @@
import * as rest from '../rest.js';
export default class System {
- constructor(api) {
- this.api = api;
- }
- versions() {
- return rest.get(`${ this.api }system/fwversions`);
- }
+ constructor(api) {
+ this.api = api;
+ }
+ versions() {
+ return rest.get(`${ this.api }system/fwversions`);
+ }
}
diff --git a/src/api/update.js b/src/api/update.js
index 098a506..729db7a 100644
--- a/src/api/update.js
+++ b/src/api/update.js
@@ -1,22 +1,22 @@
import * as rest from '../rest.js';
export default class Update {
- constructor(api) {
- this.api = api;
- }
- status() {
- return rest.get(`${ this.api }update/status`);
- }
- download() {
- //not tested
- return rest.post(`${ this.api }update/download`, {});
- }
- install() {
- //not tested
- return rest.post(`${ this.api }update/install`, {});
- }
- clear() {
- //not tested
- return rest.post(`${ this.api }update/clear`, {});
- }
+ constructor(api) {
+ this.api = api;
+ }
+ status() {
+ return rest.get(`${ this.api }update/status`);
+ }
+ download() {
+ //not tested
+ return rest.post(`${ this.api }update/download`, {});
+ }
+ install() {
+ //not tested
+ return rest.post(`${ this.api }update/install`, {});
+ }
+ clear() {
+ //not tested
+ return rest.post(`${ this.api }update/clear`, {});
+ }
}
diff --git a/src/doodle3dbox.js b/src/doodle3dbox.js
index a8e8184..2e446d9 100644
--- a/src/doodle3dbox.js
+++ b/src/doodle3dbox.js
@@ -9,99 +9,99 @@ import SystemAPI from './api/system.js';
import UpdateAPI from './api/update.js';
export default class Doodle3DBox extends EventDispatcher {
- constructor(boxData) {
- super();
+ constructor(boxData) {
+ super();
- this.boxData = boxData;
+ this.boxData = boxData;
- this.api = `http://${ boxData.localip }/d3dapi/`;
+ this.api = `http://${ boxData.localip }/d3dapi/`;
- this.alive = false;
- this.autoUpdate = false;
- this.state = {};
+ this.alive = false;
+ this.autoUpdate = false;
+ this.state = {};
- this.maxBatchSize = 10 * 1024;
- this.fullBufferTimeout = 10000;
+ this.maxBatchSize = 10 * 1024;
+ this.fullBufferTimeout = 10000;
- this.config = new ConfigAPI(this.api);
- this.info = new InfoAPI(this.api);
- this.network = new NetworkAPI(this.api);
- this.printer = new PrinterAPI(this.api);
- this.sketch = new SketchAPI(this.api);
- this.system = new SystemAPI(this.api);
- this.update = new UpdateAPI(this.api);
- }
- setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
- this.updateInterval = updateInterval;
- if (this.autoUpdate === autoUpdate) return;
+ this.config = new ConfigAPI(this.api);
+ this.info = new InfoAPI(this.api);
+ this.network = new NetworkAPI(this.api);
+ this.printer = new PrinterAPI(this.api);
+ this.sketch = new SketchAPI(this.api);
+ this.system = new SystemAPI(this.api);
+ this.update = new UpdateAPI(this.api);
+ }
+ setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
+ this.updateInterval = updateInterval;
+ if (this.autoUpdate === autoUpdate) return;
- this.autoUpdate = autoUpdate;
- if (autoUpdate) this._update();
+ this.autoUpdate = autoUpdate;
+ if (autoUpdate) this._update();
- return this;
- }
- async checkAlive() {
- const alive = await this.network.alive();
+ return this;
+ }
+ async checkAlive() {
+ const alive = await this.network.alive();
- if (alive !== this.alive) {
- const type = alive ? 'connect' : 'disconnect';
- this.dispatchEvent({ type });
- }
+ if (alive !== this.alive) {
+ const type = alive ? 'connect' : 'disconnect';
+ this.dispatchEvent({ type });
+ }
- this.alive = alive;
- return alive;
- }
- async sendGCode(gcode) {
- const printerState = await this.printer.state();
- if (printerState.state !== 'idle') {
- throw `Cannot print, print state is ${ printerState.state }`;
- }
+ this.alive = alive;
+ return alive;
+ }
+ async sendGCode(gcode) {
+ const printerState = await this.printer.state();
+ if (printerState.state !== 'idle') {
+ throw `Cannot print, print state is ${ printerState.state }`;
+ }
- if (!gcode.endsWith('\n')) {
- gcode += '\n';
- }
+ if (!gcode.endsWith('\n')) {
+ gcode += '\n';
+ }
- 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 lastIndex = 0;
+ let start = true;
+ while (lastIndex !== gcode.length) {
+ const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
+ const batch = gcode.substring(lastIndex, index);
- // const 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
- }
- }
- async _update() {
- while (this.autoUpdate) {
- if (this.alive) {
- try {
- this.state = await this.info.status();
+ start = false;
+ lastIndex = index + 1; //skip next \n
+ }
+ }
+ async _update() {
+ while (this.autoUpdate) {
+ if (this.alive) {
+ try {
+ this.state = await this.info.status();
- this.dispatchEvent({ type: 'update', state: this.state });
- } catch(error) {
- await this.checkAlive();
- }
- } else {
- await this.checkAlive();
- }
+ this.dispatchEvent({ type: 'update', state: this.state });
+ } catch(error) {
+ await this.checkAlive();
+ }
+ } else {
+ await this.checkAlive();
+ }
- await sleep(this.updateInterval);
- }
- }
- async _sendBatch(gcode, start) {
- try {
- const response = await this.printer.print(gcode, start, start);
- // maybe do something with failing response
+ await sleep(this.updateInterval);
+ }
+ }
+ 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);
- }
- }
+ await this._sendBatch(gcode, index);
+ }
+ }
}
diff --git a/src/doodle3dmanager.js b/src/doodle3dmanager.js
index af1dd89..fa82155 100644
--- a/src/doodle3dmanager.js
+++ b/src/doodle3dmanager.js
@@ -4,85 +4,85 @@ import EventDispatcher from 'casperlamboo/EventDispatcher';
import { sleep } from './utils.js';
export default class Doodle3DManager extends EventDispatcher {
- constructor() {
- super();
+ constructor() {
+ super();
- this.api = 'http://connect.doodle3d.com/api/';
+ this.api = 'http://connect.doodle3d.com/api/';
- this.boxes = [];
+ this.boxes = [];
- this.nonServerBoxes = [{
- wifiboxid: 'Wired Printer',
- localip: '192.168.5.1'
- }/*, {
- wifiboxid: 'Node JS Server',
- localip: '127.0.0.1:3000'
- }*/];
- this.checkNonServerBoxes = false;
+ this.nonServerBoxes = [{
+ wifiboxid: 'Wired Printer',
+ localip: '192.168.5.1'
+ }/*, {
+ wifiboxid: 'Node JS Server',
+ localip: '127.0.0.1:3000'
+ }*/];
+ this.checkNonServerBoxes = false;
- this.autoUpdate = false;
- }
- setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
- this.updateInterval = updateInterval;
- if (this.autoUpdate === autoUpdate) return;
+ this.autoUpdate = false;
+ }
+ setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
+ this.updateInterval = updateInterval;
+ if (this.autoUpdate === autoUpdate) return;
- this.autoUpdate = autoUpdate;
- if (autoUpdate) this._update();
+ this.autoUpdate = autoUpdate;
+ if (autoUpdate) this._update();
- return this;
- }
- async _update() {
- while (this.autoUpdate) {
- await this._checkNew();
+ return this;
+ }
+ async _update() {
+ while (this.autoUpdate) {
+ await this._checkNew();
- await sleep(this.updateInterval);
- }
- }
- async _checkNew() {
- let boxes;
- try {
- boxes = await rest.get(`${ this.api }list.php`);
- } catch(error) {
- console.warn('fail connecting to Doodle3D server');
- return;
- }
+ await sleep(this.updateInterval);
+ }
+ }
+ async _checkNew() {
+ let boxes;
+ try {
+ boxes = await rest.get(`${ this.api }list.php`);
+ } catch(error) {
+ console.warn('fail connecting to Doodle3D server');
+ return;
+ }
- if (this.checkNonServerBoxes) boxes = boxes.concat(this.nonServerBoxes);
+ if (this.checkNonServerBoxes) boxes = boxes.concat(this.nonServerBoxes);
- const knownIPsClient = this.boxes.map(box => box.boxData.localip);
- const knownIPsServer = boxes.map(box => box.localip);
+ const knownIPsClient = this.boxes.map(box => box.boxData.localip);
+ const knownIPsServer = boxes.map(box => box.localip);
- const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1);
- const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1);
+ const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1);
+ const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1);
- let changed = false;
- for (const boxData of newBoxes) {
- const box = new Doodle3DBox(boxData);
- this._addBox(box);
+ let changed = false;
+ for (const boxData of newBoxes) {
+ const box = new Doodle3DBox(boxData);
+ this._addBox(box);
- changed = true;
- }
+ changed = true;
+ }
- for (const box of removedBoxes) {
- this._removeBox(box);
+ for (const box of removedBoxes) {
+ this._removeBox(box);
- changed = true;
- }
+ changed = true;
+ }
- if (changed) {
- this.dispatchEvent({ type: 'boxeschanged', boxes: this.boxes });
- }
- }
- _addBox(box) {
- this.boxes.push(box);
+ if (changed) {
+ this.dispatchEvent({ type: 'boxeschanged', boxes: this.boxes });
+ }
+ }
+ _addBox(box) {
+ this.boxes.push(box);
- this.dispatchEvent({ type: 'boxappeared', box });
- }
- _removeBox(box) {
- const index = this.boxes.indexOf(box);
- if (index !== -1) {
- this.boxes.splice(index, 1);
- this.dispatchEvent({ type: 'boxdisappeared', box });
- }
- }
+ this.dispatchEvent({ type: 'boxappeared', box });
+ }
+ _removeBox(box) {
+ const index = this.boxes.indexOf(box);
+ if (index !== -1) {
+ this.boxes.splice(index, 1);
+ this.dispatchEvent({ type: 'boxdisappeared', box });
+ }
+ }
}
diff --git a/src/rest.js b/src/rest.js
index 6e7414c..a4685ff 100644
--- a/src/rest.js
+++ b/src/rest.js
@@ -4,29 +4,29 @@ const GET_TIMEOUT = 5000;
const POST_TIMEOUT = 10000;
export function get(url) {
- return send(url, 'GET');
+ return send(url, 'GET');
}
export function post(url, data) {
- return send(url, 'POST', data);
+ return send(url, 'POST', data);
}
function send(url, type, data) {
- const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
+ const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
- return new Promise((resolve, reject) => {
- $.ajax({
- url, type, data, timeout, dataType: 'json',
- success: (response) => {
- if (response.status === 'success') {
- resolve(response.data);
- }
- else {
- reject(response.msg);
- }
- }
- }).fail(reject);
- });
+ return new Promise((resolve, reject) => {
+ $.ajax({
+ url, type, data, timeout, dataType: 'json',
+ success: (response) => {
+ if (response.status === 'success') {
+ resolve(response.data);
+ }
+ else {
+ reject(response.msg);
+ }
+ }
+ }).fail(reject);
+ });
}
/*
@@ -34,51 +34,51 @@ import 'github/fetch';
export function get (url) {
- return new Promise((resolve, reject) => {
+ return new Promise((resolve, reject) => {
- fetch(url).then((response) => {
+ fetch(url).then((response) => {
- return response.json();
+ return response.json();
- }).then((json) => {
+ }).then((json) => {
- if (json.status === 'success') {
- resolve(json.data);
- }
- else {
- reject(json.msg);
- }
+ if (json.status === 'success') {
+ resolve(json.data);
+ }
+ else {
+ reject(json.msg);
+ }
- }).catch(reject);
+ }).catch(reject);
- });
+ });
}
export function post (url, data) {
- return new Promise((resolve, reject) => {
+ return new Promise((resolve, reject) => {
- fetch(url, {
- method: 'post',
- enctype: 'x-www-form-urlencoded',
- headers: {
- 'Accept': 'application/json'
- },
- body: JSON.stringify(data)
- }).then((response) => {
+ fetch(url, {
+ method: 'post',
+ enctype: 'x-www-form-urlencoded',
+ headers: {
+ 'Accept': 'application/json'
+ },
+ body: JSON.stringify(data)
+ }).then((response) => {
- return response.json();
+ return response.json();
- }).then((json) => {
+ }).then((json) => {
- if (json.status === 'success') {
- resolve(json.data);
- }
- else {
- reject(json.msg);
- }
+ if (json.status === 'success') {
+ resolve(json.data);
+ }
+ else {
+ reject(json.msg);
+ }
- }).catch(reject);
- });
+ }).catch(reject);
+ });
}
*/
diff --git a/src/utils.js b/src/utils.js
index 58add9e..b84fcde 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,5 +1,5 @@
export function sleep(time) {
- return new Promise((resolve, reject) => {
- setTimeout(resolve, time);
- });
+ return new Promise((resolve, reject) => {
+ setTimeout(resolve, time);
+ });
}