thermferm/server.c

changeset 380
75afd21885f2
parent 369
71f9c1288f3e
child 381
b22f8bf63b17
equal deleted inserted replaced
379:50675fd80fed 380:75afd21885f2
311 * ARCHIVE LOG filename 311 * ARCHIVE LOG filename
312 * ARCHIVE HELP 312 * ARCHIVE HELP
313 */ 313 */
314 int cmd_archive(char *buf) 314 int cmd_archive(char *buf)
315 { 315 {
316 char *opt, *param, *name = NULL, *filename = NULL, mbits[11], tstr[24]; 316 char *opt, *param, *name = NULL, *filename = NULL, *logname = NULL, mbits[11], tstr[24];
317 DIR *dd; 317 DIR *dd;
318 FILE *fp; 318 FILE *fp;
319 struct dirent entry, *result; 319 struct dirent entry, *result;
320 ls_list *lsx = NULL, *tmp; 320 ls_list *lsx = NULL, *tmp;
321 struct stat sbuf; 321 struct stat sbuf;
322 struct tm *tbuf; 322 struct tm *tbuf;
323 time_t ftime; 323 time_t ftime;
324 int found;
325 units_list *unit;
326
324 327
325 opt = strtok(buf, " \0"); 328 opt = strtok(buf, " \0");
326 opt = strtok(NULL, " \0"); 329 opt = strtok(NULL, " \0");
327 330
328 if (opt == NULL) { 331 if (opt == NULL) {
359 if (result == NULL) /* End of directory */ 362 if (result == NULL) /* End of directory */
360 break; 363 break;
361 if (result->d_name[0] != '.') { 364 if (result->d_name[0] != '.') {
362 filename = xstrcpy(name); 365 filename = xstrcpy(name);
363 filename = xstrcat(filename, result->d_name); 366 filename = xstrcat(filename, result->d_name);
364 if ((stat(filename, &sbuf)) == 0) { 367 /*
368 * Remove files from the list when they are in use
369 */
370 found = 0;
371 for (unit = Config.units; unit; unit = unit->next) {
372 if (unit->mode != UNITMODE_OFF) {
373 logname = xstrcpy(unit->name);
374 logname = xstrcat(logname, (char *)".log");
375 if (! strcmp(result->d_name, logname))
376 found = 1;
377 free(logname);
378 logname = NULL;
379 }
380 }
381 if ((found == 0) && ((stat(filename, &sbuf)) == 0)) {
365 fill_list(&lsx, result->d_name, sbuf.st_mode, sbuf.st_size, sbuf.st_mtime); 382 fill_list(&lsx, result->d_name, sbuf.st_mode, sbuf.st_size, sbuf.st_mtime);
366 } 383 }
367 free(filename); 384 free(filename);
368 filename = NULL; 385 filename = NULL;
369 } 386 }
633 srv_send((char *)"501 Subcommand missing"); 650 srv_send((char *)"501 Subcommand missing");
634 return 1; 651 return 1;
635 } 652 }
636 param = strtok(NULL, "\0"); 653 param = strtok(NULL, "\0");
637 654
655 if (strcmp(opt, (char *)"HELP") == 0) {
656 srv_send((char *)"100 Help text follows:");
657 srv_send((char *)"Recognized commands:");
658 srv_send((char *)"DEVICE ADD type Add device (RC433/DHT/I2C/SPI)");
659 srv_send((char *)"DEVICE DEL uuid Delete device by uuid");
660 srv_send((char *)"DEVICE LIST List all devices");
661 srv_send((char *)"DEVICE GET uuid Read device by uuid parameters");
662 srv_send((char *)"DEVICE PUT uuid Write device by uuid parameters");
663 srv_send((char *)".");
664 return 0;
665 }
666
638 if (strcmp(opt, (char *)"LIST") == 0) { 667 if (strcmp(opt, (char *)"LIST") == 0) {
639 srv_send((char *)"212 Devices list follows:"); 668 srv_send((char *)"212 Devices list follows:");
640 for (device = Config.devices; device; device = device->next) { 669 for (device = Config.devices; device; device = device->next) {
641 srv_send((char *)"%s,%s,%d,%d,%s,%s,%d", device->uuid, device->address, device->subdevice, 670 srv_send((char *)"%s,%s,%d,%d,%s,%s,%d", device->uuid, device->address, device->subdevice,
642 device->inuse, device->comment, DEVDIR[device->direction], device->value + device->offset); 671 device->inuse, device->comment, DEVDIR[device->direction], device->value + device->offset);
940 if (opt == NULL) { 969 if (opt == NULL) {
941 srv_send((char *)"501 Subcommand missing"); 970 srv_send((char *)"501 Subcommand missing");
942 return 1; 971 return 1;
943 } 972 }
944 973
974 if (strcmp(opt, (char *)"HELP") == 0) {
975 srv_send((char *)"100 Help text follows:");
976 srv_send((char *)"Recognized commands:");
977 srv_send((char *)"GLOBAL GET Get global settings");
978 srv_send((char *)"GLOBAL PUT Put global settings");
979 srv_send((char *)".");
980 return 0;
981 }
982
945 if (strcmp(opt, (char *)"GET") == 0) { 983 if (strcmp(opt, (char *)"GET") == 0) {
946 srv_send((char *)"213 Global Settings record follows:"); 984 srv_send((char *)"213 Global Settings record follows:");
947 srv_send((char *)"RELEASE,%s", VERSION); 985 srv_send((char *)"RELEASE,%s", VERSION);
948 srv_send((char *)"NAME,%s", Config.name); 986 srv_send((char *)"NAME,%s", Config.name);
949 srv_send((char *)"PORT,%d", Config.my_port); 987 srv_send((char *)"PORT,%d", Config.my_port);
1078 for (unit = Config.units; unit; unit = unit->next) { 1116 for (unit = Config.units; unit; unit = unit->next) {
1079 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 1117 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
1080 } 1118 }
1081 srv_send((char *)"."); 1119 srv_send((char *)".");
1082 return 1; 1120 return 1;
1121
1122 } else if (strcmp(opt, (char *)"HELP") == 0) {
1123 srv_send((char *)"100 Help text follows:");
1124 srv_send((char *)"Recognized commands:");
1125 srv_send((char *)"LIST List available units");
1126 srv_send((char *)"LIST LOG uuid List logfile of unit by uuid");
1127 srv_send((char *)".");
1128 return 0;
1083 1129
1084 } else if (strcmp(opt, (char *)"LOG") == 0) { 1130 } else if (strcmp(opt, (char *)"LOG") == 0) {
1085 1131
1086 param = strtok(NULL, "\0"); 1132 param = strtok(NULL, "\0");
1087 if (param == NULL) { 1133 if (param == NULL) {
1222 if (opt == NULL) { 1268 if (opt == NULL) {
1223 srv_send((char *)"501 Subcommand missing"); 1269 srv_send((char *)"501 Subcommand missing");
1224 return 1; 1270 return 1;
1225 } 1271 }
1226 1272
1273 if (strcmp(opt, (char *)"HELP") == 0) {
1274 srv_send((char *)"100 Help text follows:");
1275 srv_send((char *)"Recognized commands:");
1276 srv_send((char *)"PROFILE uuid,name Profile rename");
1277 srv_send((char *)"PROFILE ADD name Add new Profile with name");
1278 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid");
1279 srv_send((char *)"PROFILE LIST List available profiles");
1280 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid");
1281 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid");
1282 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
1283 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
1284 srv_send((char *)".");
1285 return 0;
1286 }
1287
1227 if (strcmp(opt, (char *)"LIST") == 0) { 1288 if (strcmp(opt, (char *)"LIST") == 0) {
1228 /* 1289 /*
1229 * Fermenting profiles 1290 * Fermenting profiles
1230 */ 1291 */
1231 srv_send((char *)"212 Profiles list follows:"); 1292 srv_send((char *)"212 Profiles list follows:");
1478 if (opt == NULL) { 1539 if (opt == NULL) {
1479 srv_send((char *)"501 Subcommand missing"); 1540 srv_send((char *)"501 Subcommand missing");
1480 return 1; 1541 return 1;
1481 } 1542 }
1482 param = strtok(NULL, "\0"); 1543 param = strtok(NULL, "\0");
1544
1545 if (strcmp(opt, (char *)"HELP") == 0) {
1546 srv_send((char *)"100 Help text follows:");
1547 srv_send((char *)"Recognized commands:");
1548 srv_send((char *)"SIMULATOR ADD name Add a new Simulator with name");
1549 srv_send((char *)"SIMULATOR DEL uuid Delete Simulator by uuid");
1550 srv_send((char *)"SIMULATOR LIST List all Simulators");
1551 srv_send((char *)"SIMULATOR GET uuid Get Simulator record by uuid");
1552 srv_send((char *)"SIMULATOR PUT uuid Put Simulator record by uuid");
1553 srv_send((char *)".");
1554 return 0;
1555 }
1483 1556
1484 if (strcmp(opt, (char *)"LIST") == 0) { 1557 if (strcmp(opt, (char *)"LIST") == 0) {
1485 srv_send((char *)"212 Simulators list follows:"); 1558 srv_send((char *)"212 Simulators list follows:");
1486 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 1559 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1487 srv_send((char *)"%s,%s", simulator->uuid, simulator->name); 1560 srv_send((char *)"%s,%s", simulator->uuid, simulator->name);
1844 srv_send((char *)"501 Subcommand missing"); 1917 srv_send((char *)"501 Subcommand missing");
1845 return 1; 1918 return 1;
1846 } 1919 }
1847 param = strtok(NULL, "\0"); 1920 param = strtok(NULL, "\0");
1848 1921
1922 if (strcmp(opt, (char *)"HELP") == 0) {
1923 srv_send((char *)"100 Help text follows:");
1924 srv_send((char *)"Recognized commands:");
1925 srv_send((char *)"UNIT ADD name Add a new Unit with name");
1926 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid");
1927 srv_send((char *)"UNIT LIST List all Units");
1928 srv_send((char *)"UNIT GET uuid Get Unit record by uuid");
1929 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid");
1930 srv_send((char *)".");
1931 return 0;
1932 }
1933
1849 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) { 1934 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
1850 srv_send((char *)"212 Fermenter list follows:"); 1935 srv_send((char *)"212 Fermenter list follows:");
1851 for (unit = Config.units; unit; unit = unit->next) { 1936 for (unit = Config.units; unit; unit = unit->next) {
1852 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 1937 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
1853 } 1938 }
2457 srv_send((char *)"Recognized commands:"); 2542 srv_send((char *)"Recognized commands:");
2458 srv_send((char *)""); 2543 srv_send((char *)"");
2459 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 2544 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
2460 srv_send((char *)"ARCHIVE <CMD> [parameters] Archive commands"); 2545 srv_send((char *)"ARCHIVE <CMD> [parameters] Archive commands");
2461 srv_send((char *)"ARCHIVE HELP Archive help screen"); 2546 srv_send((char *)"ARCHIVE HELP Archive help screen");
2462 srv_send((char *)"DEVICE ADD type Add new Device type"); 2547 srv_send((char *)"DEVICE <CMD> [parameters] Device commands");
2463 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid"); 2548 srv_send((char *)"DEVICE HELP Device help screen");
2464 srv_send((char *)"DEVICE LIST List Devices"); 2549 srv_send((char *)"GLOBAL <CMD> [parameters] Global commands");
2465 srv_send((char *)"DEVICE GET uuid Get Device record by uuid"); 2550 srv_send((char *)"GLOBAL HELP Global help screen");
2466 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid"); 2551 srv_send((char *)"LIST <CMD> [parameters] List commands");
2467 srv_send((char *)"GLOBAL GET Get global settings"); 2552 srv_send((char *)"LIST HELP List help screen");
2468 srv_send((char *)"GLOBAL PUT Put global settings");
2469 srv_send((char *)"LIST List all fermenter units");
2470 srv_send((char *)"LIST LOG uuid List logfile data graphsteps");
2471 srv_send((char *)"PING Check if server is alive"); 2553 srv_send((char *)"PING Check if server is alive");
2472 srv_send((char *)"PROFILE uuid,name Profile rename"); 2554 srv_send((char *)"PROFILE <CMD> [parameters] Profile commands");
2473 srv_send((char *)"PROFILE ADD name Add new Profile with name"); 2555 srv_send((char *)"PROFILE HELP Profile help screen");
2474 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid");
2475 srv_send((char *)"PROFILE LIST List available profiles");
2476 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid");
2477 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid");
2478 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
2479 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
2480 #ifdef USE_SIMULATOR 2556 #ifdef USE_SIMULATOR
2481 srv_send((char *)"SIMULATOR ADD name Add a new Simulator with name"); 2557 srv_send((char *)"SIMULATOR <CMD> [parameters] Simulator commands");
2482 srv_send((char *)"SIMULATOR DEL uuid Delete Simulator by uuid"); 2558 srv_send((char *)"SIMULATOR HELP Simulator help screen");
2483 srv_send((char *)"SIMULATOR LIST List all Simulators"); 2559 #endif
2484 srv_send((char *)"SIMULATOR GET uuid Get Simulator record by uuid"); 2560 srv_send((char *)"UNIT <CMD> [parameters] Unit commands");
2485 srv_send((char *)"SIMULATOR PUT uuid Put Simulator record by uuid"); 2561 srv_send((char *)"UNIT HELP Unit help screen");
2486 #endif
2487 srv_send((char *)"UNIT ADD name Add a new Unit with name");
2488 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid");
2489 srv_send((char *)"UNIT LIST List all Units");
2490 srv_send((char *)"UNIT GET uuid Get Unit record by uuid");
2491 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid");
2492 srv_send((char *)"."); 2562 srv_send((char *)".");
2493 } else if (strncmp(buf, "LIST", 4) == 0) { 2563 } else if (strncmp(buf, "LIST", 4) == 0) {
2494 cmd_list(buf); 2564 cmd_list(buf);
2495 } else if (strncmp(buf, "PING", 4) == 0) { 2565 } else if (strncmp(buf, "PING", 4) == 0) {
2496 srv_send((char *)"101 PONG"); 2566 srv_send((char *)"101 PONG");

mercurial