diff --git a/bin/logger/src/channel.c b/bin/logger/src/channel.c index e5692c4..d615938 100644 --- a/bin/logger/src/channel.c +++ b/bin/logger/src/channel.c @@ -37,6 +37,7 @@ void channel_init(channel_t *ch, const char *uuid, const char *middleware, readi ch->identifier = identifier; ch->status = status_unknown; ch->counter = counter; + ch->last = 0; ch->uuid = strdup(uuid); ch->middleware = strdup(middleware); @@ -59,13 +60,11 @@ void channel_free(channel_t *ch) { reading_t * channel_add_readings(channel_t *ch, meter_protocol_t protocol, reading_t *rds, size_t n) { reading_t *first = NULL; /* first unsent reading which has been added */ - double last; - double value; + double delta; for (int i = 0; i < n; i++) { int add = FALSE; - - if (protocol == meter_protocol_d0 || protocol == meter_protocol_sml) { + if (protocol == meter_protocol_d0 || protocol == meter_protocol_sml) { /* intelligent protocols require matching ids */ if (obis_compare(rds[i].identifier.obis, ch->identifier.obis) == 0) { add = TRUE; } @@ -75,12 +74,13 @@ reading_t * channel_add_readings(channel_t *ch, meter_protocol_t protocol, readi } if (add) { - value = rds[i].value; + delta = (ch->last == 0) ? 0 : rds[i].value - ch->last; /* ignoring first reading when no preceeding reading is available (no consumption) */ + ch->last = rds[i].value; - print(log_info, "Adding reading to queue (value=%.2f delta=%.2f ts=%.3f)", ch, value, value - last, tvtod(rds[i].time)); + print(log_info, "Adding reading to queue (value=%.2f delta=%.2f ts=%.3f)", ch, rds[i].value, delta, tvtod(rds[i].time)); - if (ch->counter) { - rds[i].value -= last; + if (ch->counter) { /* send relative consumption since last reading */ + rds[i].value = delta; } reading_t *added = buffer_push(&ch->buffer, &rds[i]); /* remember last value to calculate relative consumption */ @@ -91,7 +91,5 @@ reading_t * channel_add_readings(channel_t *ch, meter_protocol_t protocol, readi } } - ch->last = last; - return first; } diff --git a/include/meter.h b/include/meter.h index e4d92e8..391b505 100644 --- a/include/meter.h +++ b/include/meter.h @@ -54,8 +54,8 @@ typedef union reading_id { obis_id_t obis; - /* char *string; */ - /* char *uuid; */ + char *string; + char *uuid; } reading_id_t; typedef struct reading { @@ -116,6 +116,14 @@ typedef struct { */ double tvtod(struct timeval tv); +/** + * Converts double to timeval structure + * + * @param ts the double value + * @return the timeval strucure + */ +struct timeval dtotv(double ts); + /** * Get list of available meter types */ diff --git a/src/common.c b/src/common.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/meter.c b/src/meter.c index 25de90d..4721ae0 100644 --- a/src/meter.c +++ b/src/meter.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "meter.h" #include "options.h" @@ -115,3 +116,10 @@ double tvtod(struct timeval tv) { return tv.tv_sec + tv.tv_usec / 1e6; } +struct timeval dtotv(double ts) { + struct timeval tv; + tv.tv_usec = modf(timestamp, &tv.tv_sec); + + return tv; +} +