From de6d5d0f550bbab6c96265fc915da6cf1e34e080 Mon Sep 17 00:00:00 2001 From: hishizuka <12926652+hishizuka@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:07:22 +0900 Subject: [PATCH] bug fixes(GB) and improve(fit) --- modules/helper/ble_gatt_server.py | 20 ++++++++++++-------- modules/logger/logger_fit.py | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/helper/ble_gatt_server.py b/modules/helper/ble_gatt_server.py index 3662b909..aff1f2a5 100644 --- a/modules/helper/ble_gatt_server.py +++ b/modules/helper/ble_gatt_server.py @@ -1,6 +1,7 @@ import asyncio import json import re +import base64 import traceback import datetime @@ -50,6 +51,10 @@ def tx_characteristic(self, options): def send_message(self, value): self.tx_characteristic.changed(bytes(value + "\\n\n", "utf-8")) + def atob(self, matchobj): + r = base64.b64decode(matchobj.group(1)).decode() + return f"\"{r}\"" + # receive from central @characteristic(rx_characteristic_uuid, CharFlags.WRITE).setter def rx_characteristic(self, value, options): @@ -90,19 +95,18 @@ def rx_characteristic(self, value, options): ): # remove emoji text_mod = re.sub(":\w+:", "", self.value.decode().strip()[5:-2]) - # add double quotation - text_mod = re.sub('(\w+):("?\w*"?)', '"\\1":\\2', text_mod) + + # decode base64 + b64_decode_ptn = re.compile(r"atob\(\"(\S+)\"\)") + text_mod = re.sub(b64_decode_ptn, self.atob, text_mod) + message = {} try: message = json.loads("{" + text_mod + "}", strict=False) except json.JSONDecodeError: app_logger.exception("failed to load json") - app_logger.debug(self.value.decode().strip()[5:-2]) - app_logger.debug(text_mod) - try: - message = json.loads("{" + text_mod + '"}', strict=False) - except json.JSONDecodeError: - app_logger.error("failed to load json (retry)") + app_logger.exception(self.value.decode().strip()[5:-2]) + app_logger.exception(text_mod) if ( "t" in message diff --git a/modules/logger/logger_fit.py b/modules/logger/logger_fit.py index 0927ac89..0b694f73 100644 --- a/modules/logger/logger_fit.py +++ b/modules/logger/logger_fit.py @@ -396,7 +396,7 @@ def write_log_python(self): struct.pack( struct_def, end_date_epochtime, - int((end_date - start_date).total_seconds()) * 1000, + (int(end_date.timestamp()) - int(start_date.timestamp())) * 1000, 1, # num of sessions: 1(fix) 0, # activity_type: general 26, # event: activity