thermferm/rdconfig.c

changeset 259
b7c967359771
parent 245
b01b6238eb67
child 262
d0014ccec615
--- a/thermferm/rdconfig.c	Wed Aug 20 22:15:12 2014 +0200
+++ b/thermferm/rdconfig.c	Fri Aug 22 17:12:42 2014 +0200
@@ -34,7 +34,7 @@
 const char	TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" };
 const char      UNITMODE[5][8]	= { "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" };
 const char	PROFSTATE[5][6]	= { "OFF", "PAUSE", "RUN", "DONE", "ABORT" };
-const char	DEVTYPE[7][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI" };
+const char	DEVTYPE[8][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI", "SIM" };
 const char	DEVPRESENT[4][6] = { "UNDEF", "NO", "YES", "ERROR" };
 const char	DEVDIR[7][11] = { "UNDEF", "IN_BIN", "OUT_BIN", "IN_ANALOG", "OUT_ANALOG", "OUT_PWM", "INTERN" };
 
@@ -46,6 +46,9 @@
     profiles_list	*tmp3;
     prof_step		*tmp4;
     devices_list	*device;
+#ifdef USE_SIMULATOR
+    simulator_list	*simulator;
+#endif
 
     if (Config.name)
 	free(Config.name);
@@ -112,6 +115,17 @@
     }
     Config.devices = NULL;
 
+#ifdef USE_SIMULATOR
+    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	if (simulator->uuid)
+	    free(simulator->uuid);
+	if (simulator->name)
+	    free(simulator->name);
+	free(simulator);
+    }
+    Config.simulators = NULL;
+#endif
+
 #ifdef HAVE_WIRINGPI_H
     Config.lcd_cols = 16;
     Config.lcd_rows = 2;
@@ -131,6 +145,9 @@
     profiles_list	*tmp4;
     prof_step		*tmp5;
     devices_list	*device;
+#ifdef USE_SIMULATOR
+    simulator_list	*simulator;
+#endif
 
     /* 
      * Create a new XML buffer, to which the XML document will be written
@@ -585,6 +602,121 @@
 	}
     }
 
+#ifdef USE_SIMULATOR
+    if (Config.simulators) {
+	if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATORS")) < 0) {
+	    syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterStartElement");
+	    return 1;
+	}
+	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+    	    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATOR")) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterStartElement");
+	    	return 1;
+    	    }
+    	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VERSION", "%d", simulator->version)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+    	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", simulator->uuid)) < 0) {
+		syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", simulator->name)) < 0) {
+		syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME_AIR", "%d", simulator->volume_air)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME_BEER", "%d", simulator->volume_beer)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROOM_TEMPERATURE", "%.1f", simulator->room_temperature)) < 0) {
+	   	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	   	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%.1f", simulator->air_temperature)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_TEMPERATURE", "%.1f", simulator->beer_temperature)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_TEMP", "%.1f", simulator->cooler_temp)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_TIME", "%d", simulator->cooler_time)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_SIZE", "%.3f", simulator->cooler_size)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_TEMP", "%.1f", simulator->heater_temp)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_TIME", "%d", simulator->heater_time)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_SIZE", "%.3f", simulator->heater_size)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_STATE", "%d", simulator->heater_state)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_STATE", "%d", simulator->cooler_state)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;                       
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "FRIGO_ISOLATION", "%.3f", simulator->frigo_isolation)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_YEAST_HEAT", "%.1f", simulator->s_yeast_heat)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_YEAST_STARTED", "%d", (int)simulator->s_yeast_started)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_COOL_TEMP", "%.1f", simulator->s_cool_temp)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HEAT_TEMP", "%.1f", simulator->s_heat_temp)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_COOL_CHANGED", "%d", (int)simulator->s_cool_changed)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HEAT_CHANGED", "%d", (int)simulator->s_heat_changed)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement");
+	    	return 1;
+	    }
+    	    if ((rc = xmlTextWriterEndElement(writer)) < 0) {
+	    	syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterEndElement");
+	    	return 1;
+    	    }
+	}
+	if ((rc = xmlTextWriterEndElement(writer)) < 0) {
+	    syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterEndElement");
+	    return 1;
+	}
+    }
+#endif
+
     /*
      * All done, close any open elements
      */
@@ -1065,7 +1197,7 @@
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"TYPE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
-	    for (i = 0; i < 7; i++) {
+	    for (i = 0; i < 8; i++) {
 		if (! xmlStrcmp(key, (const xmlChar *)DEVTYPE[i])) {
 		    device->type = i;
 		    break;
@@ -1172,6 +1304,198 @@
 
 
 
+#ifdef USE_SIMULATOR
+int parseSimulator(xmlDocPtr doc, xmlNodePtr cur)
+{
+    xmlChar             *key;
+    simulator_list      *simulator, *tmp;
+    int                 ival;
+    float		fval;
+
+    simulator = (simulator_list *)malloc(sizeof(simulator_list));
+    simulator->next = NULL;
+    simulator->version = 1;
+    simulator->uuid = simulator->name = NULL;
+    simulator->volume_air = simulator->volume_beer = 0;
+    simulator->room_temperature = simulator->air_temperature = simulator->beer_temperature = simulator->s_cool_temp = simulator->s_heat_temp = 20.0;
+    simulator->cooler_temp = simulator->cooler_size = simulator->heater_temp = simulator->heater_size = simulator->frigo_isolation = 0.0;
+    simulator->cooler_time = simulator->heater_time = simulator->cooler_state = simulator->heater_state = 0;
+    simulator->s_yeast_started = simulator->s_cool_changed = simulator->s_heat_changed = (time_t)0;
+    simulator->s_yeast_heat = simulator->s_cool_temp = simulator->s_heat_temp = 0.0;
+
+    cur = cur->xmlChildrenNode;
+    while (cur != NULL) {
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (xmlStrcmp(key, (const xmlChar *)"1")) {
+		xmlFree(key);
+		return 1;
+	    }
+	    simulator->version = 1;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
+	    simulator->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
+	    simulator->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME_AIR"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->volume_air = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME_BEER"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->volume_beer = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"ROOM_TEMPERATURE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->room_temperature = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_TEMPERATURE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->air_temperature = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"BEER_TEMPERATURE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->beer_temperature = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLER_TEMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->cooler_temp = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLER_TIME"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->cooler_time = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLER_SIZE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->cooler_size = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HEATER_TEMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->heater_temp = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HEATER_TIME"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->heater_time = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HEATER_SIZE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->heater_size = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HEATER_STATE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->heater_state = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLER_STATE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->cooler_state = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"FRIGO_ISOLATION"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->frigo_isolation = fval;
+	    xmlFree(key);
+	}
+
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_YEAST_HEAT"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->s_yeast_heat = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_YEAST_STARTED"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->s_yeast_started = (time_t)ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_COOL_TEMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->s_cool_temp = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_HEAT_TEMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		simulator->s_heat_temp = fval;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_COOL_CHANGED"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->s_cool_changed = (time_t)ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_HEAT_CHANGED"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		simulator->s_heat_changed = (time_t)ival;
+	    xmlFree(key);
+	}
+
+	cur = cur->next;
+    }
+
+    if (Config.simulators == NULL) {
+	Config.simulators = simulator;
+    } else {
+	for (tmp = Config.simulators; tmp; tmp = tmp->next) {
+	    if (tmp->next == NULL) {
+		tmp->next = simulator;
+		break;
+	    }
+	}
+    }
+
+    return 0;
+}
+
+
+
+int parseSimulators(xmlDocPtr doc, xmlNodePtr cur)
+{
+    cur = cur->xmlChildrenNode;
+    while (cur != NULL) {
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATOR"))) {
+	    parseSimulator(doc, cur);
+	}
+	cur = cur->next;
+    }
+    return 0;
+}
+#endif
+
+
+
 int rdconfig(void) 
 {
     int		rc = 0, ival;
@@ -1269,6 +1593,11 @@
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICES"))) {
 	    parseDevices(doc, cur);
 	}
+#ifdef USE_SIMULATOR
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATORS"))) {
+	    parseSimulators(doc, cur);
+	}
+#endif
 	cur = cur->next;
     }
     xmlFreeDoc(doc);

mercurial