bmsd/fermenters.c

changeset 852
71f0fa38b634
parent 799
cf145b35d65f
equal deleted inserted replaced
851:b15fa90a9af5 852:71f0fa38b634
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-2022 6 * Copyright (C) 2018-2023
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, *query = malloc(512); 821 char *edge_node, *alias, *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 bool trigger = false;
825 struct tm *mytime; 825 struct tm *mytime;
826 time_t timestamp; 826 time_t timestamp;
827 FILE *fp;
828 827
829 strtok(topic, "/"); // ignore namespace 828 strtok(topic, "/"); // ignore namespace
830 strtok(NULL, "/"); // group_id 829 strtok(NULL, "/"); // group_id
831 strtok(NULL, "/"); // message_type 830 strtok(NULL, "/"); // message_type
832 edge_node = strtok(NULL, "/\0"); 831 edge_node = strtok(NULL, "/\0");
945 log->setpoint_low, log->setpoint_high, log->heater_power, log->cooler_power, 944 log->setpoint_low, log->setpoint_high, log->heater_power, log->cooler_power,
946 (log->event) ? log->event:"", (log->fermenter_uuid) ? log->fermenter_uuid:""); 945 (log->event) ? log->event:"", (log->fermenter_uuid) ? log->fermenter_uuid:"");
947 bms_mysql_query(query); 946 bms_mysql_query(query);
948 } 947 }
949 free(query); 948 free(query);
950
951 /*
952 * Build csv log line. Used by the web client.
953 */
954 line = xstrcpy(log->datetime);
955 line = xstrcat(line, (char *)",");
956 line = xstrcat(line, log->mode);
957 line = xstrcat(line, (char *)",");
958 line = xstrcat(line, log->stage);
959 line = xstrcat(line, (char *)",");
960 snprintf(buf, 64, "%.3f", log->temperature_air);
961 line = xstrcat(line, buf);
962 line = xstrcat(line, (char *)",");
963 snprintf(buf, 64, "%.3f", log->temperature_beer);
964 line = xstrcat(line, buf);
965 line = xstrcat(line, (char *)",");
966 snprintf(buf, 64, "%.3f", log->temperature_chiller);
967 line = xstrcat(line, buf);
968 line = xstrcat(line, (char *)",");
969 snprintf(buf, 64, "%.3f", log->temperature_room);
970 line = xstrcat(line, buf);
971 line = xstrcat(line, (char *)",");
972 snprintf(buf, 64, "%.1f", log->setpoint_low);
973 line = xstrcat(line, buf);
974 line = xstrcat(line, (char *)",");
975 snprintf(buf, 64, "%.1f", log->setpoint_high);
976 line = xstrcat(line, buf);
977 line = xstrcat(line, (char *)",");
978 snprintf(buf, 64, "%d", log->heater_power);
979 line = xstrcat(line, buf);
980 line = xstrcat(line, (char *)",");
981 snprintf(buf, 64, "%ld", log->heater_usage);
982 line = xstrcat(line, buf);
983 line = xstrcat(line, (char *)",");
984 snprintf(buf, 64, "%d", log->cooler_power);
985 line = xstrcat(line, buf);
986 line = xstrcat(line, (char *)",");
987 snprintf(buf, 64, "%ld", log->cooler_usage);
988 line = xstrcat(line, buf);
989 line = xstrcat(line, (char *)",");
990 if (log->event)
991 line = xstrcat(line, log->event);
992 line = xstrcat(line, (char *)",");
993 if (log->fermenter_uuid)
994 line = xstrcat(line, log->fermenter_uuid);
995
996 /*
997 * Build logfile name
998 */
999 logfile = xstrcpy(Config.web_root);
1000 logfile = xstrcat(logfile, (char *)"/log/fermentation/");
1001 logfile = xstrcat(logfile, log->product_code);
1002 logfile = xstrcat(logfile, (char *)" ");
1003 logfile = xstrcat(logfile, log->product_name);
1004 logfile = xstrcat(logfile, (char *)".log");
1005
1006 if (debug)
1007 fprintf(stdout, "%s %s\n", logfile, line);
1008
1009 fp = fopen(logfile, "a");
1010 if (fp) {
1011 fprintf(fp, "%s\n", line);
1012 fclose(fp);
1013 } else {
1014 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
1015 }
1016
1017 free(logfile);
1018 logfile = NULL;
1019 free(line);
1020 line = NULL;
1021 949
1022 if (log->datetime) 950 if (log->datetime)
1023 free(log->datetime); 951 free(log->datetime);
1024 if (log->product_uuid ) 952 if (log->product_uuid )
1025 free(log->product_uuid ); 953 free(log->product_uuid );

mercurial