1 /** |
1 /** |
2 * @file fermenters.c |
2 * @file fermenters.c |
3 * @brief Handle fermenters status |
3 * @brief Handle fermenters status |
4 * @author Michiel Broek <mbroek at mbse dot eu> |
4 * @author Michiel Broek <mbroek at mbse dot eu> |
5 * |
5 * |
6 * Copyright (C) 2018-2020 |
6 * Copyright (C) 2018-2022 |
7 * |
7 * |
8 * This file is part of the bms (Brewery Management System) |
8 * This file is part of the bms (Brewery Management System) |
9 * |
9 * |
10 * This is free software; you can redistribute it and/or modify it |
10 * This is free software; you can redistribute it and/or modify it |
11 * under the terms of the GNU General Public License as published by the |
11 * under the terms of the GNU General Public License as published by the |
816 |
816 |
817 |
817 |
818 |
818 |
819 void fermenter_log(char *topic, char *payload) |
819 void fermenter_log(char *topic, char *payload) |
820 { |
820 { |
821 char *edge_node, *alias, *line, buf[65], *logfile; |
821 char *edge_node, *alias, *line, buf[65], *logfile, *query = malloc(512); |
822 struct json_object *jobj, *val, *metric, *metric2; |
822 struct json_object *jobj, *val, *metric, *metric2; |
823 fermentation_log *log; |
823 fermentation_log *log; |
|
824 bool trigger = false; |
824 struct tm *mytime; |
825 struct tm *mytime; |
825 time_t timestamp; |
826 time_t timestamp; |
826 FILE *fp; |
827 FILE *fp; |
|
828 static char old_mode[17], old_stage[17]; |
827 |
829 |
828 strtok(topic, "/"); // ignore namespace |
830 strtok(topic, "/"); // ignore namespace |
829 strtok(NULL, "/"); // group_id |
831 strtok(NULL, "/"); // group_id |
830 strtok(NULL, "/"); // message_type |
832 strtok(NULL, "/"); // message_type |
831 edge_node = strtok(NULL, "/\0"); |
833 edge_node = strtok(NULL, "/\0"); |
842 timestamp = json_object_get_int(val); |
844 timestamp = json_object_get_int(val); |
843 log->datetime = malloc(73); |
845 log->datetime = malloc(73); |
844 mytime = localtime(×tamp); |
846 mytime = localtime(×tamp); |
845 snprintf(log->datetime, 73, "%04d-%02d-%02d %02d:%02d:%02d", |
847 snprintf(log->datetime, 73, "%04d-%02d-%02d %02d:%02d:%02d", |
846 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
848 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
|
849 if ((mytime->tm_min % 10) == 0) |
|
850 trigger = true; |
847 } |
851 } |
848 |
852 |
849 if (json_object_object_get_ex(jobj, "metric", &metric)) { |
853 if (json_object_object_get_ex(jobj, "metric", &metric)) { |
850 |
854 |
851 if (json_object_object_get_ex(metric, "product", &metric2)) { |
855 if (json_object_object_get_ex(metric, "product", &metric2)) { |
924 } |
928 } |
925 } |
929 } |
926 json_object_put(jobj); |
930 json_object_put(jobj); |
927 |
931 |
928 /* |
932 /* |
929 * Build csv log line |
933 * Build MySQL log. |
|
934 * Only log every 10 minutes or if something important changed. |
|
935 */ |
|
936 if (strcmp(old_mode, log->mode) || strcmp(old_stage, log->stage) || (log->event && strlen(log->event))) |
|
937 trigger = true; |
|
938 if (trigger) { |
|
939 snprintf(query, 511, |
|
940 "INSERT INTO log_fermenter SET code='%s', datetime='%s', mode='%s', stage='%s', " \ |
|
941 "temp_air='%.3f', temp_beer='%.3f', temp_chiller='%.3f', temp_room='%.3f', " \ |
|
942 "sp_low='%.3f', sp_high='%.3f', heater_power='%d', cooler_power='%d', " \ |
|
943 "event='%s', fermenter_uuid='%s'", |
|
944 log->product_code, log->datetime, log->mode, log->stage, log->temperature_air, |
|
945 log->temperature_beer, log->temperature_chiller, log->temperature_room, |
|
946 log->setpoint_low, log->setpoint_high, log->heater_power, log->cooler_power, |
|
947 (log->event) ? log->event:"", (log->fermenter_uuid) ? log->fermenter_uuid:""); |
|
948 bms_mysql_query(query); |
|
949 //syslog(LOG_NOTICE, "%s", query); |
|
950 } |
|
951 snprintf(old_mode, 16, "%s", log->mode); |
|
952 snprintf(old_stage, 16, "%s", log->stage); |
|
953 free(query); |
|
954 |
|
955 /* |
|
956 * Build csv log line. Used by the web client. |
930 */ |
957 */ |
931 line = xstrcpy(log->datetime); |
958 line = xstrcpy(log->datetime); |
932 line = xstrcat(line, (char *)","); |
959 line = xstrcat(line, (char *)","); |
933 line = xstrcat(line, log->mode); |
960 line = xstrcat(line, log->mode); |
934 line = xstrcat(line, (char *)","); |
961 line = xstrcat(line, (char *)","); |