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'; import Doodle3DManager from 'src/doodle3dmanager.js';
var list = document.getElementById('list');
var doodle3DManager = new Doodle3DManager(); var doodle3DManager = new Doodle3DManager();
doodle3DManager.addEventListener('boxappeared', (event) => { doodle3DManager.addEventListener('boxappeared', (event) => {
var box = event.box; var box = event.box;
var row = document.createElement('tr'); var node = document.createElement('li');
row.style.color = 'gray'; node.innerHTML = box.boxData.wifiboxid;
list.appendChild(node);
});
var id = document.createElement('td'); doodle3DManager.addEventListener('boxdisappeared', (event) => {
var state = document.createElement('td'); var box = event.box;
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); for (var node of list.children) {
row.appendChild(localIP); if (node.innerHTML === box.boxData.wifiboxid) {
row.appendChild(state); list.removeChild(node);
row.appendChild(currentLine); break;
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;
} }
else { }
bed.innerHTML = '';
bedTarget.innerHTML = '';
bufferedLines.innerHTML = '';
currentLine.innerHTML = '';
hasControl.innerHTML = '';
hotend.innerHTML = '';
hotendTarget.innerHTML = '';
totalLines.innerHTML = '';
}
});
box.setAutoUpdate(true);
}); });
doodle3DManager.setAutoUpdate(true); doodle3DManager.setAutoUpdate(true);

View File

@ -4,15 +4,6 @@
<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="../jspm_packages/system.js"></script>
<script type="text/javascript" src="../config.js"></script> <script type="text/javascript" src="../config.js"></script>
@ -23,23 +14,9 @@
</head> </head>
<body> <body>
<table style="width:100%"> <p>Doodle3D WiFi-Boxes</p>
<tbody id="table">
<tr> <ul id='list'></ul>
<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> </body>
</html> </html>

View File

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

View File

@ -7,19 +7,26 @@ export default class extends EventDispatcher {
super(); super();
this.boxes = []; 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/'; this.api = 'http://connect.doodle3d.com/api/';
} }
setAutoUpdate (autoUpdate = true, rate = 5000) { setAutoUpdate (autoUpdate = true, rate = 5000) {
if (autoUpdate) { if (autoUpdate) {
this._checkNew(); this._update();
if (this.interval !== undefined) { if (this.interval !== undefined) {
clearInterval(this.interval); clearInterval(this.interval);
} }
this.interval = setInterval(() => { this.interval = setInterval(() => {
this._checkNew(); this._update();
}, rate); }, rate);
} }
else if (this.interval !== undefined) { else if (this.interval !== undefined) {
@ -30,28 +37,69 @@ export default class extends EventDispatcher {
return this; return this;
} }
addBox (boxData) { _addBox (box) {
var box = new Doodle3DAPI(boxData);
this.boxes.push(box); this.boxes.push(box);
this.dispatchEvent({ this.dispatchEvent({
type: 'boxappeared', 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 () { _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); var knownIPs = this.boxes.map((box) => box.boxData.localip);
for (var boxData of boxes) { for (var boxData of boxes) {
if (knownIPs.indexOf(boxData.localip) === -1) { 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._currentBatch = 0;
this.maxBatchSize = 10*1024; this.maxBatchSize = 10*1024;
this.maxBufferedLines = 1024*1024; //yet to be implimented this.maxBufferedLines = 1024*1024;
this.fullBufferTimeout = 10000;
} }
temperature () { temperature () {
@ -56,20 +57,35 @@ export default class {
var first = (this._currentBatch === 0) ? true : false; var first = (this._currentBatch === 0) ? true : false;
var last = (this._printBatches.length === 0) ? true : false; //only for the node js server 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); console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) { var progressRequest = this.progress()
this._currentBatch ++; progressRequest.then((progress) => {
_sendBatch();
} if (this._printBatches.length > 0) {
else { if (progress['buffered_lines'] + this.maxBatchSize < this.maxBufferedLines) {
console.log('Finish sending gcode to printer'); this._currentBatch ++;
} _sendBatch();
}).catch((error) => { }
else {
setTimeout(() => {
this._sendBatch();
}, this.fullBufferTimeout);
}
}
else {
console.log('Finish sending gcode to printer');
}
});
});
printRequest.catch((error) => {
this._printBatches.unshift(gcode); this._printBatches.unshift(gcode);
setTimeout(() => { setTimeout(() => {
this._sendBatch(); this._sendBatch();
}, 1000); }, 1000);

View File

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