replace tabs with space

This commit is contained in:
casperlamboo 2016-04-21 15:52:23 +02:00
parent 2186fe2428
commit 1f8bc2906e
14 changed files with 351 additions and 351 deletions

View File

@ -2,24 +2,24 @@
Doodle3D API for communication with the Doodle3D WiFi-Box Doodle3D API for communication with the Doodle3D WiFi-Box
```javascript ```javascript
import {Doodle3DManager} from 'Doodle3D/Doodle3D-API'; import { Doodle3DManager } from 'Doodle3D/Doodle3D-API';
const doodle3DManager = new Doodle3DManager(); const doodle3DManager = new Doodle3DManager();
doodle3DManager.addEventListener('boxappeared', ({box}) => { doodle3DManager.addEventListener('boxappeared', ({box}) => {
box.addEventListener('connect', (event) => { box.addEventListener('connect', (event) => {
}); });
box.addEventListener('disconnect', (event) => { box.addEventListener('disconnect', (event) => {
}); });
box.addEventListener('update', (event) => { box.addEventListener('update', (event) => {
let status = event.state; const status = event.state;
}); });
box.setAutoUpdate(true); box.setAutoUpdate(true, 1000);
}); });
doodle3DManager.setAutoUpdate(true); doodle3DManager.setAutoUpdate(true, 1000);
``` ```

View File

@ -4,23 +4,23 @@ const doodle3DManager = new Doodle3DManager();
doodle3DManager.setAutoUpdate(true, 1000); doodle3DManager.setAutoUpdate(true, 1000);
doodle3DManager.addEventListener('boxappeared', ({ box }) => { doodle3DManager.addEventListener('boxappeared', ({ box }) => {
box.setAutoUpdate(true, 1000); box.setAutoUpdate(true, 1000);
box.addEventListener('connect', () => { box.addEventListener('connect', () => {
console.log('connect to box', box); console.log('connect to box', box);
}); });
box.addEventListener('disconnect', () => { box.addEventListener('disconnect', () => {
console.log('disonnect to box', box); console.log('disonnect to box', box);
}) })
box.addEventListener('update', ({ state }) => { box.addEventListener('update', ({ state }) => {
console.log(state); console.log(state);
}); });
}); });
doodle3DManager.addEventListener('boxdisappeared', ({ box }) => { doodle3DManager.addEventListener('boxdisappeared', ({ box }) => {
box.setAutoUpdate(false); box.setAutoUpdate(false);
}); });
doodle3DManager.addEventListener('boxeschanged', ({ boxes }) => { doodle3DManager.addEventListener('boxeschanged', ({ boxes }) => {

View File

@ -1,14 +1,14 @@
<!DOCTYPE> <!DOCTYPE>
<html> <html>
<head> <head>
<title>Doodle3D Box</title> <title>Doodle3D Box</title>
<script type="text/javascript" src="../jspm_packages/system.js"></script> <script type="text/javascript" src="../jspm_packages/system.js"></script>
<script type="text/javascript" src="../config.js"></script> <script type="text/javascript" src="../config.js"></script>
</head> </head>
<body> <body>
<script type="text/javascript"> <script type="text/javascript">
System.import('./app.js'); System.import('./app.js');
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,16 +1,16 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Config { export default class Config {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
get(...keys) { get(...keys) {
return rest.get(`${ this.api }config/?${ keys.join('=&') }=`); return rest.get(`${ this.api }config/?${ keys.join('=&') }=`);
} }
getAll() { getAll() {
return rest.get(`${ this.api }config/all`); return rest.get(`${ this.api }config/all`);
} }
set(data) { set(data) {
return rest.post(`${ this.api }config`, data); return rest.post(`${ this.api }config`, data);
} }
} }

View File

@ -1,19 +1,19 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Info { export default class Info {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
get() { get() {
return rest.get(`${ this.api }info`); return rest.get(`${ this.api }info`);
} }
status() { status() {
return rest.get(`${ this.api }info/status`); return rest.get(`${ this.api }info/status`);
} }
downloadLogFiles() { downloadLogFiles() {
window.location = `${ this.api }info/logfiles`; window.location = `${ this.api }info/logfiles`;
} }
acces() { acces() {
return rest.get(`${ this.api }info/access`); return rest.get(`${ this.api }info/access`);
} }
} }

View File

@ -1,46 +1,46 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Network { export default class Network {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
scan() { scan() {
return rest.get(`${ this.api }network/scan`); return rest.get(`${ this.api }network/scan`);
} }
known() { known() {
return rest.get(`${ this.api }network/known`); return rest.get(`${ this.api }network/known`);
} }
status() { status() {
return rest.get(`${ this.api }network/status`); return rest.get(`${ this.api }network/status`);
} }
assosiate(ssid, phrase, recreate = false) { assosiate(ssid, phrase, recreate = false) {
const data = { ssid, recreate, phrase }; const data = { ssid, recreate, phrase };
return rest.post(`${ this.api }network/associate`, data); return rest.post(`${ this.api }network/associate`, data);
} }
disassociate() { disassociate() {
//not tested //not tested
return rest.post(`${ this.api }network/disassociate`, {}); return rest.post(`${ this.api }network/disassociate`, {});
} }
openAccesPoint() { openAccesPoint() {
//not tested //not tested
return rest.post(`${ this.api }network/openap`, {}); return rest.post(`${ this.api }network/openap`, {});
} }
remove(ssid) { remove(ssid) {
return rest.post(`${ this.api }network/remove`, { return rest.post(`${ this.api }network/remove`, {
'ssid': ssid 'ssid': ssid
}); });
} }
signin() { signin() {
return rest.get(`${ this.api }network/signin`); return rest.get(`${ this.api }network/signin`);
} }
async alive() { async alive() {
try { try {
await rest.get(`${ this.api }network/alive`); await rest.get(`${ this.api }network/alive`);
return true; return true;
} catch(error) { } catch(error) {
return false; return false;
} }
} }
} }

View File

@ -1,32 +1,32 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Printer { export default class Printer {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
temperature() { temperature() {
return rest.get(`${ this.api }printer/temperature`); return rest.get(`${ this.api }printer/temperature`);
} }
progress() { progress() {
return rest.get(`${ this.api }printer/progress`); return rest.get(`${ this.api }printer/progress`);
} }
state() { state() {
return rest.get(`${ this.api }printer/state`); return rest.get(`${ this.api }printer/state`);
} }
listAll() { listAll() {
return rest.get(`${ this.api }printer/listall`); return rest.get(`${ this.api }printer/listall`);
} }
heatup() { heatup() {
return rest.post(`${ this.api }printer/heatup`, {}); return rest.post(`${ this.api }printer/heatup`, {});
} }
print(gcode = '', first = false, start = false, last) { print(gcode = '', first = false, start = false, last) {
const data = { gcode, first, start, last }; const data = { gcode, first, start, last };
return rest.post(`${ this.api }printer/print`, data); return rest.post(`${ this.api }printer/print`, data);
} }
stop(gcode = '') { stop(gcode = '') {
const data = { gcode }; const data = { gcode };
return rest.post(`${ this.api }printer/stop`, data); return rest.post(`${ this.api }printer/stop`, data);
} }
} }

View File

@ -1,19 +1,19 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Sketch { export default class Sketch {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
getSketch(id) { getSketch(id) {
return rest.get(`${ this.api }sketch/?id=${ id }`); return rest.get(`${ this.api }sketch/?id=${ id }`);
} }
set(data = '') { set(data = '') {
return rest.post(`${ this.api }sketch`, { data }); return rest.post(`${ this.api }sketch`, { data });
} }
status() { status() {
return rest.get(`${ this.api }sketch/status`); return rest.get(`${ this.api }sketch/status`);
} }
clear() { clear() {
return rest.post(`${ this.api }sketch/clear`); return rest.post(`${ this.api }sketch/clear`);
} }
} }

View File

@ -1,10 +1,10 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class System { export default class System {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
versions() { versions() {
return rest.get(`${ this.api }system/fwversions`); return rest.get(`${ this.api }system/fwversions`);
} }
} }

View File

@ -1,22 +1,22 @@
import * as rest from '../rest.js'; import * as rest from '../rest.js';
export default class Update { export default class Update {
constructor(api) { constructor(api) {
this.api = api; this.api = api;
} }
status() { status() {
return rest.get(`${ this.api }update/status`); return rest.get(`${ this.api }update/status`);
} }
download() { download() {
//not tested //not tested
return rest.post(`${ this.api }update/download`, {}); return rest.post(`${ this.api }update/download`, {});
} }
install() { install() {
//not tested //not tested
return rest.post(`${ this.api }update/install`, {}); return rest.post(`${ this.api }update/install`, {});
} }
clear() { clear() {
//not tested //not tested
return rest.post(`${ this.api }update/clear`, {}); return rest.post(`${ this.api }update/clear`, {});
} }
} }

View File

@ -9,99 +9,99 @@ import SystemAPI from './api/system.js';
import UpdateAPI from './api/update.js'; import UpdateAPI from './api/update.js';
export default class Doodle3DBox extends EventDispatcher { export default class Doodle3DBox extends EventDispatcher {
constructor(boxData) { constructor(boxData) {
super(); super();
this.boxData = boxData; this.boxData = boxData;
this.api = `http://${ boxData.localip }/d3dapi/`; this.api = `http://${ boxData.localip }/d3dapi/`;
this.alive = false; this.alive = false;
this.autoUpdate = false; this.autoUpdate = false;
this.state = {}; this.state = {};
this.maxBatchSize = 10 * 1024; this.maxBatchSize = 10 * 1024;
this.fullBufferTimeout = 10000; this.fullBufferTimeout = 10000;
this.config = new ConfigAPI(this.api); this.config = new ConfigAPI(this.api);
this.info = new InfoAPI(this.api); this.info = new InfoAPI(this.api);
this.network = new NetworkAPI(this.api); this.network = new NetworkAPI(this.api);
this.printer = new PrinterAPI(this.api); this.printer = new PrinterAPI(this.api);
this.sketch = new SketchAPI(this.api); this.sketch = new SketchAPI(this.api);
this.system = new SystemAPI(this.api); this.system = new SystemAPI(this.api);
this.update = new UpdateAPI(this.api); this.update = new UpdateAPI(this.api);
} }
setAutoUpdate(autoUpdate = true, updateInterval = 1000) { setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval; this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) return; if (this.autoUpdate === autoUpdate) return;
this.autoUpdate = autoUpdate; this.autoUpdate = autoUpdate;
if (autoUpdate) this._update(); if (autoUpdate) this._update();
return this; return this;
} }
async checkAlive() { async checkAlive() {
const alive = await this.network.alive(); const alive = await this.network.alive();
if (alive !== this.alive) { if (alive !== this.alive) {
const type = alive ? 'connect' : 'disconnect'; const type = alive ? 'connect' : 'disconnect';
this.dispatchEvent({ type }); this.dispatchEvent({ type });
} }
this.alive = alive; this.alive = alive;
return alive; return alive;
} }
async sendGCode(gcode) { async sendGCode(gcode) {
const printerState = await this.printer.state(); const printerState = await this.printer.state();
if (printerState.state !== 'idle') { if (printerState.state !== 'idle') {
throw `Cannot print, print state is ${ printerState.state }`; throw `Cannot print, print state is ${ printerState.state }`;
} }
if (!gcode.endsWith('\n')) { if (!gcode.endsWith('\n')) {
gcode += '\n'; gcode += '\n';
} }
let lastIndex = 0; let lastIndex = 0;
let start = true; let start = true;
while (lastIndex !== gcode.length) { while (lastIndex !== gcode.length) {
const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize); const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
const batch = gcode.substring(lastIndex, index); 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; start = false;
lastIndex = index + 1; //skip next \n lastIndex = index + 1; //skip next \n
} }
} }
async _update() { async _update() {
while (this.autoUpdate) { while (this.autoUpdate) {
if (this.alive) { if (this.alive) {
try { try {
this.state = await this.info.status(); this.state = await this.info.status();
this.dispatchEvent({ type: 'update', state: this.state }); this.dispatchEvent({ type: 'update', state: this.state });
} catch(error) { } catch(error) {
await this.checkAlive(); await this.checkAlive();
} }
} else { } else {
await this.checkAlive(); await this.checkAlive();
} }
await sleep(this.updateInterval); await sleep(this.updateInterval);
} }
} }
async _sendBatch(gcode, start) { async _sendBatch(gcode, start) {
try { try {
const response = await this.printer.print(gcode, start, start); const 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);
} }
} }
} }

View File

@ -4,85 +4,85 @@ import EventDispatcher from 'casperlamboo/EventDispatcher';
import { sleep } from './utils.js'; import { sleep } from './utils.js';
export default class Doodle3DManager extends EventDispatcher { export default class Doodle3DManager extends EventDispatcher {
constructor() { constructor() {
super(); super();
this.api = 'http://connect.doodle3d.com/api/'; this.api = 'http://connect.doodle3d.com/api/';
this.boxes = []; this.boxes = [];
this.nonServerBoxes = [{ this.nonServerBoxes = [{
wifiboxid: 'Wired Printer', wifiboxid: 'Wired Printer',
localip: '192.168.5.1' localip: '192.168.5.1'
}/*, { }/*, {
wifiboxid: 'Node JS Server', wifiboxid: 'Node JS Server',
localip: '127.0.0.1:3000' localip: '127.0.0.1:3000'
}*/]; }*/];
this.checkNonServerBoxes = false; this.checkNonServerBoxes = false;
this.autoUpdate = false; this.autoUpdate = false;
} }
setAutoUpdate(autoUpdate = true, updateInterval = 1000) { setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval; this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) return; if (this.autoUpdate === autoUpdate) return;
this.autoUpdate = autoUpdate; this.autoUpdate = autoUpdate;
if (autoUpdate) this._update(); if (autoUpdate) this._update();
return this; return this;
} }
async _update() { async _update() {
while (this.autoUpdate) { while (this.autoUpdate) {
await this._checkNew(); await this._checkNew();
await sleep(this.updateInterval); await sleep(this.updateInterval);
} }
} }
async _checkNew() { async _checkNew() {
let boxes; let boxes;
try { try {
boxes = await rest.get(`${ this.api }list.php`); boxes = await rest.get(`${ this.api }list.php`);
} catch(error) { } catch(error) {
console.warn('fail connecting to Doodle3D server'); console.warn('fail connecting to Doodle3D server');
return; 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 knownIPsClient = this.boxes.map(box => box.boxData.localip);
const knownIPsServer = boxes.map(box => box.localip); const knownIPsServer = boxes.map(box => box.localip);
const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1); const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1);
const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1); const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1);
let changed = false; let changed = false;
for (const boxData of newBoxes) { for (const boxData of newBoxes) {
const box = new Doodle3DBox(boxData); const box = new Doodle3DBox(boxData);
this._addBox(box); this._addBox(box);
changed = true; changed = true;
} }
for (const box of removedBoxes) { for (const box of removedBoxes) {
this._removeBox(box); this._removeBox(box);
changed = true; changed = true;
} }
if (changed) { if (changed) {
this.dispatchEvent({ type: 'boxeschanged', boxes: this.boxes }); this.dispatchEvent({ type: 'boxeschanged', boxes: this.boxes });
} }
} }
_addBox(box) { _addBox(box) {
this.boxes.push(box); this.boxes.push(box);
this.dispatchEvent({ type: 'boxappeared', box }); this.dispatchEvent({ type: 'boxappeared', box });
} }
_removeBox(box) { _removeBox(box) {
const index = this.boxes.indexOf(box); const index = this.boxes.indexOf(box);
if (index !== -1) { if (index !== -1) {
this.boxes.splice(index, 1); this.boxes.splice(index, 1);
this.dispatchEvent({ type: 'boxdisappeared', box }); this.dispatchEvent({ type: 'boxdisappeared', box });
} }
} }
} }

View File

@ -4,29 +4,29 @@ const GET_TIMEOUT = 5000;
const POST_TIMEOUT = 10000; const POST_TIMEOUT = 10000;
export function get(url) { export function get(url) {
return send(url, 'GET'); return send(url, 'GET');
} }
export function post(url, data) { export function post(url, data) {
return send(url, 'POST', data); return send(url, 'POST', data);
} }
function send(url, type, 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) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
url, type, data, timeout, dataType: 'json', url, type, data, timeout, dataType: 'json',
success: (response) => { success: (response) => {
if (response.status === 'success') { if (response.status === 'success') {
resolve(response.data); resolve(response.data);
} }
else { else {
reject(response.msg); reject(response.msg);
} }
} }
}).fail(reject); }).fail(reject);
}); });
} }
/* /*
@ -34,51 +34,51 @@ import 'github/fetch';
export function get (url) { 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') { if (json.status === 'success') {
resolve(json.data); resolve(json.data);
} }
else { else {
reject(json.msg); reject(json.msg);
} }
}).catch(reject); }).catch(reject);
}); });
} }
export function post (url, data) { export function post (url, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch(url, { fetch(url, {
method: 'post', method: 'post',
enctype: 'x-www-form-urlencoded', enctype: 'x-www-form-urlencoded',
headers: { headers: {
'Accept': 'application/json' 'Accept': 'application/json'
}, },
body: JSON.stringify(data) body: JSON.stringify(data)
}).then((response) => { }).then((response) => {
return response.json(); return response.json();
}).then((json) => { }).then((json) => {
if (json.status === 'success') { if (json.status === 'success') {
resolve(json.data); resolve(json.data);
} }
else { else {
reject(json.msg); reject(json.msg);
} }
}).catch(reject); }).catch(reject);
}); });
} }
*/ */

View File

@ -1,5 +1,5 @@
export function sleep(time) { export function sleep(time) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(resolve, time); setTimeout(resolve, time);
}); });
} }