brewco/brewco.c

changeset 438
7d1ec160d751
parent 434
eb724767860d
child 440
8df3252b688f
--- 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;
 }
 
 

mercurial