thermferm/server.c

changeset 259
b7c967359771
parent 258
e02393b29733
child 261
e4341cfbc2a8
--- a/thermferm/server.c	Wed Aug 20 22:15:12 2014 +0200
+++ b/thermferm/server.c	Fri Aug 22 17:12:42 2014 +0200
@@ -36,7 +36,7 @@
 extern sys_config       Config;
 extern const char	UNITMODE[5][8];
 extern const char	TEMPSTATE[3][8];
-extern const char	DEVTYPE[7][6];
+extern const char	DEVTYPE[8][6];
 extern const char	DEVPRESENT[4][6];
 extern const char	DEVDIR[7][11];
 extern const char	PROFSTATE[5][6];
@@ -241,7 +241,7 @@
 	    srv_send((char *)"%s,%s,%d,%d,%s,%s", device->uuid, device->address, device->subdevice, device->inuse, device->comment, DEVDIR[device->direction]);
 	}
 	srv_send((char *)".");
-	return 0;
+	return 1;
     }
 
     if (param == NULL) {
@@ -259,7 +259,7 @@
 	    device->uuid = malloc(37);
 	    uuid_generate(uu);
 	    uuid_unparse(uu, device->uuid);
-	    for (i = 0; i < 7; i++) {
+	    for (i = 0; i < 8; i++) {
 		if (strcmp(param, DEVTYPE[i]) == 0) {
 		    device->type = i;
 		    break;
@@ -379,7 +379,7 @@
 			val = strtok(NULL, "\0");
 			if (kwd && val) {
 			    if (strcmp(kwd, (char *)"TYPE") == 0) {
-				for (i = 0; i < 7; i++) {
+				for (i = 0; i < 8; i++) {
 				    if (strcmp(val, DEVTYPE[i]) == 0) {
 					device->type = i;
 					break;
@@ -601,7 +601,7 @@
 	    srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
 	}
 	srv_send((char *)".");
-	return 0;
+	return 1;
 
     } else if (strcmp(opt, (char *)"LOG") == 0) {
 
@@ -642,7 +642,7 @@
 	free(filename);
 	filename = NULL;
 	srv_send((char *)".");
-	return 0;
+	return 1;
     }
 
     srv_send((char *)"504 Subcommand error");
@@ -905,6 +905,282 @@
 
 
 
+#ifdef USE_SIMULATOR
+int delete_Simulator(char *uuid)
+{
+    simulator_list	*current = Config.simulators;
+    simulator_list	*previous = NULL;
+
+    while (current) {
+	if (strcmp(current->uuid, uuid) == 0) {
+	    if (previous == NULL) {
+		Config.simulators = current->next;
+	    	free(current->uuid);
+	    	current->uuid = NULL;
+	    	free(current->name);
+	    	current->name = NULL;
+	    	free(current);
+	    	return 1;
+	    } else {
+		free(current->uuid);
+		current->uuid = NULL;
+		free(current->name);
+		current->name = NULL;
+		previous->next = current->next;
+		free(current);
+		current = previous->next;
+		return 1;
+	    }
+	} else {
+	    previous = current;
+	    current = current->next;
+	}
+    }
+    return 0;
+}
+
+
+
+/*
+ * SIMULATOR ADD name
+ * SIMULATOR DEL uuid
+ * SIMULATOR LIST
+ * SIMULATOR GET uuid
+ * SIMULATOR PUT uuid
+ */
+int cmd_simulator(char *buf)
+{
+    char		*opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
+    simulator_list	*simulator, *tmps;
+    socklen_t		fromlen;
+    int			i, rc, rlen, ival;
+    float		fval;
+    uuid_t		uu;
+
+    opt = strtok(buf, " \0");
+    opt = strtok(NULL, " \0");
+
+    if (opt == NULL) {
+	srv_send((char *)"501 Subcommand missing");
+	return 1;
+    }
+    param = strtok(NULL, "\0");
+
+    if (strcmp(opt, (char *)"LIST") == 0) {
+	srv_send((char *)"212 Simulators list follows:");
+	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	    srv_send((char *)"%s,%s", simulator->uuid, simulator->name);
+	}
+	srv_send((char *)".");
+	return 1;
+    }
+
+    if (param == NULL) {
+	srv_send((char *)"502 Parameter missing");
+	return 1;
+    }
+
+    if (strcmp(opt, (char *)"ADD") == 0) {
+
+	/*
+	 * For now, only one simulator is allowed.
+	 */
+	if (Config.simulators) {
+	    srv_send((char *)"441 Maximum simulators reached");
+	    return 1;
+	}
+
+	simulator = (simulator_list *)malloc(sizeof(simulator_list));
+	simulator->next = NULL;
+	simulator->version = 1;
+	simulator->uuid = malloc(37);
+	uuid_generate(uu);
+	uuid_unparse(uu, simulator->uuid);
+	simulator->name = xstrcpy(param);
+	simulator->volume_air = 150;
+	simulator->volume_beer = 50;
+	simulator->room_temperature = simulator->air_temperature = simulator->beer_temperature = simulator->s_cool_temp = simulator->s_heat_temp = 20.0;
+	simulator->cooler_temp = -3.0;	/* 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->heater_state = simulator->cooler_state = 0;
+	simulator->frigo_isolation = 0.002;
+	simulator->s_yeast_heat = 0.0;
+	simulator->s_yeast_started = simulator->s_cool_changed = simulator->s_heat_changed = (int)0;
+
+	if (Config.simulators == NULL) {
+	    Config.simulators = simulator;
+	} else {
+	    for (tmps = Config.simulators; tmps; tmps = tmps->next) {
+		if (tmps->next == NULL) {
+		    tmps->next = simulator;
+		    break;
+		}
+	    }
+	}
+
+	syslog(LOG_NOTICE, "Simulator %s added", simulator->uuid);
+	srv_send((char *)"211 Simulator %s added", simulator->uuid);
+	return 0;
+    }
+
+    if (strcmp(opt, (char *)"DEL") == 0) {
+	rc = delete_Simulator(param);
+	if (rc) {
+	    syslog(LOG_NOTICE, "Simulator %s deleted", param);
+	    srv_send((char *)"211 Simulator %s deleted", param);
+	    return 0;
+	} else {
+	    srv_send((char *)"440 No such simulator");
+	    return 1;
+	}
+    }
+
+    if (strcmp(opt, (char *)"GET") == 0) {
+	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	    if (strcmp(simulator->uuid, param) == 0) {
+		srv_send((char *)"213 Simulator record follows:");
+		srv_send((char *)"NAME,%s", simulator->name);
+		srv_send((char *)"VOLUME_AIR,%d", simulator->volume_air);
+		srv_send((char *)"VOLUME_BEER,%d", simulator->volume_beer);
+		srv_send((char *)"ROOM_TEMPERATURE,%.1f", simulator->room_temperature);
+		srv_send((char *)"AIR_TEMPERATURE,%.3f", simulator->air_temperature);
+		srv_send((char *)"BEER_TEMPERATURE,%.3f", simulator->beer_temperature);
+		srv_send((char *)"COOLER_TEMP,%.1f", simulator->cooler_temp);
+		srv_send((char *)"COOLER_TIME,%d", simulator->cooler_time);
+		srv_send((char *)"COOLER_SIZE,%.3d", simulator->cooler_size);
+		srv_send((char *)"HEATER_TEMP,%.1f", simulator->heater_temp);
+		srv_send((char *)"HEATER_TIME,%d", simulator->heater_time);
+		srv_send((char *)"HEATER_SIZE,%.3f", simulator->heater_size);
+		srv_send((char *)"HEATER_STATE,%d", simulator->heater_state);
+		srv_send((char *)"COOLER_STATE,%d", simulator->cooler_state);
+		srv_send((char *)"FRIGO_ISOLATION,%.6f", simulator->frigo_isolation);
+		srv_send((char *)".");
+		return 1;
+	    }
+	}
+	srv_send((char *)"440 No such simulator");
+	return 1;
+    }
+
+    if (strcmp(opt, (char *)"PUT") == 0) {
+	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	    if (strcmp(simulator->uuid, param) == 0) {
+		while (1) {
+		    memset((char *)&ibuf, 0, SS_BUFSIZE);
+		    fromlen = sizeof(peeraddr_in);
+		    rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
+		    if (rlen == -1) {
+			syslog(LOG_WARNING, "recvfrom(): %s", strerror(errno));
+			srv_send((char *)"518 recfrom(): %s", strerror(errno));
+			return 1;
+		    }
+		    for (i = 0; i < strlen(ibuf); i++) {
+			if (ibuf[i] == '\n')
+			    ibuf[i] = '\0';
+			if (ibuf[i] == '\r')
+			    ibuf[i] = '\0';
+		    }
+		    for (i = strlen(ibuf) -1; i > 0; i--) {
+			if (ibuf[i] == ' ')
+			    ibuf[i] = '\0';
+			else
+			    break;
+		    }
+		    if (strlen(ibuf)) {
+			if (debug) {
+			    syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
+			    fprintf(stdout, "recv: \"%s\"\n", ibuf);
+			}
+			if (strcmp(ibuf, (char *)".") == 0) {
+			    srv_send((char *)"219 Accepted Simulator record");
+			    return 0;
+			}
+			kwd = strtok(ibuf, ",\0");
+			val = strtok(NULL, "\0");
+			if (kwd && val) {
+
+			    if (strcmp(kwd, (char *)"NAME") == 0) {
+				if (simulator->name)
+				    free(simulator->name);
+				simulator->name = xstrcpy(val);
+
+			    } else if (strcmp(kwd, (char *)"VOLUME_AIR") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->volume_air = ival;
+
+			    } else if (strcmp(kwd, (char *)"VOLUME_BEER") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->volume_beer = ival;
+
+			    } else if (strcmp(kwd, (char *)"ROOM_TEMPERATURE") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->room_temperature = fval;
+
+			    } else if (strcmp(kwd, (char *)"AIR_TEMPERATURE") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->air_temperature = fval;
+
+			    } else if (strcmp(kwd, (char *)"BEER_TEMPERATURE") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->beer_temperature = fval;
+
+			    } else if (strcmp(kwd, (char *)"COOLER_TEMP") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->cooler_temp = fval;
+
+			    } else if (strcmp(kwd, (char *)"COOLER_TIME") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->cooler_time = ival;
+	
+			    } else if (strcmp(kwd, (char *)"COOLER_SIZE") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->cooler_size = fval;
+
+			    } else if (strcmp(kwd, (char *)"HEATER_TEMP") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->heater_temp = fval;
+
+			    } else if (strcmp(kwd, (char *)"HEATER_TIME") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->heater_time = ival;
+
+			    } else if (strcmp(kwd, (char *)"HEATER_SIZE") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->heater_size = fval;
+
+			    } else if (strcmp(kwd, (char *)"HEATER_STATE") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->heater_state = ival;
+
+			    } else if (strcmp(kwd, (char *)"COOLER_STATE") == 0) {
+				if (sscanf(val, "%d", &ival) == 1)
+				    simulator->cooler_state = ival;
+
+			    } else if (strcmp(kwd, (char *)"FRIGO_ISOLATION") == 0) {
+				if (sscanf(val, "%f", &fval) == 1)
+				    simulator->frigo_isolation = fval;
+
+			    }
+			}
+		    }
+		}
+	    }
+	}
+	srv_send((char *)"440 No such simulator");
+	return 1;
+    }
+
+    srv_send((char *)"504 Subcommand error");
+    return 1;
+}
+#endif
+
+
+
 int delete_Unit(char *uuid)
 {
     units_list  *current = Config.units;
@@ -1381,7 +1657,6 @@
 	return 1;
     }
 
-
     srv_send((char *)"504 Subcommand error");
     return 1;
 }
@@ -1442,14 +1717,21 @@
 		srv_send((char *)"LIST                          List all fermenter units");
 		srv_send((char *)"LIST LOG uuid                 List logfile data in 1 hour lines");
 		srv_send((char *)"PROFILE uuid,name             Profile rename");
-		srv_send((char *)"PROFILE ADD name              Add new profile with name");
-		srv_send((char *)"PROFILE DEL uuid              Delete profile by uuid");
+		srv_send((char *)"PROFILE ADD name              Add new Profile with name");
+		srv_send((char *)"PROFILE DEL uuid              Delete Profile by uuid");
 		srv_send((char *)"PROFILE LIST                  List available profiles");
 		srv_send((char *)"PROFILE GET uuid              Get Profile record by uuid");
 		srv_send((char *)"PROFILE PUT uuid              Put Profile record by uuid");
 		srv_send((char *)"PROFILE GETS uuid             Profile get steps list");
 		srv_send((char *)"PROFILE PUTS uuid             Profile put steps list");
-		srv_send((char *)"UNIT ADD name                 Add a new unit with name");
+#ifdef USE_SIMULATOR
+		srv_send((char *)"SIMULATOR ADD name            Add a new Simulator with name");
+		srv_send((char *)"SIMULATOR DEL uuid            Delete Simulator by uuid");
+		srv_send((char *)"SIMULATOR LIST                List all Simulators");
+		srv_send((char *)"SIMULATOR GET uuid            Get Simulator record by uuid");
+		srv_send((char *)"SIMULATOR PUT uuid            Put Simulator record by uuid");
+#endif
+		srv_send((char *)"UNIT ADD name                 Add a new Unit with name");
 		srv_send((char *)"UNIT DEL uuid                 Delete Unit by uuid");
 		srv_send((char *)"UNIT LIST                     List all Units");
 		srv_send((char *)"UNIT GET uuid                 Get Unit record by uuid");
@@ -1460,6 +1742,11 @@
 	    } else if (strncmp(buf, "PROFILE", 7) == 0) {
 		if (cmd_profile(buf) == 0)
 		    wrconfig();
+#ifdef USE_SIMULATOR
+	    } else if (strncmp(buf, "SIMULATOR", 9) == 0) {
+		if (cmd_simulator(buf) == 0)
+		    wrconfig();
+#endif
 	    } else if (strncmp(buf, "UNIT", 4) == 0) {
 		if (cmd_unit(buf) == 0)
 		    wrconfig();

mercurial