From 7da38d2e9887d95d18246a0ebb0eb7ad284dc015 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 14 Aug 2013 20:54:48 +0200 Subject: [PATCH] Did a ajax calls and the gcode sending overhaul/rewrite. All failed calls (temp, process check, preheat, print and stop) are retried automatically. All printer communication is now handled in Printer.js. --- index.html | 1 - js/Printer.js | 150 ++++++++++++++++++++++++++++---------- js/buttonbehaviors.js | 7 +- js/doodlePrintCode.js | 63 ---------------- js/gcodeGenerating_v01.js | 4 +- 5 files changed, 118 insertions(+), 107 deletions(-) delete mode 100644 js/doodlePrintCode.js diff --git a/index.html b/index.html index b9f8334..5a7a2e9 100755 --- a/index.html +++ b/index.html @@ -88,7 +88,6 @@ - diff --git a/js/Printer.js b/js/Printer.js index 40f26f6..aa6ddf9 100644 --- a/js/Printer.js +++ b/js/Printer.js @@ -5,15 +5,25 @@ function Printer() { this.wifiboxURL; this.maxTempLastMod = 5; // max time (seconds) since the last temp info modification before the printer connection is considered lost - - - + this.checkTemperatureInterval = 3000; this.checkTemperatureDelay; this.checkProgressInterval = 3000; this.checkProgressDelay; this.timeoutTime = 3000; + 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) + // Events Printer.UPDATE = "update"; @@ -41,29 +51,103 @@ function Printer() { timeout: this.timeoutTime, success: function(data){ console.log("Printer:preheat response: ",data); - }, - error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout" - //console.log("Printer:temperature error. Status: ",status,' errorThrown: ',errorThrown); - switch(status) { - case 'timeout': - console.log("retrieving printer/heatup timeout"); - self.preheat(); - break; - } + 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 }); } + 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; + $.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 + }); + } + + this.stop = function() { console.log("Printer:stop"); - var postData = { id: 0 }; - $.post( this.wifiboxURL + "/printer/stop", postData , function(e) { - console.log("Printer:stop response: " + e); - - if (e.success = true) { - console.log(" success"); - } - }); + var postData = { id: 0 }; + $.ajax({ + url: this.wifiboxURL + "/printer/stop", + type: "POST", + data: postData, + dataType: 'json', + timeout: this.timeoutTime, + success: function(data){ + console.log("Printer:stop response: ", data); + } + }).fail(function() { + console.log("Printer:stop: failed"); + clearTimeout(self.retryStopDelay); + self.retryStopDelay = setTimeout(function() { self.stop() },self.retryDelay); // retry after delay + }); + } this.checkTemperature = function() { @@ -91,16 +175,11 @@ function Printer() { $(document).trigger(Printer.UPDATE); self.checkTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.checkTemperatureInterval); - }, - error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout" - //console.log("Printer:temperature error. Status: ",status,' errorThrown: ',errorThrown); - switch(status) { - case 'timeout': - console.log("retrieving printer/temperature timeout"); - self.checkTemperature(); - break; - } } + }).fail(function() { + console.log("Printer:checkTemperature: failed"); + clearTimeout(self.retryCheckTemperatureDelay); + self.retryCheckTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.retryDelay); // retry after delay }); } this.checkProgress = function() { @@ -127,16 +206,11 @@ function Printer() { $(document).trigger(Printer.UPDATE); self.checkProgressDelay = setTimeout(function() { self.checkProgress() },self.checkProgressInterval); - }, - error: function(jqXHR, status, errorThrown){ //the status returned will be "timeout" - //console.log("Printer:progress error. Status: ",status,' errorThrown: ',errorThrown); - switch(status) { - case 'timeout': - self.checkProgress(); - console.log("retrieving printer/progress timeout"); - break; - } } + }).fail(function() { + console.log("Printer:checkProgress: failed"); + clearTimeout(self.retryCheckProgressDelay); + self.retryCheckProgressDelay = setTimeout(function() { self.checkProgress() },self.retryDelay); // retry after delay }); } diff --git a/js/buttonbehaviors.js b/js/buttonbehaviors.js index b8a2fcc..19eef8a 100644 --- a/js/buttonbehaviors.js +++ b/js/buttonbehaviors.js @@ -241,8 +241,9 @@ function print(e) { if (_points.length > 2) { setState(PRINTING_STATE); - var gencode = generate_gcode(); - startPrint(gencode); + var gcode = generate_gcode(); + //startPrint(gencode); + printer.print(gcode); // console.log(""); // console.log(""); @@ -254,7 +255,7 @@ function print(e) { // console.log(""); // console.log(""); - $("#textdump").text(gencode.join("\n")); + $("#textdump").text(gcode.join("\n")); // copyToClipboard(gencode); //*/ } else { diff --git a/js/doodlePrintCode.js b/js/doodlePrintCode.js deleted file mode 100644 index 1313c8e..0000000 --- a/js/doodlePrintCode.js +++ /dev/null @@ -1,63 +0,0 @@ -var sendIndex; -var sendLength; - -var data = ""; -function startPrint(gcode) { - console.log("f:startPrint()"); - console.log("total # of lines: " + gcode.length); - data = gcode; - - for (i = 0; i < data.length; i++) { - data[i] += " (" + i + ")"; - } - - sendIndex = 0; - sendLength = 2000; // 2000 regels - sendGCodeSlice(sendIndex, sendLength); -} - - -function sendGCodeSlice(startIndex, sendAmt) { - console.log("f:sendGCodeSlice >> startIndex:" + startIndex + ", sendAmt:" + sendAmt); - - if (typeof startIndex == "number" && typeof sendAmt == "number") { - var lastOne = false; - if (data.length < (startIndex + sendAmt)) { - console.log("f:senGCodeSlice >> not enough data left for full slice, sending smaller (and last) one"); - sendAmt = data.length - startIndex; - lastOne = true; - } - var _tmp = data.slice(startIndex, startIndex+sendAmt); - // console.log("f:senGCodeSlice >> _tmp.length:" + _tmp.length); - -// $.post("/doodle3d.of", { data:data }, function(data) { -// btnPrint.disabled = false; -// }); - - var firstOne = false; - if (startIndex == 0) { firstOne = true; } - - var postData = { id: 0, gcode: _tmp.join("\n"), first: firstOne, last: lastOne}; - - $.post( wifiboxURL + "/printer/print", postData , function(e) { - console.log("sendBoy callback: " + e); - // console.log(e); - // console.log("e.success: " + e.success); - if (e.success = true) { - if (lastOne) { - console.log("f:sendGCodeSlice >> DONE!"); - } else { - sendGCodeSlice(startIndex + sendAmt, sendAmt); - } - } - }) - } else { - console.log(" something wrong"); - } -} - -function sendBoy(sendObj, callback) { - console.log("f:sendBoy() (dummy kastje) >> data length: " + sendObj.data.length + ", lastOne: " + sendObj.last); - console.log(""); - if (callback != undefined) callback({success:true}); -} \ No newline at end of file diff --git a/js/gcodeGenerating_v01.js b/js/gcodeGenerating_v01.js index 8743207..c6f8e01 100755 --- a/js/gcodeGenerating_v01.js +++ b/js/gcodeGenerating_v01.js @@ -107,13 +107,13 @@ function generate_gcode(callback) { if (firstLayerSlow) { //gcode.push("M220 S20"); //slow speed speed = bottomSpeed; - console.log("> speed: ",speed); + //console.log("> speed: ",speed); } } else if (layer == 2) { ////////LET OP, pas bij layer 2 weer op normale snelheid ipv layer 1 gcode.push("M106"); //fan on //gcode.push("M220 S100"); //normal speed speed = normalSpeed; - console.log("> speed: ",speed); + //console.log("> speed: ",speed); } var curLayerCommand = 0;