thermferm/server.c

changeset 331
e4a9172437bf
parent 330
79001a992f4f
child 344
acd840c9fcc0
equal deleted inserted replaced
330:79001a992f4f 331:e4a9172437bf
261 261
262 262
263 /* 263 /*
264 * ARCHIVE DIR 264 * ARCHIVE DIR
265 * ARCHIVE GET filename 265 * ARCHIVE GET filename
266 * ARCHIVE LOG filename
266 * ARCHIVE HELP 267 * ARCHIVE HELP
267 */ 268 */
268 int cmd_archive(char *buf) 269 int cmd_archive(char *buf)
269 { 270 {
270 char *opt, *param, *name = NULL, *filename = NULL, mbits[11], tstr[24]; 271 char *opt, *param, *name = NULL, *filename = NULL, mbits[11], tstr[24];
288 if (strcmp(opt, (char *)"HELP") == 0) { 289 if (strcmp(opt, (char *)"HELP") == 0) {
289 srv_send((char *)"100 Help text follows:"); 290 srv_send((char *)"100 Help text follows:");
290 srv_send((char *)"Recognized commands:"); 291 srv_send((char *)"Recognized commands:");
291 srv_send((char *)"ARCHIVE DIR Archived logfiles directory"); 292 srv_send((char *)"ARCHIVE DIR Archived logfiles directory");
292 srv_send((char *)"ARCHIVE GET filename Archived logfile download"); 293 srv_send((char *)"ARCHIVE GET filename Archived logfile download");
294 srv_send((char *)"ARCHIVE LOG filename Archived logfile data in graphsteps");
293 srv_send((char *)"."); 295 srv_send((char *)".");
294 return 0; 296 return 0;
295 } 297 }
296 298
297 if (strcmp(opt, (char *)"DIR") == 0) { 299 if (strcmp(opt, (char *)"DIR") == 0) {
393 } 395 }
394 srv_send((char *)"."); 396 srv_send((char *)".");
395 fclose(fp); 397 fclose(fp);
396 } else { 398 } else {
397 srv_send((char *)"440 No such file"); 399 srv_send((char *)"440 No such file");
398 return 1; 400 }
401
402 free(name);
403 name = NULL;
404 return 1;
405 }
406
407 if (strcmp(opt, (char *)"LOG") == 0) {
408 if (getenv((char *)"USER") == NULL) {
409 name = xstrcpy((char *)"/root");
410 } else {
411 name = xstrcpy(getenv((char *)"HOME"));
412 }
413 name = xstrcat(name, (char *)"/.thermferm/log/");
414 name = xstrcat(name, param);
415
416 if ((fp = fopen(name, "r"))) {
417 char buffer[256], outbuf[256], q[5];
418 char *date_n, *mode_n, *air_n, *beer_n, *target_n, *heater_n, *cooler_n;
419 char *heater_u, *cooler_u;
420 int lines = 0, heater_l = 0, cooler_l = 0, h = 0, c = 0, heat_used = 0, cool_used = 0, graphstep = 0;
421
422 srv_send((char *)"212 Logfile list follows:");
423 while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
424 lines++;
425 }
426 fseek(fp, 0L, SEEK_SET);
427 /*
428 * We have counted the lines in the logfile including the header lines.
429 * The header lines should be ignored but there are so few of them, we
430 * just include them in the total.
431 * Now find a reasonable interval of lines to sent to the client.
432 */
433 for (graphstep = 1; graphstep <= MAX_INTERVALS; graphstep++) {
434 if (lines < GRAPH_DATALINES[graphstep]) {
435 break;
436 }
437 }
438 syslog(LOG_NOTICE, "ARCHIVE LOG %s: lines=%d, interval=%d, graphstep=%d", param, lines, GRAPH_INTERVAL[graphstep], graphstep);
439
440 while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
441 /*
442 * 2014-11-15 18:39,BEER,20.312,19.750,20.0,0,NA,NA,NA,78105,NA,NA
443 */
444 q[0] = buffer[11];
445 q[1] = buffer[12];
446 q[2] = buffer[14];
447 q[3] = buffer[15];
448 buffer[strlen(buffer) -1] = '\0';
449 date_n = strtok(buffer, ",\0"); /* timestamp */
450 mode_n = strtok(NULL, ",\0"); /* unit mode */
451 air_n = strtok(NULL, ",\0"); /* air temp */
452 beer_n = strtok(NULL, ",\0"); /* beer temp */
453 target_n = strtok(NULL, ",\0"); /* target temp */
454 heater_n = strtok(NULL, ",\0"); /* current heater state */
455 cooler_n = strtok(NULL, ",\0"); /* current cooler state */
456 heater_u = strtok(NULL, ",\0"); /* current fan state */
457 heater_u = strtok(NULL, ",\0"); /* current door state */
458 heater_u = strtok(NULL, ",\0"); /* heater use counter */
459 cooler_u = strtok(NULL, ",\0"); /* cooler use counter */
460
461 if (strncmp(mode_n, (char *)"Mode", 4)) {
462 /*
463 * Output a line at the right intervals
464 */
465 if (((graphstep == 1)) ||
466 ((graphstep == 2) && (q[3] == '0' || q[3] == '5')) ||
467 ((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'))) ||
468 ((graphstep == 4) && ((q[2] == '0' && q[3] == '0') || (q[2] == '3' && q[3] == '0'))) ||
469 ((graphstep == 5) && (q[2] == '0' && q[3] == '0')) ) {
470 heat_used = cool_used = 0;
471 if (strcmp(heater_u, "NA") && (sscanf(heater_u, "%d", &h) == 1)) {
472 if (h && heater_l) {
473 heat_used = ((h - heater_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60);
474 }
475 }
476 if (strcmp(cooler_u, "NA") && (sscanf(cooler_u, "%d", &c) == 1)) {
477 if (c && cooler_l) {
478 cool_used = ((c - cooler_l) * 100) / (GRAPH_INTERVAL[graphstep] * 60);
479 }
480 }
481 snprintf(outbuf, 255, "%s,%s,%s,%s,%s,%s,%s,%d,%d",
482 date_n, mode_n, air_n, beer_n, target_n, heater_n, cooler_n, heat_used, cool_used);
483 srv_send(outbuf);
484 if (h && strcmp(heater_u, "NA"))
485 heater_l = h;
486 if (c & strcmp(cooler_u, "NA"))
487 cooler_l = c;
488 }
489 }
490 }
491
492 srv_send((char *)".");
493 fclose(fp);
494 } else {
495 srv_send((char *)"440 No such file");
399 } 496 }
400 497
401 free(name); 498 free(name);
402 name = NULL; 499 name = NULL;
403 return 1; 500 return 1;
2361 } else if (strncmp(buf, "HELP", 4) == 0) { 2458 } else if (strncmp(buf, "HELP", 4) == 0) {
2362 srv_send((char *)"100 Help text follows"); 2459 srv_send((char *)"100 Help text follows");
2363 srv_send((char *)"Recognized commands:"); 2460 srv_send((char *)"Recognized commands:");
2364 srv_send((char *)""); 2461 srv_send((char *)"");
2365 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 2462 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
2366 srv_send((char *)"ARCHIVE DIR Archived logfiles directory"); 2463 srv_send((char *)"ARCHIVE <CMD> [parameters] Archive commands");
2367 srv_send((char *)"ARCHIVE GET filename Archived logfile download"); 2464 srv_send((char *)"ARCHIVE HELP Archive help screen");
2368 srv_send((char *)"DEVICE ADD type Add new Device type"); 2465 srv_send((char *)"DEVICE ADD type Add new Device type");
2369 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid"); 2466 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid");
2370 srv_send((char *)"DEVICE LIST List Devices"); 2467 srv_send((char *)"DEVICE LIST List Devices");
2371 srv_send((char *)"DEVICE GET uuid Get Device record by uuid"); 2468 srv_send((char *)"DEVICE GET uuid Get Device record by uuid");
2372 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid"); 2469 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid");
2373 srv_send((char *)"GLOBAL GET Get global settings"); 2470 srv_send((char *)"GLOBAL GET Get global settings");
2374 srv_send((char *)"GLOBAL PUT Put global settings"); 2471 srv_send((char *)"GLOBAL PUT Put global settings");
2375 srv_send((char *)"LIST List all fermenter units"); 2472 srv_send((char *)"LIST List all fermenter units");
2376 srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines"); 2473 srv_send((char *)"LIST LOG uuid List logfile data graphsteps");
2377 srv_send((char *)"PING Check if server is alive"); 2474 srv_send((char *)"PING Check if server is alive");
2378 srv_send((char *)"PROFILE uuid,name Profile rename"); 2475 srv_send((char *)"PROFILE uuid,name Profile rename");
2379 srv_send((char *)"PROFILE ADD name Add new Profile with name"); 2476 srv_send((char *)"PROFILE ADD name Add new Profile with name");
2380 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid"); 2477 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid");
2381 srv_send((char *)"PROFILE LIST List available profiles"); 2478 srv_send((char *)"PROFILE LIST List available profiles");

mercurial