Added selecting output relays.

Sun, 06 Dec 2015 20:23:31 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 06 Dec 2015 20:23:31 +0100
changeset 450
8fe99759c27f
parent 449
1277fb94999f
child 451
2247970de278

Added selecting output relays.

brewco/brewco.c file | annotate | diff | comparison | revisions
brewco/brewco.h file | annotate | diff | comparison | revisions
brewco/rdconfig.c file | annotate | diff | comparison | revisions
brewco/setup.c file | annotate | diff | comparison | revisions
--- a/brewco/brewco.c	Sun Dec 06 19:34:39 2015 +0100
+++ b/brewco/brewco.c	Sun Dec 06 20:23:31 2015 +0100
@@ -190,12 +190,9 @@
 	 * Safety, turn everything off
 	 */
 	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;
+	    unit->hlt_heater.value = 0;
+	    unit->mlt_heater.value = 0;
+	    unit->mlt_pump.value = 0;
 	}
     }
 
--- a/brewco/brewco.h	Sun Dec 06 19:34:39 2015 +0100
+++ b/brewco/brewco.h	Sun Dec 06 20:23:31 2015 +0100
@@ -102,7 +102,7 @@
  * Switch structure
  */
 typedef struct _switch_var {
-    char		uuid[37];		/* switch uuid			*/
+    char		*uuid;			/* switch uuid			*/
     int			value;			/* switch value (0..100)	*/
     int			delay;			/* switch delay			*/
 } switch_var;
@@ -122,11 +122,11 @@
     int			number;			/* Unit number			*/
     int			active;			/* 0/1 active unit		*/
     sensor_var		hlt_sensor;		/* HLT sensor			*/
-    switch_var		*hlt_heater;		/* HLT heater			*/
+    switch_var		hlt_heater;		/* HLT heater			*/
     int			hlt_heater_mltfirst;	/* HLT heater MLT high priority	*/
     sensor_var		mlt_sensor;		/* MLT sensor			*/
-    switch_var		*mlt_heater;		/* MLT heater			*/
-    switch_var		*mlt_pump;		/* MLT pump			*/
+    switch_var		mlt_heater;		/* MLT heater			*/
+    switch_var		mlt_pump;		/* MLT pump			*/
     int			pump_cycle;		/* 5..15 minutes 		*/
     int			pump_rest;		/* 1..5 minutes			*/
     int			pump_premash;		/* 0/1				*/
--- a/brewco/rdconfig.c	Sun Dec 06 19:34:39 2015 +0100
+++ b/brewco/rdconfig.c	Sun Dec 06 20:23:31 2015 +0100
@@ -65,12 +65,12 @@
             free(unit->hlt_sensor.uuid);
         if (unit->mlt_sensor.uuid)
             free(unit->mlt_sensor.uuid);
-        if (unit->hlt_heater)
-            free(unit->hlt_heater);
-        if (unit->mlt_heater)
-            free(unit->mlt_heater);
-        if (unit->mlt_pump)
-            free(unit->mlt_pump);
+        if (unit->hlt_heater.uuid)
+            free(unit->hlt_heater.uuid);
+        if (unit->mlt_heater.uuid)
+            free(unit->mlt_heater.uuid);
+        if (unit->mlt_pump.uuid)
+            free(unit->mlt_pump.uuid);
 	if (unit->PID_hlt)
 	    free(unit->PID_hlt);
 	if (unit->PID_mlt)
@@ -286,19 +286,17 @@
                 return 1;
             }
 
-            if (unit->hlt_heater) {
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_UUID", "%s", unit->hlt_heater->uuid)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_VALUE", "%d", unit->hlt_heater->value)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_DELAY", "%d", unit->hlt_heater->delay)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_UUID", "%s", unit->hlt_heater.uuid)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_VALUE", "%d", unit->hlt_heater.value)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_DELAY", "%d", unit->hlt_heater.delay)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
             }
 
 	    if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_MLTFIRST", "%d", unit->hlt_heater_mltfirst)) < 0)) {
@@ -319,34 +317,30 @@
                 return 1;
             }
 
-            if (unit->mlt_heater) {
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_UUID", "%s", unit->mlt_heater->uuid)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_VALUE", "%d", unit->mlt_heater->value)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_DELAY", "%d", unit->mlt_heater->delay)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_UUID", "%s", unit->mlt_heater.uuid)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_VALUE", "%d", unit->mlt_heater.value)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_DELAY", "%d", unit->mlt_heater.delay)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
             }
 
-            if (unit->mlt_pump) {
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_UUID", "%s", unit->mlt_pump->uuid)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_VALUE", "%d", unit->mlt_pump->value)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
-                if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_DELAY", "%d", unit->mlt_pump->delay)) < 0)) {
-                    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-                    return 1;
-                }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_UUID", "%s", unit->mlt_pump.uuid)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_VALUE", "%d", unit->mlt_pump.value)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
+            }
+            if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_PUMP_DELAY", "%d", unit->mlt_pump.delay)) < 0)) {
+                syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+                return 1;
             }
 
 	    if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PUMP_CYCLE", "%d", unit->pump_cycle)) < 0)) {
@@ -730,7 +724,15 @@
     unit->mlt_sensor.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
     unit->mlt_sensor.state = 0;
     unit->mlt_sensor.value = 0;
-    unit->hlt_heater = unit->mlt_heater = unit->mlt_pump = NULL;
+    unit->hlt_heater.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->hlt_heater.value = 0;
+    unit->hlt_heater.delay = 0;
+    unit->mlt_heater.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->mlt_heater.value = 0;
+    unit->mlt_heater.delay = 0;
+    unit->mlt_pump.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->mlt_pump.value = 0;
+    unit->mlt_pump.delay = 0;
     unit->hlt_heater_mltfirst = 0;
     unit->pump_cycle = 7;
     unit->pump_rest = 2;
@@ -796,24 +798,18 @@
         }
 
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_UUID"))) {
-	    if (unit->hlt_heater == NULL) {
-		unit->hlt_heater = (switch_var *)malloc(sizeof(switch_var));
-		unit->hlt_heater->uuid[0] = '\0';
-		unit->hlt_heater->value = 0;
-		unit->hlt_heater->delay = 1;	// 15 seconds
-	    }
-            snprintf(unit->hlt_heater->uuid, 36, "%s", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
+	    unit->hlt_heater.uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
         }
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_VALUE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%d", &ival) == 1)
-		unit->hlt_heater->value = ival;
+		unit->hlt_heater.value = ival;
 	    xmlFree(key);
 	}
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_DELAY"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%d", &ival) == 1)
-                unit->hlt_heater->delay = ival;
+                unit->hlt_heater.delay = ival;
             xmlFree(key);
         }
 
@@ -841,46 +837,34 @@
         }
 
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_UUID"))) {
-	    if (unit->mlt_heater == NULL) {
-		unit->mlt_heater = (switch_var *)malloc(sizeof(switch_var));
-		unit->mlt_heater->uuid[0] = '\0';
-		unit->mlt_heater->value = 0;
-		unit->mlt_heater->delay = 1;    // 15 seconds
-	    }
-	    snprintf(unit->mlt_heater->uuid, 36, "%s", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
+	    unit->mlt_heater.uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
         }
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_VALUE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%d", &ival) == 1)
-		unit->mlt_heater->value = ival;
+		unit->mlt_heater.value = ival;
 	    xmlFree(key);
 	}
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_DELAY"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%d", &ival) == 1)
-                unit->mlt_heater->delay = ival;
+                unit->mlt_heater.delay = ival;
             xmlFree(key);
         }
 
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_PUMP_UUID"))) {
-	    if (unit->mlt_pump == NULL) {
-		unit->mlt_pump = (switch_var *)malloc(sizeof(switch_var));
-		unit->mlt_pump->uuid[0] = '\0';
-		unit->mlt_pump->value = 0;
-		unit->mlt_pump->delay = 1;    // 15 seconds
-	    }
-	    snprintf(unit->mlt_pump->uuid, 36, "%s", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
+	    unit->mlt_pump.uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
         }
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_PUMP_VALUE"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%d", &ival) == 1)
-		unit->mlt_pump->value = ival;
+		unit->mlt_pump.value = ival;
 	    xmlFree(key);
 	}
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_PUMP_DELAY"))) {
             key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
             if (sscanf((const char *)key, "%d", &ival) == 1)
-                unit->mlt_pump->delay = ival;
+                unit->mlt_pump.delay = ival;
             xmlFree(key);
         }
 
--- a/brewco/setup.c	Sun Dec 06 19:34:39 2015 +0100
+++ b/brewco/setup.c	Sun Dec 06 20:23:31 2015 +0100
@@ -394,8 +394,111 @@
 
 
 
-void editRelay(switch_var *relay, char *text)
+void editRelay(char *uuid, char *text)
 {
+    char                pmpt[81];
+    int                 i, old, choices, index = 1, key;
+    devices_list        *device;
+
+    if (debug)
+        fprintf(stdout, "editRelay(%s, %s)\n", uuid, text);
+
+    old = 1;  //             1d0e5bb8-7408-48b9-abb4-e9041d7b99fe
+    if ((i = strcmp((char *)"00000000-0000-0000-0000-000000000000", uuid))) {
+        for (device = Config.devices; device; device = device->next) {
+            if (device->direction == DEVDIR_OUT_ANALOG) {
+                old++;
+                if (strcmp(device->uuid, uuid) == 0)
+                    break;
+            }
+        }
+    }
+
+    if (debug)
+        fprintf(stdout, "editRelay(%s) old sensor index=%d\n", text, old);
+
+    for (;;) {
+        prompt(0, NULL);
+        snprintf(pmpt, Config.lcd_cols + 1, "Edit %s", text);
+        prompt(100, pmpt);
+
+        /*
+         * Count valid sensors
+         */
+        choices = 1;
+        if (old == 1) {
+            snprintf(pmpt, Config.lcd_cols + 1, "N/A");
+        }
+        for (device = Config.devices; device; device = device->next) {
+            if (device->direction == DEVDIR_OUT_ANALOG) {
+                choices++;
+                if (choices == old) {
+                    snprintf(pmpt, Config.lcd_cols + 1, "%s", device->description);
+                }
+            }
+        }
+        prompt(200, pmpt);      /* Display current relay */
+        i = 1;
+        if (index == 1) {
+            snprintf(pmpt, Config.lcd_cols + 1, "N/A");
+        } else {
+            for (device = Config.devices; device; device = device->next) {
+                if (device->direction == DEVDIR_OUT_ANALOG) {
+                    i++;
+                    if (i == index) {
+                        snprintf(pmpt, Config.lcd_cols + 1, "%s", device->description);
+                    }
+                }
+            }
+        }
+        prompt(300, pmpt);      /* Display possible new relay */
+
+        if (choices == 1)
+            prompt(405, NULL);
+        else if (index == 1)
+            prompt(402, NULL);
+        else if (index == choices)
+            prompt(404, NULL);
+        else
+            prompt(403, NULL);
+
+        key = keywait();
+        if ((key == KEY_RETURN) || my_shutdown) {
+            if (debug)
+                fprintf(stdout, "End editRelay\n");
+            return;
+        }
+
+        if ((key == KEY_UP) && (index > 1))
+            index--;
+        if ((key == KEY_DOWN) && (index < choices))
+            index++;
+
+        if ((key == KEY_ENTER) && (index != old)) {
+            /*
+             * Select new output.
+             */
+            if (index == 1) {
+                /*
+                 * Disable the output
+                 */
+                strncpy(uuid, (char *)"00000000-0000-0000-0000-000000000000", 36);
+                old = index;
+            } else {
+                i = 1;
+                for (device = Config.devices; device; device = device->next) {
+                    if (device->direction == DEVDIR_OUT_ANALOG) {
+                        i++;
+                        if (i == index) {
+                            strncpy(uuid, device->uuid, 36);
+                            break;
+                        }
+                    }
+                }
+                old = index;
+            }
+        }
+    }
 }
 
 
@@ -520,16 +623,25 @@
 				strncpy(unit->hlt_sensor.uuid, uuid, 36);
 				free(uuid);
 				break;
-		case 3:		editRelay(unit->hlt_heater, (char *)"HLT heater");
+		case 3:		uuid = xstrcpy(unit->hlt_heater.uuid);
+				editRelay(uuid, (char *)"HLT heater");
+				strncpy(unit->hlt_heater.uuid, uuid, 36);
+				free(uuid);
 				break;
 		case 4:		uuid = xstrcpy(unit->mlt_sensor.uuid);
 				editSensor(uuid, (char *)"MLT sensor");
 				strncpy(unit->mlt_sensor.uuid, uuid, 36);
 				free(uuid);
 				break;
-		case 5:		editRelay(unit->mlt_heater, (char *)"MLT heater");
+		case 5:		uuid = xstrcpy(unit->mlt_heater.uuid);
+				editRelay(uuid, (char *)"MLT heater");
+				strncpy(unit->mlt_heater.uuid, uuid, 36);
+				free(uuid);
 				break;
-		case 6:		editRelay(unit->mlt_pump, (char *)"MLT pump");
+		case 6:		uuid = xstrcpy(unit->mlt_pump.uuid);
+				editRelay(uuid, (char *)"MLT pump");
+				strncpy(unit->mlt_pump.uuid, uuid, 36);
+				free(uuid);
 				break;
 		case 7:		toggleYesNo(&unit->hlt_heater_mltfirst, (char *)"MLT heat b4 HLT");
 				break;
@@ -595,7 +707,15 @@
     unit->mlt_sensor.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
     unit->mlt_sensor.state = DEVPRESENT_UNDEF;
     unit->mlt_sensor.value = 0;
-    unit->hlt_heater = unit->mlt_heater = unit->mlt_pump = NULL;
+    unit->hlt_heater.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->hlt_heater.value = 0;
+    unit->hlt_heater.delay = 0;
+    unit->mlt_heater.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->mlt_heater.value = 0;
+    unit->mlt_heater.delay = 0;
+    unit->mlt_pump.uuid = xstrcpy((char *)"00000000-0000-0000-0000-000000000000");
+    unit->mlt_pump.value = 0;
+    unit->mlt_pump.delay = 0;
     unit->hlt_heater_mltfirst = 1;
     unit->pump_cycle = 7;
     unit->pump_rest = 2;

mercurial