From c3a4e6099feb5b47e0a9efd392ae7511a55994d1 Mon Sep 17 00:00:00 2001 From: hishizuka <12926652+hishizuka@users.noreply.github.com> Date: Wed, 3 Jan 2024 23:05:27 +0900 Subject: [PATCH] improve handling of bt tethering --- modules/helper/api.py | 53 ++++++++++++++++++++++++++------------- modules/helper/maptile.py | 23 ++++++++++------- modules/helper/network.py | 2 ++ 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/modules/helper/api.py b/modules/helper/api.py index 48f7bd20..217ece90 100644 --- a/modules/helper/api.py +++ b/modules/helper/api.py @@ -59,6 +59,8 @@ class api: maptile_with_values = None + send_livetrack_data_lock = False + send_time = {} pre_value = { "OPENMETEO_WIND": [np.nan, np.nan] @@ -545,6 +547,20 @@ def thingsboard_check(self): return (self.thingsboard_client is not None) def send_livetrack_data(self, quick_send=False): + + # check lock + if self.send_livetrack_data_lock: + return + + # check interval + if not self.check_time_interval( + "THINGSBOARD", + self.config.G_THINGSBOARD_API["INTERVAL_SEC"], + quick_send + ): + return + + # check module and network (common with send_livetrack_course_load/send_livetrack_course_reset) if not self.check_livetrack(): return asyncio.create_task(self.send_livetrack_data_internal(quick_send)) @@ -555,20 +571,14 @@ def check_livetrack(self): # print("Install tb-mqtt-client") return False # network check - if not detect_network() and not self.config.G_AUTO_BT_TETHERING: + if not self.config.G_AUTO_BT_TETHERING and not detect_network(): # print("No Internet connection") return False return True async def send_livetrack_data_internal(self, quick_send=False): - if not self.check_time_interval( - "THINGSBOARD", - self.config.G_THINGSBOARD_API["INTERVAL_SEC"], - quick_send - ): - return - + self.send_livetrack_data_lock = True f_name = self.send_livetrack_data_internal.__name__ timestamp_str = "" t = int(time.time()) @@ -580,6 +590,7 @@ async def send_livetrack_data_internal(self, quick_send=False): v = self.config.logger.sensor.values if not await self.config.network.open_bt_tethering(f_name): v["integrated"]["send_time"] = (datetime.now().strftime("%H:%M") + "OE") + self.send_livetrack_data_lock = False return speed = v["integrated"]["speed"] @@ -622,16 +633,18 @@ async def send_livetrack_data_internal(self, quick_send=False): await asyncio.sleep(5) if self.course_send_status == "LOAD": - self.send_livetrack_course() + await self.send_livetrack_course() elif self.course_send_status == "RESET": - self.send_livetrack_course(reset=True) + await self.send_livetrack_course(reset=True) # close connection if not await self.config.network.close_bt_tethering(f_name): v["integrated"]["send_time"] = (datetime.now().strftime("%H:%M") + "CE") # app_logger.info(f"[TB] end, network: {bool(detect_network())}") - def send_livetrack_course(self, reset=False): + self.send_livetrack_data_lock = False + + async def send_livetrack_course(self, reset=False): if not reset and ( not len(self.config.logger.course.latitude) or not len(self.config.logger.course.longitude) @@ -651,28 +664,34 @@ def send_livetrack_course(self, reset=False): # send as polygon sources data = {"perimeter": course} + + # open connection + f_name = self.send_livetrack_course.__name__ + if not await self.config.network.open_bt_tethering(f_name): + return + self.thingsboard_client.connect() res = self.thingsboard_client.send_telemetry(data).get() if res != TBPublishInfo.TB_ERR_SUCCESS: app_logger.error(f"thingsboard upload error: {res}") self.thingsboard_client.disconnect() + + # close connection + await self.config.network.close_bt_tethering(f_name) + self.course_send_status = "" def send_livetrack_course_load(self): self.course_send_status = "LOAD" if not self.check_livetrack(): return - asyncio.get_running_loop().run_in_executor( - None, self.send_livetrack_course, False - ) + asyncio.create_task(self.send_livetrack_course(False)) def send_livetrack_course_reset(self): self.course_send_status = "RESET" if not self.check_livetrack(): return - asyncio.get_running_loop().run_in_executor( - None, self.send_livetrack_course, True - ) + asyncio.create_task(self.send_livetrack_course(True)) def check_time_interval(self, time_key, interval_sec, quick_send): t = int(time.time()) diff --git a/modules/helper/maptile.py b/modules/helper/maptile.py index 9e87df29..1d7474f9 100644 --- a/modules/helper/maptile.py +++ b/modules/helper/maptile.py @@ -510,11 +510,20 @@ async def download_tiles(self, tiles, map_config, map_name, z): async def update_overlay_windmap_timeline(self, map_settings, map_name): if map_name.startswith("jpn_scw"): + + # check lock + if self.get_scw_lock: + return + + # check network + if not self.config.G_AUTO_BT_TETHERING and not detect_network(): + return + asyncio.create_task( self.update_jpn_scw_timeline(map_settings, self.update_overlay_wind_basetime) ) return - + if not self.update_overlay_wind_basetime(map_settings): return # basetime update @@ -528,18 +537,14 @@ async def update_jpn_scw_timeline(self, map_settings, update_basetime): update_basetime(map_settings) if map_settings["timeline_update_date"] == map_settings["current_time"]: return - - # check lock - if self.get_scw_lock: - # app_logger.debug("get_scw_list Locked") - return + # open connection + self.get_scw_lock = True f_name = self.update_jpn_scw_timeline.__name__ if not await self.config.network.open_bt_tethering(f_name): return - # app_logger.debug("get_scw_list connection start...") - self.get_scw_lock = True + # app_logger.info("get_scw_list connection start...") url = map_settings["inittime"].format(rand=random()) init_time_list = await get_scw_list(url, map_settings["referer"]) if init_time_list is None: @@ -569,7 +574,7 @@ async def update_jpn_scw_timeline(self, map_settings, update_basetime): map_settings["validtime"] = tl["it"] map_settings["subdomain"] = tl["sd"] map_settings["timeline_update_date"] = map_settings["current_time"] - # app_logger.debug(f"get_scw_list Success: {basetime} {tl['it']}]") + # app_logger.info(f"get_scw_list Success: {basetime} {tl['it']}]") return def update_overlay_wind_basetime(self, map_settings): diff --git a/modules/helper/network.py b/modules/helper/network.py index 4b49ce3b..5f13fda6 100644 --- a/modules/helper/network.py +++ b/modules/helper/network.py @@ -297,7 +297,9 @@ async def open_bt_tethering(self, f_name): app_logger.error(f"[BT] connect error, network: {bool(detect_network())}({count}s), f_name: {f_name}") return False + # app_logger.debug(f"bt_pan_status:{bt_pan_status}, {detect_network()}") await asyncio.sleep(5) + app_logger.info(f"[BT] connect, network: {bool(detect_network())}({count}s), f_name: {f_name}") return True