diff --git a/modules/config.py b/modules/config.py index ed63f2ee..c3b93552 100644 --- a/modules/config.py +++ b/modules/config.py @@ -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)}") diff --git a/modules/logger/logger_csv.py b/modules/logger/logger_csv.py index 4a2ee006..804a6dc7 100644 --- a/modules/logger/logger_csv.py +++ b/modules/logger/logger_csv.py @@ -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): @@ -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,\ diff --git a/modules/logger/logger_fit.py b/modules/logger/logger_fit.py index 8aa58610..0927ac89 100644 --- a/modules/logger/logger_fit.py +++ b/modules/logger/logger_fit.py @@ -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 @@ -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 @@ -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, @@ -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: @@ -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) @@ -428,11 +437,11 @@ def write_log_python(self): # make crc crc = struct.pack(" + + + + + + Heart-of-St--Jo + + + + + + + Heart-of-St--Jo + + 184 + 12286.5 + + 45.57873 + -122.71318 + + + 45.5788 + -122.7135 + + Active + + + + + + 45.57873 + -122.71318 + + 45.8 + 0 + + + + + 45.5787199 + -122.71328 + + 45.8 + 7.871 + + + + + 45.57868 + -122.71339 + + 45.7 + 17.529 + + + + + 45.5787199 + -122.7135 + + 45.6 + 27.187 + + + + + 45.57811 + -122.7135 + + 46.0 + 95.092 + + + + + 45.57741 + -122.7135 + + 46.5 + 173.015 + + + + + 45.57671 + -122.71351 + + 47.6 + 250.942 + + + + + 45.576 + -122.71351 + + 48.2 + 329.978 + + + + + 45.5753 + -122.71351 + + 48.5 + 407.902 + + + + + 45.57529 + -122.71384 + + 48.6 + 433.639 + + + + + 45.57523 + -122.7146 + + 48.8 + 493.234 + + + + + 45.5751 + -122.71539 + + 49.0 + 556.469 + + + + + 45.57497 + -122.71618 + + 49.2 + 619.704 + + + + + 45.57502 + -122.71586 + + 49.2 + 645.253 + + + + + 45.57484 + -122.71689 + + 49.5 + 727.974 + + + + + 45.57466 + -122.71793 + + 49.8 + 811.452 + + + + + 45.57452 + -122.71857 + + 49.9 + 863.7 + + + + + 45.57436 + -122.71909 + + 50.1 + 907.961 + + + + + 45.5741299 + -122.71974 + + 50.3 + 964.714 + + + + + 45.57389 + -122.72039 + + 50.5 + 1021.977 + + + + + 45.57344 + -122.72135 + + 50.8 + 1112.006 + + + + + 45.57341 + -122.72156 + + 50.9 + 1128.707 + + + + + 45.57345 + -122.72178 + + 50.8 + 1146.419 + + + + + 45.57349 + -122.72193 + + 50.5 + 1158.927 + + + + + 45.57388 + -122.72298 + + 49.4 + 1251.55 + + + + + 45.57427 + -122.72404 + + 49.1 + 1344.861 + + + + + 45.57466 + -122.72509 + + 48.9 + 1437.484 + + + + + 45.57504 + -122.72615 + + 48.5 + 1530.282 + + + + + 45.57543 + -122.7272 + + 48.6 + 1622.903 + + + + + 45.57582 + -122.72826 + + 48.8 + 1716.212 + + + + + 45.57621 + -122.72931 + + 48.8 + 1808.833 + + + + + 45.5766 + -122.73037 + + 48.7 + 1902.141 + + + + + 45.57696 + -122.73136 + + 48.5 + 1989.069 + + + + + 45.57733 + -122.73235 + + 48.3 + 2076.514 + + + + + 45.5777 + -122.73334 + + 48.0 + 2163.96 + + + + + 45.57781 + -122.73353 + + 48.0 + 2183.172 + + + + + 45.57831 + -122.73411 + + 47.7 + 2254.867 + + + + + 45.57881 + -122.73469 + + 47.6 + 2326.562 + + + + + 45.57931 + -122.73528 + + 47.5 + 2398.751 + + + + + 45.57943 + -122.73546 + + 47.4 + 2418.119 + + + + + 45.57975 + -122.73644 + + 47.5 + 2502.376 + + + + + 45.58008 + -122.73741 + + 47.7 + 2586.407 + + + + + 45.58022 + -122.7378 + + 47.8 + 2620.556 + + + + + 45.58071 + -122.73887 + + 47.4 + 2720.182 + + + + + 45.58079 + -122.73901 + + 47.4 + 2734.264 + + + + + 45.58111 + -122.73934 + + 47.4 + 2778.195 + + + + + 45.58142 + -122.73965 + + 47.5 + 2820.317 + + + + + 45.58182 + -122.73994 + + 47.6 + 2870.249 + + + + + 45.58194 + -122.74007 + + 47.5902082 + 2887.012 + + + + + 45.582 + -122.74016 + + 47.5845519 + 2896.696 + + + + + 45.58206 + -122.74033 + + 47.5758876 + 2911.53 + + + + + 45.5823899 + -122.74147 + + 47.5197473 + 3007.645 + + + + + 45.5824 + -122.74162 + + 47.5128904 + 3019.384 + + + + + 45.58237 + -122.7419 + + 47.5 + 3041.453 + + + + + 45.58236 + -122.74211 + + 47.5 + 3057.852 + + + + + 45.58237 + -122.74228 + + 47.5 + 3071.144 + + + + + 45.58242 + -122.74255 + + 47.5 + 3092.903 + + + + + 45.58249 + -122.74277 + + 47.3 + 3111.732 + + + + + 45.5826 + -122.74299 + + 47.4 + 3132.797 + + + + + 45.58292 + -122.7436 + + 47.2 + 3192.19 + + + + + 45.58349 + -122.74417 + + 46.9 + 3269.638 + + + + + 45.58364 + -122.74434 + + 46.8 + 3290.951 + + + + + 45.58376 + -122.74455 + + 46.6 + 3312.072 + + + + + 45.58391 + -122.74496 + + 46.8 + 3348.116 + + + + + 45.58428 + -122.7461 + + 47.1 + 3446.016 + + + + + 45.58465 + -122.74724 + + 47.3 + 3543.916 + + + + + 45.58502 + -122.74838 + + 47.3 + 3641.816 + + + + + 45.5853899 + -122.74951 + + 47.1 + 3739.009 + + + + + 45.58576 + -122.75065 + + 46.4 + 3836.907 + + + + + 45.58613 + -122.75179 + + 46.0 + 3934.805 + + + + + 45.5864199 + -122.75274 + + 45.6 + 4015.549 + + + + + 45.5867099 + -122.75368 + + 44.1 + 4095.579 + + + + + 45.587 + -122.75463 + + 39.8 + 4176.322 + + + + + 45.58722 + -122.75527 + + 38.0 + 4231.87 + + + + + 45.58743 + -122.75591 + + 36.5 + 4286.937 + + + + + 45.5877399 + -122.75625 + + 34.4 + 4330.439 + + + + + 45.5878299 + -122.75631 + + 34.7 + 4341.494 + + + + + 45.58838 + -122.75684 + + 36.4 + 4415.34 + + + + + 45.5891 + -122.75755 + + 37.7 + 4512.722 + + + + + 45.58983 + -122.75825 + + 38.4 + 4610.585 + + + + + 45.59055 + -122.75895 + + 38.1 + 4707.525 + + + + + 45.59128 + -122.75965 + + 38.3 + 4805.388 + + + + + 45.592 + -122.76036 + + 38.1 + 4902.767 + + + + + 45.59273 + -122.76106 + + 37.5 + 5000.629 + + + + + 45.59345 + -122.76176 + + 37.8 + 5097.568 + + + + + 45.59418 + -122.76247 + + 37.7 + 5195.864 + + + + + 45.59473 + -122.763 + + 37.4 + 5269.708 + + + + + 45.59528 + -122.76353 + + 37.2 + 5343.551 + + + + + 45.5959999 + -122.76413 + + 36.7 + 5436.331 + + + + + 45.59672 + -122.76473 + + 36.3 + 5529.111 + + + + + 45.59699 + -122.76406 + + 35.9 + 5589.334 + + + + + 45.59739 + -122.76308 + + 34.6 + 5677.704 + + + + + 45.59779 + -122.76209 + + 33.7 + 5766.747 + + + + + 45.59809 + -122.76131 + + 34.1 + 5836.074 + + + + + 45.5985 + -122.76032 + + 33.3 + 5925.678 + + + + + 45.59892 + -122.75933 + + 32.3 + 6015.854 + + + + + 45.59937 + -122.7596 + + 31.7 + 6070.182 + + + + + 45.60002 + -122.76 + + 31.0 + 6148.961 + + + + + 45.6006799 + -122.7604 + + 30.5 + 6228.764 + + + + + 45.60133 + -122.76079 + + 29.6 + 6307.238 + + + + + 45.60198 + -122.76118 + + 28.7 + 6385.713 + + + + + 45.60221 + -122.76064 + + 28.6 + 6434.95 + + + + + 45.6025 + -122.75994 + + 28.3 + 6498.309 + + + + + 45.6028 + -122.75924 + + 27.0 + 6562.241 + + + + + 45.60204 + -122.75878 + + 28.9 + 6654.117 + + + + + 45.60127 + -122.75832 + + 29.4 + 6747.018 + + + + + 45.6004999 + -122.75785 + + 29.8 + 6840.223 + + + + + 45.59972 + -122.75738 + + 30.7 + 6934.453 + + + + + 45.59895 + -122.75692 + + 31.4 + 7027.355 + + + + + 45.59818 + -122.75645 + + 31.6 + 7120.56 + + + + + 45.59811 + -122.75662 + + 31.9 + 7135.924 + + + + + 45.59763 + -122.75616 + + 32.7 + 7200.257 + + + + + 45.59714 + -122.7557 + + 33.7 + 7265.518 + + + + + 45.59651 + -122.75508 + + 34.6 + 7350.668 + + + + + 45.59588 + -122.75447 + + 35.3 + 7435.378 + + + + + 45.59526 + -122.75386 + + 36.5 + 7519.17 + + + + + 45.59499 + -122.7536 + + 36.8 + 7555.412 + + + + + 45.59495 + -122.75347 + + 36.9 + 7566.474 + + + + + 45.59467 + -122.75243 + + 37.3 + 7653.272 + + + + + 45.5944 + -122.75139 + + 37.9 + 7739.677 + + + + + 45.5941199 + -122.75034 + + 37.8 + 7827.204 + + + + + 45.59384 + -122.7493 + + 38.5 + 7914.003 + + + + + 45.59359 + -122.74825 + + 38.8 + 8000.397 + + + + + 45.59334 + -122.7472 + + 38.6 + 8086.792 + + + + + 45.5931 + -122.74615 + + 38.6 + 8172.834 + + + + + 45.59285 + -122.74509 + + 38.6 + 8259.967 + + + + + 45.5926 + -122.74404 + + 38.8 + 8346.362 + + + + + 45.5923299 + -122.7429 + + 39.1 + 8440.113 + + + + + 45.5920599 + -122.74177 + + 38.8 + 8533.126 + + + + + 45.59179 + -122.74063 + + 38.2 + 8626.877 + + + + + 45.59153 + -122.7395 + + 38.0 + 8719.538 + + + + + 45.59126 + -122.73836 + + 37.8 + 8813.289 + + + + + 45.59138 + -122.73827 + + 37.5 + 8828.376 + + + + + 45.59144 + -122.73815 + + 37.3 + 8839.864 + + + + + 45.59173 + -122.73702 + + 35.8 + 8933.621 + + + + + 45.59202 + -122.7359 + + 34.0 + 9026.647 + + + + + 45.59231 + -122.73478 + + 32.7 + 9119.672 + + + + + 45.59257 + -122.73374 + + 31.2 + 9205.699 + + + + + 45.59284 + -122.73269 + + 30.2 + 9292.838 + + + + + 45.59264 + -122.73192 + + 30.4 + 9356.816 + + + + + 45.59244 + -122.73114 + + 30.6 + 9421.526 + + + + + 45.59222 + -122.73026 + + 30.5 + 9494.318 + + + + + 45.592 + -122.72939 + + 30.1756975 + 9566.377 + + + + + 45.59187 + -122.72888 + + 29.9854119 + 9608.658 + + + + + 45.59184 + -122.72886 + + 29.9688272 + 9612.343 + + + + + 45.59181 + -122.72875 + + 29.9274386 + 9621.54 + + + + + 45.59172 + -122.72841 + + 29.8 + 9649.856 + + + + + 45.59174 + -122.72833 + + 29.7 + 9656.474 + + + + + 45.59171 + -122.72821 + + 30.0 + 9666.4 + + + + + 45.5915 + -122.72825 + + 31.1 + 9689.984 + + + + + 45.59072 + -122.72824 + + 32.8 + 9776.816 + + + + + 45.58988 + -122.72823 + + 35.2 + 9870.327 + + + + + 45.58903 + -122.72823 + + 36.5 + 9964.948 + + + + + 45.58819 + -122.72822 + + 38.1 + 10058.459 + + + + + 45.58735 + -122.72822 + + 39.1 + 10151.967 + + + + + 45.5865 + -122.72821 + + 40.9 + 10246.591 + + + + + 45.58643 + -122.72825 + + 41.0 + 10254.984 + + + + + 45.58644 + -122.72788 + + 40.5 + 10283.83 + + + + + 45.58645 + -122.72663 + + 39.6 + 10381.217 + + + + + 45.58643 + -122.72646 + + 39.6 + 10394.647 + + + + + 45.58653 + -122.72639 + + 39.4 + 10407.042 + + + + + 45.58654 + -122.72533 + + 38.7 + 10489.629 + + + + + 45.58655 + -122.72427 + + 38.3 + 10572.215 + + + + + 45.58656 + -122.72321 + + 38.0 + 10654.801 + + + + + 45.58657 + -122.72215 + + 37.1 + 10737.387 + + + + + 45.58658 + -122.72109 + + 36.5 + 10819.973 + + + + + 45.58658 + -122.721 + + 36.5 + 10826.985 + + + + + 45.58659 + -122.71983 + + 35.8 + 10918.139 + + + + + 45.5866 + -122.71866 + + 34.4 + 11009.294 + + + + + 45.58661 + -122.7175 + + 34.0 + 11099.67 + + + + + 45.58662 + -122.71633 + + 33.4 + 11190.825 + + + + + 45.58662 + -122.71537 + + 32.9 + 11265.613 + + + + + 45.58662 + -122.7144 + + 32.2 + 11341.18 + + + + + 45.58662 + -122.71344 + + 31.1 + 11415.968 + + + + + 45.58603 + -122.71344 + + 32.5 + 11481.646 + + + + + 45.58545 + -122.71344 + + 33.9 + 11546.211 + + + + + 45.58462 + -122.71344 + + 36.5 + 11638.606 + + + + + 45.58379 + -122.71345 + + 38.2 + 11731.004 + + + + + 45.58296 + -122.71346 + + 39.8 + 11823.402 + + + + + 45.58213 + -122.71347 + + 40.7 + 11915.8 + + + + + 45.58129 + -122.71347 + + 42.6 + 12009.308 + + + + + 45.58046 + -122.71348 + + 43.7 + 12101.706 + + + + + 45.57963 + -122.71349 + + 44.6 + 12194.103 + + + + + 45.5788 + -122.7135 + + 45.5 + 12286.501 + + + + Start of r + + + 45.57873 + -122.71318 + + Generic + Start of route + + + North Lomb + + + 45.57868 + -122.71339 + + Right + Turn right onto North Lombard Street + + + Sharp Nort + + + 45.5787199 + -122.7135 + + Left + Turn sharp left onto North Woolsey Avenue + + + North Will + + + 45.5753 + -122.71351 + + Right + Turn right onto North Willamette Boulevard + + + Sharp Nort + + + 45.57502 + -122.71586 + + Left + Turn sharp left onto North Willamette Boulevard + + + North Reno + + + 45.59672 + -122.76473 + + Right + Turn right onto North Reno Avenue + + + North Cent + + + 45.59892 + -122.75933 + + Left + Turn left onto North Central Street + + + North Bruc + + + 45.60198 + -122.76118 + + Right + Turn right onto North Bruce Avenue + + + North Smit + + + 45.6028 + -122.75924 + + Right + Turn right onto North Smith Street + + + North Sain + + + 45.59818 + -122.75645 + + Right + Turn right onto North Saint Johns Avenue + + + North Smit + + + 45.59811 + -122.75662 + + Left + Turn left onto North Smith Street + + + North Colu + + + 45.59126 + -122.73836 + + Left + Turn left onto North Columbia Way + + + North Wall + + + 45.59171 + -122.72821 + + Right + Turn right onto North Wall Avenue + + + Sharp Nort + + + 45.58643 + -122.72825 + + Left + Turn sharp left onto North Houghton Street + + + North McKe + + + 45.58643 + -122.72646 + + Left + Turn left onto North McKenna Avenue + + + North Houg + + + 45.58653 + -122.72639 + + Right + Turn right onto North Houghton Street + + + North Wool + + + 45.58662 + -122.71344 + + Right + Turn right onto North Woolsey Avenue + + + End of rou + + + 45.5788 + -122.7135 + + Generic + End of route + + + + diff --git a/tests/test_logger.py b/tests/test_logger.py new file mode 100644 index 00000000..01056fec --- /dev/null +++ b/tests/test_logger.py @@ -0,0 +1,27 @@ +import unittest + +from modules.logger.logger_csv import LoggerCsv +from modules.logger.logger_fit import LoggerFit + + +class LocalConfig: + G_LOG_DB = "tests/data/log.db-Heart_of_St._Johns_Peninsula_Ride" + G_LOG_DIR = "/tmp" # nosec + G_UNIT_ID_HEX = 0x12345678 + + +class TestLogger(unittest.TestCase): + def test_logger_fit_cython(self): + logger = LoggerFit(LocalConfig) + result = logger.write_log_cython() + self.assertTrue(result) + + def test_logger_fit_python(self): + logger = LoggerFit(LocalConfig) + result = logger.write_log_python() + self.assertTrue(result) + + def test_logger_fit_csv(self): + logger = LoggerCsv(LocalConfig) + result = logger.write_log() + self.assertTrue(result)