diff -r 79001a992f4f -r e4a9172437bf thermferm/server.c --- a/thermferm/server.c Sun Mar 08 16:51:19 2015 +0100 +++ b/thermferm/server.c Sun Mar 08 17:26:54 2015 +0100 @@ -263,6 +263,7 @@ /* * ARCHIVE DIR * ARCHIVE GET filename + * ARCHIVE LOG filename * ARCHIVE HELP */ int cmd_archive(char *buf) @@ -290,6 +291,7 @@ srv_send((char *)"Recognized commands:"); srv_send((char *)"ARCHIVE DIR Archived logfiles directory"); srv_send((char *)"ARCHIVE GET filename Archived logfile download"); + srv_send((char *)"ARCHIVE LOG filename Archived logfile data in graphsteps"); srv_send((char *)"."); return 0; } @@ -395,7 +397,102 @@ fclose(fp); } else { srv_send((char *)"440 No such file"); - return 1; + } + + free(name); + name = NULL; + return 1; + } + + if (strcmp(opt, (char *)"LOG") == 0) { + if (getenv((char *)"USER") == NULL) { + name = xstrcpy((char *)"/root"); + } else { + name = xstrcpy(getenv((char *)"HOME")); + } + name = xstrcat(name, (char *)"/.thermferm/log/"); + name = xstrcat(name, param); + + if ((fp = fopen(name, "r"))) { + char buffer[256], outbuf[256], q[5]; + char *date_n, *mode_n, *air_n, *beer_n, *target_n, *heater_n, *cooler_n; + char *heater_u, *cooler_u; + int lines = 0, heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0, graphstep = 0; + + srv_send((char *)"212 Logfile list follows:"); + 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, "ARCHIVE LOG %s: lines=%d, interval=%d, graphstep=%d", param, 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 */ + air_n = strtok(NULL, ",\0"); /* air temp */ + beer_n = strtok(NULL, ",\0"); /* beer temp */ + target_n = strtok(NULL, ",\0"); /* target temp */ + heater_n = strtok(NULL, ",\0"); /* current heater state */ + cooler_n = strtok(NULL, ",\0"); /* current cooler state */ + heater_u = strtok(NULL, ",\0"); /* current fan state */ + heater_u = strtok(NULL, ",\0"); /* current door state */ + heater_u = strtok(NULL, ",\0"); /* heater use counter */ + cooler_u = strtok(NULL, ",\0"); /* cooler use counter */ + + if (strncmp(mode_n, (char *)"Mode", 4)) { + /* + * Output a line at the right intervals + */ + 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) / (GRAPH_INTERVAL[graphstep] * 60); + } + } + 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((char *)"."); + fclose(fp); + } else { + srv_send((char *)"440 No such file"); } free(name); @@ -2363,8 +2460,8 @@ srv_send((char *)"Recognized commands:"); srv_send((char *)""); // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 - srv_send((char *)"ARCHIVE DIR Archived logfiles directory"); - srv_send((char *)"ARCHIVE GET filename Archived logfile download"); + srv_send((char *)"ARCHIVE [parameters] Archive commands"); + srv_send((char *)"ARCHIVE HELP Archive help screen"); srv_send((char *)"DEVICE ADD type Add new Device type"); srv_send((char *)"DEVICE DEL uuid Delete Device by uuid"); srv_send((char *)"DEVICE LIST List Devices"); @@ -2373,7 +2470,7 @@ srv_send((char *)"GLOBAL GET Get global settings"); srv_send((char *)"GLOBAL PUT Put global settings"); srv_send((char *)"LIST List all fermenter units"); - srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines"); + srv_send((char *)"LIST LOG uuid List logfile data graphsteps"); srv_send((char *)"PING Check if server is alive"); srv_send((char *)"PROFILE uuid,name Profile rename"); srv_send((char *)"PROFILE ADD name Add new Profile with name");