thermferm/server.c

changeset 259
b7c967359771
parent 258
e02393b29733
child 261
e4341cfbc2a8
equal deleted inserted replaced
258:e02393b29733 259:b7c967359771
34 extern int run_pause; 34 extern int run_pause;
35 extern int run_hold; 35 extern int run_hold;
36 extern sys_config Config; 36 extern sys_config Config;
37 extern const char UNITMODE[5][8]; 37 extern const char UNITMODE[5][8];
38 extern const char TEMPSTATE[3][8]; 38 extern const char TEMPSTATE[3][8];
39 extern const char DEVTYPE[7][6]; 39 extern const char DEVTYPE[8][6];
40 extern const char DEVPRESENT[4][6]; 40 extern const char DEVPRESENT[4][6];
41 extern const char DEVDIR[7][11]; 41 extern const char DEVDIR[7][11];
42 extern const char PROFSTATE[5][6]; 42 extern const char PROFSTATE[5][6];
43 43
44 44
239 srv_send((char *)"212 Devices list follows:"); 239 srv_send((char *)"212 Devices list follows:");
240 for (device = Config.devices; device; device = device->next) { 240 for (device = Config.devices; device; device = device->next) {
241 srv_send((char *)"%s,%s,%d,%d,%s,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment, DEVDIR[device->direction]); 241 srv_send((char *)"%s,%s,%d,%d,%s,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment, DEVDIR[device->direction]);
242 } 242 }
243 srv_send((char *)"."); 243 srv_send((char *)".");
244 return 0; 244 return 1;
245 } 245 }
246 246
247 if (param == NULL) { 247 if (param == NULL) {
248 srv_send((char *)"502 Parameter missing"); 248 srv_send((char *)"502 Parameter missing");
249 return 1; 249 return 1;
257 device->next = NULL; 257 device->next = NULL;
258 device->version = 1; 258 device->version = 1;
259 device->uuid = malloc(37); 259 device->uuid = malloc(37);
260 uuid_generate(uu); 260 uuid_generate(uu);
261 uuid_unparse(uu, device->uuid); 261 uuid_unparse(uu, device->uuid);
262 for (i = 0; i < 7; i++) { 262 for (i = 0; i < 8; i++) {
263 if (strcmp(param, DEVTYPE[i]) == 0) { 263 if (strcmp(param, DEVTYPE[i]) == 0) {
264 device->type = i; 264 device->type = i;
265 break; 265 break;
266 } 266 }
267 } 267 }
377 } 377 }
378 kwd = strtok(ibuf, ",\0"); 378 kwd = strtok(ibuf, ",\0");
379 val = strtok(NULL, "\0"); 379 val = strtok(NULL, "\0");
380 if (kwd && val) { 380 if (kwd && val) {
381 if (strcmp(kwd, (char *)"TYPE") == 0) { 381 if (strcmp(kwd, (char *)"TYPE") == 0) {
382 for (i = 0; i < 7; i++) { 382 for (i = 0; i < 8; i++) {
383 if (strcmp(val, DEVTYPE[i]) == 0) { 383 if (strcmp(val, DEVTYPE[i]) == 0) {
384 device->type = i; 384 device->type = i;
385 break; 385 break;
386 } 386 }
387 } 387 }
599 srv_send((char *)"212 Fermenter list follows:"); 599 srv_send((char *)"212 Fermenter list follows:");
600 for (unit = Config.units; unit; unit = unit->next) { 600 for (unit = Config.units; unit; unit = unit->next) {
601 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 601 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
602 } 602 }
603 srv_send((char *)"."); 603 srv_send((char *)".");
604 return 0; 604 return 1;
605 605
606 } else if (strcmp(opt, (char *)"LOG") == 0) { 606 } else if (strcmp(opt, (char *)"LOG") == 0) {
607 607
608 param = strtok(NULL, "\0"); 608 param = strtok(NULL, "\0");
609 if (param == NULL) { 609 if (param == NULL) {
640 } 640 }
641 } 641 }
642 free(filename); 642 free(filename);
643 filename = NULL; 643 filename = NULL;
644 srv_send((char *)"."); 644 srv_send((char *)".");
645 return 0; 645 return 1;
646 } 646 }
647 647
648 srv_send((char *)"504 Subcommand error"); 648 srv_send((char *)"504 Subcommand error");
649 return 1; 649 return 1;
650 } 650 }
900 } 900 }
901 901
902 srv_send((char *)"504 Subcommand error"); 902 srv_send((char *)"504 Subcommand error");
903 return 1; 903 return 1;
904 } 904 }
905
906
907
908 #ifdef USE_SIMULATOR
909 int delete_Simulator(char *uuid)
910 {
911 simulator_list *current = Config.simulators;
912 simulator_list *previous = NULL;
913
914 while (current) {
915 if (strcmp(current->uuid, uuid) == 0) {
916 if (previous == NULL) {
917 Config.simulators = current->next;
918 free(current->uuid);
919 current->uuid = NULL;
920 free(current->name);
921 current->name = NULL;
922 free(current);
923 return 1;
924 } else {
925 free(current->uuid);
926 current->uuid = NULL;
927 free(current->name);
928 current->name = NULL;
929 previous->next = current->next;
930 free(current);
931 current = previous->next;
932 return 1;
933 }
934 } else {
935 previous = current;
936 current = current->next;
937 }
938 }
939 return 0;
940 }
941
942
943
944 /*
945 * SIMULATOR ADD name
946 * SIMULATOR DEL uuid
947 * SIMULATOR LIST
948 * SIMULATOR GET uuid
949 * SIMULATOR PUT uuid
950 */
951 int cmd_simulator(char *buf)
952 {
953 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
954 simulator_list *simulator, *tmps;
955 socklen_t fromlen;
956 int i, rc, rlen, ival;
957 float fval;
958 uuid_t uu;
959
960 opt = strtok(buf, " \0");
961 opt = strtok(NULL, " \0");
962
963 if (opt == NULL) {
964 srv_send((char *)"501 Subcommand missing");
965 return 1;
966 }
967 param = strtok(NULL, "\0");
968
969 if (strcmp(opt, (char *)"LIST") == 0) {
970 srv_send((char *)"212 Simulators list follows:");
971 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
972 srv_send((char *)"%s,%s", simulator->uuid, simulator->name);
973 }
974 srv_send((char *)".");
975 return 1;
976 }
977
978 if (param == NULL) {
979 srv_send((char *)"502 Parameter missing");
980 return 1;
981 }
982
983 if (strcmp(opt, (char *)"ADD") == 0) {
984
985 /*
986 * For now, only one simulator is allowed.
987 */
988 if (Config.simulators) {
989 srv_send((char *)"441 Maximum simulators reached");
990 return 1;
991 }
992
993 simulator = (simulator_list *)malloc(sizeof(simulator_list));
994 simulator->next = NULL;
995 simulator->version = 1;
996 simulator->uuid = malloc(37);
997 uuid_generate(uu);
998 uuid_unparse(uu, simulator->uuid);
999 simulator->name = xstrcpy(param);
1000 simulator->volume_air = 150;
1001 simulator->volume_beer = 50;
1002 simulator->room_temperature = simulator->air_temperature = simulator->beer_temperature = simulator->s_cool_temp = simulator->s_heat_temp = 20.0;
1003 simulator->cooler_temp = -3.0; /* Cooling temperature */
1004 simulator->cooler_time = 720; /* About 12 minutes for the cooler plate */
1005 simulator->cooler_size = 0.8; /* 0.8 square meter cooler plate */
1006 simulator->heater_temp = 150.0; /* Heating temperature */
1007 simulator->heater_time = 3; /* 3 seconds to heat-up */
1008 simulator->heater_size = 0.01; /* 0.01 square meter heater plate */
1009 simulator->heater_state = simulator->cooler_state = 0;
1010 simulator->frigo_isolation = 0.002;
1011 simulator->s_yeast_heat = 0.0;
1012 simulator->s_yeast_started = simulator->s_cool_changed = simulator->s_heat_changed = (int)0;
1013
1014 if (Config.simulators == NULL) {
1015 Config.simulators = simulator;
1016 } else {
1017 for (tmps = Config.simulators; tmps; tmps = tmps->next) {
1018 if (tmps->next == NULL) {
1019 tmps->next = simulator;
1020 break;
1021 }
1022 }
1023 }
1024
1025 syslog(LOG_NOTICE, "Simulator %s added", simulator->uuid);
1026 srv_send((char *)"211 Simulator %s added", simulator->uuid);
1027 return 0;
1028 }
1029
1030 if (strcmp(opt, (char *)"DEL") == 0) {
1031 rc = delete_Simulator(param);
1032 if (rc) {
1033 syslog(LOG_NOTICE, "Simulator %s deleted", param);
1034 srv_send((char *)"211 Simulator %s deleted", param);
1035 return 0;
1036 } else {
1037 srv_send((char *)"440 No such simulator");
1038 return 1;
1039 }
1040 }
1041
1042 if (strcmp(opt, (char *)"GET") == 0) {
1043 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1044 if (strcmp(simulator->uuid, param) == 0) {
1045 srv_send((char *)"213 Simulator record follows:");
1046 srv_send((char *)"NAME,%s", simulator->name);
1047 srv_send((char *)"VOLUME_AIR,%d", simulator->volume_air);
1048 srv_send((char *)"VOLUME_BEER,%d", simulator->volume_beer);
1049 srv_send((char *)"ROOM_TEMPERATURE,%.1f", simulator->room_temperature);
1050 srv_send((char *)"AIR_TEMPERATURE,%.3f", simulator->air_temperature);
1051 srv_send((char *)"BEER_TEMPERATURE,%.3f", simulator->beer_temperature);
1052 srv_send((char *)"COOLER_TEMP,%.1f", simulator->cooler_temp);
1053 srv_send((char *)"COOLER_TIME,%d", simulator->cooler_time);
1054 srv_send((char *)"COOLER_SIZE,%.3d", simulator->cooler_size);
1055 srv_send((char *)"HEATER_TEMP,%.1f", simulator->heater_temp);
1056 srv_send((char *)"HEATER_TIME,%d", simulator->heater_time);
1057 srv_send((char *)"HEATER_SIZE,%.3f", simulator->heater_size);
1058 srv_send((char *)"HEATER_STATE,%d", simulator->heater_state);
1059 srv_send((char *)"COOLER_STATE,%d", simulator->cooler_state);
1060 srv_send((char *)"FRIGO_ISOLATION,%.6f", simulator->frigo_isolation);
1061 srv_send((char *)".");
1062 return 1;
1063 }
1064 }
1065 srv_send((char *)"440 No such simulator");
1066 return 1;
1067 }
1068
1069 if (strcmp(opt, (char *)"PUT") == 0) {
1070 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1071 if (strcmp(simulator->uuid, param) == 0) {
1072 while (1) {
1073 memset((char *)&ibuf, 0, SS_BUFSIZE);
1074 fromlen = sizeof(peeraddr_in);
1075 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
1076 if (rlen == -1) {
1077 syslog(LOG_WARNING, "recvfrom(): %s", strerror(errno));
1078 srv_send((char *)"518 recfrom(): %s", strerror(errno));
1079 return 1;
1080 }
1081 for (i = 0; i < strlen(ibuf); i++) {
1082 if (ibuf[i] == '\n')
1083 ibuf[i] = '\0';
1084 if (ibuf[i] == '\r')
1085 ibuf[i] = '\0';
1086 }
1087 for (i = strlen(ibuf) -1; i > 0; i--) {
1088 if (ibuf[i] == ' ')
1089 ibuf[i] = '\0';
1090 else
1091 break;
1092 }
1093 if (strlen(ibuf)) {
1094 if (debug) {
1095 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
1096 fprintf(stdout, "recv: \"%s\"\n", ibuf);
1097 }
1098 if (strcmp(ibuf, (char *)".") == 0) {
1099 srv_send((char *)"219 Accepted Simulator record");
1100 return 0;
1101 }
1102 kwd = strtok(ibuf, ",\0");
1103 val = strtok(NULL, "\0");
1104 if (kwd && val) {
1105
1106 if (strcmp(kwd, (char *)"NAME") == 0) {
1107 if (simulator->name)
1108 free(simulator->name);
1109 simulator->name = xstrcpy(val);
1110
1111 } else if (strcmp(kwd, (char *)"VOLUME_AIR") == 0) {
1112 if (sscanf(val, "%d", &ival) == 1)
1113 simulator->volume_air = ival;
1114
1115 } else if (strcmp(kwd, (char *)"VOLUME_BEER") == 0) {
1116 if (sscanf(val, "%d", &ival) == 1)
1117 simulator->volume_beer = ival;
1118
1119 } else if (strcmp(kwd, (char *)"ROOM_TEMPERATURE") == 0) {
1120 if (sscanf(val, "%f", &fval) == 1)
1121 simulator->room_temperature = fval;
1122
1123 } else if (strcmp(kwd, (char *)"AIR_TEMPERATURE") == 0) {
1124 if (sscanf(val, "%f", &fval) == 1)
1125 simulator->air_temperature = fval;
1126
1127 } else if (strcmp(kwd, (char *)"BEER_TEMPERATURE") == 0) {
1128 if (sscanf(val, "%f", &fval) == 1)
1129 simulator->beer_temperature = fval;
1130
1131 } else if (strcmp(kwd, (char *)"COOLER_TEMP") == 0) {
1132 if (sscanf(val, "%f", &fval) == 1)
1133 simulator->cooler_temp = fval;
1134
1135 } else if (strcmp(kwd, (char *)"COOLER_TIME") == 0) {
1136 if (sscanf(val, "%d", &ival) == 1)
1137 simulator->cooler_time = ival;
1138
1139 } else if (strcmp(kwd, (char *)"COOLER_SIZE") == 0) {
1140 if (sscanf(val, "%f", &fval) == 1)
1141 simulator->cooler_size = fval;
1142
1143 } else if (strcmp(kwd, (char *)"HEATER_TEMP") == 0) {
1144 if (sscanf(val, "%f", &fval) == 1)
1145 simulator->heater_temp = fval;
1146
1147 } else if (strcmp(kwd, (char *)"HEATER_TIME") == 0) {
1148 if (sscanf(val, "%d", &ival) == 1)
1149 simulator->heater_time = ival;
1150
1151 } else if (strcmp(kwd, (char *)"HEATER_SIZE") == 0) {
1152 if (sscanf(val, "%f", &fval) == 1)
1153 simulator->heater_size = fval;
1154
1155 } else if (strcmp(kwd, (char *)"HEATER_STATE") == 0) {
1156 if (sscanf(val, "%d", &ival) == 1)
1157 simulator->heater_state = ival;
1158
1159 } else if (strcmp(kwd, (char *)"COOLER_STATE") == 0) {
1160 if (sscanf(val, "%d", &ival) == 1)
1161 simulator->cooler_state = ival;
1162
1163 } else if (strcmp(kwd, (char *)"FRIGO_ISOLATION") == 0) {
1164 if (sscanf(val, "%f", &fval) == 1)
1165 simulator->frigo_isolation = fval;
1166
1167 }
1168 }
1169 }
1170 }
1171 }
1172 }
1173 srv_send((char *)"440 No such simulator");
1174 return 1;
1175 }
1176
1177 srv_send((char *)"504 Subcommand error");
1178 return 1;
1179 }
1180 #endif
905 1181
906 1182
907 1183
908 int delete_Unit(char *uuid) 1184 int delete_Unit(char *uuid)
909 { 1185 {
1379 } 1655 }
1380 srv_send((char *)"440 No such unit"); 1656 srv_send((char *)"440 No such unit");
1381 return 1; 1657 return 1;
1382 } 1658 }
1383 1659
1384
1385 srv_send((char *)"504 Subcommand error"); 1660 srv_send((char *)"504 Subcommand error");
1386 return 1; 1661 return 1;
1387 } 1662 }
1388 1663
1389 1664
1440 srv_send((char *)"GLOBAL GET Get global settings"); 1715 srv_send((char *)"GLOBAL GET Get global settings");
1441 srv_send((char *)"GLOBAL PUT Put global settings"); 1716 srv_send((char *)"GLOBAL PUT Put global settings");
1442 srv_send((char *)"LIST List all fermenter units"); 1717 srv_send((char *)"LIST List all fermenter units");
1443 srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines"); 1718 srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines");
1444 srv_send((char *)"PROFILE uuid,name Profile rename"); 1719 srv_send((char *)"PROFILE uuid,name Profile rename");
1445 srv_send((char *)"PROFILE ADD name Add new profile with name"); 1720 srv_send((char *)"PROFILE ADD name Add new Profile with name");
1446 srv_send((char *)"PROFILE DEL uuid Delete profile by uuid"); 1721 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid");
1447 srv_send((char *)"PROFILE LIST List available profiles"); 1722 srv_send((char *)"PROFILE LIST List available profiles");
1448 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid"); 1723 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid");
1449 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid"); 1724 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid");
1450 srv_send((char *)"PROFILE GETS uuid Profile get steps list"); 1725 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
1451 srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); 1726 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
1452 srv_send((char *)"UNIT ADD name Add a new unit with name"); 1727 #ifdef USE_SIMULATOR
1728 srv_send((char *)"SIMULATOR ADD name Add a new Simulator with name");
1729 srv_send((char *)"SIMULATOR DEL uuid Delete Simulator by uuid");
1730 srv_send((char *)"SIMULATOR LIST List all Simulators");
1731 srv_send((char *)"SIMULATOR GET uuid Get Simulator record by uuid");
1732 srv_send((char *)"SIMULATOR PUT uuid Put Simulator record by uuid");
1733 #endif
1734 srv_send((char *)"UNIT ADD name Add a new Unit with name");
1453 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid"); 1735 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid");
1454 srv_send((char *)"UNIT LIST List all Units"); 1736 srv_send((char *)"UNIT LIST List all Units");
1455 srv_send((char *)"UNIT GET uuid Get Unit record by uuid"); 1737 srv_send((char *)"UNIT GET uuid Get Unit record by uuid");
1456 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid"); 1738 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid");
1457 srv_send((char *)"."); 1739 srv_send((char *)".");
1458 } else if (strncmp(buf, "LIST", 4) == 0) { 1740 } else if (strncmp(buf, "LIST", 4) == 0) {
1459 cmd_list(buf); 1741 cmd_list(buf);
1460 } else if (strncmp(buf, "PROFILE", 7) == 0) { 1742 } else if (strncmp(buf, "PROFILE", 7) == 0) {
1461 if (cmd_profile(buf) == 0) 1743 if (cmd_profile(buf) == 0)
1462 wrconfig(); 1744 wrconfig();
1745 #ifdef USE_SIMULATOR
1746 } else if (strncmp(buf, "SIMULATOR", 9) == 0) {
1747 if (cmd_simulator(buf) == 0)
1748 wrconfig();
1749 #endif
1463 } else if (strncmp(buf, "UNIT", 4) == 0) { 1750 } else if (strncmp(buf, "UNIT", 4) == 0) {
1464 if (cmd_unit(buf) == 0) 1751 if (cmd_unit(buf) == 0)
1465 wrconfig(); 1752 wrconfig();
1466 } else { 1753 } else {
1467 srv_send((char *)"500 Unknown command"); 1754 srv_send((char *)"500 Unknown command");

mercurial