From 0d96f1b8cadaef1d447682b0905eca68d789750c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 23 Sep 2020 18:55:29 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20hook=20octoprint.printer.addi?= =?UTF-8?q?tional=5Fstate=5Fdata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use to inject additional data into the regular state pushes to PrinterCallbacks, including the push socket. As requested in OctoPrint/plugins.octoprint.org#553 --- docs/plugins/hooks.rst | 29 +++++++++++++++++++++++ src/octoprint/printer/standard.py | 39 ++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index ca1e0e37b..0d008ce53 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -1153,6 +1153,35 @@ octoprint.plugin.softwareupdate.check_config See :ref:`here `. +.. _sec-plugins-hooks-plugin-printer-additional_state_data + +octoprint.printer.additional_state_data +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. py:function:: additional_state_data_hook(initial=False, *args, **kwargs) + + .. versionadded:: 1.5.0 + + Use this to inject additional data into the data structure returned from the printer backend to the frontend + on the push socket or other registered :ref:`~octoprint.printer.PrinterCallback`s. Anything you return here + will be located beneath ``plugins.`` in the resulting initial and current data push structure. + + The ``initial`` parameter will be ``True`` if this the additional update sent to the callback. Your handler should + return a ``dict``, or ``None`` if nothing should be included. + + .. warning:: + + Make sure to not perform any computationally expensive or otherwise long running actions within these handlers as + you could stall the whole state monitor and thus updates being pushed to the frontend. + + This includes I/O of any kind. + + Cache your data! + + :param boolean initial: True if this is the initial update, False otherwise + :return: Additional data to include + :rtype: dict + .. _sec-plugins-hook-printer-factory: octoprint.printer.factory diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index f27da0150..a94d3eb24 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -112,6 +112,9 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): self._lastProgressReport = None self._progressPlugins = plugin_manager().get_implementations(ProgressPlugin) + self._additional_data_hooks = plugin_manager().get_hooks("octoprint.printer.additional_state_data") + self._blacklisted_data_hooks = [] + self._stateMonitor = StateMonitor( interval=0.5, on_update=self._sendCurrentDataCallbacks, @@ -200,13 +203,34 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): extra=dict(callback=fqcn(callback))) def _sendCurrentDataCallbacks(self, data): + plugin_data = self._get_additional_plugin_data(initial=False) for callback in self._callbacks: try: - callback.on_printer_send_current_data(copy.deepcopy(data)) + data_copy = copy.deepcopy(data) + if plugin_data: + data_copy.update(plugins=copy.deepcopy(plugin_data)) + callback.on_printer_send_current_data(data_copy) except Exception: self._logger.exception("Exception while pushing current data to callback {}".format(callback), extra=dict(callback=fqcn(callback))) + def _get_additional_plugin_data(self, initial=False): + plugin_data = {} + + for name, hook in self._additional_data_hooks.items(): + if name in self._blacklisted_data_hooks: + continue + try: + additional = hook(initial=initial) + if additional and isinstance(additional, dict): + plugin_data[name] = additional + except Exception: + self._logger.exception("Error while retrieving additional data from plugin {}, blacklisting it for further loops".format(name), + extra={"plugin": name}) + self._blacklisted_data_hooks.append(name) + + return plugin_data + #~~ callback from metadata analysis event def _on_event_MetadataAnalysisFinished(self, event, data): @@ -1040,11 +1064,14 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): def _sendInitialStateUpdate(self, callback): try: data = self._stateMonitor.get_current_data() - data.update({ - "temps": list(self._temps), - "logs": list(self._log), - "messages": list(self._messages) - }) + data.update(temps=list(self._temps), + logs=list(self._log), + messages=list(self._messages)) + + plugin_data = self._get_additional_plugin_data(initial=False) + if plugin_data: + data.update(plugins=copy.deepcopy(plugin_data)) + callback.on_printer_send_initial_data(data) except Exception: self._logger.exception(u"Error while pushing initial state update to callback {}".format(callback),