From f8b9a392dd7f5ee0275adedfe336c1c653f042cf Mon Sep 17 00:00:00 2001 From: Core447 Date: Sat, 20 Apr 2024 09:02:00 +0200 Subject: [PATCH] Fix: App freeze if ip is invalid --- OBSActionBase.py | 22 +++++++++++++++++----- actions/RecPlayPause/RecPlayPause.py | 7 ++++--- actions/SwitchScene/SwitchScene.py | 2 +- actions/ToggleRecord/ToggleRecord.py | 5 +++-- main.py | 9 ++++++++- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/OBSActionBase.py b/OBSActionBase.py index 41d916d..49b61ea 100644 --- a/OBSActionBase.py +++ b/OBSActionBase.py @@ -10,6 +10,9 @@ gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") from gi.repository import Gtk, Adw +import threading +from loguru import logger as log + class OBSActionBase(ActionBase): def __init__(self, action_id: str, action_name: str, deck_controller: "DeckController", page: Page, coords: str, plugin_base: PluginBase): @@ -70,15 +73,24 @@ class OBSActionBase(ActionBase): self.reconnect_obs() def reconnect_obs(self): - self.plugin_base.backend.connect_to( - host=self.plugin_base.get_settings().get("ip"), - port=self.plugin_base.get_settings().get("port"), - password=self.plugin_base.get_settings().get("password"), - timeout=3, legacy=False) + threading.Thread(target=self._reconnect_obs, daemon=True, name="reconnect_obs").start() + + def _reconnect_obs(self): + try: + self.plugin_base.backend.connect_to( + host=self.plugin_base.get_settings().get("ip"), + port=self.plugin_base.get_settings().get("port"), + password=self.plugin_base.get_settings().get("password"), + timeout=3, legacy=False) + except Exception as e: + log.error(e) self.update_status_label() def update_status_label(self) -> None: + threading.Thread(target=self._update_status_label, daemon=True, name="update_status_label").start() + + def _update_status_label(self): if self.plugin_base.backend.get_connected(): self.status_label.set_label(self.plugin_base.lm.get("actions.base.status.connected")) self.status_label.remove_css_class("red") diff --git a/actions/RecPlayPause/RecPlayPause.py b/actions/RecPlayPause/RecPlayPause.py index 7ce2d03..9ff9503 100644 --- a/actions/RecPlayPause/RecPlayPause.py +++ b/actions/RecPlayPause/RecPlayPause.py @@ -1,3 +1,4 @@ +import threading from plugins.com_core447_OBSPlugin.OBSActionBase import OBSActionBase from src.backend.DeckManagement.DeckController import DeckController from src.backend.PageManagement.Page import Page @@ -14,15 +15,15 @@ class RecPlayPause(OBSActionBase): def on_ready(self): # Connect to obs if not connected if self.plugin_base.backend is not None: - if not self.plugin_base.backend.get_connected(): + if not self.plugin_base.get_connected(): # self.plugin_base.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) self.reconnect_obs() # Show current rec status - self.show_current_rec_status() + threading.Thread(target=self.show_current_rec_status, daemon=True, name="show_current_rec_status").start() def show_current_rec_status(self, new_paused = False): - if not self.plugin_base.backend.get_connected(): + if not self.plugin_base.get_connected(): self.set_media(media_path=os.path.join(self.plugin_base.PATH, "assets", "error.png")) return status = self.plugin_base.backend.get_record_status() diff --git a/actions/SwitchScene/SwitchScene.py b/actions/SwitchScene/SwitchScene.py index 7a4e2bb..8a61a89 100644 --- a/actions/SwitchScene/SwitchScene.py +++ b/actions/SwitchScene/SwitchScene.py @@ -21,7 +21,7 @@ class SwitchScene(OBSActionBase): def on_ready(self): # Connect to obs if not connected if self.plugin_base.backend is not None: - if not self.plugin_base.backend.get_connected(): # self.plugin_base.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) + if not self.plugin_base.get_connected(): # self.plugin_base.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) self.reconnect_obs() media_path = os.path.join(self.plugin_base.PATH, "assets", "transition_slide.png") diff --git a/actions/ToggleRecord/ToggleRecord.py b/actions/ToggleRecord/ToggleRecord.py index a7851fe..6a035d7 100644 --- a/actions/ToggleRecord/ToggleRecord.py +++ b/actions/ToggleRecord/ToggleRecord.py @@ -1,4 +1,5 @@ # from ...OBSActionBase import OBSActionBase +import threading from plugins.com_core447_OBSPlugin.OBSActionBase import OBSActionBase from src.backend.DeckManagement.DeckController import DeckController from src.backend.PageManagement.Page import Page @@ -16,11 +17,11 @@ class ToggleRecord(OBSActionBase): self.current_state = -1 # Connect to obs if not connected if self.plugin_base.backend is not None: - if not self.plugin_base.backend.get_connected(): # self.plugin_base.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) + if not self.plugin_base.get_connected(): # self.plugin_base.obs.connect_to(host="localhost", port=4444, timeout=3, legacy=False) self.reconnect_obs() # Show current rec status - self.show_current_rec_status() + threading.Thread(target=self.show_current_rec_status, daemon=True, name="show_current_rec_status").start() def show_current_rec_status(self, new_paused = False): if self.plugin_base.backend is None: diff --git a/main.py b/main.py index 4aea471..30f3503 100644 --- a/main.py +++ b/main.py @@ -68,4 +68,11 @@ class OBS(PluginBase): self.add_action_holder(switch_scene_action_holder) # Load custom css - self.add_css_stylesheet(os.path.join(self.PATH, "style.css")) \ No newline at end of file + self.add_css_stylesheet(os.path.join(self.PATH, "style.css")) + + def get_connected(self): + try: + return self.backend.get_connected() + except Exception as e: + log.error(e) + return False \ No newline at end of file