Merge pull request #1 from stv0g/master

Thats enough for today
This commit is contained in:
Justin Otherguy 2011-11-25 16:34:49 -08:00
commit daad0a5327
4 changed files with 26 additions and 12 deletions

View file

@ -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;
}

View file

@ -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
*/

View file

View file

@ -26,6 +26,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#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;
}