Skip to content

Commit

Permalink
fix shadowing
Browse files Browse the repository at this point in the history
fix for dst + fix tests (were working only in CET)
  • Loading branch information
Ptosiek committed Oct 30, 2023
1 parent be051ba commit 56d451f
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 226 deletions.
7 changes: 4 additions & 3 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def flush(self):

class CustomRotatingFileHandler(RotatingFileHandler):
def doRollover(self):
format = "%Y-%m-%d_%H-%M-%S"
datetime_format = "%Y-%m-%d_%H-%M-%S"

if self.stream:
self.stream.close()
self.stream = None
Expand All @@ -39,7 +40,7 @@ def doRollover(self):
match = re.match(regex, f)
if match:
try:
date = datetime.strptime(match.group(1), format)
date = datetime.strptime(match.group(1), datetime_format)
if date < cut_out_date:
os.remove(f)
except Exception:
Expand All @@ -52,7 +53,7 @@ def doRollover(self):

self.rotate(
self.baseFilename,
f"{base_filename_no_ext}-{last_date.strftime(format)}{ext}",
f"{base_filename_no_ext}-{last_date.strftime(datetime_format)}{ext}",
)
if not self.delay:
self.stream = self._open()
Expand Down
3 changes: 1 addition & 2 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class Config:
G_LOG_DIR = "log"
G_LOG_DB = os.path.join(G_LOG_DIR, "log.db")
G_LOG_DEBUG_FILE = os.path.join(G_LOG_DIR, "debug.log")
G_LOG_START_DATE = None

# asyncio semaphore
G_COROUTINE_SEM = 100
Expand Down Expand Up @@ -759,7 +758,7 @@ def __init__(self):
def init_loop(self, call_from_gui=False):
if self.G_GUI_MODE == "PyQt":
if call_from_gui:
#asyncio.set_event_loop(self.loop)
# asyncio.set_event_loop(self.loop)
# workaround for latest qasync and older version(~0.24.0)
asyncio.events._set_running_loop(self.loop)
self.start_coroutine()
Expand Down
27 changes: 5 additions & 22 deletions modules/logger/cython/logger_fit.pyx
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
# cython: c_string_type=unicode, c_string_encoding=utf8

cdef extern from "logger_fit_c.hpp":
cdef bint write_log_c(const char* db_file) except +
#cdef bint write_log_c(const char* db_file)
cdef bint write_log_c(const char* db_file, const char* filename, const char* start_date, const char* end_date) except +
cdef cppclass config:
unsigned int G_UNIT_ID_HEX
char* G_LOG_START_DATE
char* G_LOG_DIR
char* G_UPLOAD_FILE
cdef void set_config_c(const config& _cfg)
cdef char* get_upload_file_name_c()
cdef char* get_start_date_str_c()

def write_log_cython(str db_file):
py_byte_string = db_file.encode('UTF-8')
cdef char* c_db_file = py_byte_string
return write_log_c(c_db_file)
def write_log_cython(str db_file, str filename, str start_date, str end_date):
return write_log_c(db_file, filename, start_date, end_date)

def set_config(G_CONFIG):
cdef config cfg
py_byte_string = G_CONFIG.G_LOG_DIR.encode('UTF-8')
cfg.G_LOG_DIR = py_byte_string
cfg.G_UNIT_ID_HEX = G_CONFIG.G_UNIT_ID_HEX
set_config_c(cfg)

def get_upload_file_name():
return get_upload_file_name_c().decode('UTF-8')

def get_start_date_str():
return get_start_date_str_c().decode('UTF-8')



70 changes: 18 additions & 52 deletions modules/logger/cython/logger_fit_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

void set_config_c(const config& _cfg) {
cfg.G_UNIT_ID_HEX = _cfg.G_UNIT_ID_HEX;
cfg.G_LOG_DIR = strdup(_cfg.G_LOG_DIR);
}

char* get_upload_file_name_c() {
return cfg.G_UPLOAD_FILE;
}
char* get_start_date_str_c() {
return cfg.G_LOG_START_DATE;
}

void reset() {
Expand Down Expand Up @@ -414,7 +406,7 @@ bool get_summary(int lap_num, sqlite3 *db) {
std::vector<unsigned int> available_data;
_ret_data[0].reserve(profile_indexes[message_num].size());
_ret_data[1].reserve(profile_indexes[message_num].size());

char _sql_ave[strlen(base_sql[message_num][0].c_str())+strlen(sql_items[message_num][0].c_str())+10];
char _sql_MAX_MIN[strlen(base_sql[message_num][1].c_str())+strlen(sql_items[message_num][1].c_str())+10];
if(message_num == 19) {
Expand Down Expand Up @@ -454,9 +446,9 @@ bool get_summary(int lap_num, sqlite3 *db) {
break;
}
}

if(_ret_data[ave_or_MAX_MIN][index].is_null) continue;

available_fields.push_back(i);
available_data.push_back(convert_value(_ret_data[ave_or_MAX_MIN][index].data.c_str(), i));
}
Expand Down Expand Up @@ -485,11 +477,10 @@ bool get_summary(int lap_num, sqlite3 *db) {
return true;
}

bool write_log_c(const char* db_file) {
bool write_log_c(const char* db_file, const char* filename, const char* start_date, const char* end_date) {
sqlite3 *db;
char *zErrMsg = 0;
int rc, max_lap, rows, lap_lows;
char start_date[30], end_date[30];
std::vector<int> _size;
std::vector<unsigned int> _data;
reset();
Expand All @@ -502,13 +493,14 @@ bool write_log_c(const char* db_file) {
rc = sqlite3_open(db_file, &db);
if(rc) { return exit_with_error("Can't open database", db); }

//get start_date and end_date
message_num = 0;
rc = sqlite3_exec(db, "SELECT MIN(timestamp) FROM BIKECOMPUTER_LOG", parse_single_str, &start_date, &zErrMsg);
if(rc) { return exit_with_error("SQL error(start_date)", db); }
// we could have start/end made optional and.or validate they are correct here
// char start_date[30], end_date[30];
// rc = sqlite3_exec(db, "SELECT MIN(timestamp) FROM BIKECOMPUTER_LOG", parse_single_str, &start_date, &zErrMsg);
// if(rc) { return exit_with_error("SQL error(start_date)", db); }
// rc = sqlite3_exec(db, "SELECT MAX(timestamp) FROM BIKECOMPUTER_LOG", parse_single_str, &end_date, &zErrMsg);
// if(rc) { return exit_with_error("SQL error(end_date)", db); }

time_t start_date_epoch = convert_value(start_date, 4);
rc = sqlite3_exec(db, "SELECT MAX(timestamp) FROM BIKECOMPUTER_LOG", parse_single_str, &end_date, &zErrMsg);
if(rc) { return exit_with_error("SQL error(end_date)", db); }
time_t end_date_epoch = convert_value(end_date, 4);

//file_id (message_num:0)
Expand Down Expand Up @@ -575,22 +567,21 @@ bool write_log_c(const char* db_file) {
if(!get_summary(lap_num, db)) return false;
//printf("lap: %d\n", (int)crc16(fit_data));
}
//make sesson summary

//make session summary
message_num = 18;
if(!get_summary(0, db)) return false;
//printf("session: %d\n", (int)crc16(fit_data));

//make activity
message_num = 34;
local_message_num = (local_message_num + 1)%16;
local_num[local_message_num] = message_num;
local_num_field[local_message_num] = {253,0,1,2,3,4,5};

//get offset of localtime (use lt.tm_gmtoff)
time_t lt_t = time(NULL);
struct tm lt = {0};
localtime_r(&lt_t, &lt);
localtime_r(&end_date_epoch, &lt);

write_definition();
get_struct_def(_size, local_message_num, false);
Expand All @@ -605,15 +596,10 @@ bool write_log_c(const char* db_file) {
};
add_fit_data(fit_data, _data, _size);
//printf("footer: %d\n", (int)crc16(fit_data));

sqlite3_close(db);

//write fit file
time_t startdate_local_epoch = start_date_epoch+epoch_datetime_sec;
char startdate_local[20], filename[100], startdate_str[25];
strftime(startdate_local, sizeof(startdate_local), "%Y-%m-%d_%H-%M-%S", localtime(&startdate_local_epoch));
strftime(startdate_str, sizeof(startdate_str), "%Y-%m-%d_%H-%M-%S.fit", localtime(&startdate_local_epoch));
sprintf(filename, "%s/%s", cfg.G_LOG_DIR, startdate_str);

//make file header
std::vector<uint8_t> file_header, header_crc, total_crc;
Expand All @@ -631,7 +617,7 @@ bool write_log_c(const char* db_file) {
_data = {(unsigned int)crc16(file_header)};
_size = {2};
add_fit_data(header_crc, _data, _size);

FILE *fp;
if((fp=fopen(filename,"w")) != NULL ) {
fwrite(file_header.data(), sizeof(uint8_t), file_header.size(), fp);
Expand All @@ -656,25 +642,5 @@ bool write_log_c(const char* db_file) {

reset();

cfg.G_LOG_START_DATE = strdup(startdate_local);
cfg.G_UPLOAD_FILE = strdup(filename);

return true;
}

int main(int argc, char *argv[]) {

if( argc!=2 ){
fprintf(stderr, "Usage: %s DATABASE\n", argv[0]);
exit(1);
}

cfg.G_UNIT_ID_HEX = 0;
const char* log_dir = "./";
cfg.G_LOG_DIR = strdup(log_dir);

bool res = !write_log_c(argv[1]);

return (int)res;
}

9 changes: 1 addition & 8 deletions modules/logger/cython/logger_fit_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ struct lap_summary_data{

struct config {
unsigned int G_UNIT_ID_HEX = 0;
char* G_LOG_START_DATE;
char* G_LOG_DIR;
char* G_UPLOAD_FILE;
};
static config cfg;

Expand All @@ -54,8 +51,6 @@ constexpr double LAT_LON_CONST = ((unsigned int)(1 << 31))/180.0; //pow(2,31) /

void set_config_c(const config& _cfg);
void reset();
char* get_upload_file_name_c();
char* get_start_date_str_c();

inline uint8_t base_type_id_from_string(std::string base_type_name) {
return base_type_id[base_type_name];
Expand All @@ -82,8 +77,6 @@ static int parse_records_message_num_20(void *user_data, int argc, char **argv,

bool get_summary(int lap_num, sqlite3 *db);

bool write_log_c(const char* db_file);

int main(int argc, char *argv[]);
bool write_log_c(const char* db_file, const char* filename, const char* start_date, const char* end_date);

#endif
67 changes: 21 additions & 46 deletions modules/logger/logger_csv.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
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/log.db"
G_LOG_DIR = "log"


class LoggerCsv(Logger):
def write_log(self):
# get start date
## SQLite
con = sqlite3.connect(
self.config.G_LOG_DB,
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES,
)
sqlite3.dbapi2.converters["DATETIME"] = sqlite3.dbapi2.converters["TIMESTAMP"]
cur = con.cursor()
cur.execute("SELECT timestamp, MIN(timestamp) FROM BIKECOMPUTER_LOG")
first_row = cur.fetchone()
if first_row is not None:
start_date = first_row[0]
else:
return False

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 = os.path.join(
self.config.G_LOG_DIR, f"{self.config.G_LOG_START_DATE}.csv"
def write_log(self, filename):
r = (
"lap,timer,timestamp,total_timer_time,elapsed_time,heart_rate,speed,cadence,power,distance,"
"accumulated_power,position_long,position_lat,raw_long,raw_lat,altitude,gps_altitude,course_altitude,"
"dem_altitude,gps_speed,gps_distance,gps_mode,gps_used_sats,gps_total_sats,gps_epx,gps_epy,gps_epv,"
"gps_pdop,gps_hdop,gps_vdop,total_ascent,total_descent,pressure,temperature,heading,gps_track,"
"motion,acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z,cpu_percent,light"
)

r = "\
lap,timer,timestamp,total_timer_time,elapsed_time,heart_rate,speed,cadence,power,distance,accumulated_power,\
position_long,position_lat,raw_long,raw_lat,altitude,gps_altitude,course_altitude,dem_altitude,gps_speed,gps_distance,gps_mode,gps_used_sats,gps_total_sats,gps_epx,gps_epy,gps_epv,gps_pdop,gps_hdop,gps_vdop,\
total_ascent,total_descent,pressure,temperature,heading,gps_track,motion,acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z,cpu_percent,light"
# voltage_battery,current_battery,voltage_out,current_out,battery_percentage\
# "
# if sqlite3 command exists, use this command (much faster)
if shutil.which("sh") is not None and shutil.which("sqlite3"):
cur.close()
con.close()
sql_cmd = (
"sqlite3 -header -csv "
+ self.config.G_LOG_DB
Expand All @@ -58,21 +31,23 @@ def write_log(self):
sqlite3_cmd = ["sh", "-c", sql_cmd]
exec_cmd(sqlite3_cmd)
else:
f = open(filename, "w", encoding="UTF-8")
con = sqlite3.connect(
self.config.G_LOG_DB,
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES,
)
sqlite3.dbapi2.converters["DATETIME"] = sqlite3.dbapi2.converters[
"TIMESTAMP"
]
cur = con.cursor()

with open(filename, "w", encoding="UTF-8") as o:
# get Lap Records
o.write(r + "\n")

# get Lap Records
f.write(r + "\n")
for row in cur.execute("SELECT %s FROM BIKECOMPUTER_LOG" % r):
f.write(",".join(map(str, row)) + "\n")
f.close()
for row in cur.execute("SELECT %s FROM BIKECOMPUTER_LOG" % r):
o.write(",".join(map(str, row)) + "\n")

cur.close()
con.close()

return True


if __name__ == "__main__":
c = config_local()
d = LoggerCsv(c)
d.write_log()
Loading

0 comments on commit 56d451f

Please sign in to comment.