splash_screen: limit reconnections, clear log, add reconnect button

This commit is contained in:
alfrix 2022-11-11 10:25:55 -03:00
parent f05cbe9f0b
commit 8b55d2f2b1
3 changed files with 37 additions and 17 deletions

View File

@ -44,17 +44,15 @@ class KlippyRest:
try:
r = requests.get(url, headers=headers)
except Exception as e:
logging.critical(e, exc_info=True)
logging.error(e)
return False
if r.status_code != 200:
return False
# TODO: Try/except
try:
data = json.loads(r.content)
except Exception as e:
logging.critical(e, exc_info=True)
logging.exception(f"Unable to parse response from moonraker:\n {r.content}")
logging.error(f"Unable to parse response from moonraker:\n {r.content}")
return False
return data

View File

@ -33,6 +33,7 @@ class KlippyWebsocket(threading.Thread):
callback_table = {}
reconnect_timeout = None
reconnect_count = 0
max_retries = 4
def __init__(self, screen, callback, host, port):
threading.Thread.__init__(self)
@ -59,7 +60,8 @@ class KlippyWebsocket(threading.Thread):
def initial_connect(self):
# Enable a timeout so that way if moonraker is not running, it will attempt to reconnect
if self.reconnect_timeout is None:
self.reconnect_count = 0
if not self.reconnect_timeout:
self.reconnect_timeout = GLib.timeout_add_seconds(6, self.reconnect)
self.connect()
@ -76,10 +78,16 @@ class KlippyWebsocket(threading.Thread):
def ws_on_open(ws):
self.on_open(ws)
self.reconnect_count += 1
try:
state = self._screen.apiclient.get_server_info()
if state is False:
if self.reconnect_count > 3:
if self.reconnect_count > self.max_retries:
self._screen.panels['splash_screen'].add_retry_button()
self._screen.panels['splash_screen'].update_text(
_("Cannot connect to Moonraker")
+ f'\n\n{self._url}')
elif self.reconnect_count > 2:
self._screen.panels['splash_screen'].update_text(
_("Cannot connect to Moonraker")
+ f'\n\n{self._url}\n\n'
@ -159,6 +167,7 @@ class KlippyWebsocket(threading.Thread):
logging.info(f"Self.connected = {self.is_connected()}")
self.connected = True
self.reconnect_count = 0
self._screen.panels['splash_screen'].remove_retry_button()
if self.reconnect_timeout is not None:
GLib.source_remove(self.reconnect_timeout)
self.reconnect_timeout = None
@ -200,9 +209,10 @@ class KlippyWebsocket(threading.Thread):
if self.is_connected():
logging.debug("Reconnected")
return False
if self.reconnect_count > self.max_retries:
logging.debug("Stopping reconnections")
return False
logging.debug("Attempting to reconnect")
self.reconnect_count += 1
self.connect()
return True

View File

@ -13,13 +13,12 @@ def create_panel(*args):
class SplashScreenPanel(ScreenPanel):
box = None
def __init__(self, screen, title, back=True):
super().__init__(screen, title, back)
def initialize(self, panel_name):
self.retry_button = False
image = self._gtk.Image("klipper", self._screen.width / 5, self._screen.height * .5)
self.labels['text'] = Gtk.Label(_("Initializing printer..."))
self.labels['text'].set_line_wrap(True)
@ -37,6 +36,9 @@ class SplashScreenPanel(ScreenPanel):
self.labels['restart_system'].connect("clicked", self.restart_system)
self.labels['shutdown'] = self._gtk.ButtonImage("shutdown", _('System Shutdown'), "color2")
self.labels['shutdown'].connect("clicked", self.shutdown)
self.labels['retry'] = self._gtk.ButtonImage("load", _('Retry'), "color3")
self.labels['retry'].connect("clicked", self.retry)
self.labels['retry'].connect("clicked", self.remove_retry_button)
self.labels['actions'] = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.labels['actions'].set_hexpand(True)
@ -45,13 +47,9 @@ class SplashScreenPanel(ScreenPanel):
self.labels['actions'].set_homogeneous(True)
self.labels['actions'].set_size_request(self._screen.base_panel.content.get_allocation().width, 0)
scroll = Gtk.ScrolledWindow()
scroll.set_property("overlay-scrolling", False)
scroll = self._gtk.ScrolledWindow()
scroll.set_hexpand(True)
scroll.set_vexpand(True)
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scroll.add_events(Gdk.EventMask.TOUCH_MASK)
scroll.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
scroll.add(self.labels['text'])
info = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
@ -83,8 +81,9 @@ class SplashScreenPanel(ScreenPanel):
if printer_cfg is not None:
power_devices = printer_cfg.get("power_devices", "")
power_devices = [str(i.strip()) for i in power_devices.split(',')]
logging.info(f"Associated power devices: {power_devices}")
self.add_power_button(self._screen.search_power_devices(power_devices))
if power_devices[0]:
logging.info(f"Associated power devices: {power_devices}")
self.add_power_button(self._screen.search_power_devices(power_devices))
if self._screen.printer is not None and self._screen.printer.state != "disconnected":
self.labels['actions'].add(self.labels['restart'])
@ -93,6 +92,8 @@ class SplashScreenPanel(ScreenPanel):
self.labels['actions'].add(self.labels['restart_system'])
self.labels['actions'].add(self.labels['shutdown'])
self.labels['actions'].add(self.labels['menu'])
if self.retry_button:
self.labels['actions'].add(self.labels['retry'])
self.labels['actions'].show_all()
def add_power_button(self, powerdevs):
@ -102,6 +103,14 @@ class SplashScreenPanel(ScreenPanel):
self.check_power_status()
self.labels['actions'].add(self.labels['power'])
def add_retry_button(self):
self.retry_button = True
def remove_retry_button(self, *args):
self.retry_button = False
self.update_text((_("Connecting to %s") % self._screen.connecting_to_printer))
self.show_restart_buttons()
def activate(self):
self.check_power_status()
self._screen.base_panel.show_macro_shortcut(False)
@ -144,3 +153,6 @@ class SplashScreenPanel(ScreenPanel):
else:
logging.info("OS Reboot")
os.system("systemctl reboot")
def retry(self, widget):
self._screen._ws.initial_connect()