diff --git a/allsky_logger/cmdlnopts.c b/allsky_logger/cmdlnopts.c index 89e785c..1577f92 100644 --- a/allsky_logger/cmdlnopts.c +++ b/allsky_logger/cmdlnopts.c @@ -40,6 +40,7 @@ glob_pars const Gdefault = { .port = "55555", // port to listen boltwood data .server = NULL, // server name .filename = NULL, // input file name + .min_storage_time = 60.,// minimal storage period }; /* @@ -52,6 +53,7 @@ myoption cmdlnopts[] = { {"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: 55555)")}, {"address", NEED_ARG, NULL, 'a', arg_string, APTR(&G.server), _("server name")}, {"filename",NEED_ARG, NULL, 'i', arg_string, APTR(&G.filename), _("input FITS file name")}, + {"mindt", NEED_ARG, NULL, 't', arg_double, APTR(&G.min_storage_time), _("minimal time interval (in seconds) for image storage (default: 60s)")}, end_option }; diff --git a/allsky_logger/cmdlnopts.h b/allsky_logger/cmdlnopts.h index ab802ce..a0874ba 100644 --- a/allsky_logger/cmdlnopts.h +++ b/allsky_logger/cmdlnopts.h @@ -34,6 +34,7 @@ typedef struct{ char *cwd; // change working directory to given char *server; // server name char *filename; // input file name + double min_storage_time;// minimal storage period } glob_pars; diff --git a/allsky_logger/imfunctions.c b/allsky_logger/imfunctions.c index 031c072..463e25c 100644 --- a/allsky_logger/imfunctions.c +++ b/allsky_logger/imfunctions.c @@ -154,7 +154,7 @@ do{ int status = 0; \ * file stored in current directory under YYYY/MM/DD/hh:mm:ss.fits.gz * @return file descriptor for new notifications */ -int store_fits(char *name, datarecord *data){ +void store_fits(char *name, datarecord *data){ DBG("Try to store %s", name); double t0 = dtime(); long modtm = 0; // modification time - to change later @@ -168,9 +168,11 @@ int store_fits(char *name, datarecord *data){ TRYFITS(fits_get_hdrspace, fptr, &nkeys, NULL); DBG("%d keys", nkeys); green("open & get hdr: %gs\n", dtime() - t0); - char dateobs[FLEN_KEYWORD], timeobs[FLEN_KEYWORD]; + char dateobs[FLEN_KEYWORD], timeobs[FLEN_KEYWORD], imtype[FLEN_KEYWORD]; TRYFITS(fits_read_key, fptr, TSTRING, "DATE-OBS", dateobs, NULL); DBG("DATE-OBS: %s", dateobs); + TRYFITS(fits_read_key, fptr, TSTRING, "IMAGETYP", imtype, NULL); + DBG("IMAGETYP: %s", dateobs); TRYFITS(fits_read_key, fptr, TSTRING, "START", timeobs, NULL); DBG("START: %s", timeobs); TRYFITS(fits_read_key, fptr, TLONG, "UNIXTIME", &modtm, NULL); @@ -180,7 +182,10 @@ int store_fits(char *name, datarecord *data){ green("+ got headers: %gs\n", dtime() - t0); gotodir(dateobs); char newname[PATH_MAX]; - snprintf(newname, PATH_MAX, "%s.fits.gz", timeobs); + if(strncasecmp(imtype, "dark", 4) == 0) + snprintf(newname, PATH_MAX, "%s.dark.fits.gz", timeobs); + else + snprintf(newname, PATH_MAX, "%s.fits.gz", timeobs); // remove if exists unlink(newname); TRYFITS(fits_create_file, &ofptr, newname); @@ -199,15 +204,17 @@ int store_fits(char *name, datarecord *data){ else fits_report_error(stderr, status); } - DBG("Boltwood data"); - // now the file copied -> add header from Boltwood's sensor - while(data->varname){ - if(data->type == INTEGR){ - WRITEIREC(data->keyname, data->data.i, data->comment); - }else{ - WRITEDREC(data->keyname, data->data.d, data->comment); + if(data){ + DBG("Boltwood data"); + // now the file copied -> add header from Boltwood's sensor + while(data->varname){ + if(data->type == INTEGR){ + WRITEIREC(data->keyname, data->data.i, data->comment); + }else{ + WRITEDREC(data->keyname, data->data.d, data->comment); + } + ++data; } - ++data; } long npix = naxes[0]*naxes[1]; imdat = MALLOC(uint16_t, npix); @@ -225,9 +232,7 @@ ret: modifytimestamp(newname, (time_t)modtm); } // as cfitsio removes old file instead of trunkate it, we need to refresh inotify every time! - int fd = watch_fits(name); if(chdir(oldwd)){ // return back to BD root directory ERR("Can't chdir"); }; - return fd; } diff --git a/allsky_logger/imfunctions.h b/allsky_logger/imfunctions.h index 6c96f20..83731c5 100644 --- a/allsky_logger/imfunctions.h +++ b/allsky_logger/imfunctions.h @@ -27,6 +27,6 @@ #include "cmdlnopts.h" int test_fits(char *name); -int store_fits(char *name, datarecord *data); +void store_fits(char *name, datarecord *data); #endif // __IMFUNCTIONS_H__ diff --git a/allsky_logger/socket.c b/allsky_logger/socket.c index 904c474..102628b 100644 --- a/allsky_logger/socket.c +++ b/allsky_logger/socket.c @@ -38,6 +38,8 @@ // Max amount of connections #define BACKLOG (30) +static double minstoragetime; + // keyname, comment, type, data static datarecord boltwood_data[] = { {"humidstatTempCode" , "HSCODE" , "hum. and ambient temp. sensor code (0 - OK)" , INTEGR, {0}}, @@ -179,20 +181,19 @@ static int waittoread(int sock){ * @return inotify file descriptor */ int watch_fits(char *name){ - static int oldfd = -1, oldwd = -1; - if(oldfd > 0 && oldwd > 0){ - inotify_rm_watch(oldfd, oldwd); - oldfd = -1, oldwd = -1; + static int fd = -1, wd = -1; + if(fd > 0 && wd > 0){ + inotify_rm_watch(fd, wd); + wd = -1; } if(!test_fits(name)) ERRX(_("File %s is not FITS file with 2D image!"), name); - int fd; - fd = inotify_init1(IN_NONBLOCK); + if(fd < 0) + fd = inotify_init1(IN_NONBLOCK); if(fd == -1) ERR("inotify_init1()"); - oldfd = fd; FILE* f = fopen(name, "r"); if(!f) ERR("fopen()"); // WTF??? fclose(f); - if((oldwd = inotify_add_watch(fd, name, IN_CLOSE_WRITE)) < 0) + if((wd = inotify_add_watch(fd, name, IN_CLOSE_WRITE)) < 0) ERR("inotify_add_watch()"); DBG("file %s added to inotify", name); return fd; @@ -211,7 +212,7 @@ static void test_path(char *path){ /** * Client daemon itself - * @param G - global parameters + * @param FITSpath - path to file watched * @param infd - inotify file descriptor * @param sock - socket's file descriptor */ @@ -225,6 +226,7 @@ static void client_(char *FITSpath, int infd, int sock){ fds.fd = infd; fds.events = POLLIN; char buf[256]; + double lastTstorage = -1.; // last time of file saving void rlc(size_t newsz){ if(newsz >= Bufsiz){ Bufsiz += 1024; @@ -255,7 +257,12 @@ static void client_(char *FITSpath, int infd, int sock){ }else{ DBG("file changed"); usleep(100000); // wait a little for file changes - fds.fd = store_fits(FITSpath, last_good_msrment); + fds.fd = watch_fits(FITSpath); + if(dtime() - lastTstorage > minstoragetime){ + DBG("lastT: %.2g, now: %.2g", lastTstorage, dtime()); + lastTstorage = dtime(); + store_fits(FITSpath, last_good_msrment); + } } } } @@ -293,6 +300,7 @@ void daemonize(glob_pars *G){ int fd = watch_fits(G->filename); // CD to archive directory if user wants test_path(G->cwd); + minstoragetime = G->min_storage_time; // run fork before socket opening to prevent daemon's death if there's no network #ifndef EBUG green("Daemonize\n");