thermferm/server.c

changeset 300
4ce46ff3e37d
parent 299
d1c52fb43e30
child 303
19a9a3912d03
--- a/thermferm/server.c	Sat Nov 15 20:35:32 2014 +0100
+++ b/thermferm/server.c	Sat Nov 29 16:07:34 2014 +0100
@@ -53,7 +53,9 @@
 #define SS_BUFSIZE      1024
 #define SS_TIMEOUT      300
 
-
+#define	MAX_INTERVALS	6
+const int	GRAPH_INTERVAL[MAX_INTERVALS] = { 0, 1, 5, 15, 30, 60, };
+const int	GRAPH_DATALINES[MAX_INTERVALS] = { 0, 800, 3200, 12000, 24000, 48000, };
 
 /*
  * Send message to client
@@ -634,7 +636,7 @@
     char		*opt, *param, *filename, q[5], buffer[256], outbuf[256];
     char		*date_n, *mode_n, *air_n, *beer_n, *target_n, *heater_n, *cooler_n;
     char		*heater_u, *cooler_u;
-    int			heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0;
+    int			heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0, lines = 0, graphstep = 0;
     units_list		*unit;
     FILE		*fp;
 
@@ -676,10 +678,36 @@
 	filename = xstrcat(filename, unit->name);
 	filename = xstrcat(filename, (char *)".log");
 	if ((fp = fopen(filename, "r"))) {
+
+	    /*
+	     * Count the lines in the logfile
+	     */
+	    lines = 0;
+	    while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
+		lines++;
+	    }
+	    fseek(fp, 0L, SEEK_SET);
+	    /*
+	     * We have counted the lines in the logfile including the header lines.
+	     * The header lines should be ignored but there are so few of them, we
+	     * just include them in the total.
+	     * Now find a reasonable interval of lines to sent to the client.
+	     */
+	    for (graphstep = 1; graphstep <= MAX_INTERVALS; graphstep++) {
+		if (lines < GRAPH_DATALINES[graphstep]) {
+		    break;
+		}
+	    }
+	    syslog(LOG_NOTICE, "LIST LOG %s: lines=%d, interval=%d, graphstep=%d", unit->name, lines, GRAPH_INTERVAL[graphstep], graphstep);
+
 	    while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
 		/*
 		 * 2014-11-15 18:39,BEER,20.312,19.750,20.0,0,NA,NA,NA,78105,NA,NA
 		 */
+		q[0] = buffer[11];
+		q[1] = buffer[12];
+		q[2] = buffer[14];
+		q[3] = buffer[15];
 		buffer[strlen(buffer) -1] = '\0';
 		date_n = strtok(buffer, ",\0");	/* timestamp		*/
 		mode_n = strtok(NULL, ",\0");	/* unit mode		*/
@@ -695,33 +723,31 @@
 
 		if (strncmp(mode_n, (char *)"Mode", 4)) {
 		    /*
-		     * Output a line each 30 minutes.
+		     * Output a line at the right intervals
 		     */
-		    if ((q[0] != buffer[11]) || (q[1] != buffer[12]) || (q[3] != buffer[14])) {
-			q[0] = buffer[11];
-			q[1] = buffer[12];
-			q[2] = buffer[13];
-			q[3] = buffer[14];
-			if ((q[3] == '0') || (q[3] == '3')) {
-			    heat_used = cool_used = 0;
-			    if (strcmp(heater_u, "NA") && (sscanf(heater_u, "%d", &h) == 1)) {
-				if (h && heater_l) {
-				    heat_used = ((h - heater_l) * 100) / 1800;
-				}
+		    if (((graphstep == 1)) ||
+			((graphstep == 2) && (q[3] == '0' || q[3] == '5')) ||
+			((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'))) ||
+			((graphstep == 4) && ((q[2] == '0' && q[3] == '0') || (q[2] == '3' && q[3] == '0'))) ||
+			((graphstep == 5) && (q[2] == '0' && q[3] == '0')) ) {
+			heat_used = cool_used = 0;
+			if (strcmp(heater_u, "NA") && (sscanf(heater_u, "%d", &h) == 1)) {
+			    if (h && heater_l) {
+				heat_used = ((h - heater_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60);
 			    }
-			    if (strcmp(cooler_u, "NA") && (sscanf(cooler_u, "%d", &c) == 1)) {
-				if (c && cooler_l) {
-				    cool_used = ((c - cooler_l) * 100) / 1800;
-				}
+			}
+			if (strcmp(cooler_u, "NA") && (sscanf(cooler_u, "%d", &c) == 1)) {
+			    if (c && cooler_l) {
+				cool_used = ((c - cooler_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60);
 			    }
-			    snprintf(outbuf, 255, "%s,%s,%s,%s,%s,%s,%s,%d,%d", 
+			}
+			snprintf(outbuf, 255, "%s,%s,%s,%s,%s,%s,%s,%d,%d", 
 					    date_n, mode_n, air_n, beer_n, target_n, heater_n, cooler_n, heat_used, cool_used);
-		    	    srv_send(outbuf);
-			    if (h && strcmp(heater_u, "NA"))
-				heater_l = h;
-			    if (c & strcmp(cooler_u, "NA"))
-				cooler_l = c;
-			}
+		    	srv_send(outbuf);
+			if (h && strcmp(heater_u, "NA"))
+			    heater_l = h;
+			if (c & strcmp(cooler_u, "NA"))
+			    cooler_l = c;
 		    }
 	    	}
 	    }

mercurial