#include <string.h>
#include <unistd.h>
#include <math.h>
+#include <time.h>
#include <libusb-1.0/libusb.h>
typedef unsigned char byte;
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
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
* - 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;
return;
}
- printf("\t-> %f %s\n", z, unit);
+ cooked_point(t + TIME_OFFSET, id, z, unit, q);
}
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++;
}