bmsd/fermenters.c

changeset 194
d202777ebae5
parent 192
7f69b43e6084
child 299
047ead629d4a
--- a/bmsd/fermenters.c	Sat Jan 19 20:42:11 2019 +0100
+++ b/bmsd/fermenters.c	Mon Jan 21 20:45:18 2019 +0100
@@ -32,7 +32,7 @@
 sys_fermenter_list	*fermenters = NULL;
 
 extern int		debug;
-
+extern sys_config       Config;
 
 
 
@@ -414,11 +414,12 @@
 
 void fermenter_log(char *topic, char *payload)
 {
-    char                *edge_node, *alias;
+    char                *edge_node, *alias, *line, buf[65], *logfile;
     struct json_object  *jobj, *val, *metric, *metric2;
     fermentation_log	*log;
     struct tm		*mytime;
     time_t		timestamp;
+    FILE		*fp;
 
     strtok(topic, "/"); // ignore namespace
     strtok(NULL, "/");	// group_id
@@ -517,26 +518,80 @@
 		log->fan_usage = json_object_get_int(val);
 	    }
 	}
-//	printf("%s\n", (char *)json_object_to_json_string_ext(metric, 0));
     }
     json_object_put(jobj);
 
-    fermentation_mysql_log(log);
-/*
-    printf("datetime      %s\n", log->datetime);
-    printf("product       %s %s\n", log->product_code, log->product_name);
-    printf("stage/mode    %s %s\n", log->stage, log->mode);
-    printf("temp air      %.3f\n", log->temperature_air);
-    printf("temp beer     %.3f\n", log->temperature_beer);
-    printf("temp chiller  %.3f\n", log->temperature_chiller);
-    printf("temp room     %.3f\n", log->temperature_room);
-    printf("setpoint      %.1f %.1f\n", log->setpoint_low, log->setpoint_high);
-    printf("heater        %3d %ld\n", log->heater_power, log->heater_usage);
-    printf("cooler        %3d %ld\n", log->cooler_power, log->cooler_usage);
-    printf("fan           %3d %ld\n", log->fan_power, log->fan_usage);
-    printf("event         %s\n", log->event);
-    printf("fermenter     %s\n", log->fermenter_uuid);
-*/
+    /*
+     * Build csv log line
+     */
+    line = xstrcpy(log->datetime);
+    line = xstrcat(line, (char *)",");
+    line = xstrcat(line, log->mode);
+    line = xstrcat(line, (char *)",");
+    line = xstrcat(line, log->stage);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.3f", log->temperature_air);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.3f", log->temperature_beer);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.3f", log->temperature_chiller);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.3f", log->temperature_room);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.1f", log->setpoint_low);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%.1f", log->setpoint_high);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%d", log->heater_power);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%ld", log->heater_usage);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%d", log->cooler_power);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    snprintf(buf, 64, "%ld", log->cooler_usage);
+    line = xstrcat(line, buf);
+    line = xstrcat(line, (char *)",");
+    if (log->event)
+	line = xstrcat(line, log->event);
+    line = xstrcat(line, (char *)",");
+    if (log->fermenter_uuid)
+	line = xstrcat(line, log->fermenter_uuid);
+
+    /*
+     * Build logfile name
+     */
+    logfile = xstrcpy(Config.web_root);
+    logfile = xstrcat(logfile, (char *)"/log/fermentation/");
+    logfile = xstrcat(logfile, log->product_code);
+    logfile = xstrcat(logfile, (char *)" ");
+    logfile = xstrcat(logfile, log->product_name);
+    logfile = xstrcat(logfile, (char *)".log");
+
+    if (debug)
+	fprintf(stdout, "%s %s\n", logfile, line);
+
+    fp = fopen(logfile, "a");
+    if (fp) {
+	fprintf(fp, "%s\n", line);
+	fclose(fp);
+    } else {
+	syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
+    }
+
+    free(logfile);
+    logfile = NULL;
+    free(line);
+    line = NULL;
+
     if (log->datetime)
     	free(log->datetime);
     if (log->product_uuid )

mercurial