2013-08-07 20:47:47 +02:00
|
|
|
function Printer() {
|
|
|
|
this.temperature = 0;
|
|
|
|
this.targetTemperature = 0;
|
2013-08-19 17:55:01 +02:00
|
|
|
this.printing;
|
2013-08-07 20:47:47 +02:00
|
|
|
|
|
|
|
this.wifiboxURL;
|
|
|
|
|
|
|
|
this.maxTempLastMod = 5; // max time (seconds) since the last temp info modification before the printer connection is considered lost
|
2013-08-14 20:54:48 +02:00
|
|
|
|
2013-08-09 22:25:14 +02:00
|
|
|
this.checkTemperatureInterval = 3000;
|
|
|
|
this.checkTemperatureDelay;
|
|
|
|
this.checkProgressInterval = 3000;
|
|
|
|
this.checkProgressDelay;
|
|
|
|
this.timeoutTime = 3000;
|
|
|
|
|
2013-08-14 20:54:48 +02:00
|
|
|
this.gcode; // gcode to be printed
|
|
|
|
this.sendLength = 6000; // max amount of gcode lines per post (limited because WiFi box can't handle to much)
|
|
|
|
|
|
|
|
this.retryDelay = 2000; // retry setTimout delay
|
|
|
|
this.retrySendPrintPartDelay; // retry setTimout instance
|
|
|
|
this.retryCheckTemperatureDelay; // retry setTimout instance
|
|
|
|
this.retryCheckProgressDelay; // retry setTimout instance
|
|
|
|
this.retryStopDelay; // retry setTimout instance
|
|
|
|
this.retryPreheatDelay; // retry setTimout instance
|
|
|
|
|
|
|
|
this.maxGCodeSize = 10; // max size of gcode in MB's (estimation)
|
|
|
|
|
2013-08-16 19:27:48 +02:00
|
|
|
this.sendStopGCodeDelay = 1000;
|
|
|
|
|
2013-08-07 20:47:47 +02:00
|
|
|
// Events
|
|
|
|
Printer.UPDATE = "update";
|
|
|
|
|
|
|
|
this.init = function() {
|
|
|
|
console.log("Printer:init");
|
|
|
|
//this.wifiboxURL = "http://" + window.location.host + "/cgi-bin/d3dapi";
|
|
|
|
//this.wifiboxURL = "http://192.168.5.1/cgi-bin/d3dapi";
|
|
|
|
this.wifiboxURL = wifiboxURL;
|
|
|
|
//this.wifiboxURL = "proxy5.php";
|
|
|
|
console.log(" wifiboxURL: ",this.wifiboxURL);
|
|
|
|
|
2013-08-27 15:34:28 +02:00
|
|
|
if(autoUpdate) {
|
|
|
|
this.checkTemperature();
|
|
|
|
this.checkProgress();
|
|
|
|
}
|
2013-08-07 20:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.preheat = function() {
|
|
|
|
console.log("Printer:preheat");
|
|
|
|
var postData = { id: 0 };
|
2013-08-12 16:52:31 +02:00
|
|
|
var self = this;
|
2013-08-19 17:55:01 +02:00
|
|
|
if (communicateWithWifibox) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.wifiboxURL + "/printer/heatup",
|
|
|
|
type: "POST",
|
|
|
|
data: postData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: this.timeoutTime,
|
|
|
|
success: function(data){
|
|
|
|
console.log("Printer:preheat response: ",data);
|
|
|
|
if(data.status == "error") {
|
|
|
|
clearTimeout(self.retryPreheatDelay);
|
|
|
|
self.retryPreheatDelay = setTimeout(function() { self.preheat() },self.retryDelay); // retry after delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log("Printer:preheat: failed");
|
|
|
|
clearTimeout(self.retryPreheatDelay);
|
|
|
|
self.retryPreheatDelay = setTimeout(function() { self.preheat() },self.retryDelay); // retry after delay
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log ("Printer >> f:preheat() >> communicateWithWifibox is false, so not executing this function");
|
|
|
|
}
|
2013-08-14 20:54:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.print = function(gcode) {
|
|
|
|
console.log("Printer:print");
|
|
|
|
console.log(" gcode total # of lines: " + gcode.length);
|
|
|
|
|
|
|
|
/*for (i = 0; i < gcode.length; i++) {
|
|
|
|
gcode[i] += " (" + i + ")";
|
|
|
|
}*/
|
|
|
|
|
|
|
|
this.sendIndex = 0;
|
|
|
|
this.gcode = gcode;
|
|
|
|
|
|
|
|
//console.log(" gcode[20]: ",gcode[20]);
|
|
|
|
var gcodeLineSize = this.byteSize(gcode[20]);
|
|
|
|
//console.log(" gcodeLineSize: ",gcodeLineSize);
|
|
|
|
var gcodeSize = gcodeLineSize*gcode.length/1024/1024; // estimate gcode size in MB's
|
|
|
|
console.log(" gcodeSize: ",gcodeSize);
|
|
|
|
|
|
|
|
if(gcodeSize > this.maxGCodeSize) {
|
|
|
|
console.log("Error: Printer:print: gcode file is probably to big ("+gcodeSize+"MB) (max: "+this.maxGCodeSize+"MB)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendPrintPart(this.sendIndex, this.sendLength);
|
|
|
|
}
|
|
|
|
this.byteSize = function(s){
|
|
|
|
return~-encodeURI(s).split(/%..|./).length;
|
|
|
|
}
|
|
|
|
this.sendPrintPart = function(sendIndex,sendLength) {
|
|
|
|
console.log("Printer:sendPrintPart sendIndex: " + sendIndex + "/" + this.gcode.length + ", sendLength: " + sendLength);
|
|
|
|
|
|
|
|
var firstOne = (sendIndex == 0)? true : false;
|
|
|
|
var lastOne = false;
|
|
|
|
if (this.gcode.length < (sendIndex + sendLength)) {
|
|
|
|
console.log(" sending less than max sendLength (and last)");
|
|
|
|
sendLength = this.gcode.length - sendIndex;
|
|
|
|
lastOne = true;
|
|
|
|
}
|
|
|
|
var gcodePart = this.gcode.slice(sendIndex, sendIndex+sendLength);
|
|
|
|
|
|
|
|
var postData = { id: 0, gcode: gcodePart.join("\n"), first: firstOne, last: lastOne};
|
|
|
|
var self = this;
|
2013-08-19 17:55:01 +02:00
|
|
|
if (communicateWithWifibox) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.wifiboxURL + "/printer/print",
|
|
|
|
type: "POST",
|
|
|
|
data: postData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: this.timeoutTime,
|
|
|
|
success: function(data){
|
|
|
|
console.log("Printer:sendPrintPart response: ",data);
|
|
|
|
|
|
|
|
if(data.status == "success") {
|
|
|
|
if (lastOne) {
|
|
|
|
console.log("Printer:sendPrintPart:gcode sending completed");
|
|
|
|
this.gcode = [];
|
|
|
|
} else {
|
|
|
|
self.sendPrintPart(sendIndex + sendLength, sendLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log("Printer:sendPrintPart: failed");
|
|
|
|
clearTimeout(self.retrySendPrintPartDelay);
|
|
|
|
self.retrySendPrintPartDelay = setTimeout(function() { self.sendPrintPart(sendIndex, sendLength) },self.retryDelay); // retry after delay
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log ("Printer >> f:sendPrintPart() >> communicateWithWifibox is false, so not executing this function");
|
|
|
|
}
|
2013-08-07 20:47:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 20:54:48 +02:00
|
|
|
|
2013-08-07 20:47:47 +02:00
|
|
|
this.stop = function() {
|
|
|
|
console.log("Printer:stop");
|
2013-08-14 20:54:48 +02:00
|
|
|
var postData = { id: 0 };
|
2013-08-16 19:27:48 +02:00
|
|
|
var self = this;
|
2013-08-19 17:55:01 +02:00
|
|
|
if (communicateWithWifibox) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.wifiboxURL + "/printer/stop",
|
|
|
|
type: "POST",
|
|
|
|
data: postData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: this.timeoutTime,
|
|
|
|
success: function(data){
|
|
|
|
console.log("Printer:stop response: ", data);
|
|
|
|
|
|
|
|
setTimeout(function() { console.log("send: ",gcodeEnd); self.print(gcodeEnd) },self.sendStopGCodeDelay);
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log("Printer:stop: failed");
|
|
|
|
clearTimeout(self.retryStopDelay);
|
|
|
|
self.retryStopDelay = setTimeout(function() { self.stop() },self.retryDelay); // retry after delay
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log ("Printer >> f:communicateWithWifibox() >> communicateWithWifibox is false, so not executing this function");
|
|
|
|
}
|
2013-08-07 20:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.checkTemperature = function() {
|
|
|
|
//console.log("Printer:checkTemperature");
|
2013-08-09 22:25:14 +02:00
|
|
|
var getData = { id: 0 };
|
|
|
|
var self = this;
|
2013-08-16 22:25:25 +02:00
|
|
|
if (communicateWithWifibox) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.wifiboxURL + "/printer/temperature",
|
|
|
|
data: getData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: this.timeoutTime,
|
|
|
|
success: function(data){
|
|
|
|
//console.log("Printer:temperature response: ",data);
|
|
|
|
if(data.status == "success") {
|
|
|
|
//console.log("temp: ",response.data.hotend+"/"+response.data.hotend_target+" ("+response.data.last_mod+")");
|
|
|
|
self.temperature = data.data.hotend;
|
|
|
|
if(data.data.hotend_target != undefined) {
|
|
|
|
self.targetTemperature = data.data.hotend_target;
|
|
|
|
}
|
|
|
|
self.alive = (data.data.last_mod < self.maxTempLastMod);
|
|
|
|
} else {
|
|
|
|
self.alive = false;
|
|
|
|
}
|
|
|
|
//console.log(" this.alive: ",self.alive);
|
|
|
|
$(document).trigger(Printer.UPDATE);
|
|
|
|
|
|
|
|
self.checkTemperatureDelay = setTimeout(function() { self.checkTemperature() }, self.checkTemperatureInterval);
|
2013-08-07 20:47:47 +02:00
|
|
|
}
|
2013-08-16 22:25:25 +02:00
|
|
|
}).fail(function() {
|
|
|
|
console.log("Printer:checkTemperature: failed");
|
|
|
|
clearTimeout(self.retryCheckTemperatureDelay);
|
|
|
|
self.retryCheckTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.retryDelay); // retry after delay
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log ("Printer >> f:checkTemperature() >> communicateWithWifibox is false, so not executing this function");
|
|
|
|
}
|
2013-08-09 22:25:14 +02:00
|
|
|
}
|
|
|
|
this.checkProgress = function() {
|
|
|
|
//console.log("Printer:checkProgress");
|
|
|
|
var getData = { id: 0 };
|
|
|
|
var self = this;
|
2013-08-16 22:25:25 +02:00
|
|
|
if (communicateWithWifibox) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.wifiboxURL + "/printer/progress",
|
|
|
|
data: getData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: this.timeoutTime,
|
|
|
|
success: function(data){
|
|
|
|
if(data.status == "success") {
|
|
|
|
|
|
|
|
self.printing = data.data.printing;
|
|
|
|
self.currentLine = data.data.current_line;
|
|
|
|
self.num_lines = data.data.num_lines;
|
|
|
|
|
|
|
|
if(self.printing) {
|
|
|
|
console.log("progress: ",data.data.current_line+"/"+data.data.num_lines+" ("+data.data.last_mod+")");
|
|
|
|
}
|
2013-08-19 16:52:18 +02:00
|
|
|
} else {
|
|
|
|
self.printing = false;
|
2013-08-16 22:25:25 +02:00
|
|
|
}
|
|
|
|
//console.log(" this.alive: ",self.alive);
|
|
|
|
$(document).trigger(Printer.UPDATE);
|
2013-08-09 22:25:14 +02:00
|
|
|
|
2013-08-16 22:25:25 +02:00
|
|
|
self.checkProgressDelay = setTimeout(function() { self.checkProgress() },self.checkProgressInterval);
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log("Printer:checkProgress: failed");
|
|
|
|
clearTimeout(self.retryCheckProgressDelay);
|
|
|
|
self.retryCheckProgressDelay = setTimeout(function() { self.checkProgress() },self.retryDelay); // retry after delay
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log ("Printer >> f:checkProgress() >> communicateWithWifibox is false, so not executing this function");
|
|
|
|
}
|
2013-08-07 20:47:47 +02:00
|
|
|
}
|
|
|
|
}
|