diff --git a/app/connections.py b/app/connections.py index 17f3d741..51602c70 100644 --- a/app/connections.py +++ b/app/connections.py @@ -273,6 +273,7 @@ def telnet(host, port=23, user="", password="", timeout=5): def http_request(host, port, user, password): base_url = "http://{}:{}/api/".format(host, port) init_auth(user, password, base_url) + while True: req_type, ref = yield url = base_url @@ -285,19 +286,22 @@ def http_request(host, port, user, password): elif req_type is HttpRequestType.STREAM: url = base_url + HttpRequestType.STREAM.value - try: - with urlopen(url, timeout=5) as f: - if req_type is HttpRequestType.STREAM: - yield f.read().decode("utf-8") - else: - yield json.loads(f.read().decode("utf-8")) - except (URLError, HTTPError): - yield None + yield from get_json(req_type, url) + + +def get_json(req_type, url): + try: + with urlopen(url, timeout=5) as f: + if req_type is HttpRequestType.STREAM: + yield f.read().decode("utf-8") + else: + yield json.loads(f.read().decode("utf-8")) + except (URLError, HTTPError): + yield None # ***************** Connections testing *******************# - def test_ftp(host, port, user, password, timeout=5): try: with FTP(host=host, user=user, passwd=password, timeout=timeout) as ftp: diff --git a/app/ui/main_app_window.py b/app/ui/main_app_window.py index 5fd07416..f2523f25 100644 --- a/app/ui/main_app_window.py +++ b/app/ui/main_app_window.py @@ -178,7 +178,6 @@ class Application(Gtk.Application): # http api self._http_api = None self._fav_click_mode = None - self._monitor_signal = False # Colors self._use_colors = False self._NEW_COLOR = None # Color for new services in the main list @@ -833,7 +832,7 @@ class Application(Gtk.Application): def open_data(self, data_path=None): """ Opening data and fill views. """ gen = self.update_data(data_path) - GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_LOW) + GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_DEFAULT_IDLE) def update_data(self, data_path): self._wait_dialog.show() @@ -956,7 +955,7 @@ class Application(Gtk.Application): yield True for index, itr in enumerate([row.iter for row in self._services_model]): self._services_model.remove(itr) - if index % 50 == 0: + if index % 25 == 0: yield True yield True self._services_view.set_model(s_model) @@ -1426,7 +1425,7 @@ class Application(Gtk.Application): if not self._bq_selected: return - YtListImportDialog(self._main_window, Profile(self._profile), self.append_imported_services).show() + YtListImportDialog(self._main_window, Profile(self._profile), self.append_imported_services).show() def on_import_m3u(self, item): """ Imports iptv from m3u files. """ @@ -1610,23 +1609,24 @@ class Application(Gtk.Application): self._player_tool_bar.set_visible(full) # ************************ HTTP API ****************************# + @run_task def init_http_api(self): - if self._http_api: - self._http_api.close() - self._http_api = None - prp = self._options.get(self._profile) self._fav_click_mode = FavClickMode(prp.get("fav_click_mode", FavClickMode.DISABLED)) if prp is Profile.NEUTRINO_MP or not prp.get("http_api_support", False): self.update_info_boxes_visible(False) + if self._http_api: + self._http_api.close() + self._http_api = None return - self._http_api = http_request(prp.get("host", "127.0.0.1"), prp.get("http_port", "80"), - prp.get("http_user", ""), prp.get("http_password", "")) - next(self._http_api) - GLib.timeout_add_seconds(1, self.update_receiver_info) + if not self._http_api: + self._http_api = http_request(prp.get("host", "127.0.0.1"), prp.get("http_port", "80"), + prp.get("http_user", ""), prp.get("http_password", "")) + next(self._http_api) + GLib.timeout_add_seconds(1, self.update_receiver_info) def on_watch(self): """ Switch to the channel and watch in the player """ @@ -1655,7 +1655,6 @@ class Application(Gtk.Application): req = self._http_api.send((HttpRequestType.ZAP, ref)) next(self._http_api) if req and req.get("result", False): - GLib.timeout_add_seconds(2, self.update_service_info) GLib.idle_add(scroll_to, path, self._fav_view) if callback is not None: callback() @@ -1682,27 +1681,31 @@ class Application(Gtk.Application): GLib.idle_add(self._receiver_info_box.set_visible, res_info) if service_info: - GLib.idle_add(self._service_name_label.set_text, service_info.get("name", "")) - GLib.timeout_add_seconds(2, self.update_signal) + gen = self.update_service_info() + GLib.timeout_add_seconds(3, lambda: next(gen, False), priority=GLib.PRIORITY_LOW) GLib.idle_add(self._signal_box.set_visible, service_info) def update_signal(self): + if not self._http_api: + return + sig = self._http_api.send((HttpRequestType.SIGNAL, None)) next(self._http_api) val = sig.get("snr", 0) self._signal_level_bar.set_value(val if val else 0) self._signal_level_bar.set_visible(val) - return self._monitor_signal - def update_service_info(self): - info = self._http_api.send((HttpRequestType.INFO, None)) - next(self._http_api) - if info: - service_info = info.get("service", None) - if service_info: - GLib.idle_add(self._service_name_label.set_text, service_info.get("name", "")) - GLib.timeout_add_seconds(1, self.update_signal) + while self._http_api: + info = self._http_api.send((HttpRequestType.INFO, None)) + next(self._http_api) + if info: + service_info = info.get("service", None) + if service_info: + GLib.idle_add(self._service_name_label.set_text, service_info.get("name", "")) + if service_info.get("onid", None): + self.update_signal() + yield self._http_api # ***************** Filter and search *********************# diff --git a/app/ui/main_window.glade b/app/ui/main_window.glade index 8e758c41..de47dcfb 100644 --- a/app/ui/main_window.glade +++ b/app/ui/main_window.glade @@ -2686,12 +2686,12 @@ Author: Dmitriy Yefremov False start + 10 True False Receiver info - 10 Receiver info @@ -2710,7 +2710,7 @@ Author: Dmitriy Yefremov 0 - + True False @@ -2752,20 +2752,21 @@ Author: Dmitriy Yefremov True True 5 - 1 + 3 False end + 10 + 10 5 True False Current service - Service name fill 1 @@ -2781,12 +2782,9 @@ Author: Dmitriy Yefremov 70 - 10 - True False Tuner signal center - 10 100