From e35b7505968c30e86e4c1feffe68a94f27c9ff6f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 9 Apr 2012 17:30:26 +0200 Subject: [PATCH] Added filtering of obviously bogus data points --- TODO | 3 +++ arexxd.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/TODO b/TODO index 71d089e..f8cc134 100644 --- a/TODO +++ b/TODO @@ -1 +1,4 @@ - Multiple receivers? +- Configuration files +- CSV output +- Rotation of debug logs diff --git a/arexxd.c b/arexxd.c index 76cbc3f..a84d9ff 100644 --- a/arexxd.c +++ b/arexxd.c @@ -21,6 +21,20 @@ #define DEFAULT_LOG_DIR "/var/log/arexxd" +/* + * Data points received from the logger are sometimes corrupted by noise. + * This effects not only the measured values, but also sensor IDs and timestamps. + * Since rrdtool cannot skip back in time, a random timestamp in the future can + * cause all further measurements to be dropped. To minimize impact of these + * problems, we drop data points which are too far in the past or in the future. + * + * Furthermore, you can ignore data from unrecognized sensors, i.e., those + * which are not handled by correct_point(). + */ +#define MAX_PAST_TIME 30*86400 +#define MAX_FUTURE_TIME 300 +#undef IGNORE_UNKNOWN_SENSORS + typedef unsigned char byte; static libusb_context *usb_ctxt; static libusb_device_handle *devh; @@ -161,6 +175,7 @@ static void rrd_point(time_t t, const char *name, double val, char *unit) #define TIME_OFFSET 946681200 // Timestamp of 2000-01-01 00:00:00 static int data_point_counter; // Since last log message +static time_t packet_rx_time; static double correct_point(int id, double val, const char **name) { @@ -185,6 +200,9 @@ static double correct_point(int id, double val, const char **name) *name = "outside"; return val + 0.44; default: +#ifdef IGNORE_UNKNOWN_SENSORS + *name = NULL; +#endif return val; } } @@ -205,6 +223,19 @@ static void cooked_point(time_t t, int id, double val, char *unit, int q) printf("== %s id=%d name=%s val=%.3f val2=%.3f unit=%s q=%d\n", tbuf, id, name, val, val2, unit, q); } + if (!name) { + log_error("Ignored data from unknown sensor %d", id); + return; + } + if (t < packet_rx_time - MAX_PAST_TIME) { + log_error("Data point from sensor %d too far in the past (%d sec)", packet_rx_time - t); + return; + } + if (t > packet_rx_time + MAX_FUTURE_TIME) { + log_error("Data point from sensor %d too far in the future (%d sec)", t - packet_rx_time); + return; + } + data_point_counter++; rrd_point(t, name, val2, unit); } @@ -452,6 +483,7 @@ static int send_and_receive(byte *req, byte *reply) log_error("Receive error: %d", err); return err; } + packet_rx_time = time(NULL); if (debug_packets) log_pkt("<< recv %d bytes\n", transferred); while (transferred < 64) -- 2.39.2