thermferm/devices.c

changeset 714
24749c296a50
parent 713
ea24b4ce02b1
child 715
f5d85af156ab
--- a/thermferm/devices.c	Sun Apr 28 15:50:42 2024 +0200
+++ b/thermferm/devices.c	Tue Apr 30 17:26:41 2024 +0200
@@ -250,6 +250,9 @@
     int			rc, i;
     char		buf[40];
 #endif
+#ifdef USE_SIMULATOR
+    simulator_list      *simulator;
+#endif
 
     if (uuid == NULL)
 	return 0;
@@ -314,22 +317,33 @@
 		}
 
 #ifdef USE_SIMULATOR
-		if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
-		    if ((strcmp((char *)"SimCooler", device->address) == 0) || (strcmp((char *)"SimHeater", device->address) == 0) ||
-			(strcmp((char *)"SimFan"   , device->address) == 0) || (strcmp((char *)"SimLight" , device->address) == 0)) {
-			if (value != device->value) {
-			    syslog(LOG_NOTICE, "SIM %s value=%d", device->address, value);
-			    device->value = value;
-			    device->timestamp = time(NULL);
-			    if (strcmp((char *)"SimCooler", device->address) == 0) 
-			    	SIMcooling = value;
-			    if (strcmp((char *)"SimHeater", device->address) == 0)
-			    	SIMheating = value;
-			    if (strcmp((char *)"SimFan", device->address) == 0)
-			    	SIMfan = value;
-			    if (strcmp((char *)"SimLight", device->address) == 0)
-			    	SIMlight = value;
-			    devices_ws();
+		if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN)) {
+		    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+			if ((strcmp(device->address, simulator->cooler_address) == 0) ||
+			    (strcmp(device->address, simulator->heater_address) == 0) ||
+			    (strcmp(device->address, simulator->fan_address) == 0) ||
+			    (strcmp(device->address, simulator->light_address) == 0)) {
+			    if (value != device->value) {
+				int rc = DEVPRESENT_UNDEF;
+			    	device->value = value;
+                            	device->timestamp = time(NULL);
+			    	if (strcmp(device->address, simulator->cooler_address) == 0) {
+                            	    simulator->cooler_power = value;
+				    rc = simulator->cooler_present;
+			    	} else if (strcmp(device->address, simulator->heater_address) == 0) {
+				    simulator->heater_power = value;
+				    rc = simulator->heater_present;
+			    	} else if (strcmp(device->address, simulator->fan_address) == 0) {
+				    simulator->fan_power = value;
+				    rc = simulator->fan_present;
+			    	} else if (strcmp(device->address, simulator->light_address) == 0) {
+				    simulator->light_power = value;
+				    rc = simulator->light_present;
+			    	}
+				syslog(LOG_NOTICE, "SIM %s value=%d, present=%s", device->address, value, DEVPRESENT[rc]);
+				device->present = rc;
+				devices_ws();
+			    }
 			}
 		    }
 		}
@@ -462,6 +476,9 @@
     int			pin;
     char		buf[40];
 #endif
+#ifdef USE_SIMULATOR
+    simulator_list	*simulator;
+#endif
 
     /*
      * Scan for 1-wire devices. These are already detected by the
@@ -686,87 +703,110 @@
 #endif
 
 #ifdef USE_SIMULATOR
-    found = 0;
-    for (device = Config.devices; device; device = device->next) {
-	if (device->type == DEVTYPE_SIM) {
-	    found++;
-	}
-    }
+
+    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	for (i = 0; i < 10; i++) {
+
+	    found = FALSE;
+            for (device = Config.devices; device; device = device->next) {
+            	if ((i == 0 && strcmp(device->address, simulator->room_tempaddress) == 0) ||
+		    (i == 1 && strcmp(device->address, simulator->air_address) == 0) ||
+		    (i == 2 && strcmp(device->address, simulator->beer_address) == 0) ||
+		    (i == 3 && strcmp(device->address, simulator->heater_address) == 0) ||
+		    (i == 4 && strcmp(device->address, simulator->cooler_address) == 0) ||
+		    (i == 5 && strcmp(device->address, simulator->room_humaddress) == 0) ||
+		    (i == 6 && strcmp(device->address, simulator->chiller_address) == 0) ||
+		    (i == 7 && strcmp(device->address, simulator->fan_address) == 0) ||
+		    (i == 8 && strcmp(device->address, simulator->light_address) == 0) ||
+		    (i == 9 && strcmp(device->address, simulator->beer_address2) == 0)) {
+                    found = TRUE;
+                    break;
+            	}
+            }
+
+	    if (found == FALSE) {
+		ndev = (devices_list *)malloc(sizeof(devices_list));
+		ndev->next = NULL;
+		ndev->uuid = malloc(37);
+                uuid_generate(uu);
+                uuid_unparse(uu, ndev->uuid);
+		ndev->type = DEVTYPE_SIM;
+        	ndev->value = ndev->offset = 0;
+        	ndev->present = DEVPRESENT_NO;
+        	ndev->gpiopin = -1;
+		ndev->subdevice = i;
+        	ndev->comment = xstrcpy((char *)"Auto detected device");
+        	ndev->timestamp = time(NULL);
+        	ndev->inuse = 0;
 
-    /*
-     * Create simulated devices, or upgrade with new devices.
-     */
-    subdevices = 10;
-    for (i = found; i < subdevices; i++) {
-	ndev = (devices_list *)malloc(sizeof(devices_list));
-	ndev->next = NULL;
-	ndev->uuid = malloc(37);
-	uuid_generate(uu);
-	uuid_unparse(uu, ndev->uuid);
-	ndev->type = DEVTYPE_SIM;
-	ndev->value = ndev->offset = 0;
-	ndev->present = DEVPRESENT_YES;
-	ndev->subdevice = i;
-	ndev->gpiopin = -1;
-	ndev->comment = xstrcpy((char *)"Auto detected device");
-	ndev->timestamp = time(NULL);
-	ndev->inuse = 0;
-	switch (i) {
-	    case 0:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimRoomTemp");
-			ndev->description = xstrcpy((char *)"Simulated room temperature");
-			break;
-	    case 1:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimAirTemp");
-			ndev->description = xstrcpy((char *)"Simulated air temperature");
-			break;
-	    case 2:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimBeerTemp");
-			ndev->description = xstrcpy((char *)"Simulated beer temperature");
-			break;
-	    case 3:	ndev->direction = DEVDIR_OUT_BIN;
-			ndev->address = xstrcpy((char *)"SimHeater");
-			ndev->description = xstrcpy((char *)"Simulated heater");
-			break;
-	    case 4:	ndev->direction = DEVDIR_OUT_BIN;
-			ndev->address = xstrcpy((char *)"SimCooler");
-			ndev->description = xstrcpy((char *)"Simulated cooler");
-			break;
-	    case 5:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimRoomHum");
-			ndev->description = xstrcpy((char *)"Simulated room humidity");
-			break;
-	    case 6:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimChillerTemp");
-			ndev->description = xstrcpy((char *)"Simulated Chiller temperature");
-			break;
-	    case 7:	ndev->direction = DEVDIR_OUT_BIN;
-			ndev->address = xstrcpy((char *)"SimFan");
-			ndev->description = xstrcpy((char *)"Simulated fan");
-			break;
-	    case 8:	ndev->direction = DEVDIR_OUT_BIN;
-			ndev->address = xstrcpy((char *)"SimLight");
-			ndev->description = xstrcpy((char *)"Simulated light");
-			break;
-	    case 9:	ndev->direction = DEVDIR_IN_ANALOG;
-			ndev->address = xstrcpy((char *)"SimBeerTemp2");
-			ndev->description = xstrcpy((char *)"Simulated beer temperature (alt)");
-			break;
-	}
+		switch (i) {
+		    case 0:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->room_tempaddress);
+				ndev->description = xstrcpy((char *)"Simulated room temperature");
+				ndev->present = DEVPRESENT_YES;
+				break;
+		    case 1:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->air_address);
+				ndev->description = xstrcpy((char *)"Simulated air temperature");
+				ndev->present = simulator->air_present;
+				break;
+		    case 2:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->beer_address);
+				ndev->description = xstrcpy((char *)"Simulated beer temperature");
+				ndev->present = simulator->beer_present;
+				break;
+		    case 3:	ndev->direction = DEVDIR_OUT_BIN;
+				ndev->address = xstrcpy(simulator->heater_address);
+				ndev->description = xstrcpy((char *)"Simulated heater");
+				ndev->present = simulator->heater_present;
+				break;
+		    case 4:	ndev->direction = DEVDIR_OUT_BIN;
+				ndev->address = xstrcpy(simulator->cooler_address);
+				ndev->description = xstrcpy((char *)"Simulated cooler");
+				ndev->present = simulator->cooler_present;
+				break;
+		    case 5:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->room_humaddress);
+				ndev->description = xstrcpy((char *)"Simulated room humidity");
+				ndev->present = DEVPRESENT_YES;
+				break;
+		    case 6:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->chiller_address);
+				ndev->description = xstrcpy((char *)"Simulated Chiller temperature");
+				ndev->present = simulator->chiller_present;
+				break;
+		    case 7:	ndev->direction = DEVDIR_OUT_BIN;
+				ndev->address = xstrcpy(simulator->fan_address);
+				ndev->description = xstrcpy((char *)"Simulated fan");
+				ndev->present = simulator->fan_present;
+				break;
+		    case 8:	ndev->direction = DEVDIR_OUT_BIN;
+				ndev->address = xstrcpy(simulator->light_address);
+				ndev->description = xstrcpy((char *)"Simulated light");
+				ndev->present = simulator->light_present;
+				break;
+		    case 9:	ndev->direction = DEVDIR_IN_ANALOG;
+				ndev->address = xstrcpy(simulator->beer_address2);
+				ndev->description = xstrcpy((char *)"Simulated beer temperature (alt)");
+				ndev->present = simulator->beer_present2;
+				break;
+		}
 
-	syslog(LOG_NOTICE, "New Simulator device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
+		syslog(LOG_NOTICE, "New Simulator device %s, subdevice %d", ndev->address, ndev->subdevice);
 
-	if (Config.devices == NULL) {
-	    Config.devices = ndev;
-	} else {
-	    for (device = Config.devices; device; device = device->next) {
-		if (device->next == NULL) {
-		    device->next = ndev;
-		    break;
+		if (Config.devices == NULL) {
+		    Config.devices = ndev;
+		} else {
+		    for (device = Config.devices; device; device = device->next) {
+			if (device->next == NULL) {
+			    device->next = ndev;
+			    break;
+			}
+		    }
 		}
+    		rc++;
 	    }
 	}
-    	rc++;
     }
 #endif
 

mercurial