thermferm/server.c

changeset 716
5c30c8ef83a8
parent 715
f5d85af156ab
child 717
22dd7ab614e5
equal deleted inserted replaced
715:f5d85af156ab 716:5c30c8ef83a8
956 } 956 }
957 957
958 958
959 959
960 #ifdef USE_SIMULATOR 960 #ifdef USE_SIMULATOR
961
962 extern int my_simulator_command, my_simulator_state;
963
961 int delete_Simulator(char *uuid) 964 int delete_Simulator(char *uuid)
962 { 965 {
963 simulator_list *current = Config.simulators; 966 simulator_list *current = Config.simulators;
964 simulator_list *previous = NULL; 967 simulator_list *previous = NULL;
965 968
966 while (current) { 969 while (current) {
967 if (strcmp(current->uuid, uuid) == 0) { 970 if (strcmp(current->uuid, uuid) == 0) {
968 if (previous == NULL) { 971 if (previous == NULL) {
969 Config.simulators = current->next; 972 Config.simulators = current->next;
970 free(current->uuid); 973 free(current->uuid);
971 current->uuid = NULL; 974 // current->uuid = NULL;
972 free(current->name); 975 free(current->name);
973 current->name = NULL; 976 // current->name = NULL;
977 free(current->air_address);
978 free(current->beer_address);
979 free(current->beer_address2);
980 free(current->chiller_address);
981 free(current->cooler_address);
982 free(current->heater_address);
983 free(current->fan_address);
984 free(current->light_address);
985 free(current->door_address);
986 free(current->psu_address);
974 free(current); 987 free(current);
975 return 1; 988 return 1;
976 } else { 989 } else {
977 free(current->uuid); 990 free(current->uuid);
978 current->uuid = NULL; 991 // current->uuid = NULL;
979 free(current->name); 992 free(current->name);
980 current->name = NULL; 993 // current->name = NULL;
994 free(current->air_address);
995 free(current->beer_address);
996 free(current->beer_address2);
997 free(current->chiller_address);
998 free(current->cooler_address);
999 free(current->heater_address);
1000 free(current->fan_address);
1001 free(current->light_address);
1002 free(current->door_address);
1003 free(current->psu_address);
981 previous->next = current->next; 1004 previous->next = current->next;
982 free(current); 1005 free(current);
983 current = previous->next; 1006 current = previous->next;
984 return 1; 1007 return 1;
985 } 1008 }
1001 * SIMULATOR PUT uuid 1024 * SIMULATOR PUT uuid
1002 */ 1025 */
1003 int cmd_simulator(int s, char *buf) 1026 int cmd_simulator(int s, char *buf)
1004 { 1027 {
1005 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 1028 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
1006 simulator_list *simulator, *tmps; 1029 simulator_list *simulator, *nsim;
1007 int rc, rlen, ival, i; 1030 int rc, rlen, ival, i;
1008 float fval; 1031 float fval;
1009 uuid_t uu; 1032 uuid_t uu;
1010 1033
1011 opt = strtok(buf, " \0"); 1034 opt = strtok(buf, " \0");
1075 1098
1076 /* 1099 /*
1077 * For now, only one simulator is allowed. 1100 * For now, only one simulator is allowed.
1078 */ 1101 */
1079 if (Config.simulators) { 1102 if (Config.simulators) {
1080 for (tmps = Config.simulators; tmps; tmps = tmps->next) { 1103 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1081 if (tmps->simno > highno) 1104 if (simulator->simno > highno)
1082 highno = tmps->simno; 1105 highno = simulator->simno;
1083 count++; 1106 count++;
1084 } 1107 }
1085 } 1108 }
1086 if (count >= 5) { 1109 if (count >= 5) {
1087 srv_send(s, (char *)"441 Maximum simulators reached"); 1110 srv_send(s, (char *)"441 Maximum simulators reached");
1088 return 0; 1111 return 0;
1089 } 1112 }
1090 1113
1091 simulator = (simulator_list *)malloc(sizeof(simulator_list)); 1114 my_simulator_command = THREAD_PAUSE;
1092 simulator->next = NULL; 1115 while (my_simulator_state != THREAD_PAUSE) { mDelay(50); };
1093 simulator->uuid = malloc(37); 1116 syslog(LOG_NOTICE, "SIMULATOR ADD thread paused");
1117
1118 nsim = (simulator_list *)malloc(sizeof(simulator_list));
1119 memset(nsim, 0, sizeof(simulator_list));
1120 nsim->next = NULL;
1121 nsim->uuid = malloc(37);
1094 uuid_generate(uu); 1122 uuid_generate(uu);
1095 uuid_unparse(uu, simulator->uuid); 1123 uuid_unparse(uu, nsim->uuid);
1096 simulator->name = xstrcpy(param); 1124 nsim->name = xstrcpy(param);
1097 simulator->simno = highno + 1; 1125 nsim->simno = highno + 1;
1098 sprintf(abuf, "%d-", simulator->simno); 1126 nsim->volume_air = 150;
1099 simulator->volume_air = 150; 1127 nsim->volume_beer = 50;
1100 simulator->volume_beer = 50; 1128 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimRoomTemp");
1101 simulator->room_tempaddress = xstrcpy(abuf); 1129 nsim->room_tempaddress = xstrcpy(abuf);
1102 simulator->room_tempaddress = xstrcat(simulator->room_tempaddress, (char *)"SimRoomTemp"); 1130 nsim->room_temperature = nsim->air_temperature = nsim->beer_temperature = nsim->beer_temperature2 = 20.0;
1103 simulator->room_temperature = simulator->air_temperature = simulator->beer_temperature = simulator->beer_temperature2 = 20.0; 1131 nsim->s_cool_temp = nsim->s_heat_temp = 20.0;
1104 simulator->s_cool_temp = simulator->s_heat_temp = 20.0; 1132 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimRoomHum");
1105 simulator->room_humaddress = xstrcpy(abuf); 1133 nsim->room_humaddress = xstrcpy(abuf);
1106 simulator->room_humaddress = xstrcat(simulator->room_humaddress, (char *)"SimRoomHum"); 1134 nsim->room_humidity = 48.6;
1107 simulator->room_humidity = 48.6; 1135 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimAirTemp");
1108 simulator->air_address = xstrcpy(abuf); 1136 nsim->air_address = xstrcpy(abuf);
1109 simulator->air_address = xstrcat(simulator->air_address, (char *)"SimAirTemp"); 1137 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimBeerTemp");
1110 simulator->beer_address = xstrcpy(abuf); 1138 nsim->beer_address = xstrcpy(abuf);
1111 simulator->beer_address = xstrcat(simulator->beer_address, (char *)"SimBeerTemp"); 1139 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimBeerTemp2");
1112 simulator->beer_address2 = xstrcpy(abuf); 1140 nsim->beer_address2 = xstrcpy(abuf);
1113 simulator->beer_address2 = xstrcat(simulator->beer_address2, (char *)"SimBeerTemp2"); 1141 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimChillerTemp");
1114 simulator->chiller_address = xstrcpy(abuf); 1142 nsim->chiller_address = xstrcpy(abuf);
1115 simulator->chiller_address = xstrcat(simulator->chiller_address, (char *)"SimChillerTemp"); 1143 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimHeater");
1116 simulator->heater_address = xstrcpy(abuf); 1144 nsim->heater_address = xstrcpy(abuf);
1117 simulator->heater_address = xstrcat(simulator->heater_address, (char *)"SimHeater"); 1145 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimCooler");
1118 simulator->cooler_address = xstrcpy(abuf); 1146 nsim->cooler_address = xstrcpy(abuf);
1119 simulator->cooler_address = xstrcat(simulator->cooler_address, (char *)"SimCooler"); 1147 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimFan");
1120 simulator->fan_address = xstrcpy(abuf); 1148 nsim->fan_address = xstrcpy(abuf);
1121 simulator->fan_address = xstrcat(simulator->fan_address, (char *)"SimFan"); 1149 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimLight");
1122 simulator->light_address = xstrcpy(abuf); 1150 nsim->light_address = xstrcpy(abuf);
1123 simulator->light_address = xstrcat(simulator->light_address, (char *)"SimLight"); 1151 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimDoor");
1124 simulator->chiller_temperature = 1.5; /* Chiller temperature */ 1152 nsim->door_address = xstrcpy(abuf);
1125 simulator->cooler_temp = 1.5; /* Cooling temperature */ 1153 snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimPSU");
1126 simulator->cooler_time = 720; /* About 12 minutes for the cooler plate */ 1154 nsim->psu_address = xstrcpy(abuf);
1127 simulator->cooler_size = 0.8; /* 0.8 square meter cooler plate */ 1155 nsim->chiller_temperature = 1.5; /* Chiller temperature */
1128 simulator->heater_temp = 150.0; /* Heating temperature */ 1156 nsim->cooler_temp = 1.5; /* Cooling temperature */
1129 simulator->heater_time = 3; /* 3 seconds to heat-up */ 1157 nsim->cooler_time = 720; /* About 12 minutes for the cooler plate */
1130 simulator->heater_size = 0.01; /* 0.01 square meter heater plate */ 1158 nsim->cooler_size = 0.8; /* 0.8 square meter cooler plate */
1131 simulator->air_present = simulator->beer_present = DEVPRESENT_YES; 1159 nsim->heater_temp = 150.0; /* Heating temperature */
1132 simulator->beer_present2 = simulator->chiller_present = simulator->cooler_present = simulator->heater_present = DEVPRESENT_UNDEF; 1160 nsim->heater_time = 3; /* 3 seconds to heat-up */
1133 simulator->frigo_isolation = 0.002; 1161 nsim->heater_size = 0.01; /* 0.01 square meter heater plate */
1134 simulator->timestamp = time(NULL); 1162 nsim->door_value = nsim->psu_value = 0;
1135 simulator->s_yeast_heat = 0.0; 1163 nsim->air_present = nsim->beer_present = DEVPRESENT_YES;
1136 simulator->s_yeast_started = simulator->s_cool_changed = simulator->s_heat_changed = (int)0; 1164 nsim->beer_present2 = nsim->chiller_present = nsim->cooler_present = nsim->heater_present = DEVPRESENT_UNDEF;
1165 nsim->door_present = nsim->psu_present = DEVPRESENT_UNDEF;
1166 nsim->frigo_isolation = 0.002;
1167 nsim->timestamp = time(NULL);
1168 nsim->s_yeast_heat = 0.0;
1169 nsim->s_yeast_started = nsim->s_cool_changed = nsim->s_heat_changed = (int)0;
1137 1170
1138 if (Config.simulators == NULL) { 1171 if (Config.simulators == NULL) {
1139 Config.simulators = simulator; 1172 syslog(LOG_NOTICE, "SIMULATOR ADD root");
1173 Config.simulators = nsim;
1140 } else { 1174 } else {
1141 for (tmps = Config.simulators; tmps; tmps = tmps->next) { 1175 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1142 if (tmps->next == NULL) { 1176 syslog(LOG_NOTICE, "SIMULATOR ADD no %d %s", simulator->simno, simulator->name);
1143 tmps->next = simulator; 1177 if (simulator->next == NULL) {
1178 simulator->next = nsim;
1179 syslog(LOG_NOTICE, "SIMULATOR ADD here");
1144 break; 1180 break;
1145 } 1181 }
1146 } 1182 }
1147 } 1183 }
1148 1184
1149 syslog(LOG_NOTICE, "Simulator %s no %d added", simulator->uuid, simulator->simno); 1185 my_simulator_command = THREAD_RUN;
1150 srv_send(s, (char *)"211 Simulator %s added", simulator->uuid); 1186 while (my_simulator_state != THREAD_RUN) { mDelay(50); };
1187 syslog(LOG_NOTICE, "SIMULATOR ADD thread runs");
1188
1189 syslog(LOG_NOTICE, "Simulator %s no %d added", param, highno + 1);
1190 srv_send(s, (char *)"211 Simulator %s added", param);
1151 return 1; 1191 return 1;
1152 } 1192 }
1153 1193
1154 if (strcmp(opt, (char *)"DEL") == 0) { 1194 if (strcmp(opt, (char *)"DEL") == 0) {
1155 // TODO: check devices in use. 1195 // TODO: check devices in use.
1156 // TODO: delete simulated devices. 1196 // TODO: delete simulated devices.
1197 syslog(LOG_NOTICE, "Simulator DEL %s", param);
1198
1199 my_simulator_command = THREAD_PAUSE;
1200 while (my_simulator_state != THREAD_PAUSE) { mDelay(50); };
1201 syslog(LOG_NOTICE, "SIMULATOR DEL thread paused");
1157 rc = delete_Simulator(param); 1202 rc = delete_Simulator(param);
1203 my_simulator_command = THREAD_RUN;
1204 while (my_simulator_state != THREAD_RUN) { mDelay(50); };
1205 syslog(LOG_NOTICE, "SIMULATOR DEL thread runs");
1206
1158 if (rc) { 1207 if (rc) {
1159 syslog(LOG_NOTICE, "Simulator %s deleted", param); 1208 syslog(LOG_NOTICE, "Simulator %s deleted", param);
1160 srv_send(s, (char *)"211 Simulator %s deleted", param); 1209 srv_send(s, (char *)"211 Simulator %s deleted", param);
1161 return 1; 1210 return 1;
1162 } else { 1211 } else {
1205 srv_send(s, (char *)"FAN_PRESENT,%s", DEVPRESENT[simulator->fan_present]); 1254 srv_send(s, (char *)"FAN_PRESENT,%s", DEVPRESENT[simulator->fan_present]);
1206 srv_send(s, (char *)"FAN_POWER,%d", simulator->fan_power); 1255 srv_send(s, (char *)"FAN_POWER,%d", simulator->fan_power);
1207 srv_send(s, (char *)"LIGHT_ADDRESS,%s", simulator->light_address); 1256 srv_send(s, (char *)"LIGHT_ADDRESS,%s", simulator->light_address);
1208 srv_send(s, (char *)"LIGHT_PRESENT,%s", DEVPRESENT[simulator->light_present]); 1257 srv_send(s, (char *)"LIGHT_PRESENT,%s", DEVPRESENT[simulator->light_present]);
1209 srv_send(s, (char *)"LIGHT_POWER,%d", simulator->light_power); 1258 srv_send(s, (char *)"LIGHT_POWER,%d", simulator->light_power);
1259 srv_send(s, (char *)"DOOR_ADDRESS,%s", simulator->door_address);
1260 srv_send(s, (char *)"DOOR_PRESENT,%s", DEVPRESENT[simulator->door_present]);
1261 srv_send(s, (char *)"DOOR_VALUE,%d", simulator->door_value);
1262 srv_send(s, (char *)"PSU_ADDRESS,%s", simulator->psu_address);
1263 srv_send(s, (char *)"PSU_PRESENT,%s", DEVPRESENT[simulator->psu_present]);
1264 srv_send(s, (char *)"PSU_VALUE,%d", simulator->psu_value);
1210 srv_send(s, (char *)"FRIGO_ISOLATION,%.3f", simulator->frigo_isolation); 1265 srv_send(s, (char *)"FRIGO_ISOLATION,%.3f", simulator->frigo_isolation);
1211 srv_send(s, (char *)"TIMESTAMP,%ld", (long)simulator->timestamp); 1266 srv_send(s, (char *)"TIMESTAMP,%ld", (long)simulator->timestamp);
1212 srv_send(s, (char *)"."); 1267 srv_send(s, (char *)".");
1213 return 0; 1268 return 0;
1214 } 1269 }
1454 if (sscanf(val, "%d", &ival) == 1) { 1509 if (sscanf(val, "%d", &ival) == 1) {
1455 if (simulator->fan_power != ival) 1510 if (simulator->fan_power != ival)
1456 syslog(LOG_NOTICE, "Simulator %s light power %d to %d", simulator->uuid, simulator->light_power, ival); 1511 syslog(LOG_NOTICE, "Simulator %s light power %d to %d", simulator->uuid, simulator->light_power, ival);
1457 simulator->light_power = ival; 1512 simulator->light_power = ival;
1458 } 1513 }
1514
1515 } else if (strcmp(kwd, (char *)"DOOR_PRESENT") == 0) {
1516 for (i = 0; i < 4; i++) {
1517 if (strcmp(val, DEVPRESENT[i]) == 0) {
1518 if (simulator->door_present != i)
1519 syslog(LOG_NOTICE, "Simulator %s door_present %s to %s", simulator->uuid, DEVPRESENT[simulator->door_present], DEVPRESENT[i]);
1520 simulator->door_present = i;
1521 device_present(simulator->door_address, i);
1522 break;
1523 }
1524 }
1525
1526 } else if (strcmp(kwd, (char *)"DOOR_VALUE") == 0) {
1527 ival = (strcmp(val, (char *)"true")) ? 0:1;
1528 if (simulator->door_value != ival)
1529 syslog(LOG_NOTICE, "Simulator %s door value %d to %d", simulator->uuid, simulator->door_value, ival);
1530 simulator->door_value = ival;
1531
1532 } else if (strcmp(kwd, (char *)"PSU_PRESENT") == 0) {
1533 for (i = 0; i < 4; i++) {
1534 if (strcmp(val, DEVPRESENT[i]) == 0) {
1535 if (simulator->psu_present != i)
1536 syslog(LOG_NOTICE, "Simulator %s psu_present %s to %s", simulator->uuid, DEVPRESENT[simulator->psu_present], DEVPRESENT[i]);
1537 simulator->psu_present = i;
1538 device_present(simulator->psu_address, i);
1539 break;
1540 }
1541 }
1542
1543 } else if (strcmp(kwd, (char *)"PSU_VALUE") == 0) {
1544 ival = (strcmp(val, (char *)"true")) ? 0:1;
1545 if (simulator->psu_value != ival)
1546 syslog(LOG_NOTICE, "Simulator %s psu value %d to %d", simulator->uuid, simulator->psu_value, ival);
1547 simulator->psu_value = ival;
1459 1548
1460 } else if (strcmp(kwd, (char *)"FRIGO_ISOLATION") == 0) { 1549 } else if (strcmp(kwd, (char *)"FRIGO_ISOLATION") == 0) {
1461 if (sscanf(val, "%f", &fval) == 1) { 1550 if (sscanf(val, "%f", &fval) == 1) {
1462 if (simulator->frigo_isolation != fval) 1551 if (simulator->frigo_isolation != fval)
1463 syslog(LOG_NOTICE, "Simulator %s frigo isolation %.1f to %.1f", simulator->uuid, simulator->frigo_isolation, fval); 1552 syslog(LOG_NOTICE, "Simulator %s frigo isolation %.1f to %.1f", simulator->uuid, simulator->frigo_isolation, fval);
2262 unit->mqtt_flag |= MQTT_FLAG_DATA; 2351 unit->mqtt_flag |= MQTT_FLAG_DATA;
2263 /* Initialize log if the unit is turned on */ 2352 /* Initialize log if the unit is turned on */
2264 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) { 2353 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) {
2265 unit->mqtt_flag |= MQTT_FLAG_BIRTH; 2354 unit->mqtt_flag |= MQTT_FLAG_BIRTH;
2266 } 2355 }
2267 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); 2356 if (unit->mode != i)
2357 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
2268 unit->mode = i; 2358 unit->mode = i;
2269 /* Allways turn everything off after a mode change */ 2359 /* Allways turn everything off after a mode change */
2270 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; 2360 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
2271 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; 2361 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
2272 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = unit->light_timer = 0; 2362 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = unit->light_timer = 0;
2584 close(ls); 2674 close(ls);
2585 my_server_state = 0; 2675 my_server_state = 0;
2586 return 0; 2676 return 0;
2587 } 2677 }
2588 2678
2589 // if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { 2679 if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
2590 // syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); 2680 syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
2591 // close(ls); 2681 close(ls);
2592 // my_server_state = 0; 2682 my_server_state = 0;
2593 // return 0; 2683 return 0;
2594 // } 2684 }
2595 2685
2596 if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { 2686 if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
2597 syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); 2687 syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno));
2598 close(ls); 2688 close(ls);
2599 my_server_state = 0; 2689 my_server_state = 0;

mercurial