From c73f7f56edcade32319cf68c6c76bbda637d34e9 Mon Sep 17 00:00:00 2001 From: losinshi Date: Sun, 17 Dec 2006 14:10:23 +0000 Subject: [PATCH] New feature in Chart: Now you can define a 'time-fixpoint' in the past and the Image will generatet everytime,when ((now-fixpoint) % interval) ~ 0. so you can say: generate it every day at 12.00. git-svn-id: file:///home/jan/tmp/wetterstation/trunk@203 dd492736-c11a-0410-ad51-8c26713eaf7f --- cronjob/chart/definitions.h | 2 + cronjob/chart/image_conf/example.conf | 3 ++ cronjob/chart/image_conf/testimage.conf | 4 +- cronjob/chart/image_file/image_common.h | 2 + cronjob/chart/image_file/image_config.c | 49 ++++++++++++++++++++++++- cronjob/chart/image_file/image_file.c | 48 +++++++++++++++++++++--- debian/changelog | 6 +++ 7 files changed, 106 insertions(+), 8 deletions(-) diff --git a/cronjob/chart/definitions.h b/cronjob/chart/definitions.h index dcc2639..c5011be 100644 --- a/cronjob/chart/definitions.h +++ b/cronjob/chart/definitions.h @@ -36,6 +36,7 @@ #define DEFAULT_GEN_INTERVAL "15m" #define DEFAULT_SHOW_INTERVAL "2d" #define DEFAULT_LABEL_INTERVAL "2h" +#define DEFAULT_FIX_TIMEPOINT "no" #define DEFAULT_LABEL_SUM "no" #define DEFAULT_ZERO_MIN "no" #define DEFAULT_AS_BARS "no" @@ -86,6 +87,7 @@ #define FALSE 0 #define TRUE 1 #define IMG_FONT "/usr/share/fonts/truetype/freefont/FreeSans.ttf" +#define CRON_INTERVAL 15 //Minuten /* Debug --------------------------------------------------------------- */ diff --git a/cronjob/chart/image_conf/example.conf b/cronjob/chart/image_conf/example.conf index 28f51a4..8e24808 100644 --- a/cronjob/chart/image_conf/example.conf +++ b/cronjob/chart/image_conf/example.conf @@ -29,6 +29,9 @@ gen_interval 1 # gibt an, in welchem Interval das Bild generiert werden soll show_interval 10d # gibt an, welcher Interval angezeigt werden soll label_interval 1d # gibt an, Welche zeitspanne (ab generierungszeitraum) angezeigt werden soll +fixed_timepoint yes +timepoint 2006-12-17-13-00 + # Hoehe und Breite des Bildes width 800 height 300 diff --git a/cronjob/chart/image_conf/testimage.conf b/cronjob/chart/image_conf/testimage.conf index 1e3e49f..ff40799 100644 --- a/cronjob/chart/image_conf/testimage.conf +++ b/cronjob/chart/image_conf/testimage.conf @@ -5,9 +5,11 @@ manual_table no manual_table_name auss2_data table_field temp #table_field press -gen_interval 1 +gen_interval 10 show_interval 10d label_interval 1d +fixed_timepoint yes +timepoint 2006-12-17-13-00 width 800 height 300 ensor_id 1 diff --git a/cronjob/chart/image_file/image_common.h b/cronjob/chart/image_file/image_common.h index f7a14d0..7e616fd 100644 --- a/cronjob/chart/image_file/image_common.h +++ b/cronjob/chart/image_file/image_common.h @@ -40,6 +40,8 @@ typedef struct image_cfg { long gen_interval; /* Generierungsinterval */ long show_interval; /* Anzeigeinterval */ long label_interval; /* Label - Interval */ + long fix_timepoint; /* Zu festem Zeitpunkt generieren */ + long timepoint; /* Startzeitpunkt */ int label_sum; /* Sollen Werte aufsummiert statt gemittelt werden */ int width; /* Breite */ int height; /* Hoehe */ diff --git a/cronjob/chart/image_file/image_config.c b/cronjob/chart/image_file/image_config.c index b9ac484..dd0bd81 100644 --- a/cronjob/chart/image_file/image_config.c +++ b/cronjob/chart/image_file/image_config.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "../common.h" #include "../config.h" #include "image_common.h" @@ -35,7 +36,7 @@ static int read_time(const char *, void *); static int read_color(const char *, void *); static int read_double(const char *, void *); static int read_fmt_str(const char *, void *); - +static int read_date(const char *, void *); /* Zuordnung zwischen Schluesselwoertern in der Config, Der Funktion, die diese auswertet @@ -51,6 +52,8 @@ static const config_keyword keywords[] = { {"gen_interval", read_time, &(img_cfg.gen_interval), DEFAULT_GEN_INTERVAL}, {"show_interval", read_time, &(img_cfg.show_interval), DEFAULT_SHOW_INTERVAL}, {"label_interval", read_time, &(img_cfg.label_interval), DEFAULT_LABEL_INTERVAL}, + {"fixed_timepoint", read_yn, &(img_cfg.fix_timepoint), DEFAULT_FIX_TIMEPOINT}, + {"timepoint", read_date, &(img_cfg.timepoint), ""}, {"label_sum", read_yn, &(img_cfg.label_sum), DEFAULT_LABEL_SUM}, {"width", read_int, &(img_cfg.width), DEFAULT_WIDTH}, {"height", read_int, &(img_cfg.height), DEFAULT_HEIGHT}, @@ -130,7 +133,8 @@ static int read_color(const char *line, void *arg){ char *buff = malloc(sizeof(char)*3); if(strlen(line) == 11){ /* Wenn String auch wirkle 11 Zeichen lang */ - if (strchr(line, ':') != NULL){ /* und min. 1 : vorkommt */ + if (strchr(line, ':') != NULL){ /* und min. 1 : vorkommt */ + strcpy(buff, "00\0"); tmp = malloc(sizeof(img_color_t)); /* Neues Farbelement allocieren */ tmp->r = strtol(strncpy(buff, line, 2), NULL, 16); /* r */ tmp->g = strtol(strncpy(buff, line+3, 2), NULL, 16); /* g */ @@ -191,6 +195,47 @@ static int read_time(const char *line, void *arg){ return 1; } +/* Liest das Datum, an dem die Bilder-generierung + * 'festgemacht' werden soll. + * Format: yyyy-mm-dd-hh-mm */ +static int read_date(const char *line, void *arg){ + struct tm timestruct; + long *dest = arg; + char *buff = malloc(sizeof(char)*5); + time_t temp = 0; + + if(line == NULL){ + *dest = 0; + free(buff); + return 1; + } + + if(strlen(line) != 16){ + DEBUGOUT2(" Invalid Date-String '%s'!\n", line); + *dest = 0; + free(buff); + return 1; + } else { + timestruct.tm_sec = 0; + timestruct.tm_isdst = -1; + strcpy(buff, "0000\0"); + timestruct.tm_year = (atol(strncpy(buff, line, 4)) - 1901); + strcpy(buff, "00\0"); + timestruct.tm_mon = atol(strncpy(buff, line+5, 2)); + timestruct.tm_mday = atol(strncpy(buff, line+8, 2)); + timestruct.tm_hour = atol(strncpy(buff, line+11, 2)); + timestruct.tm_min = atol(strncpy(buff, line+14, 2)); + + temp = mktime(×truct); + + DEBUGOUT2(" Datum gelesen: %s\n", ctime(&temp)); + } + + *dest = temp; + + free(buff); + return 1; +} /* Liest ein Bild-Configfile */ int get_image_cfg(char *file){ diff --git a/cronjob/chart/image_file/image_file.c b/cronjob/chart/image_file/image_file.c index 68aaa10..3dfb176 100644 --- a/cronjob/chart/image_file/image_file.c +++ b/cronjob/chart/image_file/image_file.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "../definitions.h" #include "../common.h" @@ -42,7 +43,8 @@ image_cfg_t img_cfg; /* Funktionsdefinitionen */ static void regenerate_image(); static int check_file_interval(); - +static int check_time_with_stat(); +static int check_time_with_fixpoint(); /* Handelt ein Bild */ @@ -94,10 +96,6 @@ void process_image_cfg(char *image_cfg_file){ * neu zu erstellen */ static int check_file_interval(){ - struct stat stat_buff; - time_t now; - long diff_sek; - /* Pruefen ob Bild vorhanden */ if(access(img_cfg.file_name, F_OK) == -1){ DEBUGOUT2("Datei '%s' existiert nicht\n", img_cfg.file_name); @@ -105,6 +103,20 @@ static int check_file_interval(){ return 1; } + if(img_cfg.fix_timepoint){ + return check_time_with_fixpoint(); + } else { + return check_time_with_stat(); + } + +} + +static int check_time_with_stat(){ + + struct stat stat_buff; + time_t now; + long diff_sek; + /* Pruefen ob Bild zu alt */ if ((stat(img_cfg.file_name, &stat_buff)) != -1){ @@ -124,8 +136,34 @@ static int check_file_interval(){ } DEBUGOUT1("Datei ist aktuell genug!\n"); return 0; + } +static int check_time_with_fixpoint(){ + + time_t now; + int max_diff = (CRON_INTERVAL -1) * 60; + int exact_val = 0; + int diff = 0; + + now = time(NULL); + + exact_val = (floor( ((double)difftime(now, img_cfg.timepoint)) / ((double)img_cfg.gen_interval) ) * img_cfg.gen_interval) + img_cfg.timepoint; + diff = difftime(now, exact_val); + + if(diff < max_diff){ + DEBUGOUT3("Bild wird generiert, denn es ist kurz (%d sec) nach %s\n", diff, ctime(&exact_val)); + + return 1; + } else { + DEBUGOUT1("Bild ist aktuell genug\n"); + return 0; + } + + +} + + /* Bild (neu-) generieren */ static void regenerate_image(){ FILE *fd = fopen(img_cfg.file_name, "wb"); /* Datei oeffnen */ diff --git a/debian/changelog b/debian/changelog index 6474674..7907151 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +weatherstation (1.0.5-1) stable; urgency=low + + * New feature added in chart-program: building graphs on a 'fixpoint' + + -- Jan Losinski Thu, 2 Nov 2006 09:38:14 +0100 + weatherstation (1.0.4-2) stable; urgency=low * third try to fix negative tempatatures (seems to be correct!)