2015-07-24 13:15:00 +02:00
|
|
|
import * as rest from './restapi.js';
|
|
|
|
import Doodle3DAPI from './doodle3dapi.js';
|
|
|
|
import EventDispatcher from 'casperlamboo/EventDispatcher';
|
|
|
|
|
|
|
|
export default class extends EventDispatcher {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.boxes = [];
|
2015-07-27 16:32:11 +02:00
|
|
|
this.nonServerBoxes = [{
|
|
|
|
wifiboxid: 'Wired Printer',
|
|
|
|
localip: '192.168.5.1'
|
|
|
|
}, {
|
|
|
|
wifiboxid: 'Node JS Server',
|
|
|
|
localip: '127.0.0.1:2000'
|
|
|
|
}];
|
2015-07-27 17:13:23 +02:00
|
|
|
this.checkNonServerBoxes = true;
|
2015-07-24 13:15:00 +02:00
|
|
|
this.api = 'http://connect.doodle3d.com/api/';
|
|
|
|
}
|
|
|
|
|
|
|
|
setAutoUpdate (autoUpdate = true, rate = 5000) {
|
|
|
|
if (autoUpdate) {
|
2015-07-27 16:32:11 +02:00
|
|
|
this._update();
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2015-07-24 17:37:01 +02:00
|
|
|
if (this.interval !== undefined) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
this.interval = setInterval(() => {
|
2015-07-27 16:32:11 +02:00
|
|
|
this._update();
|
2015-07-24 13:15:00 +02:00
|
|
|
}, rate);
|
|
|
|
}
|
|
|
|
else if (this.interval !== undefined) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
delete this.interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:32:11 +02:00
|
|
|
_addBox (box) {
|
2015-07-24 13:15:00 +02:00
|
|
|
this.boxes.push(box);
|
|
|
|
|
|
|
|
this.dispatchEvent({
|
|
|
|
type: 'boxappeared',
|
2015-07-27 16:32:11 +02:00
|
|
|
box
|
2015-07-24 13:15:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:32:11 +02:00
|
|
|
_removeBox (box) {
|
|
|
|
var index = this.boxes.indexOf(box);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.boxes.splice(index, 1);
|
|
|
|
this.dispatchEvent({
|
|
|
|
type: 'boxdisappeared',
|
|
|
|
box
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_update () {
|
|
|
|
this._checkAlive();
|
|
|
|
this._checkNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkAlive () {
|
|
|
|
for (var box of this.boxes) {
|
|
|
|
((box) => {
|
|
|
|
var request = box.network.alive();
|
|
|
|
request.catch(() => {
|
|
|
|
this._removeBox(box);
|
|
|
|
});
|
|
|
|
})(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 13:15:00 +02:00
|
|
|
_checkNew () {
|
2015-07-27 16:32:11 +02:00
|
|
|
|
|
|
|
var request = rest.get(this.api + 'list.php');
|
|
|
|
request.then((boxes) => {
|
|
|
|
|
2015-07-27 17:13:23 +02:00
|
|
|
if (this.checkNonServerBoxes) {
|
|
|
|
boxes = boxes.concat(this.nonServerBoxes);
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:37:01 +02:00
|
|
|
var knownIPs = this.boxes.map((box) => box.boxData.localip);
|
2015-07-24 13:15:00 +02:00
|
|
|
|
2015-07-24 17:37:01 +02:00
|
|
|
for (var boxData of boxes) {
|
2015-07-24 13:15:00 +02:00
|
|
|
if (knownIPs.indexOf(boxData.localip) === -1) {
|
2015-07-27 16:32:11 +02:00
|
|
|
var box = new Doodle3DAPI(boxData);
|
|
|
|
((box) => {
|
|
|
|
var request = box.network.alive();
|
|
|
|
request.then((data, msg) => {
|
|
|
|
this._addBox(box);
|
|
|
|
});
|
|
|
|
request.catch(() => {
|
|
|
|
console.log(`failed to connect with ${box.boxData.wifiboxid}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
})(box);
|
2015-07-24 13:15:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 16:32:11 +02:00
|
|
|
});
|
|
|
|
request.catch(() => {
|
|
|
|
console.warn('fail connecting to Doodle3D server');
|
2015-07-24 13:15:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|