]> mj.ucw.cz Git - arexx.git/commitdiff
Better displaying of cooked data
authorMartin Mares <mj@ucw.cz>
Mon, 26 Dec 2011 20:44:51 +0000 (21:44 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 26 Dec 2011 20:44:51 +0000 (21:44 +0100)
arexx.c

diff --git a/arexx.c b/arexx.c
index 04825ced7705162820277bf03b6d55c645a5fe40..1ab341c8d663b42fe2db9bd22c6accbe545ba7d9 100644 (file)
--- a/arexx.c
+++ b/arexx.c
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <math.h>
+#include <time.h>
 #include <libusb-1.0/libusb.h>
 
 typedef unsigned char byte;
@@ -17,6 +18,7 @@ static libusb_context *usb_ctxt;
 static libusb_device_handle *devh;
 
 static int debug_packets = 0;
+static int debug_raw_data = 0;
 
 #define TIME_OFFSET 946681200          // Timestamp of 2000-01-01 00:00:00
 
@@ -119,7 +121,18 @@ static void put_le32(byte *p, unsigned int x)
        put_le16(p+2, x>>16);
 }
 
-static void raw_point(int t, int id, int raw)
+static void cooked_point(time_t t, int id, double val, char *unit, int q)
+{
+       struct tm tm;
+       localtime_r(&t, &tm);
+
+       char tbuf[64];
+       strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
+
+       printf("== %s id=%d val=%.3f unit=%s q=%d\n", tbuf, id, val, unit, q);
+}
+
+static void raw_point(int t, int id, int raw, int q)
 {
        /*
         *  The binary blob provided by Arexx contains an embedded XML fragment
@@ -147,7 +160,8 @@ static void raw_point(int t, int id, int raw)
         *      - drop if outside the interval [vLo,vUp]
         *
         *  This function applies the necessary transform for sensors we've
-        *  seen in the wild.
+        *  seen in the wild. We deliberately ignore the "dm" parameter as we want
+        *  to report different channels of a single sensor as multiple sensors.
         */
 
        double z = raw;
@@ -207,7 +221,7 @@ static void raw_point(int t, int id, int raw)
                return;
        }
 
-       printf("\t-> %f %s\n", z, unit);
+       cooked_point(t + TIME_OFFSET, id, z, unit, q);
 }
 
 static int parse_packet(byte *reply)
@@ -225,21 +239,24 @@ static int parse_packet(byte *reply)
                if (!len || len == 0xff)
                        break;
                if (len < 9 || len > 10) {
-                       printf("### Unknown packet length %02x\n", len);
+                       printf("### Unknown tuple length %02x\n", len);
                        break;
                }
                if (pos + len > 64) {
-                       printf("### Packet truncated\n");
+                       printf("### Tuple truncated\n");
                        break;
                }
                int id = get_le16(p+1);
                int raw = get_be16(p+3);
                int t = get_le32(p+5);
-               printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
-               if (len > 9)
-                       printf(" q=%d", p[9]);
-               printf("\n");
-               raw_point(t, id, raw);
+               int q = (len > 9) ? p[9] : -1;
+               if (debug_raw_data) {
+                       printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
+                       if (len > 9)
+                               printf(" q=%d", q);
+                       printf("\n");
+               }
+               raw_point(t, id, raw, q);
                pos += len;
                points++;
        }