2013-12-20 16:31:41 +01:00
/ *
* This file is part of the Doodle3D project ( http : //doodle3d.com).
*
* Copyright ( c ) 2013 , Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later .
* See file LICENSE . txt or visit http : //www.gnu.org/licenses/gpl.html for full license details.
* /
2013-09-17 13:08:52 +02:00
/ * n o t u s i n g t h i s n o w
2013-09-07 17:08:52 +02:00
var $printProgressContainer = $ ( "#printProgressContainer" ) ;
var $progressbar = $ ( "#progressbar" ) ;
var $progressAmount = $ ( ".progressAmount" ) ;
function setPrintprogress ( val ) {
2014-03-05 13:49:15 +01:00
if ( isNaN ( val ) ) return ;
// console.log("f:setPrintprogress() >> val " + val);
$progressbar . css ( "width" , val * 100 + "%" ) ;
$progressAmount . text ( Math . floor ( val * 100 ) + "%" ) ;
2013-09-07 17:08:52 +02:00
}
2013-09-17 13:08:52 +02:00
//*/
2013-09-07 17:08:52 +02:00
2013-08-07 20:47:47 +02:00
function Printer ( ) {
2016-02-14 16:17:00 +01:00
/* CONSTANTS */
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
Printer . WIFIBOX _DISCONNECTED _STATE = "wifibox disconnected" ;
Printer . UNKNOWN _STATE = "unknown" ; // happens when a printer is connection but there isn't communication yet
Printer . DISCONNECTED _STATE = "disconnected" ; // printer disconnected
2014-03-05 17:35:01 +01:00
Printer . CONNECTING _STATE = "connecting" ; // printer connecting (printer found, but driver has not yet finished setting up the connection)
Printer . IDLE _STATE = "idle" ; // printer found and ready to use, but idle
2014-03-05 13:49:15 +01:00
Printer . BUFFERING _STATE = "buffering" ; // printer is buffering (recieving) data, but not yet printing
Printer . PRINTING _STATE = "printing" ;
Printer . STOPPING _STATE = "stopping" ; // when you stop (abort) a print it prints the endcode
Printer . TOUR _STATE = "tour" ; // when in joyride mode
2013-10-11 12:39:05 +02:00
2014-03-05 17:35:01 +01:00
Printer . ON _BEFORE _UNLOAD _MESSAGE = "You're doodle is still being sent to the printer, leaving will result in a incomplete 3D print" ;
2013-11-22 16:51:26 +01:00
2016-02-14 16:17:00 +01:00
//after buffer full message has been received, wait until the buffer load is below this ratio before sending new data
Printer . GCODE _BUFFER _WAIT _LOAD _RATIO = 0.75 ;
Printer . BUFFER _SPACE _WAIT _TIMEOUT = 30000 ; // how often to recheck buffer load
2016-04-19 18:03:52 +02:00
//time to wait when wifibox connection is lost while printing
Printer . DISCONNECTED _RETRY _DELAY = 5000 ;
2016-02-14 16:17:00 +01:00
Printer . MAX _LINES _PER _POST = 500 ; // max amount of gcode lines per post (limited because WiFi box can't handle too much)
Printer . MAX _GCODE _SIZE = 10 ; // max size of gcode in MB's (estimation)
// Events
Printer . UPDATE = "update" ;
/* MEMBER VARIABLES */
2014-03-05 13:49:15 +01:00
this . temperature = 0 ;
2013-10-11 12:39:05 +02:00
this . targetTemperature = 0 ;
2014-03-05 13:49:15 +01:00
this . currentLine = 0 ;
this . totalLines = 0 ;
this . bufferedLines = 0 ;
2016-02-12 17:34:05 +01:00
this . bufferSize = 0 ;
this . maxBufferSize = 0 ;
2014-03-05 13:49:15 +01:00
this . state = Printer . UNKNOWN _STATE ;
this . hasControl = true ; // whether this client has control access
2013-11-22 16:51:26 +01:00
this . wifiboxURL ;
2014-03-05 13:49:15 +01:00
this . checkStatusInterval = 3000 ;
2013-10-11 12:39:05 +02:00
this . checkStatusDelay ;
2014-03-05 13:49:15 +01:00
this . timeoutTime = 3000 ;
this . sendPrintPartTimeoutTime = 5000 ;
2013-11-22 16:51:26 +01:00
2016-02-14 17:13:13 +01:00
this . gcode = [ ] ; // gcode to be printed
this . gcodeNumChunks = 0 ; //number of chunks to be sent (used for sequence numbering)
2013-08-14 20:54:48 +02:00
2014-03-05 13:49:15 +01:00
this . retryDelay = 2000 ; // retry setTimout delay
this . retrySendPrintPartDelay ; // retry setTimout instance
2013-10-11 12:39:05 +02:00
this . retryCheckStatusDelay ; // retry setTimout instance
2014-03-05 13:49:15 +01:00
this . retryStopDelay ; // retry setTimout instance
this . retryPreheatDelay ; // retry setTimout instance
2013-11-22 16:51:26 +01:00
2013-10-14 17:40:56 +02:00
this . stateOverruled = false ;
2013-11-22 16:51:26 +01:00
2016-02-14 16:17:00 +01:00
/* FUNCTIONS */
2013-11-22 16:51:26 +01:00
2013-10-11 12:39:05 +02:00
var self = this ;
2013-11-22 16:51:26 +01:00
2013-08-07 20:47:47 +02:00
this . init = function ( ) {
2014-03-05 13:49:15 +01:00
//console.log("Printer:init");
2013-08-07 20:47:47 +02:00
//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";
2014-03-05 13:49:15 +01:00
//console.log(" wifiboxURL: ",this.wifiboxURL);
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
if ( autoUpdate ) {
this . startStatusCheckInterval ( ) ;
}
}
2013-11-22 16:51:26 +01:00
2013-08-07 20:47:47 +02:00
this . preheat = function ( ) {
2014-03-05 13:49:15 +01:00
console . log ( "Printer:preheat" ) ;
2014-03-05 17:35:01 +01:00
if ( this . state != Printer . IDLE _STATE ) return ;
2014-03-05 13:49:15 +01:00
var self = this ;
if ( communicateWithWifibox ) {
$ . ajax ( {
url : this . wifiboxURL + "/printer/heatup" ,
type : "POST" ,
dataType : 'json' ,
timeout : this . timeoutTime ,
success : function ( data ) {
console . log ( "Printer:preheat response: " , data ) ;
if ( data . status != "success" ) {
clearTimeout ( self . retryPreheatDelay ) ;
2013-08-19 17:55:01 +02:00
self . retryPreheatDelay = setTimeout ( function ( ) { self . preheat ( ) } , self . retryDelay ) ; // retry after delay
2014-03-05 13:49:15 +01:00
}
2013-08-19 17:55:01 +02:00
}
2013-11-22 16:51:26 +01:00
} ) . fail ( function ( ) {
2013-08-19 17:55:01 +02:00
console . log ( "Printer:preheat: failed" ) ;
clearTimeout ( self . retryPreheatDelay ) ;
self . retryPreheatDelay = setTimeout ( function ( ) { self . preheat ( ) } , self . retryDelay ) ; // retry after delay
} ) ;
} else {
2014-03-05 13:49:15 +01:00
console . log ( "Printer >> f:preheat() >> communicateWithWifibox is false, so not executing this function" ) ;
}
2013-08-14 20:54:48 +02:00
}
2013-11-22 16:51:26 +01:00
2013-08-14 20:54:48 +02:00
this . print = function ( gcode ) {
2014-03-05 13:49:15 +01:00
console . log ( "Printer:print" ) ;
console . log ( " gcode total # of lines: " + gcode . length ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
message . set ( "Sending doodle to printer..." , Message . NOTICE ) ;
self . addLeaveWarning ( ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
/ * f o r ( i = 0 ; i < g c o d e . l e n g t h ; i + + ) {
2013-08-14 20:54:48 +02:00
gcode [ i ] += " (" + i + ")" ;
} * /
2013-11-22 16:51:26 +01:00
2013-08-14 20:54:48 +02:00
this . sendIndex = 0 ;
2014-03-05 13:49:15 +01:00
this . gcode = gcode ;
2016-02-14 17:13:13 +01:00
this . gcodeNumChunks = Math . ceil ( this . gcode . length / Printer . MAX _LINES _PER _POST ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
//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 ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
if ( gcodeSize > Printer . MAX _GCODE _SIZE ) {
2014-03-05 17:35:01 +01:00
var msg = "Error: Printer:print: gcode file is probably too big (" + gcodeSize + "MB) (max: " + Printer . MAX _GCODE _SIZE + "MB)" ;
alert ( msg ) ;
console . log ( msg ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
this . overruleState ( Printer . IDLE _STATE ) ;
this . startStatusCheckInterval ( ) ;
message . hide ( ) ;
self . removeLeaveWarning ( ) ;
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
return ;
}
2013-11-22 16:51:26 +01:00
2014-03-05 13:49:15 +01:00
//this.targetTemperature = settings["printer.temperature"]; // slight hack
2013-11-22 16:51:26 +01:00
2016-02-14 16:17:00 +01:00
this . sendPrintPart ( this . sendIndex , Printer . MAX _LINES _PER _POST ) ;
2013-08-14 20:54:48 +02:00
}
2014-03-05 13:49:15 +01:00
2013-08-14 20:54:48 +02:00
this . byteSize = function ( s ) {
return ~ - encodeURI ( s ) . split ( /%..|./ ) . length ;
}
2014-03-05 13:49:15 +01:00
2013-08-14 20:54:48 +02:00
this . sendPrintPart = function ( sendIndex , sendLength ) {
2016-05-11 12:22:54 +02:00
var self = this ;
// Abort if stopping
// sendPrintPart can be called by delayed retry after request timeout for example
if ( self . state === Printer . STOPPING _STATE ) return ;
2014-03-05 13:49:15 +01:00
var completed = false ;
if ( this . gcode . length < ( sendIndex + sendLength ) ) {
sendLength = this . gcode . length - sendIndex ;
completed = true ;
}
2016-02-14 16:40:28 +01:00
/* prepare post data */
var gcodePart = this . gcode . slice ( sendIndex , sendIndex + sendLength ) ;
var firstOne = ( sendIndex == 0 ) ? true : false ;
var start = firstOne ; // start printing right away
2016-02-14 17:13:13 +01:00
var seqNum = Math . floor ( sendIndex / Printer . MAX _LINES _PER _POST ) ;
var postData = {
gcode : gcodePart . join ( "\n" ) , total _lines : this . gcode . length ,
clear : firstOne , start : start ,
seq _number : seqNum , seq _total : this . gcodeNumChunks
} ;
2016-02-14 16:40:28 +01:00
2016-02-14 17:29:23 +01:00
/* inform user what's going on */
var lessThanMaxText = completed ? " (last one, max=" + Printer . MAX _LINES _PER _POST + ")" : "" ;
console . log ( "Printer:sendPrintPart: sendIndex=" + sendIndex + "/" + this . gcode . length +
", sendLength=" + sendLength + lessThanMaxText +
", sequence numbers: " + seqNum + "/" + this . gcodeNumChunks ) ;
var sendPercentage = Math . round ( sendIndex / this . gcode . length * 100 ) ;
message . set ( "Sending doodle to printer: " + sendPercentage + "%" , Message . NOTICE , false , true ) ;
2016-02-14 16:40:28 +01:00
/* send data */
2014-03-05 13:49:15 +01:00
if ( communicateWithWifibox ) {
$ . ajax ( {
url : this . wifiboxURL + "/printer/print" ,
type : "POST" ,
data : postData ,
dataType : 'json' ,
timeout : this . sendPrintPartTimeoutTime ,
success : function ( data ) {
2016-04-19 18:03:52 +02:00
//console.log("Printer:sendPrintPart success response: ", data);
2014-03-05 13:49:15 +01:00
if ( data . status == "success" ) {
if ( completed ) {
2016-02-14 17:13:13 +01:00
console . log ( "Printer:sendPrintPart: gcode sending completed" ) ;
2014-03-05 13:49:15 +01:00
this . gcode = [ ] ;
2016-02-14 17:13:13 +01:00
this . gcodeNumChunks = 0 ;
2014-03-05 13:49:15 +01:00
self . removeLeaveWarning ( ) ;
message . set ( "Doodle has been sent to printer..." , Message . INFO , true ) ;
//self.targetTemperature = settings["printer.temperature"]; // slight hack
} else {
2014-03-05 17:35:01 +01:00
// only if the state hasn't been changed (by for example pressing stop) we send more gcode
2014-03-05 13:49:15 +01:00
//console.log("Printer:sendPrintPart:gcode part received (state: ",self.state,")");
if ( self . state == Printer . PRINTING _STATE || self . state == Printer . BUFFERING _STATE ) {
//console.log("Printer:sendPrintPart:sending next part");
self . sendPrintPart ( sendIndex + sendLength , sendLength ) ;
2016-04-19 18:03:52 +02:00
} else if ( Printer . WIFIBOX _DISCONNECTED _STATE ) {
console . warn ( "Printer:sendPrintPart: wifibox connection lost while printing, retrying in " + ( Printer . DISCONNECTED _RETRY _DELAY / 1000 ) + " seconds" ) ;
clearTimeout ( self . retrySendPrintPartDelay ) ;
self . retrySendPrintPartDelay = setTimeout ( function ( ) {
console . log ( "Printer:sendPrintPart: retrying after wifibox disconnect was detected" ) ;
self . sendPrintPart ( sendIndex , sendLength ) ;
} , Printer . DISCONNECTED _RETRY _DELAY ) ;
2014-03-05 13:49:15 +01:00
}
}
2016-02-12 17:34:05 +01:00
} else if ( data . status == "fail" ) {
if ( data . data . status == "buffer_full" ) {
2016-03-11 15:49:32 +01:00
console . log ( "Printer:sendPrintPart: print server reported buffer full, pausing data transmission" ) ;
2016-02-12 17:34:05 +01:00
//this will wait in a setTimeout loop until enough room is available and then call sendPrintPart again.
self . waitForBufferSpace ( sendIndex , sendLength ) ;
2016-04-19 18:03:52 +02:00
} else if ( data . data . status == "seq_num_mismatch" && data . data . seq _number == seqNum ) {
console . warn ( "Printer:sendPrintPart: received sequence error, server is one chunk ahead. Proceeding with next chunk..." ) ;
self . sendPrintPart ( sendIndex + sendLength , sendLength ) ;
2016-02-12 17:34:05 +01:00
} else {
2016-04-19 18:03:52 +02:00
console . error ( "Printer:sendPrintPart: unexpected failure response for API endpoint printer/print (" +
data . data . status + ", current server seq. info: " + data . data . seq _number + "/" + data . data . seq _total + ")" ) ;
2016-02-14 17:13:13 +01:00
//sequence errors should not occur, except perhaps when 'stop' was clicked while still sending (https://github.com/Doodle3D/doodle3d-client/issues/226).
if ( self . state != Printer . STOPPING _STATE ) {
message . set ( "Unexpected error sending doodle to printer (" + data . data . status + "), please retry" , Message . ERROR , false , true ) ;
}
2016-02-12 17:34:05 +01:00
}
2014-03-05 13:49:15 +01:00
}
2016-02-12 17:34:05 +01:00
// after we know the first gcode part has been received or failed
2013-11-22 16:51:26 +01:00
// (and the driver had time to update the printer.state)
2013-10-16 18:27:10 +02:00
// we start checking the status again
if ( sendIndex == 0 ) {
self . startStatusCheckInterval ( ) ;
}
2013-08-19 17:55:01 +02:00
}
2016-03-11 15:49:32 +01:00
} ) . fail ( function ( jqXHr , textStatus , errorThrown ) {
2016-04-19 18:03:52 +02:00
console . error ( "Printer:sendPrintPart: failed (AJAX status: '" + textStatus + "') AJAX exception (if any):" , errorThrown ) ;
console . warn ( "Printer:sendPrintPart: retrying in " + ( Printer . DISCONNECTED _RETRY _DELAY / 1000 ) + " seconds" ) ;
2013-08-19 17:55:01 +02:00
clearTimeout ( self . retrySendPrintPartDelay ) ;
2013-10-11 15:33:37 +02:00
self . retrySendPrintPartDelay = setTimeout ( function ( ) {
2016-04-19 18:03:52 +02:00
console . log ( "Printer:sendPrintPart: retrying after AJAX failure" ) ;
2013-11-22 16:51:26 +01:00
self . sendPrintPart ( sendIndex , sendLength )
2016-04-19 18:03:52 +02:00
} , Printer . DISCONNECTED _RETRY _DELAY ) ;
2013-11-22 16:51:26 +01:00
2013-10-16 18:27:10 +02:00
// after we know the gcode packed has bin received or failed
2013-11-22 16:51:26 +01:00
// (and the driver had time to update the printer.state)
2013-10-16 18:27:10 +02:00
// we start checking the status again
self . startStatusCheckInterval ( ) ;
2013-08-19 17:55:01 +02:00
} ) ;
} else {
2014-03-05 13:49:15 +01:00
console . log ( "Printer >> f:sendPrintPart() >> communicateWithWifibox is false, so not executing this function" ) ;
}
2013-08-07 20:47:47 +02:00
}
2016-02-12 17:34:05 +01:00
/ *
* Called by sendPrintPart when a buffer _full fail response is received .
* This function keeps calling itself until the GCodeBuffer ' s load ratio
* drops below a predefined value and then calls sendPrintPart again .
* /
this . waitForBufferSpace = function ( sendIndex , sendLength ) {
var fillRatio = this . bufferSize / this . maxBufferSize ;
var self = this ;
//console.log("buffer fill state: " + self.bufferSize + "/" + self.maxBufferSize + " (" + fillPercent + "%)");
if ( fillRatio >= Printer . GCODE _BUFFER _WAIT _LOAD _RATIO ) {
var fillPercent = ( fillRatio * 100 ) . toFixed ( 2 ) ;
console . log ( "Printer:waitForBufferSpace: waiting until gcode buffer load ratio is below " +
( Printer . GCODE _BUFFER _WAIT _LOAD _RATIO * 100 ) + "% (current: " + fillPercent + "% of " +
( self . maxBufferSize / 1024 ) + "KiB)" ) ;
2016-05-11 12:22:54 +02:00
self . waitForBufferSpaceDelay = setTimeout ( function ( ) { self . waitForBufferSpace ( sendIndex , sendLength ) ; } , Printer . BUFFER _SPACE _WAIT _TIMEOUT ) ;
2016-02-12 17:34:05 +01:00
} else {
if ( self . state == Printer . PRINTING _STATE || self . state == Printer . BUFFERING _STATE ) {
console . log ( "Printer:waitForBufferSpace: load ratio dropped below " + ( Printer . GCODE _BUFFER _WAIT _LOAD _RATIO * 100 ) + "%, calling sendPrintPart..." ) ;
self . sendPrintPart ( sendIndex , sendLength ) ;
} else {
console . log ( "Printer:waitForBufferSpace: load ratio dropped far enough but printer state is not printing or buffering anymore, not resuming." ) ;
}
}
}
2013-11-22 16:51:26 +01:00
2013-08-07 20:47:47 +02:00
this . stop = function ( ) {
2014-03-05 13:49:15 +01:00
console . log ( "Printer:stop" ) ;
2016-05-11 12:22:54 +02:00
var self = this ;
if ( self . retrySendPrintPartDelay !== undefined ) {
clearTimeout ( self . retrySendPrintPartDelay ) ;
}
if ( self . waitForBufferSpaceDelay !== undefined ) {
clearTimeout ( self . waitForBufferSpaceDelay ) ;
}
2014-03-05 13:49:15 +01:00
endCode = generateEndCode ( ) ;
console . log ( " endCode: " , endCode ) ;
var postData = { gcode : endCode . join ( "\n" ) } ;
2013-08-19 17:55:01 +02:00
if ( communicateWithWifibox ) {
2014-03-05 13:49:15 +01:00
$ . ajax ( {
url : this . wifiboxURL + "/printer/stop" ,
type : "POST" ,
data : postData ,
dataType : 'json' ,
timeout : this . timeoutTime ,
success : function ( data ) {
console . log ( "Printer:stop response: " , data ) ;
// after we know the stop has bin received or failed
2013-11-22 16:51:26 +01:00
// (and the driver had time to update the printer.state)
2013-10-16 18:27:10 +02:00
// we start checking the status again
self . startStatusCheckInterval ( ) ;
2014-03-05 13:49:15 +01:00
}
2013-11-22 16:51:26 +01:00
} ) . fail ( function ( ) {
2013-08-19 17:55:01 +02:00
console . log ( "Printer:stop: failed" ) ;
clearTimeout ( self . retryStopDelay ) ;
self . retryStopDelay = setTimeout ( function ( ) { self . stop ( ) } , self . retryDelay ) ; // retry after delay
2013-11-22 16:51:26 +01:00
2013-10-16 18:27:10 +02:00
// after we know the stop has bin received or failed
2013-11-22 16:51:26 +01:00
// (and the driver had time to update the printer.state)
2013-10-16 18:27:10 +02:00
// we start checking the status again
self . startStatusCheckInterval ( ) ;
2013-08-19 17:55:01 +02:00
} ) ;
} else {
2014-03-05 17:35:01 +01:00
console . log ( "Printer >> f:stop() >> communicateWithWifibox is false, so not executing this function" ) ;
2014-03-05 13:49:15 +01:00
}
2013-08-07 20:47:47 +02:00
}
2014-03-05 13:49:15 +01:00
2013-10-16 18:27:10 +02:00
this . startStatusCheckInterval = function ( ) {
console . log ( "Printer:startStatusCheckInterval" ) ;
self . checkStatus ( ) ;
clearTimeout ( self . checkStatusDelay ) ;
clearTimeout ( self . retryCheckStatusDelay ) ;
self . checkStatusDelay = setTimeout ( function ( ) { self . checkStatus ( ) } , self . checkStatusInterval ) ;
}
2014-03-05 13:49:15 +01:00
2013-10-16 18:27:10 +02:00
this . stopStatusCheckInterval = function ( ) {
console . log ( "Printer:stopStatusCheckInterval" ) ;
clearTimeout ( self . checkStatusDelay ) ;
clearTimeout ( self . retryCheckStatusDelay ) ;
}
2014-03-05 13:49:15 +01:00
2013-10-11 12:39:05 +02:00
this . checkStatus = function ( ) {
2014-01-20 12:58:21 +01:00
//console.log("Printer:checkStatus");
2013-10-14 17:40:56 +02:00
this . stateOverruled = false ;
2013-10-18 19:11:10 +02:00
//console.log(" stateOverruled: ",this.stateOverruled);
2014-03-05 13:49:15 +01:00
var self = this ;
if ( communicateWithWifibox ) {
$ . ajax ( {
url : this . wifiboxURL + "/info/status" ,
dataType : 'json' ,
timeout : this . timeoutTime ,
success : function ( response ) {
//console.log(" Printer:status: ",response.data.state); //," response: ",response);
self . handleStatusUpdate ( response ) ;
clearTimeout ( self . checkStatusDelay ) ;
clearTimeout ( self . retryCheckStatusDelay ) ;
self . checkStatusDelay = setTimeout ( function ( ) { self . checkStatus ( ) } , self . checkStatusInterval ) ;
}
} ) . fail ( function ( ) {
console . log ( "Printer:checkStatus: failed" ) ;
self . state = Printer . WIFIBOX _DISCONNECTED _STATE ;
clearTimeout ( self . checkStatusDelay ) ;
clearTimeout ( self . retryCheckStatusDelay ) ;
self . retryCheckStatusDelay = setTimeout ( function ( ) { self . checkStatus ( ) } , self . retryDelay ) ; // retry after delay
$ ( document ) . trigger ( Printer . UPDATE ) ;
} ) ;
} else {
console . log ( "Printer >> f:checkStatus() >> communicateWithWifibox is false, so not executing this function" ) ;
}
2013-08-07 20:47:47 +02:00
}
2014-03-05 13:49:15 +01:00
2013-10-11 12:39:05 +02:00
this . handleStatusUpdate = function ( response ) {
2014-01-20 12:58:21 +01:00
//console.log("Printer:handleStatusUpdate response: ",response);
2013-10-11 12:39:05 +02:00
var data = response . data ;
if ( response . status != "success" ) {
self . state = Printer . UNKNOWN _STATE ;
} else {
// state
2013-10-18 19:11:10 +02:00
//console.log(" stateOverruled: ",this.stateOverruled);
2013-10-14 17:40:56 +02:00
if ( ! this . stateOverruled ) {
2014-03-05 17:35:01 +01:00
self . state = data . state ;
2013-10-18 19:11:10 +02:00
//console.log(" state > ",self.state);
2013-10-14 17:40:56 +02:00
}
2013-11-22 16:51:26 +01:00
2013-10-11 12:39:05 +02:00
// temperature
2014-03-05 17:35:01 +01:00
self . temperature = data . hotend ;
self . targetTemperature = data . hotend _target ;
2013-11-22 16:51:26 +01:00
2013-10-11 12:39:05 +02:00
// progress
2014-03-05 17:35:01 +01:00
self . currentLine = data . current _line ;
self . totalLines = data . total _lines ;
2016-02-12 17:34:05 +01:00
self . bufferedLines = data . buffered _lines ;
self . bufferSize = data . buffer _size ;
self . maxBufferSize = data . max _buffer _size ;
2013-09-18 18:55:37 +02:00
2013-10-11 12:39:05 +02:00
// access
2014-03-05 17:35:01 +01:00
self . hasControl = data . has _control ;
2013-11-22 16:51:26 +01:00
2013-10-11 12:39:05 +02:00
if ( self . state == Printer . PRINTING _STATE || self . state == Printer . STOPPING _STATE ) {
console . log ( "progress: " , self . currentLine + "/" + self . totalLines + " (" + self . bufferedLines + ") (" + self . state + ")" ) ;
}
}
$ ( document ) . trigger ( Printer . UPDATE ) ;
2013-09-18 18:55:37 +02:00
}
2014-03-05 13:49:15 +01:00
2013-10-14 17:40:56 +02:00
this . overruleState = function ( newState ) {
this . stateOverruled = true ;
console . log ( " stateOverruled: " , this . stateOverruled ) ;
2013-11-22 16:51:26 +01:00
2013-10-14 17:40:56 +02:00
self . state = newState ;
2013-11-22 16:51:26 +01:00
2013-10-14 17:40:56 +02:00
$ ( document ) . trigger ( Printer . UPDATE ) ;
2013-11-22 16:51:26 +01:00
2013-10-16 18:27:10 +02:00
this . stopStatusCheckInterval ( ) ;
2013-10-14 17:40:56 +02:00
}
2013-11-22 16:51:26 +01:00
2013-10-18 16:38:20 +02:00
this . removeLeaveWarning = function ( ) {
window . onbeforeunload = null ;
}
2014-03-05 13:49:15 +01:00
2013-10-18 16:38:20 +02:00
this . addLeaveWarning = function ( ) {
window . onbeforeunload = function ( ) {
2014-03-05 13:49:15 +01:00
console . log ( "WARNING:" + Printer . ON _BEFORE _UNLOAD _MESSAGE ) ;
return Printer . ON _BEFORE _UNLOAD _MESSAGE ;
2013-10-18 16:38:20 +02:00
} ;
}
2013-11-22 16:51:26 +01:00
}