brewco/brewco.c

changeset 441
bde74a8f2ad7
parent 440
8df3252b688f
child 442
1193bd7d460f
--- a/brewco/brewco.c	Sat Nov 28 13:47:00 2015 +0100
+++ b/brewco/brewco.c	Sat Nov 28 20:15:38 2015 +0100
@@ -20,8 +20,9 @@
  * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  *****************************************************************************/
 
+#include "brewco.h"
 #include "rdconfig.h"
-#include "brewco.h"
+#include "rdsession.h"
 #include "futil.h"
 #include "xutil.h"
 #include "lcd-pcf8574.h"
@@ -211,13 +212,85 @@
 }
 
 
+
+void editUnit(units_list *unit)
+{
+    if (debug)
+    	fprintf(stdout, "Start edit brewsystem %d %s\n", unit->number, unit->uuid);
+
+    if (debug)
+	fprintf(stdout, "End edit brewsystem %d %s\n", unit->number, unit->uuid);
+}
+
+
+
+void addUnit(int number)
+{
+    units_list	*tmpu, *unit = (units_list *)malloc(sizeof(units_list));
+    char	name[81];
+    uuid_t	uu;
+
+    if (debug)
+	fprintf(stdout, "Adding new brewsystem %d\n", number);
+    unit->next = NULL;
+    unit->version = 1;
+    unit->uuid = malloc(37);
+    uuid_generate(uu);
+    uuid_unparse(uu, unit->uuid);
+    snprintf(name, 20, "System %d", number);
+    unit->name = xstrcpy(name);
+    unit->number = number;
+    if (number == 1)
+	unit->active = 1;
+    else
+	unit->active = 0;
+    unit->hlt_sensor = unit->mlt_sensor = NULL;
+    unit->hlt_heater = unit->mlt_heater = unit->mlt_pump = NULL;
+    unit->hlt_heater_mltfirst = 1;
+    unit->pump_cycle = 7;
+    unit->pump_rest = 2;
+    unit->pump_premash = 1;
+    unit->pump_onmash = 1;
+    unit->pump_mashout = 0;
+    unit->pump_onboil = 0;
+    unit->pump_stop = 90;
+    unit->skip_add = 0;
+    unit->skip_remove = 0;
+    unit->skip_iodine = 0;
+    unit->iodine_time = 90;
+    unit->whirlpool = 1;
+    unit->PID_hlt = (pid_var *)malloc(sizeof(pid_var));
+    unit->PID_mlt = (pid_var *)malloc(sizeof(pid_var));
+    InitPID(unit->PID_hlt);
+    InitPID(unit->PID_mlt);
+
+    editUnit(unit);
+
+    if (Config.units == NULL) {
+	Config.units = unit;
+    } else {
+	for (tmpu = Config.units; tmpu; tmpu = tmpu->next) {
+	    if (tmpu->next == NULL) {
+		tmpu->next = unit;
+		break;
+	    }
+	}
+    }
+    syslog(LOG_NOTICE, "Brewsystem %d added to the configuration", number);
+    if (debug)
+	fprintf(stdout, "Brewsystem %d added to the configuration\n", number);
+}
+
+
+
 int server(void);
 int server(void)
 {
-    int 	rc = 0, run = 1, key;
-    units_list	*unit;
+    int 		rc = 0, run = 1, key;
+    units_list		*unit;
+    brew_session	*brew = NULL;
 #ifndef HAVE_WIRINGPI_H
-    long	t = 0;
+    long		t = 0;
 #endif
 
 //    if (lockprog((char *)"brewco")) {
@@ -225,8 +298,13 @@
 //	return 1;
 //    }
 
+    if (debug)
+	fprintf(stdout, "Begin server()\n");
+
     if ((rc = devices_detect())) {
 	syslog(LOG_NOTICE, "Detected %d new devices", rc);
+	if (debug)
+	    fprintf(stdout, "Detected %d new devices\n", rc);
 	wrconfig();
     }
 
@@ -245,13 +323,13 @@
     }
 
 #ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_panel_loop);
+    rc = piThreadCreate(my_keyboard_loop);
 #else
-    rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t );
+    rc = pthread_create(&threads[t], NULL, my_keyboard_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);
+	fprintf(stderr, "my_keyboard_loop thread didn't start rc=%d\n", rc);
+	syslog(LOG_NOTICE, "my_keyboard_loop thread didn't start rc=%d", rc);
 #ifndef HAVE_WIRINGPI_H
     } else {
 	t++;
@@ -277,13 +355,19 @@
     /*
      * Initialize units for processing
      */
-//    for (unit = Config.units; unit; unit = unit->next) {
+    for (unit = Config.units; unit; unit = unit->next) {
 	/*
 	 * Safety, turn everything off
 	 */
-//	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0;
-//	unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
-//    }
+	if (unit->active) {
+	    if (unit->hlt_heater)
+	    	unit->hlt_heater->value = 0;
+	    if (unit->mlt_heater)
+	    	unit->mlt_heater->value = 0;
+	    if (unit->mlt_pump)
+		unit->mlt_pump->value = 0;
+	}
+    }
 
     prompt(101);
     if (! Config.units) {
@@ -294,39 +378,49 @@
 	prompt(407);    /* ---  ---   Ok   --- */
 
 	do {
-	    usleep(10000);
-	    slcdDummy(slcdHandle);
-	    key = keycheck();
+	    key = keywait();
 	} while (key != KEY_RETURN);
+
+	if (key == KEY_RETURN) {
+	    addUnit(1);
+	}
     }
 
-    /* Check for a crash and recover */
-
-    prompt(0);
-    prompt(401);
+    /*
+     * During automation there will be a state file:
+     * ~/.brewco/var/brewing.xml
+     * If this file is present, there has been a crash.
+     */
+    brew = (brew_session *)malloc(sizeof(brew_session));
+    if (rdsession(brew) == 0) {
+    } else {
+	/*
+	 * No active brew session, make that permanent.
+	 */
+	free(brew);
+	brew = NULL;
+    }
 
     do {
-	if (my_shutdown)
+	if (my_shutdown) {
 	    run = 0;
-
-	/*
-	 * If there is no unit, setup a unit
-	 */
-	if (! Config.units) {
+	    break;
 	}
 
-	/*
-	 * Select the active unit
-	 */
+	if (brew) {
+	    /*
+	     * Automate mode
+	     */
 
-	/*
-	 * If running, do the things
-	 */
-
-	/*
-	 * Not running, do manual, setup etc
-	 */
-
+	} else {
+	    /*
+	     * Not running.
+	     */
+	    prompt(0);
+	    prompt(101);
+	    prompt(401);
+	    key = keywait();
+	}
 
 	usleep(100000);
 
@@ -401,6 +495,8 @@
 	syslog(LOG_NOTICE, "Error reading configuration: halted");
 	return 1;
     }
+    if (debug)
+	fprintf(stdout, "configuration loaded\n");
 
     /*
      *  Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored

mercurial