Added simulator configuration.

Fri, 27 Nov 2015 22:56:00 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 27 Nov 2015 22:56:00 +0100
changeset 438
7d1ec160d751
parent 437
5664743eaf2f
child 439
27f7f634c6d3

Added simulator configuration.

brewco/brewco.c file | annotate | diff | comparison | revisions
brewco/brewco.h file | annotate | diff | comparison | revisions
brewco/devices.c file | annotate | diff | comparison | revisions
brewco/keyboard.c file | annotate | diff | comparison | revisions
brewco/rdconfig.c file | annotate | diff | comparison | revisions
brewco/simulator.c file | annotate | diff | comparison | revisions
brewco/simulator.h file | annotate | diff | comparison | revisions
--- a/brewco/brewco.c	Fri Nov 27 22:00:23 2015 +0100
+++ b/brewco/brewco.c	Fri Nov 27 22:56:00 2015 +0100
@@ -28,6 +28,8 @@
 #include "slcd.h"
 #include "lock.h"
 #include "devices.h"
+#include "keyboard.h"
+#include "simulator.h"
 
 
 int			my_shutdown = FALSE;
@@ -35,6 +37,7 @@
 extern sys_config       Config;
 extern int              lcdHandle;
 extern int		slcdHandle;
+extern int		sock;
 
 #ifndef HAVE_WIRINGPI_H
 pthread_t               threads[5];
@@ -80,12 +83,13 @@
 int server(void);
 int server(void)
 {
-    int 	rc = 0;
+    int 	rc = 0, run = 1;
+    units_list	*unit;
 #ifndef HAVE_WIRINGPI_H
     long	t = 0;
 #endif
 
-//    if (lockprog((char *)"thermferm")) {
+//    if (lockprog((char *)"brewco")) {
 //	syslog(LOG_NOTICE, "Can't lock");
 //	return 1;
 //    }
@@ -109,6 +113,36 @@
 #endif
     }
 
+#ifdef HAVE_WIRINGPI_H
+        rc = piThreadCreate(my_panel_loop);
+#else
+	    rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t );
+#endif
+	        if (rc) {
+			        fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc);
+				        syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc);
+#ifndef HAVE_WIRINGPI_H
+					    } else {
+						            t++;
+#endif
+							        }
+
+#ifdef USE_SIMULATOR
+#ifdef HAVE_WIRINGPI_H
+		    rc = piThreadCreate(my_simulator_loop);
+#else
+		        rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t );
+#endif
+			    if (rc) {
+				            fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc);
+					            syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc);
+#ifndef HAVE_WIRINGPI_H
+						        } else {
+								        t++;
+#endif
+									    }
+#endif
+
     /*
      * Initialize units for processing
      */
@@ -131,7 +165,42 @@
 	piUnlock(LOCK_LCD);
 #endif
 
-    return rc;
+
+    do {
+	if (my_shutdown)
+	    run = 0;
+
+
+	usleep(100000);
+
+    } while (run);
+
+    /*
+     * Stop units processing in a neat way
+     */
+    for (unit = Config.units; unit; unit = unit->next) {
+
+    }
+
+    syslog(LOG_NOTICE, "Out of loop");
+    if (debug)
+	fprintf(stdout, (char *)"Out of loop\n");
+
+    /*
+     * Give threads time to cleanup
+     */
+    usleep(1500000);
+
+//    stopLCD();
+    if (sock != -1) {
+	if (shutdown(sock, SHUT_RDWR)) {
+	    syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno));
+	}
+	sock = -1;
+    }
+    wrconfig();
+//    ulockprog((char *)"brewco");
+    return 0;
 }
 
 
--- a/brewco/brewco.h	Fri Nov 27 22:00:23 2015 +0100
+++ b/brewco/brewco.h	Fri Nov 27 22:56:00 2015 +0100
@@ -232,10 +232,6 @@
  * Simulate a HLT and MLT.
  */
 typedef struct _simulator {
-    struct _simulator   *next;
-    int                 version;                /* Version of this record       */
-    char                *uuid;                  /* Simulator uuid               */
-    char                *name;                  /* Simulator name               */
     double		room_temperature;	/* Simulated envionment temp	*/
     double		hlt_temperature;	/* Simulated HLT temperature	*/
     double		hlt_heater_temp;	/* Maximum heater temp		*/
@@ -253,7 +249,7 @@
      */
     double		s_hlt_temp;		/* Temp HLT			*/
     double		s_mlt_temp;		/* Temp MLT			*/
-} simulator_list;
+} simulator_var;
 
 #endif
 
@@ -270,7 +266,7 @@
     units_list		*units;			/* Brewing units		*/
     devices_list	*devices;		/* Sensors and switches		*/
 #ifdef USE_SIMULATOR
-    simulator_list      *simulators;            /* Simulators                   */
+    simulator_var	*simulator;		/* Simulator			*/
 #endif
 } sys_config;
 
--- a/brewco/devices.c	Fri Nov 27 22:00:23 2015 +0100
+++ b/brewco/devices.c	Fri Nov 27 22:56:00 2015 +0100
@@ -32,8 +32,8 @@
 
 #ifdef USE_SIMULATOR
 
-/*extern*/ int	SIM_hlt_temp = 0;
-/*extern*/ int	SIM_mlt_temp = 0;
+extern int	SIM_hlt_value;
+extern int	SIM_mlt_value;
 
 #endif
 
@@ -215,9 +215,9 @@
 			}
 			device->value = value;
 			if (strcmp((char *)"SimHLTheater", device->address) == 0)
-			    SIM_hlt_temp = value;
+			    SIM_hlt_value = value;
 			if (strcmp((char *)"SimMLTheater", device->address) == 0)
-			    SIM_mlt_temp = value;
+			    SIM_mlt_value = value;
 		    }
 		}
 #endif
@@ -565,14 +565,13 @@
 #endif
 {
     devices_list	*device;
-#ifdef USE_SIMULATOR
-    simulator_list	*simulator;
-#endif
     char		*addr = NULL, line[60], *p = NULL;
     FILE		*fp;
     int			temp, rc;
 
     syslog(LOG_NOTICE, "Thread my_devices_loop started");
+    if (debug)
+	fprintf(stdout, "Thread my_devices_loop started\n");
 
 #ifdef HAVE_WIRINGPI_H
     if ((rc = piHiPri(10)))
@@ -759,16 +758,15 @@
 #ifdef HAVE_WIRINGPI_H
 			piLock(LOCK_DEVICES);
 #endif
-			if (Config.simulators) {
-			    simulator = Config.simulators;
+			if (Config.simulator) {
 			    if (device->subdevice == 0) {
-			    	device->value = (int)(simulator->room_temperature * 1000);
+			    	device->value = (int)(Config.simulator->room_temperature * 1000);
 			    	device->timestamp = time(NULL);
 			    } else if (device->subdevice == 1) {
-			    	device->value = (int)(simulator->hlt_temperature * 1000);
+			    	device->value = (int)(Config.simulator->hlt_temperature * 1000);
 			    	device->timestamp = time(NULL);
 			    } else if (device->subdevice == 2) {
-			    	device->value = (int)(simulator->mlt_temperature * 1000);
+			    	device->value = (int)(Config.simulator->mlt_temperature * 1000);
 			    	device->timestamp = time(NULL);
 			    }
 			}
@@ -786,6 +784,9 @@
 	     */
 	    usleep(10000);
 	}
+
+	if (my_shutdown)
+	    break;
 	/*
 	 * Delay a bit after all devices
 	 */
@@ -793,6 +794,8 @@
     }
 
     syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
+    if (debug)
+	fprintf(stdout, "Thread my_devices_loop stopped\n");
     return 0;
 }
 
--- a/brewco/keyboard.c	Fri Nov 27 22:00:23 2015 +0100
+++ b/brewco/keyboard.c	Fri Nov 27 22:56:00 2015 +0100
@@ -115,6 +115,8 @@
 #endif
 
     syslog(LOG_NOTICE, "Thread my_panel_loop started");
+    if (debug)
+	fprintf(stdout, "Thread my_panel_loop started\n");
 
     /*
      * Loop forever until the external shutdown variable is set.
@@ -237,6 +239,8 @@
     }
 
     syslog(LOG_NOTICE, "Thread my_panel_loop stopped");
+    if (debug)
+	fprintf(stdout, "Thread my_panel_loop stopped\n");
     return 0;
 }
 
--- a/brewco/rdconfig.c	Fri Nov 27 22:00:23 2015 +0100
+++ b/brewco/rdconfig.c	Fri Nov 27 22:56:00 2015 +0100
@@ -42,9 +42,6 @@
 {
     units_list          *unit;
     devices_list        *device;
-#ifdef USE_SIMULATOR
-    simulator_list      *simulator;
-#endif
 
     if (Config.name)
 	free(Config.name);
@@ -95,14 +92,9 @@
     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;
+    if (Config.simulator)
+	free(Config.simulator);
+    Config.simulator = NULL;
 #endif
 }
 
@@ -118,9 +110,6 @@
     xmlBufferPtr	buf;
     units_list		*unit;
     devices_list	*device;
-#ifdef USE_SIMULATOR
-    simulator_list      *simulator;
-#endif
 
     /* 
      * Create a new XML buffer, to which the XML document will be written
@@ -602,34 +591,66 @@
     }
 
 #ifdef USE_SIMULATOR
-    if (Config.simulators) {
-        if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATORS")) < 0) {
-            syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
+    if (Config.simulator) {
+        if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATOR")) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
+	    return 1;
+        }
+        if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROOM_TEMPERATURE", "%f", Config.simulator->room_temperature)) < 0) {
+            syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
             return 1;
         }
-        for (simulator = Config.simulators; simulator; simulator = simulator->next) {
-            if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATOR")) < 0) {
-                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
-                return 1;
-            }
-            if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VERSION", "%d", simulator->version)) < 0) {
-                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
-                return 1;
-            }
-            if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", simulator->uuid)) < 0) {
-                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                return 1;
-            }
-            if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", simulator->name)) < 0) {
-                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                return 1;
-            }
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_TEMPERATURE", "%f", Config.simulator->hlt_temperature)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_TEMP", "%f", Config.simulator->hlt_heater_temp)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_TIME", "%d", Config.simulator->hlt_heater_time)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_SIZE", "%f", Config.simulator->hlt_heater_size)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_STATE", "%d", Config.simulator->hlt_heater_state)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
 
-            if ((rc = xmlTextWriterEndElement(writer)) < 0) {
-                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
-                return 1;
-            }
-        }
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_TEMPERATURE", "%f", Config.simulator->mlt_temperature)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_TEMP", "%f", Config.simulator->mlt_heater_temp)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_TIME", "%d", Config.simulator->mlt_heater_time)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}                                       
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_SIZE", "%f", Config.simulator->mlt_heater_size)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_STATE", "%d", Config.simulator->mlt_heater_state)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HLT_TEMP", "%f", Config.simulator->s_hlt_temp)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_MLT_TEMP", "%f", Config.simulator->s_mlt_temp)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+	    return 1;
+	}
+
         if ((rc = xmlTextWriterEndElement(writer)) < 0) {
             syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
             return 1;
@@ -1299,142 +1320,104 @@
 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->room_temperature = simulator->hlt_heater_temp = simulator->mlt_heater_temp = simulator->s_hlt_temp = simulator->s_mlt_temp = 20.0;
-    simulator->hlt_heater_temp = simulator->hlt_heater_size = simulator->mlt_heater_temp = simulator->mlt_heater_size = 0.0;
-    simulator->hlt_heater_time = simulator->mlt_heater_time = simulator->hlt_heater_state = simulator->mlt_heater_state = 0;
-    simulator->s_hlt_temp = simulator->s_mlt_temp = 0.0;
+    /*
+     * First time, allocate memory and set defaults.
+     */
+    if (! Config.simulator) {
+	Config.simulator = (simulator_var *)malloc(sizeof(simulator_var));
+	Config.simulator->room_temperature = Config.simulator->hlt_temperature = Config.simulator->hlt_heater_temp = \
+		Config.simulator->mlt_temperature = Config.simulator->mlt_heater_temp = \
+		Config.simulator->s_hlt_temp = Config.simulator->s_mlt_temp = 0.0;
+	Config.simulator->hlt_heater_size = Config.simulator->mlt_heater_size = 0.0;
+	Config.simulator->hlt_heater_time = Config.simulator->mlt_heater_time = Config.simulator->hlt_heater_state = Config.simulator->mlt_heater_state = 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 *)"ROOM_TEMPERATURE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->room_temperature = fval;
+		Config.simulator->room_temperature = fval;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_TEMPERATURE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->hlt_temperature = fval;
+		Config.simulator->hlt_temperature = fval;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TEMP"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->hlt_heater_temp = fval;
+		Config.simulator->hlt_heater_temp = fval;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TIME"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%d", &ival) == 1)
-		simulator->hlt_heater_time = ival;
+		Config.simulator->hlt_heater_time = ival;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_SIZE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->hlt_heater_size = fval;
+		Config.simulator->hlt_heater_size = fval;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_STATE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%d", &ival) == 1)
-		simulator->hlt_heater_state = ival;
+		Config.simulator->hlt_heater_state = ival;
 	    xmlFree(key);
 	}
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_TEMPERATURE"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%f", &fval) == 1)
-                simulator->mlt_temperature = fval;
+                Config.simulator->mlt_temperature = fval;
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TEMP"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%f", &fval) == 1)
-                simulator->mlt_heater_temp = fval;
+                Config.simulator->mlt_heater_temp = fval;
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TIME"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%d", &ival) == 1)
-                simulator->mlt_heater_time = ival;
+                Config.simulator->mlt_heater_time = ival;
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_SIZE"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%f", &fval) == 1)
-                simulator->mlt_heater_size = fval;
+                Config.simulator->mlt_heater_size = fval;
             xmlFree(key);
         }
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_STATE"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%d", &ival) == 1)
-                simulator->mlt_heater_state = ival;
+                Config.simulator->mlt_heater_state = ival;
             xmlFree(key);
         }
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_HLT_TEMP"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->s_hlt_temp = fval;
+		Config.simulator->s_hlt_temp = fval;
 	    xmlFree(key);
 	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_MLT_TEMP"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &fval) == 1)
-		simulator->s_mlt_temp = fval;
+		Config.simulator->s_mlt_temp = fval;
 	    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
@@ -1536,8 +1519,8 @@
 	    parseDevices(doc, cur);
 	}
 #ifdef USE_SIMULATOR
-	if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATORS"))) {
-	    parseSimulators(doc, cur);
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATOR"))) {
+	    parseSimulator(doc, cur);
 	}
 #endif
 	cur = cur->next;
@@ -1547,6 +1530,21 @@
     free(mypath);
     mypath = NULL;
 
+#ifdef USE_SIMULATOR
+    /*
+     * If we didn't find any simulator values, initialize a new simulator.
+     */
+    if (! Config.simulator) {
+	Config.simulator = (simulator_var *)malloc(sizeof(simulator_var));
+	syslog(LOG_NOTICE, "rdconfig() init a new simulator");
+	Config.simulator->room_temperature = Config.simulator->hlt_temperature = Config.simulator->hlt_heater_temp = \
+		Config.simulator->mlt_temperature = Config.simulator->mlt_heater_temp = \
+		Config.simulator->s_hlt_temp = Config.simulator->s_mlt_temp = 20.0;
+	Config.simulator->hlt_heater_size = Config.simulator->mlt_heater_size = 0.0;
+	Config.simulator->hlt_heater_time = Config.simulator->mlt_heater_time = Config.simulator->hlt_heater_state = Config.simulator->mlt_heater_state = 0;
+    }
+#endif
+
     return rc;
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/brewco/simulator.c	Fri Nov 27 22:56:00 2015 +0100
@@ -0,0 +1,107 @@
+/*****************************************************************************
+ * Copyright (C) 2015
+ *   
+ * Michiel Broek <mbroek at mbse dot eu>
+ *
+ * This file is part of the mbsePi-apps
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * mbsePi-apps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with ThermFerm; see the file COPYING.  If not, write to the Free
+ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *****************************************************************************/
+
+#include "brewco.h"
+#include "simulator.h"
+
+#ifdef USE_SIMULATOR
+
+extern int		my_shutdown;
+extern int		debug;
+extern sys_config	Config;
+
+int			SIM_hlt_value = 0;
+int			SIM_mlt_value = 0;
+
+
+#ifdef HAVE_WIRINGPI_H
+PI_THREAD (my_simulator_loop)
+#else
+void *my_simulator_loop(void *threadid)
+#endif
+{
+    time_t		now, last = (time_t)0;
+    int			seconds = 0;
+//    double		hlt_heat_transfer, mlt_heat_transfer;
+//    double		vhc_air = 0.00121, vhc_water = 4.1796;
+
+    syslog(LOG_NOTICE, "Thread my_simulator_loop started");
+    if (debug)
+	fprintf(stdout, "Thread my_simulator_loop started\n");
+
+    /*
+     * Heater and cooler have the air temperature
+     */
+    Config.simulator->hlt_heater_temp = Config.simulator->mlt_heater_temp = Config.simulator->room_temperature;
+
+    for (;;) {
+	if (my_shutdown)
+	    break;
+
+	now = time(NULL);
+	if (now != last) {
+	    last = now;
+	    /*
+	     * Each second
+	     */
+	    seconds++;
+
+		/*
+		 * First calculate the heat loss for the HLT and MLT liquids.
+		 * Then decrease both liquid temperatures.
+		 */
+
+		/*
+		 * If heating, calculate the heating element temperature.
+		 * If not heating, shift towards the liquid temperature.
+		 */
+
+		/*
+		 * Shift the liquid temperature towards the heating elements temperature,
+		 * but never higher then 100 degrees celcius.
+		 */
+
+	    	/*
+	    	 * If heating, calculate temperature of the heating plate. If heating is off but
+	    	 * the plate is warmer then the air, calculate the cooling down temperature.
+	    	 * Finally, calculate the new air and plate temperature.
+	    	 */
+
+//		syslog(LOG_NOTICE, "air=%.3f beer=%.3f heater=%.3f cooler=%.3f", simulator->air_temperature, simulator->beer_temperature,
+//				simulator->s_heat_temp, simulator->s_cool_temp);
+
+//		if (debug)
+//		    fprintf(stdout, "sqm_room_air=%f air=%f air_heat_transfer=%f air_change=%f  sqm_beer_air=%f beer=%f beer_heat_transfer=%f\n",
+//			sqm_room_air, simulator->air_temperature, air_heat_transfer, air_change,
+//			sqm_beer_air, simulator->beer_temperature, beer_heat_transfer);
+	}
+	usleep(100000);
+    }
+
+    syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
+    if (debug)
+	fprintf(stdout, "Thread my_simulator_loop stopped\n");
+    return 0;
+}
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/brewco/simulator.h	Fri Nov 27 22:56:00 2015 +0100
@@ -0,0 +1,15 @@
+#ifndef SIMULATOR_H
+#define	SIMULATOR_H
+
+#ifdef USE_SIMULATOR
+
+
+#ifdef HAVE_WIRINGPI_H
+PI_THREAD (my_simulator_loop);
+#else
+void *my_simulator_loop(void *);
+#endif
+
+#endif
+
+#endif

mercurial