add README
[trinked.git] / main.c
diff --git a/main.c b/main.c
index 4b2aab5..f283be0 100644 (file)
--- 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
@@ -26,8 +26,8 @@
  *-------------------------------------------------------------------------
  */
 
+#include <errno.h>
 #include <fcntl.h>
-#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <hidapi/hidapi.h>
 #include <sys/param.h>
 
+#include <tlog.h>
 #include <trinket.h>
 
 static char*   dbdir = NULL;
-static int             period = (150);
+static int             period = 300; /* in seconds */
+static int             tics = 1;
 static char*   pidfile = NULL;
-static char*   logfile = NULL;
 static bool            daemonize = false;
 static char            tfile[MAXPATHLEN],
                                ofile[MAXPATHLEN];
 
-static const double coefficient = 1.0;
-
-static bool                    stopRequest = false;
-
-static void
-sig_int(int sig) {
-       stopRequest = true;
-}
+static const double coefficient = (248e-6 / 2637.0);
 
 static void
 usage(const char *errmsg) {
        puts("trinketd - collecting info from AtomPro");
        puts("Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>");
-       puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir]");
+       puts("trinketd [-d] [-p pidfile] [-P period] [-D datadir] [-i tics] [-l logfile");
 
        if (errmsg) {
                puts("");
@@ -71,56 +65,72 @@ 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) {
+               tlog(TL_CRIT, "could not allocate array, exiting...");
+               return;
        }
 
-       while(42 && stopRequest == false) {
+       while(42) {
+
+               if (trinketOpen() != ERR_OK) {
+                       tlog(TL_ALARM, "trinketOpen fails");
+                       sleep(1);
+                       continue;
+               }
+
                if (trinketGetCumDose(&curDose) != ERR_OK) {
-                       fprintf(stderr, "trinketGetCumDose fails\n");
+                       tlog(TL_ALARM, "trinketGetCumDose fails");
                        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 / (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;
 
                        if (daemonize) {
                                if ((fh = fopen(tfile, "w")) == NULL) {
-                                       fprintf(stderr, "fopen fails\n");
+                                       tlog(TL_ALARM, "fopen fails: %s", strerror(errno));
                                        successOpen = false;
                                }
                        }
 
                        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 != stdout) {
+                       if (fh && fh != stdout) {
                                fflush(fh);
                                fclose(fh);
                                rename(tfile, ofile);
                        }
                }
 
-               prevDose = curDose;
                sleep(period);
        }
-
-       trinketClose();
 }
 
 
@@ -129,13 +139,12 @@ extern int opterr, optind;
 
 int
 main(int argn, char* argv[]) {
-       int i;
-       int     pidfd,
-               logfd;
-       struct sigaction sa;
+       int             i;
+       int             pidfd;
+       char    *logfile = NULL;
 
        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;
@@ -143,6 +152,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;
@@ -161,6 +176,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");
 
@@ -173,48 +191,37 @@ main(int argn, char* argv[]) {
                        usage("could not write pidfile");
        }
 
-       if (logfile) {
-               logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0666);
-               if (logfd < 0)
-                       usage("could not write logfile");
-       }
-
        if (daemonize) {
+               opentlog((logfile == NULL) ? TL_OPEN_SYSLOG : TL_OPEN_FILE,
+                                TL_INFO, logfile);
+
                if (daemon(0, 0) == -1)
-                       usage("could not daemonize");
+                       tlog(TL_CRIT | TL_EXIT, "could not daemonize");
+       } else {
+               opentlog(TL_OPEN_STDERR | TL_OPEN_FILE, TL_INFO, logfile);
        }
 
+
+
        if (pidfile) {
                char pid[64];
 
                snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
                if (write(pidfd, pid, strlen(pid)) != strlen(pid))
-                       usage("could not write pid");
+                       tlog(TL_CRIT | TL_EXIT, "could not write pid");
                close(pidfd);
        }
 
-       if (logfile) {
-               dup2(logfd, fileno(stderr));
-               close(logfd);
-       }
+       tlog(TL_CRIT, "trinketd started, pid: %lld", (long long)getpid());
 
-       if (hid_init() < 0) {
-               fprintf(stderr, "hid_init fails\n");
-               exit(1);
-       }
+       if (hid_init() < 0)
+               tlog(TL_CRIT | TL_EXIT, "hid_init fails");
 
        if (dbdir) {
                snprintf(tfile, MAXPATHLEN, "%s/trinket.tmp", dbdir);
                snprintf(ofile, MAXPATHLEN, "%s/trinket.dat", dbdir);
        }
 
-       memset(&sa, 0, sizeof(sa));
-       sigemptyset(&sa.sa_mask);
-       sa.sa_handler = sig_int;
-       sigaction(SIGINT, &sa, 0);
-       sigaction(SIGTERM, &sa, 0);
-       sigaction(SIGHUP, &sa, 0);
-
        main_loop();
 
        hid_exit();