X-Git-Url: http://git.itanic.dy.fi/?p=log-plotter;a=blobdiff_plain;f=data.c;h=56a6b72faeca6cd445135421bbc5910851c32dd3;hp=81a8345743d4552109fd2950161d9a461d090c69;hb=HEAD;hpb=9abf935040bd368c4b3c1828a79691d200fd45a9 diff --git a/data.c b/data.c index 81a8345..56a6b72 100644 --- a/data.c +++ b/data.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -14,11 +16,15 @@ #include "event.h" #include "utils.h" #include "event.h" +#include "plotter_status.h" + +#define MAX_CHANNELS 4 struct dataparser_struct { struct eventhandler_entry evhandler; + struct plotter_config *cfg; int infd; - int outfd; + int outfd[MAX_CHANNELS]; int offset; time_t start_time; char buf[256]; @@ -80,8 +86,6 @@ static int parse_logline(const char *buf, struct charger_data *data) entry_count = separate_entries(str, entries, ARRAY_SIZE(entries)); - pr_debug("Entry count: %d\n", entry_count); - init_data(data); if (entries[0][0] != '$') { @@ -89,12 +93,11 @@ static int parse_logline(const char *buf, struct charger_data *data) goto out; } - for (i = 0; i < entry_count; i++) - pr_debug("Entry %d: data: %s\n", i, entries[i]); - i = 0; entries[i]++; /* discard the dollar sign */ data->channel = atoi(entries[i++]); + if (data->channel > MAX_CHANNELS) + data->channel = 0; data->state = atoi(entries[i++]); @@ -120,6 +123,9 @@ static int parse_logline(const char *buf, struct charger_data *data) for (j = 0; j < max_cells; j++, i++) { d = atoi(entries[i]); ASSIGN_OR_NAN(data->cell_voltage[j], d / 1000.0); + + if (d) + data->cell_count++; } d = atoi(entries[i++]); @@ -185,9 +191,11 @@ static void print_status_line(struct charger_data *data) } cell_avg /= (double)active_cells; - pr_info("\r\033[K%8s Ubat: %.3fV Ucell avg: %.3fV " + pr_info("\r\033[K%s [%s] Ubat: %.3fV Ucell avg: %.3fV " "Current: %.2fA Charge %.0fmAh ", - time_str, data->charging_voltage, cell_avg, + time_str, + state_to_str(data->state), + data->charging_voltage, cell_avg, data->charging_current, data->total_charge); fflush(stdout); @@ -217,6 +225,8 @@ static int read_log_line(int infd, char *buf, size_t bufsize, int *offset) if (ret == 0) { pr_err("Read EOF, stopping\n"); + + set_plotter_system_status(SYSTEM_STATUS_NO_USB); return -1; } buf[*offset + ret] = 0; @@ -248,6 +258,30 @@ static int read_log_line(int infd, char *buf, size_t bufsize, int *offset) return 0; } +static int open_new_logfile(struct dataparser_struct *dt, int channel) +{ + char path[2048]; + struct tm *tm; + time_t cur_time = time(NULL); + + tm = localtime(&cur_time); + strftime(path, sizeof(path), dt->cfg->log_path, tm); + + store_int_variable_value_to_array("channel", channel, dt->cfg); + replace_variables_with_values(path, sizeof(path), dt->cfg); + + pr_debug("Opening %s for writing the log file\n", path); + + dt->outfd[channel] = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664); + if (dt->outfd[channel] < 0) { + pr_err("Failed to open file %s for writing: %m\n", + path); + return -1; + } + + return 0; +} + static int read_data(struct eventhandler_entry *h) { struct dataparser_struct *dt; @@ -260,7 +294,6 @@ static int read_data(struct eventhandler_entry *h) dt = container_of(h, struct dataparser_struct, evhandler); ret = read_log_line(dt->infd, dt->buf, sizeof(dt->buf), &dt->offset); - pr_debug("Line now %d: %s\n", dt->offset, dt->buf); if (ret <= 0) return ret; @@ -280,17 +313,34 @@ static int read_data(struct eventhandler_entry *h) parse_logline(dt->buf, &data); + set_plotter_system_status(data.state); + /* Fill in possibly missing timestamp */ if (isnan(data.timestamp) || data.timestamp == 0) data.timestamp = cur_time - dt->start_time; print_status_line(&data); - dump_data(&data); + if (0) + dump_data(&data); - if (!dt->outfd) + if (!dt->cfg->log_path) return 0; + if (state_has_changed()) { + dt->start_time = time(NULL); + data.timestamp = 0; + store_str_variable_value_to_array("status", + state_to_str(plotter_state.system_status), dt->cfg); + + store_int_variable_value_to_array("cell_count", + data.cell_count, dt->cfg); + + ret = open_new_logfile(dt, data.channel); + if (ret < 0) + return ret; + } + len = snprintf(str, sizeof(str), "%d;%d;%.1f;" "%.3f;%.3f;%.3f;" @@ -318,7 +368,10 @@ static int read_data(struct eventhandler_entry *h) data.ext_temp, data.total_charge); - ret = write(dt->outfd, str, len); + if (dt->outfd[data.channel] <= 0) + open_new_logfile(dt, data.channel); + + ret = write(dt->outfd[data.channel], str, len); if (ret < 0) { pr_err("write: %m\n"); return -1;; @@ -333,13 +386,13 @@ static struct dataparser_struct dataparser = { .evhandler.handle_event = read_data, }; -int init_data_parser(int infd, int outfd) +int init_data_parser(int infd, struct plotter_config *cfg) { int ret; dataparser.evhandler.fd = infd; dataparser.infd = infd; - dataparser.outfd = outfd; + dataparser.cfg = cfg; ret = register_event_handler(&dataparser.evhandler); if (ret < 0)