From 0495c92cecb0edc29799fedab6bc9e296e4496e1 Mon Sep 17 00:00:00 2001 From: jneilliii Date: Thu, 27 Apr 2023 00:21:02 -0400 Subject: [PATCH] switch print progress to a 5 second repeated timer on print start to allow for use of time based progress percentage provided by Print Time Genius, #103 --- octoprint_mqtt/__init__.py | 41 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/octoprint_mqtt/__init__.py b/octoprint_mqtt/__init__.py index ad5469c..d6e6c32 100644 --- a/octoprint_mqtt/__init__.py +++ b/octoprint_mqtt/__init__.py @@ -9,7 +9,7 @@ import octoprint.plugin from octoprint.events import Events -from octoprint.util import dict_minimal_mergediff +from octoprint.util import dict_minimal_mergediff, RepeatedTimer class MqttPlugin(octoprint.plugin.SettingsPlugin, @@ -58,6 +58,9 @@ def __init__(self): self.lastTemp = {} + self.progress_timer = None + self.last_progress = {"storage": "", "path": "", "progress": -1} + def initialize(self): self._printer.register_callback(self) @@ -164,14 +167,10 @@ def on_settings_save(self, data): ##~~ EventHandlerPlugin API def on_event(self, event, payload): - if event == Events.PRINT_STARTED: - self.on_print_progress(payload["origin"], payload["path"], 0) - elif event == Events.PRINT_DONE: - self.on_print_progress(payload["origin"], payload["path"], 100) - elif event == Events.FILE_SELECTED: - self.on_print_progress(payload["origin"], payload["path"], 0) - elif event == Events.FILE_DESELECTED: - self.on_print_progress("", "", 0) + if event in [Events.PRINT_STARTED, Events.PRINT_DONE, Events.FILE_SELECTED, Events.FILE_DESELECTED]: + if self.progress_timer is None: + self.progress_timer = RepeatedTimer(5, self._update_progress, [payload["origin"], payload["path"]]) + self.progress_timer.start() topic = self._get_topic("event") @@ -186,18 +185,34 @@ def on_event(self, event, payload): ##~~ ProgressPlugin API - def on_print_progress(self, storage, path, progress): + def _update_progress(self, storage, path): topic = self._get_topic("progress") if topic: + printer_data = self._printer.get_current_data() + print_job_progress = printer_data["progress"] + progress = 0 + + if "completion" in print_job_progress and print_job_progress["completion"] is not None: + progress = round(float(print_job_progress["completion"])) + if "printTimeLeftOrigin" in print_job_progress and print_job_progress["printTimeLeftOrigin"] == "genius": + progress = round(float(print_job_progress["printTime"] or 0) / (float(print_job_progress["printTime"] or 0) + float(print_job_progress["printTimeLeft"])) * 100) + + if print_job_progress.get("completion") in [None, 100]: + if self.progress_timer is not None: + self.progress_timer.cancel() + self.progress_timer = None + data = dict(location=storage, path=path, progress=progress) if self._settings.get_boolean(["publish", "printerData"]): - data['printer_data'] = self._printer.get_current_data() + data['printer_data'] = printer_data - self.mqtt_publish_with_timestamp(topic.format(progress="printing"), data, retained=True) + if self.last_progress["progress"] != data["progress"] or self.last_progress["path"] != data["path"]: + self.mqtt_publish_with_timestamp(topic.format(progress="printing"), data, retained=True) + self.last_progress = data def on_slicing_progress(self, slicer, source_location, source_path, destination_location, destination_path, progress): topic = self._get_topic("progress") @@ -446,7 +461,7 @@ def _on_mqtt_connect(self, client, userdata, flags, rc): self._mqtt_connected = True if self._mqtt_reset_state: - self.on_print_progress("", "", 0) + self._update_progress("", "") self.on_slicing_progress("", "", "", "", "", 0) self._mqtt_reset_state = False