From e7f53844274170d21687e332c927e7ec870b2d59 Mon Sep 17 00:00:00 2001 From: Bryan Mayland Date: Sun, 21 Jul 2013 06:48:06 -0400 Subject: [PATCH] Refresh gcode visualization if the selected file is modified This passes the file modification time to the javascript side and if they don't match then the file is re-downloaded and parsed. The mtime is also used in the AJAX request as it prevents the browser from serving the new request from cache. --- octoprint/printer.py | 8 +++++++- octoprint/static/js/ui.js | 10 +++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/octoprint/printer.py b/octoprint/printer.py index 8844a6d..d5b56fa 100644 --- a/octoprint/printer.py +++ b/octoprint/printer.py @@ -303,10 +303,16 @@ class Printer(): formattedFilename = None formattedFilesize = None estimatedPrintTime = None + fileMTime = None filament = None if filename: formattedFilename = os.path.basename(filename) + # Use a string for mtime because it could be float and the + # javascript needs to exact match + if not sd: + fileMTime = str(os.stat(filename).st_mtime) + if filesize: formattedFilesize = util.getFormattedSize(filesize) @@ -317,7 +323,7 @@ class Printer(): if "filament" in fileData["gcodeAnalysis"].keys(): filament = fileData["gcodeAnalysis"]["filament"] - self._stateMonitor.setJobData({"filename": formattedFilename, "filesize": formattedFilesize, "estimatedPrintTime": estimatedPrintTime, "filament": filament, "sd": sd}) + self._stateMonitor.setJobData({"filename": formattedFilename, "filesize": formattedFilesize, "estimatedPrintTime": estimatedPrintTime, "filament": filament, "sd": sd, "mtime": fileMTime}) def _sendInitialStateUpdate(self, callback): try: diff --git a/octoprint/static/js/ui.js b/octoprint/static/js/ui.js index c11ab6c..c309f4f 100644 --- a/octoprint/static/js/ui.js +++ b/octoprint/static/js/ui.js @@ -1098,6 +1098,7 @@ function GcodeViewModel(loginStateViewModel) { self.loginState = loginStateViewModel; self.loadedFilename = undefined; + self.loadedFileMTime = undefined; self.status = 'idle'; self.enabled = false; @@ -1108,16 +1109,18 @@ function GcodeViewModel(loginStateViewModel) { GCODE.ui.initHandlers(); } - self.loadFile = function(filename){ + self.loadFile = function(filename, mtime){ if (self.status == 'idle' && self.errorCount < 3) { self.status = 'request'; $.ajax({ url: AJAX_BASEURL + "gcodefiles/" + filename, + data: { "mtime": mtime }, type: "GET", success: function(response, rstatus) { if(rstatus === 'success'){ self.showGCodeViewer(response, rstatus); self.loadedFilename=filename; + self.loadedFileMTime=mtime; self.status = 'idle'; } }, @@ -1148,7 +1151,8 @@ function GcodeViewModel(loginStateViewModel) { if (!self.enabled) return; if (!data.job.filename) return; - if(self.loadedFilename && self.loadedFilename == data.job.filename) { + if(self.loadedFilename && self.loadedFilename == data.job.filename && + self.loadedFileMTime == data.job.mtime) { if (data.state.flags && (data.state.flags.printing || data.state.flags.paused)) { var cmdIndex = GCODE.gCodeReader.getCmdIndexForPercentage(data.progress.progress * 100); if(cmdIndex){ @@ -1158,7 +1162,7 @@ function GcodeViewModel(loginStateViewModel) { } self.errorCount = 0 } else if (data.job.filename) { - self.loadFile(data.job.filename); + self.loadFile(data.job.filename, data.job.mtime); } }