0
0
mirror of https://github.com/Doodle3D/Doodle3D-API synced 2024-12-22 05:43:48 +01:00

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
```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);
```

View File

@ -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 }) => {

View File

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

View File

@ -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);
}
}

View File

@ -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`);
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}

View File

@ -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`);
}
}

View File

@ -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`);
}
}

View File

@ -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`, {});
}
}

View File

@ -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);
}
}
}

View File

@ -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 });
}
}
}

View File

@ -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);
});
}
*/

View File

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