diff options
Diffstat (limited to 'input_gadgets.c')
-rw-r--r-- | input_gadgets.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/input_gadgets.c b/input_gadgets.c index 16a263d..40d22b1 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>(.|\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) @@ -969,3 +971,74 @@ 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; + + 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; + char *ende = output; + 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]; + sprintf(tmp_str, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), (char*)(chunk.memory + rm[1].rm_so)); + humidity = atof(tmp_str); + sprintf(tmp_str, "%.*s", (int)(rm[2].rm_eo - rm[2].rm_so), (char*)(chunk.memory + rm[2].rm_so)); + temperature = atof(tmp_str); + + output += snprintf(output, max_len, "%f -> %f; %f -> %f", fh_temperature, temperature, fh_humidity, humidity); + free(chunk.memory); + return output; +} |