Merge branch 'develop'

This commit is contained in:
casperlamboo 2016-07-19 12:34:45 +02:00
commit 6474ffe90a
27 changed files with 592 additions and 598 deletions

7
.gitignore vendored
View File

@ -1,2 +1,7 @@
jspm_packages/*
jspm_packages/*
example/test.gcode
build.js
build.js.map

View File

@ -2,18 +2,24 @@
Doodle3D API for communication with the Doodle3D WiFi-Box
```javascript
import Doodle3DAPI from 'doodle3dapi';
import { Doodle3DManager } from 'Doodle3D/Doodle3D-API';
var localIP = "192.168.5.1";
var doodle3DAPI = new Doodle3DAPI(localIP);
doodle3DAPI.onconnect = function () {
const doodle3DManager = new Doodle3DManager();
};
doodle3DAPI.ondisconnect = function () {
doodle3DManager.addEventListener('boxappeared', ({box}) => {
box.addEventListener('connect', (event) => {
});
};
doodle3DAPI.onupdate = function (data) {
box.addEventListener('disconnect', (event) => {
});
box.addEventListener('update', (event) => {
const status = event.state;
});
box.setAutoUpdate(true, 1000);
});
doodle3DManager.setAutoUpdate(true, 1000);
};
doodle3DAPI.startUpdateLoop();
```

View File

@ -1,37 +0,0 @@
System.config({
"baseURL": "/",
"defaultJSExtensions": true,
"transpiler": "babel",
"babelOptions": {
"optional": [
"runtime"
]
},
"paths": {
"github:*": "jspm_packages/github/*",
"npm:*": "jspm_packages/npm/*"
}
});
System.config({
"map": {
"babel": "npm:babel-core@5.7.4",
"babel-runtime": "npm:babel-runtime@5.7.0",
"core-js": "npm:core-js@0.9.18",
"github/fetch": "github:github/fetch@0.9.0",
"traceur": "github:jmcriffey/bower-traceur@0.0.90",
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90",
"github:jspm/nodelibs-process@0.1.1": {
"process": "npm:process@0.10.1"
},
"npm:babel-runtime@5.7.0": {
"process": "github:jspm/nodelibs-process@0.1.1"
},
"npm:core-js@0.9.18": {
"fs": "github:jspm/nodelibs-fs@0.1.2",
"process": "github:jspm/nodelibs-process@0.1.1",
"systemjs-json": "github:systemjs/plugin-json@0.1.0"
}
}
});

View File

@ -1,106 +1,28 @@
import Doodle3DAPI from 'src/index.js';
import * as rest from 'src/restapi.js';
import { Doodle3DManager } from 'src/index.js';
var api = 'http://connect.doodle3d.com/api/';
const doodle3DManager = new Doodle3DManager();
doodle3DManager.setAutoUpdate(true, 1000);
var addBox = (function () {
var known = [];
doodle3DManager.addEventListener('boxappeared', ({ box }) => {
box.setAutoUpdate(true, 1000);
return function (boxData) {
if (known.indexOf(boxData.localip) === -1) {
known.push(boxData.localip);
box.addEventListener('connect', () => {
console.log('connect to box', box);
});
var row = document.createElement('tr');
row.style.color = 'gray';
box.addEventListener('disconnect', () => {
console.log('disonnect to box', box);
})
var id = document.createElement('td');
var state = document.createElement('td');
var localIP = document.createElement('td');
var bed = document.createElement('td');
var bedTarget = document.createElement('td');
var bufferedLines = document.createElement('td');
var currentLine = document.createElement('td');
var hasControl = document.createElement('td');
var hotend = document.createElement('td');
var hotendTarget = document.createElement('td');
var totalLines = document.createElement('td');
row.appendChild(id);
row.appendChild(localIP);
row.appendChild(state);
row.appendChild(currentLine);
row.appendChild(bufferedLines);
row.appendChild(totalLines);
row.appendChild(hotend);
row.appendChild(hotendTarget);
row.appendChild(bed);
row.appendChild(bedTarget);
row.appendChild(hasControl);
id.innerHTML = boxData.wifiboxid;
localIP.innerHTML = boxData.localip;
document.getElementById('table').appendChild(row);
var doodle3DAPI = new Doodle3DAPI(boxData.localip);
doodle3DAPI.onconnect = function () {
row.style.color = 'black';
};
doodle3DAPI.ondisconnect = function () {
row.style.color = 'gray';
};
doodle3DAPI.onupdate = function (data) {
state.innerHTML = data.state;
if (data.state !== 'disconnected' && data.state !== 'connecting' && data.state !== 'unknown') {
bed.innerHTML = data.bed;
bedTarget.innerHTML = data.bed_target;
bufferedLines.innerHTML = data.buffered_lines;
currentLine.innerHTML = data.current_line;
hasControl.innerHTML = data.has_control;
hotend.innerHTML = data.hotend;
hotendTarget.innerHTML = data.hotend_target;
state.innerHTML = data.state;
totalLines.innerHTML = data.total_lines;
}
else {
bed.innerHTML = '';
bedTarget.innerHTML = '';
bufferedLines.innerHTML = '';
currentLine.innerHTML = '';
hasControl.innerHTML = '';
hotend.innerHTML = '';
hotendTarget.innerHTML = '';
state.innerHTML = '';
totalLines.innerHTML = '';
}
};
doodle3DAPI.startUpdateLoop();
}
}
})();
function searchBoxes () {
rest.get(api + 'list.php').then((boxes) => {
for (var i = 0; i < boxes.length; i ++) {
var box = boxes[i];
addBox(box);
}
});
}
setInterval(searchBoxes, 5000);
searchBoxes();
/*
addBox({
localip: '127.0.0.1:3000',
wifiboxid: 'Node Server'
box.addEventListener('update', ({ state }) => {
console.log(state);
});
});
addBox({
localip: '192.168.5.1',
wifiboxid: 'Wired Printer'
doodle3DManager.addEventListener('boxdisappeared', ({ box }) => {
box.setAutoUpdate(false);
});
*/
doodle3DManager.addEventListener('boxeschanged', ({ boxes }) => {
});

View File

@ -1,45 +1,14 @@
<!DOCTYPE>
<html>
<head>
<title>Doodle3D Box</title>
<title>Doodle3D Box</title>
<style>
table, th, td {
border: 1px solid #dddddd;
font-size: 15px;
font-family: Verdana, Geneva, Tahoma, Arial, Helvetica, sans-serif;
line-height: 20px;
}
</style>
<script type="text/javascript" src="../jspm_packages/system.js"></script>
<script type="text/javascript" src="../config.js"></script>
<script type="text/javascript">
System.import('example/app.js');
</script>
<script type="text/javascript" src="../jspm_packages/system.js"></script>
<script type="text/javascript" src="../jspm.config.js"></script>
</head>
<table style="width:100%">
<tbody id="table">
<tr>
<th>ID</th>
<th>Local IP</th>
<th>State</th>
<th>Current Line</th>
<th>Buffered Lines</th>
<th>Total Lines</th>
<th>Hotend</th>
<th>Hotend Target</th>
<th>Bed</th>
<th>Bed Target</th>
<th>Has Control</th>
</tr>
</tbody>
</table>
<body>
<script type="text/javascript">
System.import('./app.js');
</script>
</body>
</html>
</html>

51
jspm.config.js Normal file
View File

@ -0,0 +1,51 @@
SystemJS.config({
paths: {
"github:": "jspm_packages/github/",
"npm:": "jspm_packages/npm/",
"Doodle3D-API/": "src/"
},
browserConfig: {
"baseURL": "/"
},
devConfig: {
"map": {
"babel-runtime": "npm:babel-runtime@5.8.38",
"core-js": "npm:core-js@1.2.7",
"traceur": "github:jmcriffey/bower-traceur@0.0.90",
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90",
"process": "github:jspm/nodelibs-process@0.2.0-alpha",
"fs": "github:jspm/nodelibs-fs@0.2.0-alpha",
"plugin-babel": "npm:systemjs-plugin-babel@0.0.12",
"path": "github:jspm/nodelibs-path@0.2.0-alpha"
}
},
transpiler: "plugin-babel",
babelOptions: {
"optional": [
"runtime"
],
"stage": 0
},
map: {
"babel": "npm:babel-core@5.7.4"
},
packages: {
"Doodle3D-API": {
"main": "index.js"
}
}
});
SystemJS.config({
packageConfigPaths: [
"npm:@*/*.json",
"npm:*.json",
"github:*/*.json"
],
map: {
"EventDispatcher": "github:mrdoob/eventdispatcher.js@1.0.0",
"github/fetch": "github:github/fetch@0.9.0",
"jquery": "github:components/jquery@2.2.4"
},
packages: {}
});

View File

@ -1,18 +1,54 @@
{
"jspm": {
"main": "./index.js",
"name": "Doodle3D-API",
"main": "index.js",
"directories": {
"lib": "src"
},
"dependencies": {
"github/fetch": "github:github/fetch@^0.9.0"
"EventDispatcher": "github:mrdoob/eventdispatcher.js@^1.0.0",
"github/fetch": "github:github/fetch@^0.9.0",
"jquery": "github:components/jquery@^2.1.4"
},
"devDependencies": {
"babel": "npm:babel-core@^5.1.13",
"babel-runtime": "npm:babel-runtime@^5.1.13",
"core-js": "npm:core-js@^0.9.4",
"core-js": "npm:core-js@^1.2.0",
"fs": "github:jspm/nodelibs-fs@^0.2.0-alpha",
"path": "github:jspm/nodelibs-path@^0.2.0-alpha",
"plugin-babel": "npm:systemjs-plugin-babel@^0.0.12",
"process": "github:jspm/nodelibs-process@^0.2.0-alpha",
"traceur": "github:jmcriffey/bower-traceur@0.0.90",
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90"
},
"overrides": {
"github:components/jquery@2.2.4": {
"main": "jquery"
},
"github:jmcriffey/bower-traceur-runtime@0.0.90": {
"format": "global",
"meta": {
"traceur-runtime.js": {
"exports": "$traceurRuntime",
"format": "global"
}
}
},
"github:jmcriffey/bower-traceur@0.0.90": {
"format": "global",
"meta": {
"traceur.js": {
"exports": "traceur",
"format": "global"
}
}
},
"npm:babel-runtime@5.8.38": {
"main": false,
"dependencies": {},
"optionalDependencies": {
"core-js": "^1.2.0"
}
}
}
}
}

16
src/api/config.js Normal file
View File

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

19
src/api/info.js Normal file
View File

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

46
src/api/network.js Normal file
View File

@ -0,0 +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 };
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;
}
}
}

32
src/api/printer.js Normal file
View File

@ -0,0 +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 };
return rest.post(`${ this.api }printer/print`, data);
}
stop(gcode = '') {
const data = { gcode };
return rest.post(`${ this.api }printer/stop`, data);
}
}

19
src/api/sketch.js Normal file
View File

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

10
src/api/system.js Normal file
View File

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

22
src/api/update.js Normal file
View File

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

View File

@ -1,22 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = 'http://' + localIP + '/d3dapi/';
}
get (keys) {
return rest.get(this.api + 'config/?' + keys.join('=&') + '=');
}
getAll () {
return rest.get(this.api + 'config/all');
}
set (data) {
var scope = this;
return rest.post(this.api + 'config', data);
}
}

109
src/doodle3dbox.js Normal file
View File

@ -0,0 +1,109 @@
import EventDispatcher from 'EventDispatcher';
import { sleep } from './utils.js';
import ConfigAPI from './api/config.js';
import InfoAPI from './api/info.js';
import NetworkAPI from './api/network.js';
import PrinterAPI from './api/printer.js';
import SketchAPI from './api/sketch.js';
import SystemAPI from './api/system.js';
import UpdateAPI from './api/update.js';
export default class Doodle3DBox extends EventDispatcher {
constructor(boxData) {
super();
this.boxData = boxData;
this.api = `http://${ boxData.localip }/d3dapi/`;
this.alive = false;
this.autoUpdate = false;
this.state = {};
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.autoUpdate = autoUpdate;
if (autoUpdate) this._update();
return this;
}
async checkAlive() {
const alive = await this.network.alive();
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 }`;
}
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);
// const progress = await this.printer.progress();
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();
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
console.log(`batch sent: ${ index }`);
} catch(error) {
console.log(`failed sending batch: ${ index }`);
await sleep(1000);
await this._sendBatch(gcode, index);
}
}
}

88
src/doodle3dmanager.js Normal file
View File

@ -0,0 +1,88 @@
import * as rest from './rest.js';
import Doodle3DBox from './doodle3dbox.js';
import EventDispatcher from 'EventDispatcher';
import { sleep } from './utils.js';
export default class Doodle3DManager extends EventDispatcher {
constructor() {
super();
this.api = 'http://connect.doodle3d.com/api/';
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.autoUpdate = false;
}
setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) return;
this.autoUpdate = autoUpdate;
if (autoUpdate) this._update();
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;
}
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 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);
changed = true;
}
for (const box of removedBoxes) {
this._removeBox(box);
changed = true;
}
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 });
}
}
}

View File

@ -1,158 +1,4 @@
import * as rest from './restapi.js';
import ConfigAPI from './configapi.js';
import InfoAPI from './infoapi.js';
import NetworkAPI from './networkapi.js';
import PrinterAPI from './printerapi.js';
import SketchAPI from './sketchapi.js';
import SystemAPI from './systemapi.js';
import UpdateAPI from './updateapi.js';
import Doodle3DManager from './doodle3dmanager.js';
import Doodle3DBox from './doodle3dbox.js';
export default class {
constructor (localIP) {
this.config = new ConfigAPI(localIP);
this.info = new InfoAPI(localIP);
this.network = new NetworkAPI(localIP);
this.printer = new PrinterAPI(localIP);
this.sketch = new SketchAPI(localIP);
this.system = new SystemAPI(localIP);
this.update = new UpdateAPI(localIP);
this.alive = false;
this.batchSize = 512;
this.maxBufferedLines = 4096;
this.state = {};
this._printBatches = [];
this._currentBatch = 0;
this.loaded = false;
}
startUpdateLoop () {
this.network.alive().then(() => {
this.alive = true;
if (this.onconnect !== undefined) {
this.onconnect();
}
if (!this.loaded) {
this.loaded = true;
}
this._updateState();
}).catch(() => {
if (this.alive) {
this.alive = false;
if (this.ondisconnect !== undefined) {
this.ondisconnect();
}
}
console.warn(error);
setTimeout(() => {
this.startUpdateLoop();
}, 1000);
});
return this;
}
print (gcode) {
this._currentBatch = 0;
gcode = gcode.split('\n');
//split gcode in batches
while (gcode.length > 0) {
var gcodeBatch = gcode.splice(0, Math.min(this.batchSize, gcode.length));
this._printBatches.push(gcodeBatch.join('\n'));
}
return this;
}
stopPrint (settings) {
this._printBatches = [];
this._currentBatch = 0;
this.printer.stop({
'gcode': settings.endCode()
}).then((data) => {
console.log('Printer stop command sent');
}).catch((error) => {
console.warn(error);
});
return this;
}
_updateLoop () {
if (this._printBatches.length > 0 && (this.state['buffered_lines'] + this._printBatches[0].length) <= this.maxBufferedLines) {
//if (this._printBatches.length > 0 ) {
this._printBatch();
}
else {
setTimeout(() => {
this._updateState();
}, 1000);
}
}
_updateState () {
//que api calls so they don't overload the d3d box
this.info.status().then((data) => {
this.state = data;
if (this.onupdate !== undefined) {
this.onupdate(data);
}
this._updateLoop();
}).catch((error) => {
console.warn(error);
this.startUpdateLoop();
});
}
_printBatch () {
var gcode = this._printBatches.shift();
this.printer.print({
'start': ((this._currentBatch === 0) ? true : false),
'first': ((this._currentBatch === 0) ? true : false),
'gcode': gcode,
'last': ((this._printBatches.length === 0) ? true : false) //only for debug purposes
}).then((data) => {
console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) {
this._currentBatch ++;
}
else {
console.log('Finish sending gcode to printer');
}
this._updateState();
}).catch((error) => {
this._printBatches.unshift(gcode);
console.warn(error);
this.startUpdateLoop();
});
}
}
export { Doodle3DManager, Doodle3DBox };

View File

@ -1,24 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = `http://${localIP}/d3dapi/`;
}
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,54 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = `http://${localIP}/d3dapi/`;
}
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 = null, recreate = false) {
var data = {ssid, recreate};
if (phrase) phrase = phrase;
return rest.post(this.api + 'network/associate', data);
}
disassosiate () {
//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');
}
alive () {
return rest.get(this.api + 'network/alive');
}
}

View File

@ -1,36 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = `http://${localIP}/d3dapi/`;
}
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 (data) {
return rest.post(this.api + 'printer/print', data);
}
stop (data) {
return rest.post(this.api + 'printer/stop', data);
}
}

84
src/rest.js Normal file
View File

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

View File

@ -1,50 +0,0 @@
import 'github/fetch';
export function get (url) {
return new Promise((resolve, reject) => {
fetch(url).then((response) => {
return response.json();
}).then((json) => {
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
}).catch(reject);
});
}
export function post (url, data) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then((response) => {
return response.json();
}).then((json) => {
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
}).catch(reject);
});
}

View File

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

View File

@ -1,12 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = `http://${localIP}/d3dapi/`;
}
versions () {
return rest.get(this.api + 'system/fwversions');
}
}

View File

@ -1,30 +0,0 @@
import * as rest from './restapi.js';
export default class {
constructor (localIP) {
this.localIP = localIP;
this.api = `http://${localIP}/d3dapi/`;
}
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', {});
}
}

5
src/utils.js Normal file
View File

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