From c701c6a180c649d1440eb5b4faa68c08f8751de3 Mon Sep 17 00:00:00 2001 From: Florian Loeffler Date: Tue, 3 May 2022 19:35:54 +0200 Subject: [PATCH] first working version --- fbdash.c | 147 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 110 insertions(+), 37 deletions(-) diff --git a/fbdash.c b/fbdash.c index 4aebf7f..db15228 100644 --- a/fbdash.c +++ b/fbdash.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -35,6 +36,114 @@ char *fbp = 0; + + +struct s_sourcedata { + char* text; + char* temperature; + char* humidity; +}; +typedef struct s_sourcedata sourcedata; + +void finish_with_error(MYSQL *con) { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); +} + + +void updateData() { + // query database for latest values + printf("MySQL client version: %s\n", mysql_get_client_info()); + MYSQL *con = mysql_init(NULL); + if (con == NULL) { + fprintf(stderr, "%s\n", mysql_error(con)); + exit(1); + } + if (mysql_real_connect(con, "web-pi", "grafanaReader", "grafanaReader", "grafanaData", 0, NULL, 0) == NULL) { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + char* query = "\ +SELECT \ + time, \ + source, \ + metric, \ + value \ +FROM readings AS r \ +INNER JOIN sources AS s ON r.source_id = s.id \ +INNER JOIN metrics AS m ON r.metric_id = m.id \ +WHERE \ + time > (NOW() - 60 * 15) AND \ + metric IN ('Luftfeuchte','Temperatur') AND \ + source IN ('Esszimmer','Wohnzimmer') \ +ORDER BY time DESC \ +LIMIT 100;"; + +printf("%s\n",query); + + if (mysql_query(con, query)) { + finish_with_error(con); + } + MYSQL_RES *result = mysql_store_result(con); + if (result == NULL) { + finish_with_error(con); + } + + sourcedata esszimmer = { "Esszimmer", NULL, NULL }; + sourcedata wohnzimmer = { "Wohnzimmer", NULL, NULL }; + + int num_fields = mysql_num_fields(result); + MYSQL_ROW row; + while ((row = mysql_fetch_row(result))) { + row[3][5] = '\0'; // loose the third decimal ... + + // data binding + if (strcmp(row[1], "Esszimmer") == 0) { + if (strcmp(row[2], "Temperatur") == 0 && esszimmer.temperature == NULL) esszimmer.temperature = row[3]; + else if (strcmp(row[2], "Luftfeuchte") == 0 && esszimmer.humidity == NULL) esszimmer.humidity = row[3]; + } + if (strcmp(row[1], "Wohnzimmer") == 0) { + if (strcmp(row[2], "Temperatur") == 0 && wohnzimmer.temperature == NULL) wohnzimmer.temperature = row[3]; + else if (strcmp(row[2], "Luftfeuchte") == 0 && wohnzimmer.humidity == NULL) wohnzimmer.humidity = row[3]; + } + + // debugging + for(int i = 0; i < num_fields; i++) { + printf("%s ", row[i] ? row[i] : "NULL"); + } + printf("\n"); + } + + + render_string(fbp, ubuntu_mono_24, esszimmer.text, 15, 7, 255, 255, 255); + + //printf("%s: %s\n", esszimmer.text, esszimmer.temperature); + render_string(fbp, ubuntu_mono_48, esszimmer.temperature, 115, -2, 255, 255, 255); + render_string(fbp, ubuntu_mono_48, "C", 200, -2, 255, 255, 255); + + //printf("%s: %s\n", esszimmer.text, esszimmer.humidity); + render_string(fbp, ubuntu_mono_48, esszimmer.humidity, 115, 33, 255, 255, 255); + render_string(fbp, ubuntu_mono_48, "%", 200, 33, 255, 255, 255); + + + + render_string(fbp, ubuntu_mono_24, wohnzimmer.text, 15, 87, 255, 255, 255); + + //printf("%s: %s\n", wohnzimmer.text, wohnzimmer.temperature); + render_string(fbp, ubuntu_mono_48, wohnzimmer.temperature, 115, 77, 255, 255, 255); + render_string(fbp, ubuntu_mono_48, "C", 200, 77, 255, 255, 255); + + //printf("%s: %s\n", wohnzimmer.text, wohnzimmer.humidity); + render_string(fbp, ubuntu_mono_48, wohnzimmer.humidity, 115, 113, 255, 255, 255); + render_string(fbp, ubuntu_mono_48, "%", 200, 113, 255, 255, 255); + + mysql_free_result(result); + mysql_close(con); + +} + void draw() { clear_screen(fbp); @@ -55,7 +164,7 @@ void draw() { } - + updateData(); /* char *text = "Hell O_o!"; render_string(fbp, ubuntu_mono_48, text, 15, 0, 255, 255, 255); @@ -64,46 +173,10 @@ void draw() { */ } -void finish_with_error(MYSQL *con) -{ - fprintf(stderr, "%s\n", mysql_error(con)); - mysql_close(con); - exit(1); -} // application entry point int main(int argc, char* argv[]) { - - printf("MySQL client version: %s\n", mysql_get_client_info()); - MYSQL *con = mysql_init(NULL); - if (con == NULL) { - fprintf(stderr, "%s\n", mysql_error(con)); - exit(1); - } - if (mysql_real_connect(con, "web-pi", "root", "root_passwd", NULL, 0, NULL, 0) == NULL) { - fprintf(stderr, "%s\n", mysql_error(con)); - mysql_close(con); - exit(1); - } - if (mysql_query(con, "SELECT * FROM grafanaData.readings ORDER BY time DESC LIMIT 1")) { - finish_with_error(con); - } - MYSQL_RES *result = mysql_store_result(con); - if (result == NULL) { - finish_with_error(con); - } - int num_fields = mysql_num_fields(result); - MYSQL_ROW row; - while ((row = mysql_fetch_row(result))) { - for(int i = 0; i < num_fields; i++) { - printf("%s ", row[i] ? row[i] : "NULL"); - } - printf("\n"); - } - mysql_free_result(result); - mysql_close(con); - int fbfd = 0; long int screensize = 0;