From dbe3f03019ba242bb4c671c07a88cca511ebebaa Mon Sep 17 00:00:00 2001 From: alfrix Date: Thu, 15 Jun 2023 09:03:10 -0300 Subject: [PATCH] refactor graph update timer --- panels/main_menu.py | 21 +++++++++++++-------- panels/temperature.py | 9 ++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/panels/main_menu.py b/panels/main_menu.py index c1fd1280..3b6f846f 100644 --- a/panels/main_menu.py +++ b/panels/main_menu.py @@ -17,6 +17,7 @@ def create_panel(*args, **kwargs): class MainPanel(MenuPanel): def __init__(self, screen, title, items=None): super().__init__(screen, title, items) + self.graph_retry_timeout = None self.left_panel = None self.devices = {} self.graph_update = None @@ -33,8 +34,6 @@ class MainPanel(MenuPanel): if stats["temperature_devices"]["count"] > 0 or stats["extruders"]["count"] > 0: self._gtk.reset_temp_color() self.main_menu.attach(self.create_left_panel(), 0, 0, 1, 1) - else: - self.graph_update = False if self._screen.vertical_mode: self.labels['menu'] = self.arrangeMenuItems(items, 3, True) self.main_menu.attach(self.labels['menu'], 0, 1, 1, 1) @@ -47,7 +46,8 @@ class MainPanel(MenuPanel): if self.left_panel is None or not self._printer.get_temp_store_devices(): self.graph_retry += 1 if self.graph_retry < 5: - GLib.timeout_add_seconds(5, self.update_graph_visibility) + if self.graph_retry_timeout is None: + self.graph_retry_timeout = GLib.timeout_add_seconds(5, self.update_graph_visibility) else: logging.debug(f"Could not create graph {self.left_panel} {self._printer.get_temp_store_devices()}") return False @@ -69,23 +69,28 @@ class MainPanel(MenuPanel): self.left_panel.add(self.labels['da']) self.labels['da'].queue_draw() self.labels['da'].show() + if self.graph_update is None: + # This has a high impact on load + self.graph_update = GLib.timeout_add_seconds(5, self.update_graph) elif self.labels['da'] in self.left_panel: self.left_panel.remove(self.labels['da']) + if self.graph_update is not None: + GLib.source_remove(self.graph_update) + self.graph_update = None self.graph_retry = 0 return False def activate(self): - # For this case False != None - if self.graph_update is None: - # This has a high impact on load - self.graph_update = GLib.timeout_add_seconds(5, self.update_graph) self.update_graph_visibility() self._screen.base_panel_show_all() def deactivate(self): - if self.graph_update: + if self.graph_update is not None: GLib.source_remove(self.graph_update) self.graph_update = None + if self.graph_retry_timeout is not None: + GLib.source_remove(self.graph_retry_timeout) + self.graph_retry_timeout = None if self.active_heater is not None: self.hide_numpad() diff --git a/panels/temperature.py b/panels/temperature.py index 06433ac9..939eb7b1 100644 --- a/panels/temperature.py +++ b/panels/temperature.py @@ -194,13 +194,16 @@ class TemperaturePanel(ScreenPanel): self.left_panel.add(self.labels['da']) self.labels['da'].queue_draw() self.labels['da'].show() + if self.graph_update is None: + # This has a high impact on load + self.graph_update = GLib.timeout_add_seconds(5, self.update_graph) elif self.labels['da'] in self.left_panel: self.left_panel.remove(self.labels['da']) + if self.graph_update is not None: + GLib.source_remove(self.graph_update) + self.graph_update = None def activate(self): - if self.graph_update is None: - # This has a high impact on load - self.graph_update = GLib.timeout_add_seconds(5, self.update_graph) self.update_graph_visibility() def deactivate(self):