Doodle3D-Slicer/src/box.js

400 lines
8.6 KiB
JavaScript
Raw Normal View History

2015-04-24 16:12:48 +02:00
/******************************************************
*
* Box
* Representation of de Doodle3DBox
* Handles all communication with the doodle box
* JavaScript shell for api communication
* Check http://www.doodle3d.com/help/api-documentation
2015-04-24 16:12:48 +02:00
*
******************************************************/
//TODO
//Als meerdere clients met box zouden verbinden zal de api te veel requests krijgen waardoor hij crasht
//implimentatie van het veranderen van onder andere naam, netwerkverbinding etc
D3D.Box = function (localIp) {
"use strict";
var self = this;
this.batchSize = 1024 * 10; //10kb
this.maxBufferSize = 1024 * 1024 * 2; //2mb
this.bytesSend = 0;
2015-04-24 16:12:48 +02:00
this.localIp = localIp;
this.api = "http://" + localIp + "/d3dapi/";
2015-04-30 18:26:34 +02:00
this.config = {};
this.status = {};
2015-04-30 18:26:34 +02:00
2015-04-24 16:12:48 +02:00
this.printBatches = [];
this.currentBatch = 0;
this.loaded = false;
2015-05-07 14:09:36 +02:00
this.getConfigAll(function (data) {
self.updateConfig(data);
2015-04-24 16:12:48 +02:00
self.printer = new D3D.Printer(data);
self.update();
self.loaded = true;
if (self.onload !== undefined) {
self.onload();
}
});
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.updateConfig = function (config) {
"use strict";
for (var i in config) {
if (i.indexOf("doodle3d") === 0) {
this.config[i] = config[i];
}
}
return this;
};
2015-04-24 16:12:48 +02:00
D3D.Box.prototype.update = function () {
"use strict";
//TODO
//Code is zo op gezet dat maar api call te gelijk is
//Bij error wordt gelijk zelfde data opnieuw gestuurd
//Als DoodleBox ontkoppeld wordt komt er een error in de loop waardoor pagina breekt en ververst moet worden
if (this.printBatches.length > 0 && (this.status["buffered_lines"]*30 + this.batchSize) <= this.maxBufferSize) {
//if (this.printBatches.length > 0 ) {
2015-04-24 16:12:48 +02:00
this.printBatch();
}
else {
this.updateState();
}
};
D3D.Box.prototype.updateState = function () {
2015-05-07 14:09:36 +02:00
//que api calls so they don't overload the d3d box
2015-04-24 16:12:48 +02:00
"use strict";
var self = this;
2015-05-07 14:09:36 +02:00
this.getInfoStatus(function (data) {
self.status = data;
2015-04-24 16:12:48 +02:00
2015-05-07 14:09:36 +02:00
if (self.onupdate !== undefined) {
self.onupdate(data);
}
2015-04-28 16:08:56 +02:00
self.update();
2015-04-24 16:12:48 +02:00
});
};
D3D.Box.prototype.print = function (gcode) {
"use strict";
this.currentBatch = 0;
//gcode split in batches
for (var i = 0; i < gcode.length; i += this.batchSize) {
var gcodeBatch = gcode.substring(i, Math.min(i + this.batchSize, gcode.length));
2015-04-24 16:12:48 +02:00
this.printBatches.push(gcodeBatch);
}
2015-05-06 15:06:04 +02:00
return this;
2015-04-24 16:12:48 +02:00
};
D3D.Box.prototype.printBatch = function () {
"use strict";
var self = this;
var gcode = this.printBatches.shift();
2015-05-07 14:09:36 +02:00
this.setPrinterPrint({
"start": ((this.currentBatch === 0) ? true : false),
"first": ((this.currentBatch === 0) ? true : false),
"gcode": gcode
2015-04-24 16:12:48 +02:00
}, function (data) {
console.log("batch sent: " + self.currentBatch, data);
2015-04-24 16:12:48 +02:00
if (self.printBatches.length > 0) {
//sent new batch
self.currentBatch ++;
}
else {
2015-05-07 14:09:36 +02:00
//finish sending
2015-04-24 16:12:48 +02:00
}
self.updateState();
});
};
D3D.Box.prototype.stopPrint = function () {
2015-04-24 16:12:48 +02:00
"use strict";
this.printBatches = [];
this.currentBatch = 0;
var finishMove = [
"M107 ;fan off",
"G91 ;relative positioning",
"G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure",
"G1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more",
"G28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way",
"M84 ;disable axes / steppers",
"G90 ;absolute positioning",
"M104 S180",
";M140 S70",
"M117 Done ;display message (20 characters to clear whole screen)"
];
2015-05-07 14:09:36 +02:00
this.setPrinterStop({
//"gcode": {}
2015-04-24 16:12:48 +02:00
"gcode": finishMove.join("\n")
}, function (data) {
console.log("Printer stop command sent");
2015-04-24 16:12:48 +02:00
});
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
//COMMUNICATION SHELL
//see http://www.doodle3d.com/help/api-documentation
D3D.Box.prototype.getConfig = function (keys, callback) {
"use strict";
getAPI(this.api + "config/?" + keys.join("=&") + "=", callback);
return this;
2015-05-07 14:09:36 +02:00
};
D3D.Box.prototype.getConfigAll = function (callback) {
"use strict";
getAPI(this.api + "config/all", callback);
return this;
2015-05-07 14:09:36 +02:00
};
D3D.Box.prototype.setConfig = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
var self = this;
2015-05-06 15:06:04 +02:00
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "config", data, function (response) {
for (var i in response.validation) {
if (response.validation[i] !== "ok") {
delete data[i];
}
}
self.updateConfig(data);
self.printer.updateConfig(data);
if (callback !== undefined) {
callback(response);
}
});
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getInfo = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
getAPI(this.api + "info", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getInfoStatus = function (callback) {
"use strict";
getAPI(this.api + "info/status", callback);
return this;
};
D3D.Box.prototype.downloadInfoLogFiles = function () {
//works in google chrome... not tested in other browsers
//some browsers may redirect using this code
"use strict";
2015-05-06 15:06:04 +02:00
window.location = this.api + "info/logfiles";
2015-05-06 15:06:04 +02:00
};
D3D.Box.prototype.getInfoAcces = function (callback) {
"use strict";
getAPI(this.api + "info/access", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkScan = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
getAPI(this.api + "network/scan", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkKnown = function (callback) {
"use strict";
getAPI(this.api + "network/known", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkStatus = function (callback) {
"use strict";
getAPI(this.api + "network/status", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkAssosiate = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/associate", data, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkDisassosiate = function (callback) {
//not tested
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/disassociate", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkOpenAP = function (callback) {
//not tested
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/openap", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkRemove = function (ssid, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/remove", {
"ssid": ssid
}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getNetworkSignin = function (callback) {
"use strict";
getAPI(this.api + "network/signin", callback);
return this;
};
2015-05-06 15:06:04 +02:00
D3D.Box.prototype.getNetworkAlive = function (callback) {
"use strict";
getAPI(this.api + "network/alive", callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getPrinterTemperature = function (callback) {
"use strict";
getAPI(this.api + "printer/temperature", callback);
return this;
};
D3D.Box.prototype.getPrinterProgress = function (callback) {
"use strict";
getAPI(this.api + "printer/progress", callback);
return this;
};
D3D.Box.prototype.getPrinterState = function (callback) {
"use strict";
getAPI(this.api + "printer/state", callback);
return this;
};
2015-05-06 15:06:04 +02:00
D3D.Box.prototype.getPrinterListAll = function (callback) {
"use strict";
getAPI(this.api + "printer/listall", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setPrinterHeatup = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "printer/heatup", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.setPrinterPrint = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "printer/print", data, callback);
return this;
};
D3D.Box.prototype.setPrinterStop = function (data, callback) {
"use strict";
sendAPI(this.api + "printer/stop", data, callback);
return this;
};
2015-05-07 11:04:48 +02:00
D3D.Box.prototype.getSketch = function (id, callback) {
2015-05-07 14:09:36 +02:00
"use strict";
getAPI(this.api + "sketch/?id=" + id, callback);
return this;
};
D3D.Box.prototype.setSketch = function (data, callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "sketch", {
"data": data
}, callback);
2015-05-07 11:04:48 +02:00
return this;
};
D3D.Box.prototype.getSketchStatus = function (callback) {
"use strict";
getAPI(this.api + "sketch/status", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.setSketchClear = function (callback) {
"use strict";
2015-05-06 15:06:04 +02:00
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "sketch/clear", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getSystemVersions = function (callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
getAPI(this.api + "system/fwversions", callback);
2015-05-07 11:04:48 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getUpdateStatus = function (callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
getAPI(this.api + "update/status", callback);
2015-05-07 11:04:48 +02:00
return this;
};
D3D.Box.prototype.setUpdateDownload = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/download", {}, callback);
return this;
};
D3D.Box.prototype.setUpdateInstall = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/install", {}, callback);
return this;
};
D3D.Box.prototype.setUpdateClear = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/clear", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
2015-04-24 16:12:48 +02:00
};