From 0737ee65bd6aadc3b609860aa1a8b0e5d7c36051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Sat, 20 Dec 2014 00:31:46 +0100 Subject: [PATCH 1/2] Support action: commands sent from printer, for now only pause, resume and disconnect are supported, plus a new hook "octoprint.comm.protocol.action" to add handlers of custom actions via plugins. Added "!!DEBUG:" command to virtual printer to be able to trigger such commands to be sent by it. --- src/octoprint/printer/__init__.py | 3 +++ src/octoprint/util/comm.py | 25 +++++++++++++++++++++++++ src/octoprint/util/virtual.py | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/octoprint/printer/__init__.py b/src/octoprint/printer/__init__.py index 05b7c6443..73839462e 100644 --- a/src/octoprint/printer/__init__.py +++ b/src/octoprint/printer/__init__.py @@ -658,6 +658,9 @@ class Printer(): def mcReceivedRegisteredMessage(self, command, output): self._sendFeedbackCommandOutput(command, output) + def mcForceDisconnect(self): + self.disconnect() + #~~ sd file handling def getSdFiles(self): diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 149a5db33..1ba81bd87 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -161,6 +161,7 @@ class MachineCom(object): # hooks self._pluginManager = octoprint.plugin.plugin_manager() self._gcode_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.gcode") + self._printer_action_hooks = self._pluginManager.get_hooks("octoprint.comm.protocol.action") # SD status data self._sdAvailable = False @@ -670,6 +671,27 @@ class MachineCom(object): if line.strip() is not "": self._timeout = getNewTimeout("communication") + ##~~ debugging output handling + if line.startswith("//"): + debugging_output = line[2:].strip() + if debugging_output.startswith("action:"): + action_command = debugging_output[len("action:"):].strip() + + if action_command == "pause": + self._log("Pausing on request of the printer...") + self.setPause(True) + elif action_command == "resume": + self._log("Resuming on request of the printer...") + self.setPause(False) + elif action_command == "disconnect": + self._log("Disconnecting on request of the printer...") + self._callback.mcForceDisconnect() + else: + for hook in self._printer_action_hooks: + self._printer_action_hooks[hook](self, line, action_command) + else: + continue + ##~~ Error handling line = self._handleErrors(line) @@ -1311,6 +1333,9 @@ class MachineComPrintCallback(object): def mcReceivedRegisteredMessage(self, command, message): pass + def mcForceDisconnect(self): + pass + ### Printing file information classes ################################################################################## class PrintingFileInformation(object): diff --git a/src/octoprint/util/virtual.py b/src/octoprint/util/virtual.py index 9ddb43851..62e63b7a0 100644 --- a/src/octoprint/util/virtual.py +++ b/src/octoprint/util/virtual.py @@ -227,6 +227,8 @@ class VirtualPrinter(): self._relative = True elif "G92" in data: self._setPosition(data) + elif data.startswith("!!DEBUG:"): + self._debugTrigger(data[len("!!DEBUG:"):].strip()) elif data.startswith("G0") or data.startswith("G1") or data.startswith("G2") or data.startswith("G3") \ or data.startswith("G28") or data.startswith("G29") or data.startswith("G30") \ @@ -237,6 +239,14 @@ class VirtualPrinter(): if len(data.strip()) > 0: self._sendOk() + def _debugTrigger(self, data): + if data == "action_pause": + self.outgoing.put("// action:pause") + elif data == "action_resume": + self.outgoing.put("// action:resume") + elif data == "action_disconnect": + self.outgoing.put("// action:disconnect") + def _listSd(self): self.outgoing.put("Begin file list") if settings().getBoolean(["devel", "virtualPrinter", "extendedSdFileList"]): From 256c2dfdbe49928e5f83f8bd1fdfa75a38c8a59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Sat, 20 Dec 2014 01:06:40 +0100 Subject: [PATCH 2/2] More error resilient handling of .metadata.yaml writes in local storage Writes updated metadata to a backup file first and then copies it atomically (at least under anything that is not windows where the operation is a bit more complicated and hence not atomic) --- src/octoprint/filemanager/storage.py | 19 +++++++++++++------ src/octoprint/util/__init__.py | 10 ++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 54d13a434..b2cf61425 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -9,9 +9,12 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import logging import os import pylru +import tempfile import octoprint.filemanager +from octoprint.util import safeRename + class StorageInterface(object): @@ -867,12 +870,16 @@ class LocalFileStorage(StorageInterface): def _save_metadata(self, path, metadata): metadata_path = os.path.join(path, ".metadata.yaml") + fh, metadata_temporary_path = tempfile.mkstemp() + os.close(fh) + with self._metadata_lock: - with open(metadata_path, "w") as f: - try: + try: + with open(metadata_temporary_path, "w") as f: import yaml yaml.safe_dump(metadata, stream=f, default_flow_style=False, indent=" ", allow_unicode=True) - except: - self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals())) - else: - self._metadata_cache[path] = metadata + safeRename(metadata_temporary_path, metadata_path, throw_error=True) + except: + self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals())) + else: + self._metadata_cache[path] = metadata diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 8f6b1537a..2c7b72bd6 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -147,7 +147,7 @@ def findCollisionfreeName(input, extension, existingFilenames): raise ValueError("Can't create a collision free filename") -def safeRename(old, new): +def safeRename(old, new, throw_error=False): """ Safely renames a file. @@ -172,12 +172,14 @@ def safeRename(old, new): os.rename(new, backup) os.rename(old, new) os.remove(backup) - except OSError: + except OSError as e: # if anything went wrong, try to rename the backup file to its original name logger.error("Could not perform safe rename, trying to revert") if os.path.exists(backup): - os.remove(new) - os.rename(backup, new) + silentRemove(new) + os.rename(backup, new) + if throw_error: + raise e else: # on anything else than windows it's ooooh so much easier... os.rename(old, new)