Skip to content

Commit

Permalink
improve handling of bt tethering
Browse files Browse the repository at this point in the history
  • Loading branch information
hishizuka committed Jan 3, 2024
1 parent a4076b6 commit c3a4e60
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
53 changes: 36 additions & 17 deletions modules/helper/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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))
Expand All @@ -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())
Expand All @@ -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"]
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand Down
23 changes: 14 additions & 9 deletions modules/helper/maptile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions modules/helper/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3a4e60

Please sign in to comment.