X-Git-Url: http://sigaev.ru/git/gitweb.cgi?a=blobdiff_plain;f=main.c;h=ff77ec5c05a32a3e9661a7d789bfb795718cc940;hb=5ccb9f10a0ef71a8b97934ca52123e763f455820;hp=7b2ab8f973cb2f65d6e9649aed39bd2f17d58cf8;hpb=767d08d38964048ea000c3d7700e9765c83dd87f;p=trinked.git diff --git a/main.c b/main.c index 7b2ab8f..ff77ec5 100644 --- a/main.c +++ b/main.c @@ -7,10 +7,10 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -38,7 +38,8 @@ #include static char* dbdir = NULL; -static int period = (300); /* in seconds */ +static int period = 300; /* in seconds */ +static int tics = 1; static char* pidfile = NULL; static char* logfile = NULL; static bool daemonize = false; @@ -51,7 +52,7 @@ static void usage(const char *errmsg) { puts("trinketd - collecting info from AtomPro"); puts("Copyright (c) 2016, Teodor Sigaev "); - puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir]"); + puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-i tics]"); if (errmsg) { puts(""); @@ -63,29 +64,46 @@ usage(const char *errmsg) { static void main_loop() { - double prevDose, + double *prevDose, curDose; + int i; + int collected = 0; -restart: - prevDose = -1; - - if (trinketOpen() != ERR_OK) { - fprintf(stderr, "trinketOpen fails\n"); - sleep(1); - goto restart; + prevDose = malloc(sizeof(*prevDose) * (tics + 1)); + if (!prevDose) { + fprintf(stderr, "could not allocate array, exiting...\n"); + return; } - while(42 && stopRequest == false) { + while(42) { + + if (trinketOpen() != ERR_OK) { + fprintf(stderr, "trinketOpen fails\n"); + sleep(1); + continue; + } + if (trinketGetCumDose(&curDose) != ERR_OK) { fprintf(stderr, "trinketGetCumDose fails\n"); trinketClose(); sleep(1); - goto restart; + continue; + } + + trinketClose(); + + if (collected <= tics) { + prevDose[collected++] = curDose; + } else { + for (i=1; i<=tics; i++) + prevDose[i - 1] = prevDose[i]; + prevDose[tics] = curDose; } - if (prevDose >= 0.0) { - double counts = curDose - prevDose; - double radlevel = coefficient * counts * 3600.0 / (double)period; + if (collected > 1) { + double counts = curDose - prevDose[0]; + double radlevel = 1e6 * coefficient * counts * 3600.0 / + (double)(period * (collected - 1)); FILE *fh = stdout; bool successOpen = true; @@ -99,7 +117,8 @@ restart: if (successOpen) { fprintf(fh, "total: %lld\n", (long long)curDose); fprintf(fh, "counts: %lld\n", (long long)counts); - fprintf(fh, "radlevel: %e\n", radlevel); + fprintf(fh, "tics: %d\n", collected - 1); + fprintf(fh, "radlevel: %.02f\n", radlevel); } if (fh && fh != stdout) { @@ -109,11 +128,8 @@ restart: } } - prevDose = curDose; sleep(period); } - - trinketClose(); } @@ -127,7 +143,7 @@ main(int argn, char* argv[]) { logfd; opterr = 0; - while((i=getopt(argn,argv,"dD:p:P:h")) != EOF) { + while((i=getopt(argn,argv,"dD:i:l:p:P:h")) != EOF) { switch(i) { case 'd': daemonize = true; @@ -135,6 +151,12 @@ main(int argn, char* argv[]) { case 'D': dbdir = strdup(optarg); break; + case 'i': + tics = atoi(optarg); + break; + case 'l': + logfile = strdup(optarg); + break; case 'p': pidfile = strdup(optarg); break; @@ -153,6 +175,9 @@ main(int argn, char* argv[]) { if (period <= 0) usage("Collecting period could not be zero nor negative"); + if (tics <= 0) + usage("Number of tics could not be zero nor negative"); + if (daemonize && !dbdir) usage("trinketd: it is useless to use -d without -D");