51 struct hostent *hp; |
51 struct hostent *hp; |
52 |
52 |
53 #define SS_BUFSIZE 1024 |
53 #define SS_BUFSIZE 1024 |
54 #define SS_TIMEOUT 300 |
54 #define SS_TIMEOUT 300 |
55 |
55 |
56 |
56 #define MAX_INTERVALS 6 |
|
57 const int GRAPH_INTERVAL[MAX_INTERVALS] = { 0, 1, 5, 15, 30, 60, }; |
|
58 const int GRAPH_DATALINES[MAX_INTERVALS] = { 0, 800, 3200, 12000, 24000, 48000, }; |
57 |
59 |
58 /* |
60 /* |
59 * Send message to client |
61 * Send message to client |
60 */ |
62 */ |
61 int srv_send(const char *format, ...) |
63 int srv_send(const char *format, ...) |
632 int cmd_list(char *buf) |
634 int cmd_list(char *buf) |
633 { |
635 { |
634 char *opt, *param, *filename, q[5], buffer[256], outbuf[256]; |
636 char *opt, *param, *filename, q[5], buffer[256], outbuf[256]; |
635 char *date_n, *mode_n, *air_n, *beer_n, *target_n, *heater_n, *cooler_n; |
637 char *date_n, *mode_n, *air_n, *beer_n, *target_n, *heater_n, *cooler_n; |
636 char *heater_u, *cooler_u; |
638 char *heater_u, *cooler_u; |
637 int heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0; |
639 int heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0, lines = 0, graphstep = 0; |
638 units_list *unit; |
640 units_list *unit; |
639 FILE *fp; |
641 FILE *fp; |
640 |
642 |
641 opt = strtok(buf, " \0"); |
643 opt = strtok(buf, " \0"); |
642 opt = strtok(NULL, " \0"); |
644 opt = strtok(NULL, " \0"); |
674 } |
676 } |
675 filename = xstrcat(filename, (char *)"/.thermferm/log/"); |
677 filename = xstrcat(filename, (char *)"/.thermferm/log/"); |
676 filename = xstrcat(filename, unit->name); |
678 filename = xstrcat(filename, unit->name); |
677 filename = xstrcat(filename, (char *)".log"); |
679 filename = xstrcat(filename, (char *)".log"); |
678 if ((fp = fopen(filename, "r"))) { |
680 if ((fp = fopen(filename, "r"))) { |
|
681 |
|
682 /* |
|
683 * Count the lines in the logfile |
|
684 */ |
|
685 lines = 0; |
|
686 while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { |
|
687 lines++; |
|
688 } |
|
689 fseek(fp, 0L, SEEK_SET); |
|
690 /* |
|
691 * We have counted the lines in the logfile including the header lines. |
|
692 * The header lines should be ignored but there are so few of them, we |
|
693 * just include them in the total. |
|
694 * Now find a reasonable interval of lines to sent to the client. |
|
695 */ |
|
696 for (graphstep = 1; graphstep <= MAX_INTERVALS; graphstep++) { |
|
697 if (lines < GRAPH_DATALINES[graphstep]) { |
|
698 break; |
|
699 } |
|
700 } |
|
701 syslog(LOG_NOTICE, "LIST LOG %s: lines=%d, interval=%d, graphstep=%d", unit->name, lines, GRAPH_INTERVAL[graphstep], graphstep); |
|
702 |
679 while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { |
703 while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { |
680 /* |
704 /* |
681 * 2014-11-15 18:39,BEER,20.312,19.750,20.0,0,NA,NA,NA,78105,NA,NA |
705 * 2014-11-15 18:39,BEER,20.312,19.750,20.0,0,NA,NA,NA,78105,NA,NA |
682 */ |
706 */ |
|
707 q[0] = buffer[11]; |
|
708 q[1] = buffer[12]; |
|
709 q[2] = buffer[14]; |
|
710 q[3] = buffer[15]; |
683 buffer[strlen(buffer) -1] = '\0'; |
711 buffer[strlen(buffer) -1] = '\0'; |
684 date_n = strtok(buffer, ",\0"); /* timestamp */ |
712 date_n = strtok(buffer, ",\0"); /* timestamp */ |
685 mode_n = strtok(NULL, ",\0"); /* unit mode */ |
713 mode_n = strtok(NULL, ",\0"); /* unit mode */ |
686 air_n = strtok(NULL, ",\0"); /* air temp */ |
714 air_n = strtok(NULL, ",\0"); /* air temp */ |
687 beer_n = strtok(NULL, ",\0"); /* beer temp */ |
715 beer_n = strtok(NULL, ",\0"); /* beer temp */ |
693 heater_u = strtok(NULL, ",\0"); /* heater use counter */ |
721 heater_u = strtok(NULL, ",\0"); /* heater use counter */ |
694 cooler_u = strtok(NULL, ",\0"); /* cooler use counter */ |
722 cooler_u = strtok(NULL, ",\0"); /* cooler use counter */ |
695 |
723 |
696 if (strncmp(mode_n, (char *)"Mode", 4)) { |
724 if (strncmp(mode_n, (char *)"Mode", 4)) { |
697 /* |
725 /* |
698 * Output a line each 30 minutes. |
726 * Output a line at the right intervals |
699 */ |
727 */ |
700 if ((q[0] != buffer[11]) || (q[1] != buffer[12]) || (q[3] != buffer[14])) { |
728 if (((graphstep == 1)) || |
701 q[0] = buffer[11]; |
729 ((graphstep == 2) && (q[3] == '0' || q[3] == '5')) || |
702 q[1] = buffer[12]; |
730 ((graphstep == 3) && ((q[2] == '0' && q[3] == '0') || (q[2] == '1' && q[3] == '5') || (q[2] == '3' && q[3] == '0') || (q[2] == '4' && q[3] == '5'))) || |
703 q[2] = buffer[13]; |
731 ((graphstep == 4) && ((q[2] == '0' && q[3] == '0') || (q[2] == '3' && q[3] == '0'))) || |
704 q[3] = buffer[14]; |
732 ((graphstep == 5) && (q[2] == '0' && q[3] == '0')) ) { |
705 if ((q[3] == '0') || (q[3] == '3')) { |
733 heat_used = cool_used = 0; |
706 heat_used = cool_used = 0; |
734 if (strcmp(heater_u, "NA") && (sscanf(heater_u, "%d", &h) == 1)) { |
707 if (strcmp(heater_u, "NA") && (sscanf(heater_u, "%d", &h) == 1)) { |
735 if (h && heater_l) { |
708 if (h && heater_l) { |
736 heat_used = ((h - heater_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60); |
709 heat_used = ((h - heater_l) * 100) / 1800; |
|
710 } |
|
711 } |
737 } |
712 if (strcmp(cooler_u, "NA") && (sscanf(cooler_u, "%d", &c) == 1)) { |
738 } |
713 if (c && cooler_l) { |
739 if (strcmp(cooler_u, "NA") && (sscanf(cooler_u, "%d", &c) == 1)) { |
714 cool_used = ((c - cooler_l) * 100) / 1800; |
740 if (c && cooler_l) { |
715 } |
741 cool_used = ((c - cooler_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60); |
716 } |
742 } |
717 snprintf(outbuf, 255, "%s,%s,%s,%s,%s,%s,%s,%d,%d", |
743 } |
|
744 snprintf(outbuf, 255, "%s,%s,%s,%s,%s,%s,%s,%d,%d", |
718 date_n, mode_n, air_n, beer_n, target_n, heater_n, cooler_n, heat_used, cool_used); |
745 date_n, mode_n, air_n, beer_n, target_n, heater_n, cooler_n, heat_used, cool_used); |
719 srv_send(outbuf); |
746 srv_send(outbuf); |
720 if (h && strcmp(heater_u, "NA")) |
747 if (h && strcmp(heater_u, "NA")) |
721 heater_l = h; |
748 heater_l = h; |
722 if (c & strcmp(cooler_u, "NA")) |
749 if (c & strcmp(cooler_u, "NA")) |
723 cooler_l = c; |
750 cooler_l = c; |
724 } |
|
725 } |
751 } |
726 } |
752 } |
727 } |
753 } |
728 } |
754 } |
729 free(filename); |
755 free(filename); |