thermferm/server.c

changeset 716
5c30c8ef83a8
parent 715
f5d85af156ab
child 717
22dd7ab614e5
--- a/thermferm/server.c	Wed May 01 14:38:37 2024 +0200
+++ b/thermferm/server.c	Thu May 02 15:49:16 2024 +0200
@@ -958,6 +958,9 @@
 
 
 #ifdef USE_SIMULATOR
+
+extern int	my_simulator_command, my_simulator_state;
+
 int delete_Simulator(char *uuid)
 {
     simulator_list	*current = Config.simulators;
@@ -968,16 +971,36 @@
 	    if (previous == NULL) {
 		Config.simulators = current->next;
 	    	free(current->uuid);
-	    	current->uuid = NULL;
+//	    	current->uuid = NULL;
 	    	free(current->name);
-	    	current->name = NULL;
+//	    	current->name = NULL;
+		free(current->air_address);
+		free(current->beer_address);
+		free(current->beer_address2);
+		free(current->chiller_address);
+		free(current->cooler_address);
+		free(current->heater_address);
+		free(current->fan_address);
+		free(current->light_address);
+		free(current->door_address);
+		free(current->psu_address);
 	    	free(current);
 	    	return 1;
 	    } else {
 		free(current->uuid);
-		current->uuid = NULL;
+//		current->uuid = NULL;
 		free(current->name);
-		current->name = NULL;
+//		current->name = NULL;
+		free(current->air_address);
+                free(current->beer_address);
+                free(current->beer_address2);
+                free(current->chiller_address);
+                free(current->cooler_address);
+                free(current->heater_address);
+                free(current->fan_address);
+                free(current->light_address);
+                free(current->door_address);
+                free(current->psu_address);
 		previous->next = current->next;
 		free(current);
 		current = previous->next;
@@ -1003,7 +1026,7 @@
 int cmd_simulator(int s, char *buf)
 {
     char		*opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
-    simulator_list	*simulator, *tmps;
+    simulator_list	*simulator, *nsim;
     int			rc, rlen, ival, i;
     float		fval;
     uuid_t		uu;
@@ -1077,9 +1100,9 @@
 	 * For now, only one simulator is allowed.
 	 */
 	if (Config.simulators) {
-	    for (tmps = Config.simulators; tmps; tmps = tmps->next) {
-		if (tmps->simno > highno)
-		    highno = tmps->simno;
+	    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+		if (simulator->simno > highno)
+		    highno = simulator->simno;
 		count++;
 	    }
 	}
@@ -1088,73 +1111,99 @@
 	    return 0;
 	}
 
-	simulator = (simulator_list *)malloc(sizeof(simulator_list));
-	simulator->next = NULL;
-	simulator->uuid = malloc(37);
+	my_simulator_command = THREAD_PAUSE;
+        while (my_simulator_state != THREAD_PAUSE) { mDelay(50); };
+	syslog(LOG_NOTICE, "SIMULATOR ADD thread paused");
+
+	nsim = (simulator_list *)malloc(sizeof(simulator_list));
+	memset(nsim, 0, sizeof(simulator_list));
+	nsim->next = NULL;
+	nsim->uuid = malloc(37);
 	uuid_generate(uu);
-	uuid_unparse(uu, simulator->uuid);
-	simulator->name = xstrcpy(param);
-	simulator->simno = highno + 1;
-	sprintf(abuf, "%d-", simulator->simno);
-	simulator->volume_air = 150;
-	simulator->volume_beer = 50;
-	simulator->room_tempaddress = xstrcpy(abuf);
-	simulator->room_tempaddress = xstrcat(simulator->room_tempaddress, (char *)"SimRoomTemp");
-	simulator->room_temperature = simulator->air_temperature = simulator->beer_temperature = simulator->beer_temperature2 = 20.0;
-	simulator->s_cool_temp = simulator->s_heat_temp = 20.0;
-	simulator->room_humaddress = xstrcpy(abuf);
-        simulator->room_humaddress = xstrcat(simulator->room_humaddress, (char *)"SimRoomHum");
-	simulator->room_humidity = 48.6;
-	simulator->air_address = xstrcpy(abuf);
-	simulator->air_address = xstrcat(simulator->air_address, (char *)"SimAirTemp");
-	simulator->beer_address = xstrcpy(abuf);
-        simulator->beer_address = xstrcat(simulator->beer_address, (char *)"SimBeerTemp");
-	simulator->beer_address2 = xstrcpy(abuf);
-        simulator->beer_address2 = xstrcat(simulator->beer_address2, (char *)"SimBeerTemp2");
-	simulator->chiller_address = xstrcpy(abuf);
-        simulator->chiller_address = xstrcat(simulator->chiller_address, (char *)"SimChillerTemp");
-	simulator->heater_address = xstrcpy(abuf);
-        simulator->heater_address = xstrcat(simulator->heater_address, (char *)"SimHeater");
-	simulator->cooler_address = xstrcpy(abuf);
-        simulator->cooler_address = xstrcat(simulator->cooler_address, (char *)"SimCooler");
-	simulator->fan_address = xstrcpy(abuf);
-        simulator->fan_address = xstrcat(simulator->fan_address, (char *)"SimFan");
-	simulator->light_address = xstrcpy(abuf);
-        simulator->light_address = xstrcat(simulator->light_address, (char *)"SimLight");
-	simulator->chiller_temperature = 1.5;	/* Chiller temperature */
-	simulator->cooler_temp =  1.5;	/* Cooling temperature */
-	simulator->cooler_time = 720;	/* About 12 minutes for the cooler plate */
-	simulator->cooler_size = 0.8;	/* 0.8 square meter cooler plate */
-	simulator->heater_temp = 150.0;	/* Heating temperature */
-	simulator->heater_time = 3;	/* 3 seconds to heat-up	*/
-	simulator->heater_size = 0.01;	/* 0.01 square meter heater plate */
-	simulator->air_present = simulator->beer_present = DEVPRESENT_YES;
-	simulator->beer_present2 = simulator->chiller_present = simulator->cooler_present = simulator->heater_present = DEVPRESENT_UNDEF;
-	simulator->frigo_isolation = 0.002;
-	simulator->timestamp = time(NULL);
-	simulator->s_yeast_heat = 0.0;
-	simulator->s_yeast_started = simulator->s_cool_changed = simulator->s_heat_changed = (int)0;
+	uuid_unparse(uu, nsim->uuid);
+	nsim->name = xstrcpy(param);
+	nsim->simno = highno + 1;
+	nsim->volume_air = 150;
+	nsim->volume_beer = 50;
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimRoomTemp");
+	nsim->room_tempaddress = xstrcpy(abuf);
+	nsim->room_temperature = nsim->air_temperature = nsim->beer_temperature = nsim->beer_temperature2 = 20.0;
+	nsim->s_cool_temp = nsim->s_heat_temp = 20.0;
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimRoomHum");
+	nsim->room_humaddress = xstrcpy(abuf);
+	nsim->room_humidity = 48.6;
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimAirTemp");
+	nsim->air_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimBeerTemp");
+	nsim->beer_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimBeerTemp2");
+	nsim->beer_address2 = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimChillerTemp");
+	nsim->chiller_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimHeater");
+	nsim->heater_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimCooler");
+	nsim->cooler_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimFan");
+	nsim->fan_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimLight");
+	nsim->light_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimDoor");
+	nsim->door_address = xstrcpy(abuf);
+	snprintf(abuf, 63, "%d-%s", nsim->simno, (char *)"SimPSU");
+	nsim->psu_address = xstrcpy(abuf);
+	nsim->chiller_temperature = 1.5;	/* Chiller temperature */
+	nsim->cooler_temp =  1.5;		/* Cooling temperature */
+	nsim->cooler_time = 720;		/* About 12 minutes for the cooler plate */
+	nsim->cooler_size = 0.8;		/* 0.8 square meter cooler plate */
+	nsim->heater_temp = 150.0;		/* Heating temperature */
+	nsim->heater_time = 3;			/* 3 seconds to heat-up	*/
+	nsim->heater_size = 0.01;		/* 0.01 square meter heater plate */
+	nsim->door_value = nsim->psu_value = 0;
+	nsim->air_present = nsim->beer_present = DEVPRESENT_YES;
+	nsim->beer_present2 = nsim->chiller_present = nsim->cooler_present = nsim->heater_present = DEVPRESENT_UNDEF;
+	nsim->door_present = nsim->psu_present = DEVPRESENT_UNDEF;
+	nsim->frigo_isolation = 0.002;
+	nsim->timestamp = time(NULL);
+	nsim->s_yeast_heat = 0.0;
+	nsim->s_yeast_started = nsim->s_cool_changed = nsim->s_heat_changed = (int)0;
 
 	if (Config.simulators == NULL) {
-	    Config.simulators = simulator;
+		syslog(LOG_NOTICE, "SIMULATOR ADD root");
+	    Config.simulators = nsim;
 	} else {
-	    for (tmps = Config.simulators; tmps; tmps = tmps->next) {
-		if (tmps->next == NULL) {
-		    tmps->next = simulator;
+	    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+		    syslog(LOG_NOTICE, "SIMULATOR ADD no %d %s", simulator->simno, simulator->name);
+		if (simulator->next == NULL) {
+		    simulator->next = nsim;
+		    syslog(LOG_NOTICE, "SIMULATOR ADD here");
 		    break;
 		}
 	    }
 	}
 
-	syslog(LOG_NOTICE, "Simulator %s no %d added", simulator->uuid, simulator->simno);
-	srv_send(s, (char *)"211 Simulator %s added", simulator->uuid);
+	my_simulator_command = THREAD_RUN;
+        while (my_simulator_state != THREAD_RUN) { mDelay(50); };
+        syslog(LOG_NOTICE, "SIMULATOR ADD thread runs");
+
+	syslog(LOG_NOTICE, "Simulator %s no %d added", param, highno + 1);
+	srv_send(s, (char *)"211 Simulator %s added", param);
 	return 1;
     }
 
     if (strcmp(opt, (char *)"DEL") == 0) {
 	    // TODO: check devices in use.
 	    // TODO: delete simulated devices.
+	syslog(LOG_NOTICE, "Simulator DEL %s", param);
+
+	my_simulator_command = THREAD_PAUSE;
+        while (my_simulator_state != THREAD_PAUSE) { mDelay(50); };
+        syslog(LOG_NOTICE, "SIMULATOR DEL thread paused");
 	rc = delete_Simulator(param);
+	my_simulator_command = THREAD_RUN;
+        while (my_simulator_state != THREAD_RUN) { mDelay(50); };
+        syslog(LOG_NOTICE, "SIMULATOR DEL thread runs");
+
 	if (rc) {
 	    syslog(LOG_NOTICE, "Simulator %s deleted", param);
 	    srv_send(s, (char *)"211 Simulator %s deleted", param);
@@ -1207,6 +1256,12 @@
 		srv_send(s, (char *)"LIGHT_ADDRESS,%s", simulator->light_address);
 		srv_send(s, (char *)"LIGHT_PRESENT,%s", DEVPRESENT[simulator->light_present]);
 		srv_send(s, (char *)"LIGHT_POWER,%d", simulator->light_power);
+		srv_send(s, (char *)"DOOR_ADDRESS,%s", simulator->door_address);
+                srv_send(s, (char *)"DOOR_PRESENT,%s", DEVPRESENT[simulator->door_present]);
+                srv_send(s, (char *)"DOOR_VALUE,%d", simulator->door_value);
+		srv_send(s, (char *)"PSU_ADDRESS,%s", simulator->psu_address);
+                srv_send(s, (char *)"PSU_PRESENT,%s", DEVPRESENT[simulator->psu_present]);
+                srv_send(s, (char *)"PSU_VALUE,%d", simulator->psu_value);
 		srv_send(s, (char *)"FRIGO_ISOLATION,%.3f", simulator->frigo_isolation);
 		srv_send(s, (char *)"TIMESTAMP,%ld", (long)simulator->timestamp);
 		srv_send(s, (char *)".");
@@ -1457,6 +1512,40 @@
                                     simulator->light_power = ival;
                                 }
 
+			    } else if (strcmp(kwd, (char *)"DOOR_PRESENT") == 0) {
+                                for (i = 0; i < 4; i++) {
+                                    if (strcmp(val, DEVPRESENT[i]) == 0) {
+                                        if (simulator->door_present != i)
+                                            syslog(LOG_NOTICE, "Simulator %s door_present %s to %s", simulator->uuid, DEVPRESENT[simulator->door_present], DEVPRESENT[i]);
+                                        simulator->door_present = i;
+                                        device_present(simulator->door_address, i);
+                                        break;
+                                    }
+                                }
+
+			    } else if (strcmp(kwd, (char *)"DOOR_VALUE") == 0) {
+				ival = (strcmp(val, (char *)"true")) ? 0:1;
+                                if (simulator->door_value != ival)
+                            	    syslog(LOG_NOTICE, "Simulator %s door value %d to %d", simulator->uuid, simulator->door_value, ival);
+                                simulator->door_value = ival;
+
+			    } else if (strcmp(kwd, (char *)"PSU_PRESENT") == 0) {
+                                for (i = 0; i < 4; i++) {
+                                    if (strcmp(val, DEVPRESENT[i]) == 0) {
+                                        if (simulator->psu_present != i)
+                                            syslog(LOG_NOTICE, "Simulator %s psu_present %s to %s", simulator->uuid, DEVPRESENT[simulator->psu_present], DEVPRESENT[i]);
+                                        simulator->psu_present = i;
+                                        device_present(simulator->psu_address, i);
+                                        break;
+                                    }
+                                }
+
+			    } else if (strcmp(kwd, (char *)"PSU_VALUE") == 0) {
+				ival = (strcmp(val, (char *)"true")) ? 0:1;
+                                if (simulator->psu_value != ival)
+                                    syslog(LOG_NOTICE, "Simulator %s psu value %d to %d", simulator->uuid, simulator->psu_value, ival);
+                                simulator->psu_value = ival;
+
 			    } else if (strcmp(kwd, (char *)"FRIGO_ISOLATION") == 0) {
 				if (sscanf(val, "%f", &fval) == 1) {
 				    if (simulator->frigo_isolation != fval)
@@ -2264,7 +2353,8 @@
 					if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) {
 					    unit->mqtt_flag |= MQTT_FLAG_BIRTH;
 					}
-					syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
+					if (unit->mode != i)
+					    syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
 					unit->mode = i;
 					/* Allways turn everything off after a mode change */
 					unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
@@ -2586,12 +2676,12 @@
 	return 0;
     }
 
-//    if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
-//	syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
-//	close(ls);
-//	my_server_state = 0;
-//	return 0;
-//    }
+    if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
+	syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
+	close(ls);
+	my_server_state = 0;
+	return 0;
+    }
 
     if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
 	syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno));

mercurial