/*
* Linux Interfece for Arexx Data Loggers
*
- * (c) 2011 Martin Mares <mj@ucw.cz>
+ * (c) 2011-2012 Martin Mares <mj@ucw.cz>
*/
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <rrd.h>
-#define LOG_PATH "/var/log/arexxd"
+#define DEFAULT_LOG_DIR "/var/log/arexxd"
typedef unsigned char byte;
static libusb_context *usb_ctxt;
static int debug_mode;
static int debug_packets;
static int debug_raw_data;
+static char *log_dir = DEFAULT_LOG_DIR;
static void die(char *fmt, ...)
{
/*** RRD interface ***/
-#define SLOT_SIZE 10 // 10 seconds per averaging slot
#define MAX_ARGS 20
#define MAX_ARG_SIZE 1024
arg_pos += len;
}
-static void rrd_point(time_t t, int id, double val)
+static void rrd_point(time_t t, int id, double val, char *unit)
{
char rr_name[256];
snprintf(rr_name, sizeof(rr_name), "sensor-%d.rrd", id);
arg_push("%d", (int) time(NULL) - 28*86400);
arg_push("--step");
arg_push("60");
- arg_push("DS:temp:GAUGE:300:0:10000"); // Anything over 10 kW is considered a ghost
+ if (!strcmp(unit, "%RH"))
+ arg_push("DS:rh:GAUGE:300:0:100");
+ else if (!strcmp(unit, "ppm"))
+ arg_push("DS:ppm:GAUGE:300:0:1000000");
+ else
+ arg_push("DS:temp:GAUGE:300:-200:200");
arg_push("RRA:AVERAGE:0.25:1:20160"); // Last 14 days with full resolution
arg_push("RRA:AVERAGE:0.25:60:88800"); // Last 10 years with 1h resolution
+ arg_push("RRA:MIN:0.25:60:88800"); // including minima and maxima
+ arg_push("RRA:MAX:0.25:60:88800");
rrd_create(arg_cnt, arg_ptr);
if (rrd_test_error()) {
log_error("rrd_create on %s failed: %s", rr_name, rrd_get_error());
static int data_point_counter;
+static double correct_point(int id, double val)
+{
+ /*
+ * Manually calculated corrections for my sensors.
+ * Replace with your formulae.
+ */
+ switch (id) {
+ case 10415:
+ return val - 0.93;
+ case 19246:
+ return val + 0.49;
+ case 12133:
+ return val + 0.44;
+ default:
+ return val;
+ }
+}
+
static void cooked_point(time_t t, int id, double val, char *unit, int q)
{
+ double val2 = correct_point(id, val);
+
if (debug_raw_data) {
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);
+ printf("== %s id=%d val=%.3f val2=%.3f unit=%s q=%d\n", tbuf, id, val, val2, unit, q);
}
data_point_counter++;
- rrd_point(t, id, val);
+ rrd_point(t, id, val2, unit);
}
static void raw_point(int t, int id, int raw, int q)
static const struct option long_options[] = {
{ "debug", 0, NULL, 'd' },
- { "log-packets", 0, NULL, 'p' },
+ { "log-dir", 1, NULL, 'l' },
+ { "debug-packets", 0, NULL, 'p' },
+ { "debug-raw", 0, NULL, 'r' },
{ NULL, 0, NULL, 0 },
};
static void usage(void)
{
- fprintf(stderr, "Usage: arexxd [--debug] [--log-packets]\n");
+ fprintf(stderr, "\n\
+Usage: arexxd <options>\n\
+\n\
+Options:\n\
+-d, --debug Debug mode (no chdir, no fork, no syslog)\n\
+-l, --log-dir=<dir> Directory where all received data should be stored\n\
+-p, --debug-packets Log all packets sent and received\n\
+-r, --debug-raw Log conversion from raw values\n\
+");
exit(1);
}
int main(int argc, char **argv)
{
int opt;
- while ((opt = getopt_long(argc, argv, "dp", long_options, NULL)) >= 0)
+ while ((opt = getopt_long(argc, argv, "dl:pr", long_options, NULL)) >= 0)
switch (opt) {
case 'd':
debug_mode++;
break;
+ case 'l':
+ log_dir = optarg;
+ break;
case 'p':
debug_packets++;
+ break;
+ case 'r':
debug_raw_data++;
break;
default:
// libusb_set_debug(usb_ctxt, 3);
if (!debug_mode) {
- if (chdir(LOG_PATH) < 0)
- die("Cannot change directory to %s: %m", LOG_PATH);
+ if (chdir(log_dir) < 0)
+ die("Cannot change directory to %s: %m", log_dir);
if (debug_packets || debug_raw_data) {
close(1);
if (open("debug", O_WRONLY | O_CREAT | O_APPEND, 0666) < 0)
want_sleep = 1;
if (err > 0 && parse_packet(reply))
want_sleep = 0;
- want_stats = 1;
sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
- sleep(4);
+ if (want_sleep) {
+ sleep(4);
+ want_stats = 1;
+ }
sigprocmask(SIG_BLOCK, &term_sigs, NULL);
}