bmsd/fermenters.c

changeset 194
d202777ebae5
parent 192
7f69b43e6084
child 299
047ead629d4a
equal deleted inserted replaced
193:3cbd81ea9391 194:d202777ebae5
30 30
31 31
32 sys_fermenter_list *fermenters = NULL; 32 sys_fermenter_list *fermenters = NULL;
33 33
34 extern int debug; 34 extern int debug;
35 35 extern sys_config Config;
36 36
37 37
38 38
39 void fermenter_set(char *edge_node, char *alias, bool birth, char *payload) 39 void fermenter_set(char *edge_node, char *alias, bool birth, char *payload)
40 { 40 {
412 412
413 413
414 414
415 void fermenter_log(char *topic, char *payload) 415 void fermenter_log(char *topic, char *payload)
416 { 416 {
417 char *edge_node, *alias; 417 char *edge_node, *alias, *line, buf[65], *logfile;
418 struct json_object *jobj, *val, *metric, *metric2; 418 struct json_object *jobj, *val, *metric, *metric2;
419 fermentation_log *log; 419 fermentation_log *log;
420 struct tm *mytime; 420 struct tm *mytime;
421 time_t timestamp; 421 time_t timestamp;
422 FILE *fp;
422 423
423 strtok(topic, "/"); // ignore namespace 424 strtok(topic, "/"); // ignore namespace
424 strtok(NULL, "/"); // group_id 425 strtok(NULL, "/"); // group_id
425 strtok(NULL, "/"); // message_type 426 strtok(NULL, "/"); // message_type
426 edge_node = strtok(NULL, "/\0"); 427 edge_node = strtok(NULL, "/\0");
515 } 516 }
516 if (json_object_object_get_ex(metric2, "usage", &val)) { 517 if (json_object_object_get_ex(metric2, "usage", &val)) {
517 log->fan_usage = json_object_get_int(val); 518 log->fan_usage = json_object_get_int(val);
518 } 519 }
519 } 520 }
520 // printf("%s\n", (char *)json_object_to_json_string_ext(metric, 0));
521 } 521 }
522 json_object_put(jobj); 522 json_object_put(jobj);
523 523
524 fermentation_mysql_log(log); 524 /*
525 /* 525 * Build csv log line
526 printf("datetime %s\n", log->datetime); 526 */
527 printf("product %s %s\n", log->product_code, log->product_name); 527 line = xstrcpy(log->datetime);
528 printf("stage/mode %s %s\n", log->stage, log->mode); 528 line = xstrcat(line, (char *)",");
529 printf("temp air %.3f\n", log->temperature_air); 529 line = xstrcat(line, log->mode);
530 printf("temp beer %.3f\n", log->temperature_beer); 530 line = xstrcat(line, (char *)",");
531 printf("temp chiller %.3f\n", log->temperature_chiller); 531 line = xstrcat(line, log->stage);
532 printf("temp room %.3f\n", log->temperature_room); 532 line = xstrcat(line, (char *)",");
533 printf("setpoint %.1f %.1f\n", log->setpoint_low, log->setpoint_high); 533 snprintf(buf, 64, "%.3f", log->temperature_air);
534 printf("heater %3d %ld\n", log->heater_power, log->heater_usage); 534 line = xstrcat(line, buf);
535 printf("cooler %3d %ld\n", log->cooler_power, log->cooler_usage); 535 line = xstrcat(line, (char *)",");
536 printf("fan %3d %ld\n", log->fan_power, log->fan_usage); 536 snprintf(buf, 64, "%.3f", log->temperature_beer);
537 printf("event %s\n", log->event); 537 line = xstrcat(line, buf);
538 printf("fermenter %s\n", log->fermenter_uuid); 538 line = xstrcat(line, (char *)",");
539 */ 539 snprintf(buf, 64, "%.3f", log->temperature_chiller);
540 line = xstrcat(line, buf);
541 line = xstrcat(line, (char *)",");
542 snprintf(buf, 64, "%.3f", log->temperature_room);
543 line = xstrcat(line, buf);
544 line = xstrcat(line, (char *)",");
545 snprintf(buf, 64, "%.1f", log->setpoint_low);
546 line = xstrcat(line, buf);
547 line = xstrcat(line, (char *)",");
548 snprintf(buf, 64, "%.1f", log->setpoint_high);
549 line = xstrcat(line, buf);
550 line = xstrcat(line, (char *)",");
551 snprintf(buf, 64, "%d", log->heater_power);
552 line = xstrcat(line, buf);
553 line = xstrcat(line, (char *)",");
554 snprintf(buf, 64, "%ld", log->heater_usage);
555 line = xstrcat(line, buf);
556 line = xstrcat(line, (char *)",");
557 snprintf(buf, 64, "%d", log->cooler_power);
558 line = xstrcat(line, buf);
559 line = xstrcat(line, (char *)",");
560 snprintf(buf, 64, "%ld", log->cooler_usage);
561 line = xstrcat(line, buf);
562 line = xstrcat(line, (char *)",");
563 if (log->event)
564 line = xstrcat(line, log->event);
565 line = xstrcat(line, (char *)",");
566 if (log->fermenter_uuid)
567 line = xstrcat(line, log->fermenter_uuid);
568
569 /*
570 * Build logfile name
571 */
572 logfile = xstrcpy(Config.web_root);
573 logfile = xstrcat(logfile, (char *)"/log/fermentation/");
574 logfile = xstrcat(logfile, log->product_code);
575 logfile = xstrcat(logfile, (char *)" ");
576 logfile = xstrcat(logfile, log->product_name);
577 logfile = xstrcat(logfile, (char *)".log");
578
579 if (debug)
580 fprintf(stdout, "%s %s\n", logfile, line);
581
582 fp = fopen(logfile, "a");
583 if (fp) {
584 fprintf(fp, "%s\n", line);
585 fclose(fp);
586 } else {
587 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
588 }
589
590 free(logfile);
591 logfile = NULL;
592 free(line);
593 line = NULL;
594
540 if (log->datetime) 595 if (log->datetime)
541 free(log->datetime); 596 free(log->datetime);
542 if (log->product_uuid ) 597 if (log->product_uuid )
543 free(log->product_uuid ); 598 free(log->product_uuid );
544 if (log->product_code ) 599 if (log->product_code )

mercurial