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-2022 |
6 * Copyright (C) 2019-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 |
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, *query = malloc(512); |
361 char *edge_node, *alias, buf[128], *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; |
|
367 bool trigger = false; |
366 bool trigger = false; |
368 |
367 |
369 strtok(topic, "/"); // ignore namespace |
368 strtok(topic, "/"); // ignore namespace |
370 strtok(NULL, "/"); // group_id |
369 strtok(NULL, "/"); // group_id |
371 strtok(NULL, "/"); // message_type |
370 strtok(NULL, "/"); // message_type |
435 } |
434 } |
436 } |
435 } |
437 } |
436 } |
438 |
437 |
439 /* |
438 /* |
440 * Build the MySQL log |
439 * Build the MySQL log and insert if trigger is set. |
441 */ |
440 */ |
442 if (trigger) { |
441 if (trigger) { |
443 snprintf(query, 511, "INSERT IGNORE INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \ |
442 snprintf(query, 511, "INSERT IGNORE INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \ |
444 "pressure='%.4f', uuid='%s'", |
443 "pressure='%.4f', uuid='%s'", |
445 log->product_code, log->datetime, log->temperature, log->pressure, log->uuid); |
444 log->product_code, log->datetime, log->temperature, log->pressure, log->uuid); |
446 //syslog(LOG_NOTICE, "%s", query); |
445 //syslog(LOG_NOTICE, "%s", query); |
447 bms_mysql_query(query); |
446 bms_mysql_query(query); |
448 } |
447 } |
449 |
448 |
450 /* |
|
451 * Build csv log line |
|
452 */ |
|
453 line = xstrcpy(log->datetime); |
|
454 line = xstrcat(line, (char *)","); |
|
455 snprintf(buf, 64, "%.3f", log->temperature); |
|
456 line = xstrcat(line, buf); |
|
457 line = xstrcat(line, (char *)","); |
|
458 snprintf(buf, 64, "%.3f", log->pressure); |
|
459 line = xstrcat(line, buf); |
|
460 line = xstrcat(line, (char *)","); |
|
461 line = xstrcat(line, log->uuid); |
|
462 |
|
463 /* |
|
464 * Build logfile name |
|
465 */ |
|
466 logfile = xstrcpy(Config.web_root); |
|
467 logfile = xstrcat(logfile, (char *)"/log/co2pressure/"); |
|
468 logfile = xstrcat(logfile, log->product_code); |
|
469 logfile = xstrcat(logfile, (char *)" "); |
|
470 logfile = xstrcat(logfile, log->product_name); |
|
471 logfile = xstrcat(logfile, (char *)".log"); |
|
472 |
|
473 if (debug) |
|
474 fprintf(stdout, "%s %s\n", logfile, line); |
|
475 |
|
476 fp = fopen(logfile, "a"); |
|
477 if (fp) { |
|
478 fprintf(fp, "%s\n", line); |
|
479 fclose(fp); |
|
480 } else { |
|
481 syslog(LOG_NOTICE, "cannot append to `%s'", logfile); |
|
482 } |
|
483 |
|
484 free(logfile); |
|
485 logfile = NULL; |
|
486 free(line); |
|
487 line = NULL; |
|
488 |
|
489 if (log->datetime) |
449 if (log->datetime) |
490 free(log->datetime); |
450 free(log->datetime); |
491 if (log->product_uuid ) |
451 if (log->product_uuid ) |
492 free(log->product_uuid ); |
452 free(log->product_uuid ); |
493 if (log->product_code ) |
453 if (log->product_code ) |
494 free(log->product_code ); |
454 free(log->product_code ); |
495 if (log->product_name ) |
455 if (log->product_name ) |
496 free(log->product_name ); |
456 free(log->product_name ); |
497 if (log->uuid) |
457 if (log->uuid) |
498 free(log->uuid); |
458 free(log->uuid); |
499 if (log->node) |
459 if (log->node) |
500 free(log->node); |
460 free(log->node); |
501 if (log->alias) |
461 if (log->alias) |
502 free(log->alias); |
462 free(log->alias); |
503 free(log); |
463 free(log); |
504 } |
464 } |
505 |
465 |
506 |
466 |
507 |
467 |