diff --git a/.gitignore b/.gitignore index b56c68b..53b0e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -jspm_packages/* \ No newline at end of file +jspm_packages/* +example/test.gcode + +build.js + +build.js.map diff --git a/README.md b/README.md index a73c584..1bf105f 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/config.js b/config.js deleted file mode 100644 index e32f34d..0000000 --- a/config.js +++ /dev/null @@ -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" - } - } -}); - diff --git a/example/app.js b/example/app.js index eb130bf..c90aeec 100644 --- a/example/app.js +++ b/example/app.js @@ -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); }); -*/ \ No newline at end of file +doodle3DManager.addEventListener('boxeschanged', ({ boxes }) => { + +}); diff --git a/example/index.html b/example/index.html index bedade4..f40af86 100644 --- a/example/index.html +++ b/example/index.html @@ -1,45 +1,14 @@ + Doodle3D Box -Doodle3D Box - - - - - - - - + + - - - - - - - - - - - - - - - - - -
IDLocal IPStateCurrent LineBuffered LinesTotal LinesHotendHotend TargetBedBed TargetHas Control
- + - \ No newline at end of file + diff --git a/jspm.config.js b/jspm.config.js new file mode 100644 index 0000000..22e105d --- /dev/null +++ b/jspm.config.js @@ -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: {} +}); diff --git a/package.json b/package.json index 2340d62..51e08e8 100644 --- a/package.json +++ b/package.json @@ -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" + } + } } } } diff --git a/src/api/config.js b/src/api/config.js new file mode 100644 index 0000000..8a9e010 --- /dev/null +++ b/src/api/config.js @@ -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); + } +} diff --git a/src/api/info.js b/src/api/info.js new file mode 100644 index 0000000..b59c1fa --- /dev/null +++ b/src/api/info.js @@ -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`); + } +} diff --git a/src/api/network.js b/src/api/network.js new file mode 100644 index 0000000..0348bc2 --- /dev/null +++ b/src/api/network.js @@ -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; + } + } +} diff --git a/src/api/printer.js b/src/api/printer.js new file mode 100644 index 0000000..1d1a53d --- /dev/null +++ b/src/api/printer.js @@ -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); + } +} diff --git a/src/api/sketch.js b/src/api/sketch.js new file mode 100644 index 0000000..8a0a08d --- /dev/null +++ b/src/api/sketch.js @@ -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`); + } +} diff --git a/src/api/system.js b/src/api/system.js new file mode 100644 index 0000000..e811393 --- /dev/null +++ b/src/api/system.js @@ -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`); + } +} diff --git a/src/api/update.js b/src/api/update.js new file mode 100644 index 0000000..729db7a --- /dev/null +++ b/src/api/update.js @@ -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`, {}); + } +} diff --git a/src/configapi.js b/src/configapi.js deleted file mode 100644 index b72ce03..0000000 --- a/src/configapi.js +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/doodle3dbox.js b/src/doodle3dbox.js new file mode 100644 index 0000000..dfb7da6 --- /dev/null +++ b/src/doodle3dbox.js @@ -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); + } + } +} diff --git a/src/doodle3dmanager.js b/src/doodle3dmanager.js new file mode 100644 index 0000000..46d0615 --- /dev/null +++ b/src/doodle3dmanager.js @@ -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 }); + } + } +} diff --git a/src/index.js b/src/index.js index 79232a7..ecefbad 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); - }); - } -} \ No newline at end of file +export { Doodle3DManager, Doodle3DBox }; diff --git a/src/infoapi.js b/src/infoapi.js deleted file mode 100644 index 80e7e27..0000000 --- a/src/infoapi.js +++ /dev/null @@ -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'); - } -} \ No newline at end of file diff --git a/src/networkapi.js b/src/networkapi.js deleted file mode 100644 index 0342a4a..0000000 --- a/src/networkapi.js +++ /dev/null @@ -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'); - } -} \ No newline at end of file diff --git a/src/printerapi.js b/src/printerapi.js deleted file mode 100644 index 11955cd..0000000 --- a/src/printerapi.js +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/rest.js b/src/rest.js new file mode 100644 index 0000000..a4685ff --- /dev/null +++ b/src/rest.js @@ -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); + }); +} +*/ diff --git a/src/restapi.js b/src/restapi.js deleted file mode 100644 index 250a366..0000000 --- a/src/restapi.js +++ /dev/null @@ -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); - }); -} \ No newline at end of file diff --git a/src/sketchapi.js b/src/sketchapi.js deleted file mode 100644 index c93a9c2..0000000 --- a/src/sketchapi.js +++ /dev/null @@ -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'); - } -} \ No newline at end of file diff --git a/src/systemapi.js b/src/systemapi.js deleted file mode 100644 index d011fba..0000000 --- a/src/systemapi.js +++ /dev/null @@ -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'); - } -} \ No newline at end of file diff --git a/src/updateapi.js b/src/updateapi.js deleted file mode 100644 index 7695fa5..0000000 --- a/src/updateapi.js +++ /dev/null @@ -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', {}); - } -} \ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..b84fcde --- /dev/null +++ b/src/utils.js @@ -0,0 +1,5 @@ +export function sleep(time) { + return new Promise((resolve, reject) => { + setTimeout(resolve, time); + }); +}