Skip to content

Commit

Permalink
Ptosiek/fix-loggers-path-outputからのプルリクエスト#30をマージする
Browse files Browse the repository at this point in the history
Fix path for csv/fit log files
  • Loading branch information
hishizuka authored Sep 29, 2023
2 parents b1dd32f + 5e3ea94 commit 8a26cfb
Show file tree
Hide file tree
Showing 8 changed files with 1,966 additions and 62 deletions.
28 changes: 0 additions & 28 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,31 +1245,3 @@ def get_lon_lat_from_tile_xy(z, x, y):
lat = math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * y / n))))

return lon, lat

@staticmethod
# replacement of dateutil.parser.parse
def datetime_myparser(ts):
if len(ts) == 14:
# 20190322232414 / 14 chars
dt = datetime.datetime(
int(ts[0:4]), # %Y
int(ts[4:6]), # %m
int(ts[6:8]), # %d
int(ts[8:10]), # %H
int(ts[10:12]), # %M
int(ts[12:14]), # %s
)
return dt
elif 24 <= len(ts) <= 26:
# 2019-03-22T23:24:14.280604 / 26 chars
# 2019-09-30T12:44:55.000Z / 24 chars
dt = datetime.datetime(
int(ts[0:4]), # %Y
int(ts[5:7]), # %m
int(ts[8:10]), # %d
int(ts[11:13]), # %H
int(ts[14:16]), # %M
int(ts[17:19]), # %s
)
return dt
app_logger.error(f"Could not parse date {ts} {len(ts)}")
17 changes: 8 additions & 9 deletions modules/logger/logger_csv.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import sqlite3
import time
import datetime
import os
import sqlite3
import shutil
import time

from modules.utils.cmd import exec_cmd
from .logger import Logger


class config_local:
G_LOG_DB = "./log.db~"
G_LOG_DIR = "./"
G_PRODUCT = "Pizero BIKECOMPUTER"
G_VERSION_MAJOR = 0
G_VERSION_MINOR = 1
G_UNIT_ID = "0000000000000000"
G_LOG_DB = "log/log.db"
G_LOG_DIR = "log"


class LoggerCsv(Logger):
Expand All @@ -36,7 +33,9 @@ def write_log(self):
offset = time.localtime().tm_gmtoff
startdate_local = start_date + datetime.timedelta(seconds=offset)
self.config.G_LOG_START_DATE = startdate_local.strftime("%Y%m%d%H%M%S")
filename = self.config.G_LOG_DIR + self.config.G_LOG_START_DATE + ".csv"
filename = os.path.join(
self.config.G_LOG_DIR, f"{self.config.G_LOG_START_DATE}.csv"
)

r = "\
lap,timer,timestamp,total_timer_time,elapsed_time,heart_rate,speed,cadence,power,distance,accumulated_power,\
Expand Down
52 changes: 29 additions & 23 deletions modules/logger/logger_fit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sqlite3
import time
import datetime
import os
import sqlite3
import struct
import time

from logger import app_logger
from modules.utils.date import datetime_myparser
from .logger import Logger

# cython
Expand All @@ -22,13 +24,12 @@

MODE = "Cython"
except ImportError:
from .cython.crc16_p import crc16
pass


class config_local:
G_LOG_DB = "./log/log.db~"
G_LOG_DIR = "./log/"
G_IS_DEBUG = True
G_LOG_DB = "log/log.db"
G_LOG_DIR = "log"
G_UNIT_ID_HEX = 0x12345678


Expand Down Expand Up @@ -224,18 +225,25 @@ def base_type_format_from_id(base_type_id):

def write(self, out):
self.fit_data.append(out)
# if self.config.G_IS_DEBUG: print(type(out),out)

# referenced by https://opensource.quarq.us/fit_json/
def write_log(self):
if MODE == "Cython":
if write_log_cython(self.config.G_LOG_DB):
self.config.G_UPLOAD_FILE = get_upload_file_name()
self.config.G_LOG_START_DATE = get_start_date_str()
return True
# try Cython if available/resolve to pure python if writing fails
if MODE == "Cython" and self.write_log_cython():
return True
return self.write_log_python()

def write_log_cython(self):
res = write_log_cython(self.config.G_LOG_DB)
if res:
self.config.G_UPLOAD_FILE = get_upload_file_name()
self.config.G_LOG_START_DATE = get_start_date_str()
return res

def write_log_python(self):
# make sure crc16 is imported is we resolve to using python code
from .cython.crc16_p import crc16

## SQLite
con = sqlite3.connect(
self.config.G_LOG_DB,
Expand Down Expand Up @@ -338,7 +346,6 @@ def write_log_python(self):
)
)

# if self.config.G_IS_DEBUG: print(available_fields, available_data)
l_num = self.get_local_message_num(message_num, available_fields)
l_num_used = True
if l_num == -1:
Expand Down Expand Up @@ -406,7 +413,9 @@ def write_log_python(self):

startdate_local = start_date + datetime.timedelta(seconds=offset)
self.config.G_LOG_START_DATE = startdate_local.strftime("%Y%m%d%H%M%S")
filename = self.config.G_LOG_DIR + self.config.G_LOG_START_DATE + ".fit"
filename = os.path.join(
self.config.G_LOG_DIR, f"{self.config.G_LOG_START_DATE}.fit"
)
# filename = "test.fit"
fd = open(filename, "wb")
write_data = b"".join(self.fit_data)
Expand All @@ -428,11 +437,11 @@ def write_log_python(self):
# make crc
crc = struct.pack("<H", crc16(file_header))
fd.write(file_header)
# if self.config.G_IS_DEBUG: print("write crc", crc)

fd.write(crc)
fd.write(write_data)
crc = struct.pack("<H", crc16(file_header + crc + write_data))
# if self.config.G_IS_DEBUG: print("write crc", crc)

fd.write(crc)
fd.close()

Expand Down Expand Up @@ -492,10 +501,7 @@ def convertValue(self, v, message_num, defnum):
value = self.get_epoch_time(v[0])
elif field[0] == "total_elapsed_time": # message_num in [18, 19]
value = field[2] * int(
(
self.config.datetime_myparser(v[0])
- self.config.datetime_myparser(v[1])
).total_seconds()
(datetime_myparser(v[0]) - datetime_myparser(v[1])).total_seconds()
)
elif len(field) == 4: # with scale and offset (altitude)
value = field[2] * (v[0] + field[3])
Expand Down Expand Up @@ -577,14 +583,14 @@ def get_epoch_time(self, nowdate):
return seconds

def get_epoch_time_str(self, strtime):
return self.get_epoch_time(self.config.datetime_myparser(strtime))
return self.get_epoch_time(datetime_myparser(strtime))


if __name__ == "__main__":
# from line_profiler import LineProfiler
c = config_local()
l = LoggerFit(c)
l.write_log()
d = LoggerFit(c)
d.write_log()

# prf = LineProfiler()
# prf.add_function(l.convertValue)
Expand Down
5 changes: 3 additions & 2 deletions modules/logger_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from crdp import rdp

from modules.utils.cmd import exec_cmd
from modules.utils.date import datetime_myparser
from logger import app_logger


Expand Down Expand Up @@ -815,7 +816,7 @@ def resume(self):
first_row = self.cur.fetchone()
if first_row[0] is not None:
self.values["start_time"] = int(
self.config.datetime_myparser(first_row[0]).timestamp() - 1
datetime_myparser(first_row[0]).timestamp() - 1
)

# if not self.config.G_IS_RASPI and self.config.G_DUMMY_OUTPUT:
Expand Down Expand Up @@ -911,7 +912,7 @@ def update_track(self, timestamp):
cur.execute("SELECT MAX(timestamp) FROM BIKECOMPUTER_LOG")
first_row = cur.fetchone()
if first_row[0] is not None:
timestamp_new = self.config.datetime_myparser(first_row[0])
timestamp_new = datetime_myparser(first_row[0])

cur.close()
con.close()
Expand Down
31 changes: 31 additions & 0 deletions modules/utils/date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime

from logger import app_logger


# replacement of dateutil.parser.parse
def datetime_myparser(ts):
if len(ts) == 14:
# 20190322232414 / 14 chars
dt = datetime(
int(ts[0:4]), # %Y
int(ts[4:6]), # %m
int(ts[6:8]), # %d
int(ts[8:10]), # %H
int(ts[10:12]), # %M
int(ts[12:14]), # %s
)
return dt
elif 24 <= len(ts) <= 26:
# 2019-03-22T23:24:14.280604 / 26 chars
# 2019-09-30T12:44:55.000Z / 24 chars
dt = datetime(
int(ts[0:4]), # %Y
int(ts[5:7]), # %m
int(ts[8:10]), # %d
int(ts[11:13]), # %H
int(ts[14:16]), # %M
int(ts[17:19]), # %s
)
return dt
app_logger.error(f"Could not parse date {ts} {len(ts)}")
Binary file not shown.
Loading

0 comments on commit 8a26cfb

Please sign in to comment.