#include <trinket.h>
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;
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] [-w]");
+ puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-i tics] [-w]");
if (errmsg) {
puts("");
static void
main_loop() {
- double prevDose,
+ double *prevDose,
curDose;
+ int i;
+ int collected;
+
+ prevDose = malloc(sizeof(*prevDose) * (tics + 1));
+ if (!prevDose) {
+ fprintf(stderr, "could not allocate array, exiting...\n");
+ return;
+ }
restart:
- prevDose = -1;
+ collected = 0;
if (trinketOpen() != ERR_OK) {
fprintf(stderr, "trinketOpen fails\n");
}
while(42) {
+
if (workaround)
trinketPing();
goto restart;
}
- if (prevDose >= 0.0) {
- double counts = curDose - prevDose;
- double radlevel = 1e6 * coefficient * counts * 3600.0 / (double)period;
+ if (collected <= tics) {
+ prevDose[collected++] = curDose;
+ } else {
+ for (i=1; i<=tics; i++)
+ prevDose[i - 1] = prevDose[i];
+ prevDose[tics] = curDose;
+ }
+
+ 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 (successOpen) {
fprintf(fh, "total: %lld\n", (long long)curDose);
fprintf(fh, "counts: %lld\n", (long long)counts);
+ fprintf(fh, "tics: %d\n", collected - 1);
fprintf(fh, "radlevel: %.02f\n", radlevel);
}
}
}
- prevDose = curDose;
sleep(period);
}
logfd;
opterr = 0;
- while((i=getopt(argn,argv,"dD:l:p:P:wh")) != EOF) {
+ while((i=getopt(argn,argv,"dD:i:l:p:P:wh")) != EOF) {
switch(i) {
case 'd':
daemonize = true;
case 'D':
dbdir = strdup(optarg);
break;
+ case 'i':
+ tics = atoi(optarg);
+ break;
case 'l':
logfile = strdup(optarg);
break;
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");