✨ New hook octoprint.printer.additional_state_data

Use to inject additional data into the regular state pushes to
PrinterCallbacks, including the push socket.

As requested in OctoPrint/plugins.octoprint.org#553
This commit is contained in:
Gina Häußge committed 2020-09-23 18:55:29 +02:00
1 parent 290de2f15c
commit 0d96f1b8ca
2 files changed
+62 -6

No files matched your search

+29
View File
@@ -1153,6 +1153,35 @@ octoprint.plugin.softwareupdate.check_config
See :ref:`here <sec-bundledplugins-softwareupdate-hooks-check_config>`.
.. _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.<your plugin id>`` 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
+33 -6
View File
@@ -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),