added disconnect logic to doodle3dmanager

fixed https://github.com/Doodle3D/Doodle3D-API/issues/9
This commit is contained in:
casperlamboo 2015-07-27 16:32:11 +02:00
parent 65495c0491
commit b686f36856
6 changed files with 107 additions and 119 deletions

View File

@ -1,83 +1,26 @@
import Doodle3DManager from 'src/doodle3dmanager.js';
var list = document.getElementById('list');
var doodle3DManager = new Doodle3DManager();
doodle3DManager.addEventListener('boxappeared', (event) => {
var box = event.box;
var row = document.createElement('tr');
row.style.color = 'gray';
var node = document.createElement('li');
node.innerHTML = box.boxData.wifiboxid;
list.appendChild(node);
});
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');
doodle3DManager.addEventListener('boxdisappeared', (event) => {
var box = event.box;
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 = box.boxData.wifiboxid;
localIP.innerHTML = box.boxData.localip;
document.getElementById('table').appendChild(row);
box.addEventListener('connect', (event) => {
row.style.color = 'black';
console.log('connect');
box.printer.sendGCode("G1 X100 X100");
});
box.addEventListener('disconnect', (event) => {
row.style.color = 'gray';
console.log('disconnect');
});
box.addEventListener('update', (event) => {
var status = event.state;
state.innerHTML = status.state;
if (status.state !== 'disconnected' && status.state !== 'connecting' && status.state !== 'unknown') {
bed.innerHTML = status.bed;
bedTarget.innerHTML = status.bed_target;
bufferedLines.innerHTML = status.buffered_lines;
currentLine.innerHTML = status.current_line;
hasControl.innerHTML = status.has_control;
hotend.innerHTML = status.hotend;
hotendTarget.innerHTML = status.hotend_target;
totalLines.innerHTML = status.total_lines;
for (var node of list.children) {
if (node.innerHTML === box.boxData.wifiboxid) {
list.removeChild(node);
break;
}
else {
bed.innerHTML = '';
bedTarget.innerHTML = '';
bufferedLines.innerHTML = '';
currentLine.innerHTML = '';
hasControl.innerHTML = '';
hotend.innerHTML = '';
hotendTarget.innerHTML = '';
totalLines.innerHTML = '';
}
});
box.setAutoUpdate(true);
}
});
doodle3DManager.setAutoUpdate(true);

View File

@ -4,15 +4,6 @@
<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>
@ -23,23 +14,9 @@
</head>
<body>
<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>
<p>Doodle3D WiFi-Boxes</p>
<ul id='list'></ul>
</body>
</html>

View File

@ -49,7 +49,9 @@ export default class extends EventDispatcher {
_initLoop () {
this.network.alive().then(() => {
var request = this.network.alive();
request.then(() => {
this.alive = true;
@ -59,7 +61,9 @@ export default class extends EventDispatcher {
this._updateStateLoop();
}).catch((error) => {
});
request.catch((error) => {
if (this.alive) {

View File

@ -7,19 +7,26 @@ export default class extends EventDispatcher {
super();
this.boxes = [];
this.nonServerBoxes = [{
wifiboxid: 'Wired Printer',
localip: '192.168.5.1'
}, {
wifiboxid: 'Node JS Server',
localip: '127.0.0.1:2000'
}];
this.api = 'http://connect.doodle3d.com/api/';
}
setAutoUpdate (autoUpdate = true, rate = 5000) {
if (autoUpdate) {
this._checkNew();
this._update();
if (this.interval !== undefined) {
clearInterval(this.interval);
}
this.interval = setInterval(() => {
this._checkNew();
this._update();
}, rate);
}
else if (this.interval !== undefined) {
@ -30,28 +37,69 @@ export default class extends EventDispatcher {
return this;
}
addBox (boxData) {
var box = new Doodle3DAPI(boxData);
_addBox (box) {
this.boxes.push(box);
this.dispatchEvent({
type: 'boxappeared',
box: box
box
});
}
_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);
}
}
_checkNew () {
rest.get(this.api + 'list.php').then((boxes) => {
var request = rest.get(this.api + 'list.php');
request.then((boxes) => {
boxes = boxes.concat(this.nonServerBoxes);
var knownIPs = this.boxes.map((box) => box.boxData.localip);
for (var boxData of boxes) {
if (knownIPs.indexOf(boxData.localip) === -1) {
this.addBox(boxData);
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);
}
}
});
request.catch(() => {
console.warn('fail connecting to Doodle3D server');
});
}
}

View File

@ -8,7 +8,8 @@ export default class {
this._currentBatch = 0;
this.maxBatchSize = 10*1024;
this.maxBufferedLines = 1024*1024; //yet to be implimented
this.maxBufferedLines = 1024*1024;
this.fullBufferTimeout = 10000;
}
temperature () {
@ -56,20 +57,35 @@ export default class {
var first = (this._currentBatch === 0) ? true : false;
var last = (this._printBatches.length === 0) ? true : false; //only for the node js server
this.print(gcode, start, first, last).then((data) => {
var printRequest = this.print(gcode, start, first, last);
printRequest.then((data) => {
console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) {
this._currentBatch ++;
_sendBatch();
}
else {
console.log('Finish sending gcode to printer');
}
}).catch((error) => {
var progressRequest = this.progress()
progressRequest.then((progress) => {
if (this._printBatches.length > 0) {
if (progress['buffered_lines'] + this.maxBatchSize < this.maxBufferedLines) {
this._currentBatch ++;
_sendBatch();
}
else {
setTimeout(() => {
this._sendBatch();
}, this.fullBufferTimeout);
}
}
else {
console.log('Finish sending gcode to printer');
}
});
});
printRequest.catch((error) => {
this._printBatches.unshift(gcode);
setTimeout(() => {
this._sendBatch();
}, 1000);

View File

@ -11,7 +11,7 @@ export function get (url) {
success: (response) => {
if (response.status === 'success') {
resolve(response.data);
resolve(response.data, response.msg);
}
else {
reject(response.msg);