From 3b26d8cdb9e60a75e1247ffae02559c562057a9f Mon Sep 17 00:00:00 2001 From: losinshi Date: Thu, 7 Sep 2006 17:23:05 +0000 Subject: [PATCH] Chart: - Parsing config-files - test if an image should be regenerated git-svn-id: file:///home/jan/tmp/wetterstation/trunk@70 dd492736-c11a-0410-ad51-8c26713eaf7f --- cronjob/chart/Makefile | 3 +- cronjob/chart/chart.conf | 4 +- cronjob/chart/definitions.h | 1 + cronjob/chart/image_file/image_common.h | 10 ++- cronjob/chart/image_file/image_config.c | 83 ++++++++++++++++++++----- cronjob/chart/image_file/image_file.c | 50 +++++++++++++-- cronjob/chart/image_file/testimage.conf | 10 ++- 7 files changed, 135 insertions(+), 26 deletions(-) diff --git a/cronjob/chart/Makefile b/cronjob/chart/Makefile index 8f0a08c..b3a4f8b 100644 --- a/cronjob/chart/Makefile +++ b/cronjob/chart/Makefile @@ -25,8 +25,7 @@ all: $(BIN_NAME) # Binary Linken $(BIN_NAME): $(OBJS) @ echo Linke: $(LD) $(DEBUG) $(NOLOG) -L$$(pg_config --libdir)/pgsql -lpq $(LDFLAS) $(BIN_NAME) $(OBJS) -# @ $(LD) $(DEBUG) $(NOLOG) -L$$(pg_config --libdir)/pgsql -lpq $(LDFLAS) $(BIN_NAME) $(OBJS) - @ $(LD) $(DEBUG) $(NOLOG) $(LDFLAS) $(BIN_NAME) $(OBJS) + @ $(LD) $(DEBUG) $(NOLOG) -L$$(pg_config --libdir)/pgsql -lpq $(LDFLAS) $(BIN_NAME) $(OBJS) @ echo Binary $(BIN_NAME) ist fertig! # Abhängigkeiten diff --git a/cronjob/chart/chart.conf b/cronjob/chart/chart.conf index 56db7b9..0fbd4dd 100644 --- a/cronjob/chart/chart.conf +++ b/cronjob/chart/chart.conf @@ -2,8 +2,8 @@ fork yes image_cfg_location image_file/ image_cfg testimage.conf -image_cfg bla -image_cfg blo +#image_cfg bla +#image_cfg blo # Postgres-Einstellungen pg_host 141.30.228.39 diff --git a/cronjob/chart/definitions.h b/cronjob/chart/definitions.h index 8b2747b..b9a0922 100644 --- a/cronjob/chart/definitions.h +++ b/cronjob/chart/definitions.h @@ -35,6 +35,7 @@ /* Fehlermeldungen ------------------------------------------------------*/ #define ERROR_SEIINST "Signal-Fehler: Kann Signalhandler zum beenden nicht installieren\n" #define ERROR_FORK "Fork-Fehler: Kann keinen neuen Prozess anlegen\n" +#define ERROR_STAT "Kann Stat nicht ausführen\n" /* Puffergrößen -------------------------------------------------------- */ diff --git a/cronjob/chart/image_file/image_common.h b/cronjob/chart/image_file/image_common.h index 71b7771..640c45a 100644 --- a/cronjob/chart/image_file/image_common.h +++ b/cronjob/chart/image_file/image_common.h @@ -1,5 +1,13 @@ typedef struct image_cfg_t { - char *img_name; + char *file_name; + char *headline; + long gen_interval; + long show_interval; + long label_interval; + int label_sum; + int width; + int height; + int sens_id; } image_cfg; diff --git a/cronjob/chart/image_file/image_config.c b/cronjob/chart/image_file/image_config.c index 9cbfd22..ec411ac 100644 --- a/cronjob/chart/image_file/image_config.c +++ b/cronjob/chart/image_file/image_config.c @@ -27,28 +27,83 @@ #include "../config.h" #include "image_common.h" +static int read_time(const char *, void *); + + + + /* Zuordnung zwischen Schlüsselwörtern in der Config, Der Funktion, die diese auswertet * und dem eld in der Options-Struktur */ static const config_keyword keywords[] = { /* keyword handler variable address default */ - {"filename", read_str, &(img_cfg.img_name), ""}, -/* {"image_cfg", add_image_cfg, &(global_opts.image_cfg), ""}, - {"image_cfg_location",read_str, &(global_opts.image_cfg_location), DEFAULT_PG_HOST}, - */ + {"filename", read_str, &(img_cfg.file_name), ""}, + {"headline", read_str, &(img_cfg.headline), ""}, + {"gen_interval", read_time, &(img_cfg.gen_interval), ""}, + {"show_interval", read_time, &(img_cfg.show_interval), ""}, + {"label_interval", read_time, &(img_cfg.label_interval), ""}, + {"label_sum", read_yn, &(img_cfg.label_sum), ""}, + {"width", read_int, &(img_cfg.width), ""}, + {"height", read_int, &(img_cfg.height), ""}, + {"sensor_id", read_int, &(img_cfg.sens_id), ""}, + {"", NULL, NULL, ""} }; -int get_image_cfg(char *file){ -int ret_var; -char *buff; -buff = malloc(sizeof(char)*(strlen(file)+strlen(global_opts.image_cfg_location)+1)); -buff = strcpy(buff, global_opts.image_cfg_location); -buff = strcat(buff, file); -DEBUGOUT2("Lese Config-File: '%s' \n", buff); - -ret_var = read_config(buff, 1, keywords); -return ret_var; +/* Ein Interval einlesen. + * wandelt Zeitangaben von der Form: + * 10s 10m 10h 10d 10y + * in Sekunden-Werte um */ +static int read_time(const char *line, void *arg){ + long *dest = arg; + long mult = 0; + char *p = NULL; + + p = strchr(line, '\0'); + if(p != NULL){ + switch( *(p - 1) ){ + case 's': + mult = 1; + break; + case 'm': + mult = 60; + break; + case 'h': + mult = 3600; + break; + case 'd': + mult = 86400; + break; + case 'y': + mult = 31536000; + break; + default: + break; + } + if( mult ){ + strcpy(p - 1, "\0"); + } else { + mult = 1; + } + } + *dest = atol(line) * mult; + return 1; +} + + +/* Liest ein Bild-Configfile */ +int get_image_cfg(char *file){ + int ret_var; + char *buff; + + buff = malloc(sizeof(char)*(strlen(file)+strlen(global_opts.image_cfg_location)+1)); + buff = strcpy(buff, global_opts.image_cfg_location); + buff = strcat(buff, file); + + DEBUGOUT2("Lese Config-File: '%s' \n", buff); + + ret_var = read_config(buff, 1, keywords); + return ret_var; } diff --git a/cronjob/chart/image_file/image_file.c b/cronjob/chart/image_file/image_file.c index 9b6e7ae..ed43e1b 100644 --- a/cronjob/chart/image_file/image_file.c +++ b/cronjob/chart/image_file/image_file.c @@ -21,6 +21,9 @@ */ #include #include +#include +#include +#include #include "../definitions.h" #include "../common.h" #include "image_common.h" @@ -31,15 +34,50 @@ image_cfg img_cfg; void process_image_cfg(char *image_cfg_file){ - printf("%s\n",image_cfg_file); - - get_image_cfg(image_cfg_file); - if (img_cfg.img_name != NULL){ - DEBUGOUT2("Image-Name = %s\n",img_cfg.img_name); + if (img_cfg.file_name != NULL){ + DEBUGOUT2("File-Name = %s\n", img_cfg.file_name); + DEBUGOUT2("Ueberschr. = %s\n", img_cfg.headline); + DEBUGOUT2("Gen.Interval = %d\n", img_cfg.gen_interval); + DEBUGOUT2("ShowInterval = %d\n", img_cfg.show_interval); + DEBUGOUT2("LabelInterval = %d\n", img_cfg.label_interval); + DEBUGOUT2("LabelSum = %d\n", img_cfg.label_sum); + DEBUGOUT2("Width = %d\n", img_cfg.width); + DEBUGOUT2("Height = %d\n", img_cfg.height); + DEBUGOUT2("SensorId = %d\n", img_cfg.sens_id); + DEBUGOUT1("\n"); + } else { + return; } - + check_file_interval(); sleep(3); } + + +int check_file_interval(){ + + struct stat stat_buff; + time_t now; + long diff_sek; + + if(access(img_cfg.file_name, F_OK) == -1){ + DEBUGOUT2("Datei '%s' existiert nicht\n", img_cfg.file_name); + return 1; + } + if ((stat(img_cfg.file_name, &stat_buff)) != -1){ + + now = time(NULL); + diff_sek = difftime(now, stat_buff.st_mtime); + + DEBUGOUT3("Datei '%s' ist %d Sek. alt \n", img_cfg.file_name, diff_sek); + + if(diff_sek > (img_cfg.gen_interval * 60)) + return 1; + + } else { + exit_error(ERROR_STAT); + } + return 0; +} diff --git a/cronjob/chart/image_file/testimage.conf b/cronjob/chart/image_file/testimage.conf index 99488ac..bd83138 100644 --- a/cronjob/chart/image_file/testimage.conf +++ b/cronjob/chart/image_file/testimage.conf @@ -1 +1,9 @@ -filename testbildl +filename /home/jan/Testbild.png +headline Testbild +gen_interval 15m +show_interval 1h +label_interval 10m +label_sum no +width 400 +height 250 +sensor_id 1