Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrintTimeGenius aware print progress #134

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions octoprint_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -165,14 +168,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")

Expand All @@ -192,18 +191,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)
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")
Expand Down Expand Up @@ -459,7 +474,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

Expand Down