2016-04-21 15:32:11 +02:00
|
|
|
import * as rest from './rest.js';
|
2016-04-21 15:30:07 +02:00
|
|
|
import Doodle3DBox from './doodle3dbox.js';
|
2015-07-24 13:15:00 +02:00
|
|
|
import EventDispatcher from 'casperlamboo/EventDispatcher';
|
2016-04-21 15:09:49 +02:00
|
|
|
import { sleep } from './utils.js';
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2015-10-15 10:56:01 +02:00
|
|
|
export default class Doodle3DManager extends EventDispatcher {
|
2016-04-21 15:44:20 +02:00
|
|
|
constructor() {
|
2015-07-24 13:15:00 +02:00
|
|
|
super();
|
|
|
|
|
2015-10-12 12:33:33 +02:00
|
|
|
this.api = 'http://connect.doodle3d.com/api/';
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.boxes = [];
|
2015-10-12 12:33:33 +02:00
|
|
|
|
2015-07-27 16:32:11 +02:00
|
|
|
this.nonServerBoxes = [{
|
2016-04-16 18:05:51 +02:00
|
|
|
wifiboxid: 'Wired Printer',
|
2015-07-27 16:32:11 +02:00
|
|
|
localip: '192.168.5.1'
|
2015-10-12 12:33:33 +02:00
|
|
|
}/*, {
|
2016-04-16 18:05:51 +02:00
|
|
|
wifiboxid: 'Node JS Server',
|
2015-10-12 12:33:33 +02:00
|
|
|
localip: '127.0.0.1:3000'
|
|
|
|
}*/];
|
2016-04-21 15:02:50 +02:00
|
|
|
this.checkNonServerBoxes = false;
|
2015-10-12 12:33:33 +02:00
|
|
|
|
|
|
|
this.autoUpdate = false;
|
2015-07-24 13:15:00 +02:00
|
|
|
}
|
2016-04-21 15:44:20 +02:00
|
|
|
setAutoUpdate(autoUpdate = true, updateInterval = 1000) {
|
2015-10-12 12:33:33 +02:00
|
|
|
this.updateInterval = updateInterval;
|
2016-04-21 15:02:36 +02:00
|
|
|
if (this.autoUpdate === autoUpdate) return;
|
2015-10-12 12:33:33 +02:00
|
|
|
|
|
|
|
this.autoUpdate = autoUpdate;
|
2016-04-21 15:02:36 +02:00
|
|
|
if (autoUpdate) this._update();
|
2015-07-24 13:15:00 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2016-04-21 15:44:20 +02:00
|
|
|
async _update() {
|
2015-10-12 12:33:33 +02:00
|
|
|
while (this.autoUpdate) {
|
|
|
|
await this._checkNew();
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2015-10-12 12:33:33 +02:00
|
|
|
await sleep(this.updateInterval);
|
2015-07-27 16:32:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-21 15:44:20 +02:00
|
|
|
async _checkNew() {
|
2016-04-16 18:07:11 +02:00
|
|
|
let boxes;
|
|
|
|
try {
|
2016-04-21 15:09:49 +02:00
|
|
|
boxes = await rest.get(`${ this.api }list.php`);
|
2016-04-16 18:07:11 +02:00
|
|
|
} catch(error) {
|
2016-04-21 15:02:36 +02:00
|
|
|
console.warn('fail connecting to Doodle3D server');
|
|
|
|
return;
|
2016-04-16 18:07:11 +02:00
|
|
|
}
|
2015-07-27 16:32:11 +02:00
|
|
|
|
2016-04-21 15:02:36 +02:00
|
|
|
if (this.checkNonServerBoxes) boxes = boxes.concat(this.nonServerBoxes);
|
2015-07-27 17:13:23 +02:00
|
|
|
|
2016-04-21 15:02:36 +02:00
|
|
|
const knownIPsClient = this.boxes.map(box => box.boxData.localip);
|
|
|
|
const knownIPsServer = boxes.map(box => box.localip);
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2016-04-21 15:02:36 +02:00
|
|
|
const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1);
|
|
|
|
const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1);
|
|
|
|
|
2016-04-21 15:17:38 +02:00
|
|
|
let changed = false;
|
2016-04-21 15:02:36 +02:00
|
|
|
for (const boxData of newBoxes) {
|
2016-04-21 15:30:07 +02:00
|
|
|
const box = new Doodle3DBox(boxData);
|
2016-04-21 15:02:36 +02:00
|
|
|
this._addBox(box);
|
2016-04-21 15:17:38 +02:00
|
|
|
|
|
|
|
changed = true;
|
2016-04-21 15:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const box of removedBoxes) {
|
|
|
|
this._removeBox(box);
|
2016-04-21 15:17:38 +02:00
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
this.dispatchEvent({ type: 'boxeschanged', boxes: this.boxes });
|
2016-04-16 18:07:11 +02:00
|
|
|
}
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|
2016-04-21 15:44:20 +02:00
|
|
|
_addBox(box) {
|
2015-10-12 12:33:33 +02:00
|
|
|
this.boxes.push(box);
|
|
|
|
|
2016-04-16 18:05:15 +02:00
|
|
|
this.dispatchEvent({ type: 'boxappeared', box });
|
2015-07-24 13:15:00 +02:00
|
|
|
}
|
2016-04-21 15:44:20 +02:00
|
|
|
_removeBox(box) {
|
2016-04-16 18:06:36 +02:00
|
|
|
const index = this.boxes.indexOf(box);
|
2015-10-12 12:33:33 +02:00
|
|
|
if (index !== -1) {
|
|
|
|
this.boxes.splice(index, 1);
|
2016-04-16 18:05:15 +02:00
|
|
|
this.dispatchEvent({ type: 'boxdisappeared', box });
|
2015-10-12 12:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|