Added SET commands

Wed, 02 Jul 2014 22:09:35 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 02 Jul 2014 22:09:35 +0200
changeset 88
12259362595b
parent 87
55f1315c94f1
child 89
8bab04a663dd

Added SET commands

thermferm/server.c file | annotate | diff | comparison | revisions
--- a/thermferm/server.c	Wed Jul 02 17:57:06 2014 +0200
+++ b/thermferm/server.c	Wed Jul 02 22:09:35 2014 +0200
@@ -106,6 +106,9 @@
 
 
 
+/*
+ * ADD name
+ */
 int unit_add(char *buf)
 {
     units_list	*unit, *tmp;
@@ -277,7 +280,7 @@
 		    }
 		    srv_send((char *)"Unit mode                  %s", UNITMODE[unit->mode]);
 		    srv_send((char *)"Fridge temperature set to  %.1f", unit->fridge_set);
-		    srv_send((char *)"Beer temperature set to    %.1f", unit->fridge_set);
+		    srv_send((char *)"Beer temperature set to    %.1f", unit->beer_set);
 		    if (unit->profile) {
 			srv_send((char *)"Profile name               %s", unit->profile);
 		    }
@@ -297,6 +300,153 @@
 
 
 /*
+ * Set new operating mode
+ * MODE OFF|NONE|BEER|FRIDGE|PROFILE
+ */
+int cmd_mode(char *buf)
+{
+    return 1;
+}
+
+
+
+/*
+ * SET commands
+ */
+int cmd_set(char *buf)
+{
+    char                *opt, *param;
+    units_list          *unit;
+    int                 i, rc;
+    float		fval;
+
+    opt = strtok(buf, " \0");
+    opt = strtok(NULL, " \0");
+    if (opt == NULL) {
+	srv_send((char *)"501 Subcommand missing");
+	return 1;
+    }
+
+    param = strtok(NULL, "\0");
+    if (param == NULL) {
+	srv_send((char *)"501 Parameter missing");
+	return 1;
+    }
+
+    rc = sscanf(param, "%f", &fval);
+
+    if (debug)
+	fprintf(stdout, "opt='%s' param='%s' rc=%d fval=%.1f\n", opt, param, rc, fval);
+
+    /*
+     * Commands below need a selected unit
+     */
+    if (current_unit == -1) {
+	srv_send((char *)"401 No fermenter unit selected");
+	return 1;
+    }
+
+    i = 0;
+    for (unit = Config.units; unit; unit = unit->next) {
+	i++;
+	if (i == current_unit)
+	    break;
+    }
+
+    /*
+     * If a valid parameter float value
+     */
+    if (rc == 1) {
+	if (strcmp(opt, (char *)"BEER") == 0) {
+	    if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) {
+	    	unit->beer_set = fval;
+	    	srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval);
+	    	return 0;
+	    } else {
+		srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max);
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"FRIDGE") == 0) {
+	    if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) {
+	    	unit->fridge_set = fval;
+	    	srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval);
+	    	return 0;
+	    } else {
+		srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max);
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"VOLUME") == 0) {
+	    /*
+	     * Must fit in a 2 TEU container
+	     */
+	    if ((fval >= 0.0) && (fval <=  77020.0)) {
+	    	unit->volume = fval;
+	    	srv_send((char *)"214 Unit %d VOLUME set to %.1f", current_unit, fval);
+	    	return 0;
+	    } else {
+		srv_send((char *)"510 New volume not between 0 and 77020");
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"IDLE_LOW") == 0) {
+	    if ((fval >= -5.0) && (fval <=  -0.1)) {
+		unit->idle_rangeL = fval;
+		srv_send((char *)"214 Unit %d IDLE_LOW set to %.1f", current_unit, fval);
+		return 0;
+	    } else {
+		srv_send((char *)"510 New value not between -5.0 and -0.1");
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"IDLE_HIGH") == 0) {
+	   if ((fval >= 0.1) && (fval <=  5.0)) {
+		unit->idle_rangeH = fval;
+		srv_send((char *)"214 Unit %d IDLE_HIGH set to %.1f", current_unit, fval);
+		return 0;
+	    } else {
+		srv_send((char *)"510 New value not between -5.0 and -0.1");
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"TEMP_MIN") == 0) {
+	    if ((fval >= -2.0) && (fval <= 35.0) && (fval < unit->temp_set_max)) {
+		unit->temp_set_min = fval;
+		srv_send((char *)"214 Unit %d TEMP_MIN set to %.1f", current_unit, fval);
+		return 0;
+	    } else {
+		srv_send((char *)"510 New value not between -2.0 and 35.0 and lower then TEMP_MAX");
+		return 1;
+	    }
+	} else if (strcmp(opt, (char *)"TEMP_MAX") == 0) {
+	    if ((fval >= -2.0) && (fval <= 35.0) && (fval > unit->temp_set_min)) {
+		unit->temp_set_max = fval;
+		srv_send((char *)"214 Unit %d TEMP_MAX set to %.1f", current_unit, fval);
+		return 0;
+	    } else {
+		srv_send((char *)"510 New value not between -2.0 and 35.0 and higher then TEMP_MIN");
+		return 1;
+	    }
+	}
+    }
+
+    /*
+     * Set new unit or beer name
+     */
+    if (strcmp(opt, (char *)"NAME") == 0) {
+	if (unit->name)
+	    free(unit->name);
+	unit->name = xstrcpy(param);
+	srv_send((char *)"214 Unit %d NAME set to '%s'", current_unit, param);
+	// TODO: change logfile name
+	return 0;
+    } else if (strcmp(opt, (char *)"PROFILE") == 0) {
+	// check profile file, excists, type, at least one profile item
+    }
+
+    srv_send((char *)"502 Unknown command option");
+    return 1;
+}
+
+
+
+/*
  * UNIT n
  * UNIT uuid
  */
@@ -408,20 +558,18 @@
 		srv_send((char *)"LIST BUS                  List 1-wire bus");
 		srv_send((char *)"LIST PROFILES             List available profiles");
 		srv_send((char *)"LIST UNIT                 List fermenter unit");
-//		srv_send((char *)"MODE off|none|beer|fridge|profile");
+		srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
 //		srv_send((char *)"PROFILE                   Profile status");
 //		srv_send((char *)"PROFILE start|stop|pause  Profile start, stop or pause");
-//		srv_send((char *)"SET BEER                  Set beer temperature");
-//		srv_send((char *)"SET FRIDGE                Set fridge temperature");
-//		srv_send((char *)"SET IDLE LOW val          Set idle temperature low");
-//		srv_send((char *)"SET IDLE HIGH val         Set idle temperature high");
-//		srv_send((char *)"SET NAME name             Set name or beername for the unit");
-//		srv_send((char *)"SET NONE                  Set unit to none, but still logs");
-//		srv_send((char *)"SET OFF                   Set unit off");
+		srv_send((char *)"SET BEER val              Set beer temperature");
+		srv_send((char *)"SET FRIDGE val            Set fridge temperature");
+		srv_send((char *)"SET IDLE_LOW val          Set idle temperature low (-5.0 .. -0.1)");
+		srv_send((char *)"SET IDLE_HIGH val         Set idle temperature high (0.1 .. 5.0)");
+		srv_send((char *)"SET NAME name             Set name or beername for the unit");
 //		srv_send((char *)"SET PROFILE name          Set named profile");
-//		srv_send((char *)"SET TEMP MIN val          Set unit minimum temperature");
-//		srv_send((char *)"SET TEMP MAX val          Set unit maximum temperature");
-//		srv_send((char *)"SET VOLUME                Set unit volume");
+		srv_send((char *)"SET TEMP_MIN val          Set unit minimum temperature");
+		srv_send((char *)"SET TEMP_MAX val          Set unit maximum temperature");
+		srv_send((char *)"SET VOLUME val            Set unit volume");
 		srv_send((char *)"UNIT n|uuid               Select unit by number or uuid");
 		srv_send((char *)".");
 	    } else if (strncmp(buf, "LCD", 3) == 0) {
@@ -440,6 +588,10 @@
 #endif
 	    } else if (strncmp(buf, "LIST", 4) == 0) {
 		cmd_list(buf);
+	    } else if (strncmp(buf, "MODE", 4) == 0) {
+		cmd_mode(buf);
+	    } else if (strncmp(buf, "SET", 3) == 0) {
+		cmd_set(buf);
 	    } else if (strncmp(buf, "UNIT", 4) == 0) {
 		cmd_unit(buf);
 	    } else if (strncmp(buf, "ack", 3) == 0) {

mercurial