diff options
author | Erich Eckner <git@eckner.net> | 2019-09-24 10:16:10 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2019-09-24 10:16:10 +0200 |
commit | 9ee88c6b2c0e1abcc2db39b531a9192662bb68b6 (patch) | |
tree | 1de873641e4dd53f6b1bf1c362ff971401a36df0 /input_gadgets.c | |
parent | e103f73f5025f9fda6067040f22a211ea3af753d (diff) | |
parent | 375324e5f4c0cc0a4137c60df03334d1bef92fff (diff) | |
download | anzeige-9ee88c6b2c0e1abcc2db39b531a9192662bb68b6.tar.xz |
Merge branch 'humidity'
Diffstat (limited to 'input_gadgets.c')
-rw-r--r-- | input_gadgets.c | 101 |
1 files changed, 97 insertions, 4 deletions
diff --git a/input_gadgets.c b/input_gadgets.c index 907e05e..863baab 100644 --- a/input_gadgets.c +++ b/input_gadgets.c @@ -11,10 +11,12 @@ #include <zip.h> #include "input_gadgets.h" +#include "humidity.h" -#define fh_temperature_regex "\n\\s*Wetterdaten vom ([0-9]{2}\\.[0-9]{2}\\.[0-9]{4} um [0-9]{2}:[0-9]{2} MEZ)</strong>(.|\n)*>Temperatur</td>\\s*\n\\s*<td [^>]*><strong>([^<]*)</strong>" +#define fh_temperature_regex "\n\\s*Wetterdaten vom ([0-9]{2}\\.[0-9]{2}\\.[0-9]{4} um [0-9]{2}:[0-9]{2} MEZ)</strong>(.|\n)*>Temperatur</td>\\s*\n\\s*<td [^>]*><strong>([^<]*)</strong>(.|\n)*>Luftfeuchtigkeit</td>\\s*\n\\s*<td [^>]*><strong>([^<]*)</strong>" #define wetter_warnung_filename_regex "^2\\.49\\.0\\.1\\.276\\.0\\.DWD\\.PVW\\.([0-9]+)\\." #define warnings_future_timespan (60*60*10) +#define kpi_regex "RH: (-?[0-9.]+) %\nT: (-?[0-9.]+) " static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) @@ -138,7 +140,7 @@ size_t adaptive_strftime(char *s, size_t max, const struct tm *tm_print) return strftime(s, max, "%H:%M", tm_print); } -char *gadgets_retrieve_current_temperature(char *output, int max_len) +char *gadgets_retrieve_current_temperature(char *output, int max_len, double *temperature, double *humidity) { CURL *curl_handle; CURLcode res; @@ -180,13 +182,13 @@ char *gadgets_retrieve_current_temperature(char *output, int max_len) } regex_t re; - regmatch_t rm[4]; + regmatch_t rm[6]; if (regcomp(&re, fh_temperature_regex, REG_EXTENDED|REG_NEWLINE) != 0) { fprintf(stderr, "Failed to compile regex '%s'\n", fh_temperature_regex); free(chunk.memory); return NULL; } - if (ret_val = regexec(&re, chunk.memory, 4, rm, 0)) { + if (ret_val = regexec(&re, chunk.memory, 6, rm, 0)) { char *reg_err; reg_err = malloc(1024); regerror(ret_val, &re, reg_err, 1024); @@ -210,6 +212,11 @@ char *gadgets_retrieve_current_temperature(char *output, int max_len) ende += snprintf(output, max_len, "%.*s", (int)(rm[3].rm_eo - rm[3].rm_so), (char*)(chunk.memory + rm[3].rm_so) // temperature ); + char tmp_str[7]; + snprintf(tmp_str, 6, "%.*s", (int)(rm[3].rm_eo - rm[3].rm_so - 3), (char*)(chunk.memory + rm[3].rm_so)); + *temperature = atof(tmp_str); + snprintf(tmp_str, 6, "%.*s", (int)(rm[5].rm_eo - rm[5].rm_so - 2), (char*)(chunk.memory + rm[5].rm_so)); + *humidity = atof(tmp_str) / 100.; for (char *i = output; i < ende; i++) if (*i == '.') *i = ','; @@ -964,3 +971,89 @@ char *gadgets_retrieve_weather_warnings(char *output, int max_len) return ende; } + +char *gadgets_retrieve_humidity(char *output, int max_len, double fh_temperature, double fh_humidity) +{ + CURL *curl_handle; + CURLcode res; + MemoryStruct chunk; + int ret_val; + double temperature, humidity; + char *ende = output; + + chunk.memory = malloc(1); + chunk.size = 0; + + curl_global_init(CURL_GLOBAL_ALL); + + curl_handle = curl_easy_init(); + if (!curl_handle) { + perror("Failed to init curl"); + free(chunk.memory); + return NULL; + } + curl_easy_setopt(curl_handle, CURLOPT_URL, "https://kpi.ddns.eckner.net/tmp/sensor"); + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + res = curl_easy_perform(curl_handle); + curl_easy_cleanup(curl_handle); + if (res != CURLE_OK) { + fprintf( + stderr, + "curl_easy_perform(%s) failed: %s\n", + "https://kpi.ddns.eckner.net/tmp/sensor", + curl_easy_strerror(res) + ); + free(chunk.memory); + if (res != CURLE_COULDNT_RESOLVE_HOST && res != CURLE_COULDNT_CONNECT) + return NULL; + if (max_len > 0) + ende += snprintf(ende, max_len, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x05, 0xEE, 0x06, 0xEE, 0x05, 0xEE, 0x06); + return ende; + } + + regex_t re; + regmatch_t rm[3]; + if (regcomp(&re, kpi_regex, REG_EXTENDED|REG_NEWLINE) != 0) { + fprintf(stderr, "Failed to compile regex '%s'\n", kpi_regex); + free(chunk.memory); + return NULL; + } + if (ret_val = regexec(&re, chunk.memory, 3, rm, 0)) { + char *reg_err; + reg_err = malloc(1024); + regerror(ret_val, &re, reg_err, 1024); + fprintf(stderr, "%d %s\n",ret_val,reg_err); + regfree(&re); + free(chunk.memory); + return NULL; + } + regfree(&re); + + char tmp_str[12]; + snprintf(tmp_str, 11, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), (char*)(chunk.memory + rm[1].rm_so)); + humidity = atof(tmp_str) / 100.; + snprintf(tmp_str, 11, "%.*s", (int)(rm[2].rm_eo - rm[2].rm_so), (char*)(chunk.memory + rm[2].rm_so)); + temperature = atof(tmp_str); + + humidity *= maximale_luftfeuchte(temperature); + fh_humidity *= maximale_luftfeuchte(fh_temperature); + + if ((output + max_len - ende > 0) && (temperature <= taupunkt(fh_humidity))) + ende += snprintf(ende, max_len, "!TP %.1f K; ", taupunkt(fh_humidity) - temperature); + if ((output + max_len - ende > 0) && (temperature <= taupunkt(humidity))) + ende += snprintf(ende, max_len, "!TP %.1f K; ", taupunkt(humidity) - temperature); + if (output + max_len - ende > 0) + ende += snprintf(ende, max_len, "%.0f %%", humidity * 100 / fh_humidity); + free(chunk.memory); + + while (output < ende) { + if (*output == '.') + *output = ','; + output++; + } + + return ende; +} |