bmsd/co2meters.c

changeset 790
98bd22f6629b
parent 718
59b02b64131b
child 799
cf145b35d65f
equal deleted inserted replaced
789:e6e696add0b3 790:98bd22f6629b
1 /** 1 /**
2 * @file co2meters.c 2 * @file co2meters.c
3 * @brief Handle co2meters status 3 * @brief Handle co2meters status
4 * @author Michiel Broek <mbroek at mbse dot eu> 4 * @author Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * Copyright (C) 2019-2020 6 * Copyright (C) 2019-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
356 356
357 357
358 358
359 void co2meter_log(char *topic, char *payload) 359 void co2meter_log(char *topic, char *payload)
360 { 360 {
361 char *edge_node, *alias, *line, buf[128], *logfile; 361 char *edge_node, *alias, *line, buf[128], *logfile, *query = malloc(512);
362 struct json_object *jobj, *val, *metric; 362 struct json_object *jobj, *val, *metric;
363 co2pressure_log *log; 363 co2pressure_log *log;
364 struct tm *mytime; 364 struct tm *mytime;
365 time_t timestamp; 365 time_t timestamp;
366 FILE *fp; 366 FILE *fp;
367 bool trigger = false;
367 368
368 strtok(topic, "/"); // ignore namespace 369 strtok(topic, "/"); // ignore namespace
369 strtok(NULL, "/"); // group_id 370 strtok(NULL, "/"); // group_id
370 strtok(NULL, "/"); // message_type 371 strtok(NULL, "/"); // message_type
371 edge_node = strtok(NULL, "/\0"); 372 edge_node = strtok(NULL, "/\0");
381 timestamp = time(NULL); 382 timestamp = time(NULL);
382 log->datetime = malloc(73); 383 log->datetime = malloc(73);
383 mytime = localtime(&timestamp); 384 mytime = localtime(&timestamp);
384 snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", 385 snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d",
385 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); 386 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
387 if ((mytime->tm_min % 10) == 0)
388 trigger = true;
386 389
387 if (json_object_object_get_ex(jobj, "metric", &metric)) { 390 if (json_object_object_get_ex(jobj, "metric", &metric)) {
388 if (json_object_object_get_ex(metric, "uuid", &val)) { 391 if (json_object_object_get_ex(metric, "uuid", &val)) {
389 if (strcmp((char *)"(null)", json_object_get_string(val))) 392 if (strcmp((char *)"(null)", json_object_get_string(val)))
390 log->uuid = xstrcpy((char *)json_object_get_string(val)); 393 log->uuid = xstrcpy((char *)json_object_get_string(val));
432 } 435 }
433 } 436 }
434 } 437 }
435 438
436 /* 439 /*
440 * Build the MySQL log
441 */
442 if (trigger) {
443 snprintf(query, 511, "INSERT INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \
444 "pressure='%.4f', uuid='%s'",
445 log->product_code, log->datetime, log->temperature, log->pressure, log->uuid);
446 //syslog(LOG_NOTICE, "%s", query);
447 bms_mysql_query(query);
448 }
449
450 /*
437 * Build csv log line 451 * Build csv log line
438 */ 452 */
439 line = xstrcpy(log->datetime); 453 line = xstrcpy(log->datetime);
440 line = xstrcat(line, (char *)","); 454 line = xstrcat(line, (char *)",");
441 snprintf(buf, 64, "%.3f", log->temperature); 455 snprintf(buf, 64, "%.3f", log->temperature);

mercurial