Version 0.3.39. Log ispindel, fermenters and co2pressure in MySQL. Limit logging to at most once per 10 minutes.

Fri, 17 Jun 2022 16:18:04 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 17 Jun 2022 16:18:04 +0200
changeset 790
98bd22f6629b
parent 789
e6e696add0b3
child 791
b7c7dc0a65c9

Version 0.3.39. Log ispindel, fermenters and co2pressure in MySQL. Limit logging to at most once per 10 minutes.

bmsd/co2meters.c file | annotate | diff | comparison | revisions
bmsd/fermenters.c file | annotate | diff | comparison | revisions
bmsd/ispindels.c file | annotate | diff | comparison | revisions
config.status file | annotate | diff | comparison | revisions
configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
--- a/bmsd/co2meters.c	Thu Mar 24 15:46:36 2022 +0100
+++ b/bmsd/co2meters.c	Fri Jun 17 16:18:04 2022 +0200
@@ -3,7 +3,7 @@
  * @brief Handle co2meters status
  * @author Michiel Broek <mbroek at mbse dot eu>
  *
- * Copyright (C) 2019-2020
+ * Copyright (C) 2019-2022
  *
  * This file is part of the bms (Brewery Management System)
  *
@@ -358,12 +358,13 @@
 
 void co2meter_log(char *topic, char *payload)
 {
-    char                *edge_node, *alias, *line, buf[128], *logfile;
+    char                *edge_node, *alias, *line, buf[128], *logfile, *query = malloc(512);
     struct json_object  *jobj, *val, *metric;
     co2pressure_log	*log;
     struct tm		*mytime;
     time_t		timestamp;
     FILE		*fp;
+    bool		trigger = false;
 
     strtok(topic, "/"); // ignore namespace
     strtok(NULL, "/");	// group_id
@@ -383,6 +384,8 @@
     mytime = localtime(&timestamp);
     snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d",
 	mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
+    if ((mytime->tm_min % 10) == 0)
+	trigger = true;
 
     if (json_object_object_get_ex(jobj, "metric", &metric)) {
 	if (json_object_object_get_ex(metric, "uuid", &val)) {
@@ -434,6 +437,17 @@
     }
 
     /*
+     * Build the MySQL log
+     */
+    if (trigger) {
+	snprintf(query, 511, "INSERT INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \
+			"pressure='%.4f', uuid='%s'",
+			log->product_code, log->datetime, log->temperature, log->pressure, log->uuid);
+	//syslog(LOG_NOTICE, "%s", query);
+	bms_mysql_query(query);
+    }
+
+    /*
      * Build csv log line
      */
     line = xstrcpy(log->datetime);
--- a/bmsd/fermenters.c	Thu Mar 24 15:46:36 2022 +0100
+++ b/bmsd/fermenters.c	Fri Jun 17 16:18:04 2022 +0200
@@ -3,7 +3,7 @@
  * @brief Handle fermenters status
  * @author Michiel Broek <mbroek at mbse dot eu>
  *
- * Copyright (C) 2018-2020
+ * Copyright (C) 2018-2022
  *
  * This file is part of the bms (Brewery Management System)
  *
@@ -818,12 +818,14 @@
 
 void fermenter_log(char *topic, char *payload)
 {
-    char                *edge_node, *alias, *line, buf[65], *logfile;
+    char                *edge_node, *alias, *line, buf[65], *logfile, *query = malloc(512);
     struct json_object  *jobj, *val, *metric, *metric2;
     fermentation_log	*log;
+    bool		trigger = false;
     struct tm		*mytime;
     time_t		timestamp;
     FILE		*fp;
+    static char		old_mode[17], old_stage[17];
 
     strtok(topic, "/"); // ignore namespace
     strtok(NULL, "/");	// group_id
@@ -844,6 +846,8 @@
     	mytime = localtime(&timestamp);
     	snprintf(log->datetime, 73, "%04d-%02d-%02d %02d:%02d:%02d",
 	    mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
+	if ((mytime->tm_min % 10) == 0)
+	    trigger = true;
     }
 
     if (json_object_object_get_ex(jobj, "metric", &metric)) {
@@ -926,7 +930,30 @@
     json_object_put(jobj);
 
     /*
-     * Build csv log line
+     * Build MySQL log.
+     * Only log every 10 minutes or if something important changed.
+     */
+    if (strcmp(old_mode, log->mode) || strcmp(old_stage, log->stage) || (log->event && strlen(log->event)))
+	trigger = true;
+    if (trigger) {
+    	snprintf(query, 511,
+		"INSERT INTO log_fermenter SET code='%s', datetime='%s', mode='%s', stage='%s', " \
+		"temp_air='%.3f', temp_beer='%.3f', temp_chiller='%.3f', temp_room='%.3f', " \
+		"sp_low='%.3f', sp_high='%.3f', heater_power='%d', cooler_power='%d', " \
+		"event='%s', fermenter_uuid='%s'",
+		log->product_code, log->datetime, log->mode, log->stage, log->temperature_air,
+		log->temperature_beer, log->temperature_chiller, log->temperature_room,
+		log->setpoint_low, log->setpoint_high, log->heater_power, log->cooler_power,
+		(log->event) ? log->event:"", (log->fermenter_uuid) ? log->fermenter_uuid:"");
+        bms_mysql_query(query);
+	//syslog(LOG_NOTICE, "%s", query);
+    }
+    snprintf(old_mode, 16, "%s", log->mode);
+    snprintf(old_stage, 16, "%s", log->stage);
+    free(query);
+
+    /*
+     * Build csv log line. Used by the web client.
      */
     line = xstrcpy(log->datetime);
     line = xstrcat(line, (char *)",");
--- a/bmsd/ispindels.c	Thu Mar 24 15:46:36 2022 +0100
+++ b/bmsd/ispindels.c	Fri Jun 17 16:18:04 2022 +0200
@@ -3,7 +3,7 @@
  * @brief Handle ispindels data
  * @author Michiel Broek <mbroek at mbse dot eu>
  *
- * Copyright (C) 2019-2020
+ * Copyright (C) 2019-2022
  *
  * This file is part of the bms (Brewery Management System)
  *
@@ -201,7 +201,7 @@
     sys_ispindel_list	*ispindel, *tmpp;
     struct json_object  *jobj, *metric, *val;
     bool		new_ispindel = true;
-    char		*datetime, buf[65], *line, *logfile;
+    char		*datetime, buf[65], *line, *logfile, *query = malloc(512);
     struct tm           *mytime;
     time_t              timestamp;
     FILE		*fp;
@@ -327,6 +327,16 @@
         line = xstrcat(line, (char *)",");
 	line = xstrcat(line, ispindel->uuid);
 
+	snprintf(query, 511, "INSERT INTO log_ispindel SET code='%s', datetime='%s', " \
+			"temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \
+			"angle='%.5f', refresh='%d', uuid='%s'",
+			ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity,
+			1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) +
+				(4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity),
+			ispindel->battery, ispindel->angle, ispindel->interval, ispindel->uuid);
+	//syslog(LOG_NOTICE, "%s", query);
+	bms_mysql_query(query);
+
 	/*
 	 * Build logfile name
 	 */
--- a/config.status	Thu Mar 24 15:46:36 2022 +0100
+++ b/config.status	Fri Jun 17 16:18:04 2022 +0200
@@ -619,9 +619,9 @@
 S["CFLAGS"]="-g -O2 -fomit-frame-pointer -fno-strict-aliasing -Wall -Wshadow -Wwrite-strings -Wstrict-prototypes -Winline -I/usr/include/libxml2 -I/usr/include/u"\
 "uid"
 S["CC"]="gcc"
-S["CYEARS"]="2016-2021"
+S["CYEARS"]="2016-2022"
 S["COPYRIGHT"]="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved"
-S["VERSION"]="0.3.38"
+S["VERSION"]="0.3.39"
 S["PACKAGE"]="bms"
 S["SUBDIRS"]="bmsd doc script tools www"
 S["target_alias"]=""
@@ -709,7 +709,7 @@
 D["PACKAGE_STRING"]=" \"\""
 D["PACKAGE_BUGREPORT"]=" \"\""
 D["PACKAGE_URL"]=" \"\""
-D["VERSION"]=" \"0.3.38\""
+D["VERSION"]=" \"0.3.39\""
 D["COPYRIGHT"]=" \"Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved\""
 D["STDC_HEADERS"]=" 1"
 D["HAVE_SYS_TYPES_H"]=" 1"
--- a/configure	Thu Mar 24 15:46:36 2022 +0100
+++ b/configure	Fri Jun 17 16:18:04 2022 +0200
@@ -2031,9 +2031,9 @@
 
 
 PACKAGE="bms"
-VERSION="0.3.38"
+VERSION="0.3.39"
 COPYRIGHT="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved"
-CYEARS="2016-2021"
+CYEARS="2016-2022"
 
 
 
--- a/configure.ac	Thu Mar 24 15:46:36 2022 +0100
+++ b/configure.ac	Fri Jun 17 16:18:04 2022 +0200
@@ -8,9 +8,9 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="bms"
-VERSION="0.3.38"
+VERSION="0.3.39"
 COPYRIGHT="Copyright (C) 2016-2021 Michiel Broek, All Rights Reserved"
-CYEARS="2016-2021"
+CYEARS="2016-2022"
 AC_SUBST(PACKAGE)
 AC_SUBST(VERSION)
 AC_SUBST(COPYRIGHT)

mercurial