Always use uuid to select units and remove numeric record id's.

Thu, 24 Jul 2014 20:45:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 24 Jul 2014 20:45:15 +0200
changeset 131
528dc0bb81ab
parent 130
25158b08843f
child 132
8bd209d1c020

Always use uuid to select units and remove numeric record id's.

thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
www-thermferm/getprofiles.php file | annotate | diff | comparison | revisions
www-thermferm/getunits.php file | annotate | diff | comparison | revisions
www-thermferm/index.php file | annotate | diff | comparison | revisions
www-thermferm/js/maintenance_panel.js file | annotate | diff | comparison | revisions
www-thermferm/liveview.php file | annotate | diff | comparison | revisions
--- a/thermferm/server.c	Thu Jul 24 17:42:34 2014 +0200
+++ b/thermferm/server.c	Thu Jul 24 20:45:15 2014 +0200
@@ -28,7 +28,7 @@
 
 extern int		my_shutdown;
 extern int		debug;
-extern int		current_unit;
+extern char		*current_unit;
 #ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
 extern unsigned char	lcdbuf[MAX_LCDS][20][4];
@@ -101,7 +101,6 @@
     profiles_list	*profile, *tmpp;
     uuid_t		uu;
     char		*opt, *param;
-    int			id = 1;
 
     opt = strtok(buf, " \0");
     opt = strtok(NULL, " \0");
@@ -151,6 +150,9 @@
     	unit->uuid = malloc(37);
     	uuid_generate(uu);
     	uuid_unparse(uu, unit->uuid);
+	if (current_unit)
+	    free(current_unit);
+	current_unit = xstrcpy(unit->uuid);
     	unit->name = xstrcpy(param);
     	unit->air_address = unit->beer_address = unit->io1_address = unit->io2_address = unit->profile = NULL;
     	unit->volume = 0.0;
@@ -169,7 +171,6 @@
 	    Config.units = unit;
     	} else {
 	    for (tmpu = Config.units; tmpu; tmpu = tmpu->next) {
-	    	id++;
 	    	if (tmpu->next == NULL) {
 		    tmpu->next = unit;
 		    break;
@@ -177,9 +178,8 @@
 	    }
     	}
 
-    	syslog(LOG_NOTICE, "Unit %d with uuid %s added", id, unit->uuid);
-    	srv_send((char *)"211 Unit %d with uuid %s added", id, unit->uuid);
-    	current_unit = id;
+    	syslog(LOG_NOTICE, "Unit with uuid %s added", unit->uuid);
+    	srv_send((char *)"211 Unit with uuid %s added", unit->uuid);
     	return 0;
     }
 
@@ -202,7 +202,7 @@
     units_list		*unit;
     profiles_list	*profile;
     prof_step		*step;
-    int			i, j, ref;
+    int			j, ref;
     DIR			*fd;
     FILE		*fp;
     struct dirent	*de;
@@ -215,10 +215,8 @@
 	 * Default, list available units
 	 */
 	srv_send((char *)"212 Fermenter list follows:");
-	i = 0;
 	for (unit = Config.units; unit; unit = unit->next) {
-	    i++;
-	    srv_send((char *)"%d,%s,%s,%s", i, unit->uuid, unit->name, UNITMODE[unit->mode]);
+	    srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
 	}
 	srv_send((char *)".");
 	return 0;
@@ -287,16 +285,14 @@
 	/*
 	 * Get the logfile data and emit only one line per hour.
 	 */
-	if (current_unit == -1) {
+	if (current_unit == NULL) {
 	    srv_send((char *)"401 No fermenter unit selected");
 	    return 1;
 	}
 
-	i = 0;
 	q[0] = q[1] = 'a';
 	for (unit = Config.units; unit; unit = unit->next) {
-	    i++;
-	    if (i == current_unit)
+	    if (strcmp(current_unit, unit->uuid) == 0)
 		break;
 	}
 
@@ -332,13 +328,11 @@
 	 * Fermenting profiles
 	 */
 	srv_send((char *)"212 profiles:");
-	i = 0;
 	for (profile = Config.profiles; profile; profile = profile->next) {
-	    i++;
 	    j = 0;
 	    for (step = profile->steps; step; step = step->next)
 		j++;
-	    srv_send((char *)"%d,%s,%s,%d", i, profile->uuid, profile->name, j);
+	    srv_send((char *)"%s,%s,%d", profile->uuid, profile->name, j);
 	}
 	srv_send((char *)".");
 	return 0;
@@ -347,16 +341,14 @@
 	/*
 	 * List configured and selected fermenter unit
 	 */
-	if (current_unit == -1) {
+	if (current_unit == NULL) {
 	    srv_send((char *)"401 No fermenter unit selected");
 	    return 1;
 	}
 
-	srv_send((char *)"213 Unit %d listing follows:", current_unit);
-	i = 0;
+	srv_send((char *)"213 Unit %s listing follows:", current_unit);
 	for (unit = Config.units; unit; unit = unit->next) {
-	    i++;
-	    if (i == current_unit) {
+	    if (strcmp(current_unit, unit->uuid) == 0) {
 		srv_send((char *)"NAME,%s", unit->name);
 		srv_send((char *)"UUID,%s", unit->uuid);
 		if (unit->air_address) {
@@ -432,7 +424,7 @@
 {
     char                *opt, *param;
     units_list          *unit;
-    int                 i, rc;
+    int                 rc;
     float		fval;
 
     opt = strtok(buf, " \0");
@@ -456,15 +448,13 @@
     /*
      * Commands below need a selected unit
      */
-    if (current_unit == -1) {
+    if (current_unit == NULL) {
 	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)
+	if (strcmp(current_unit, unit->uuid) == 0)
 	    break;
     }
 
@@ -475,7 +465,7 @@
 	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);
+	    	srv_send((char *)"214 Unit %s 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);
@@ -484,7 +474,7 @@
 	} 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);
+	    	srv_send((char *)"214 Unit %s 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);
@@ -496,7 +486,7 @@
 	     */
 	    if ((fval >= 0.0) && (fval <=  77020.0)) {
 	    	unit->volume = fval;
-	    	srv_send((char *)"214 Unit %d VOLUME set to %.1f", current_unit, fval);
+	    	srv_send((char *)"214 Unit %s VOLUME set to %.1f", current_unit, fval);
 	    	return 0;
 	    } else {
 		srv_send((char *)"510 New volume not between 0 and 77020");
@@ -505,7 +495,7 @@
 	} 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);
+		srv_send((char *)"214 Unit %s IDLE_LOW set to %.1f", current_unit, fval);
 		return 0;
 	    } else {
 		srv_send((char *)"510 New value not between -5.0 and -0.1");
@@ -514,7 +504,7 @@
 	} 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);
+		srv_send((char *)"214 Unit %s IDLE_HIGH set to %.1f", current_unit, fval);
 		return 0;
 	    } else {
 		srv_send((char *)"510 New value not between -5.0 and -0.1");
@@ -523,7 +513,7 @@
 	} 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);
+		srv_send((char *)"214 Unit %s 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");
@@ -532,7 +522,7 @@
 	} 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);
+		srv_send((char *)"214 Unit %s 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");
@@ -548,7 +538,7 @@
 	if (unit->name)
 	    free(unit->name);
 	unit->name = xstrcpy(param);
-	srv_send((char *)"214 Unit %d NAME set to '%s'", current_unit, param);
+	srv_send((char *)"214 Unit %s NAME set to '%s'", current_unit, param);
 	// TODO: change logfile name
 	return 0;
     } else if (strcmp(opt, (char *)"PROFILE") == 0) {
@@ -562,14 +552,12 @@
 
 
 /*
- * UNIT n
  * UNIT uuid
  */
 int cmd_unit(char *buf)
 {
     char                *opt;
     units_list          *tmp;
-    int                 i, unit_no;
 
     opt = strtok(buf, " \0");
     opt = strtok(NULL, " \0");
@@ -579,32 +567,16 @@
 	return 1;
     }
 
-    i = 0;
     if (strlen(opt) == 36) {
 	/*
 	 * Search using uuid
 	 */
 	for (tmp = Config.units; tmp; tmp = tmp->next) {
-	    i++;
 	    if (strcmp(opt, tmp->uuid) == 0) {
-		srv_send((char *)"210 Unit %d selected", i);
-		current_unit = i;
-		return 0;
-	    }
-	}
-	srv_send((char *)"410 No such unit");
-	return 1;
-    }
-
-    if (sscanf(opt, "%d", &unit_no) == 1) {
-	/*
-	 * We got a number, see if it is valid.
-	 */
-	for (tmp = Config.units; tmp; tmp = tmp->next) {
-	    i++;
-	    if (unit_no == i) {
-		srv_send((char *)"210 Unit %d selected", i);
-		current_unit = i;
+		srv_send((char *)"210 Unit %s selected", tmp->uuid);
+		if (current_unit)
+		    free(current_unit);
+		current_unit = xstrcpy(tmp->uuid);;
 		return 0;
 	    }
 	}
@@ -678,11 +650,11 @@
 		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 PROFILE uuid,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 val            Set unit volume");
-		srv_send((char *)"UNIT n|uuid               Select unit by number or uuid");
+		srv_send((char *)"UNIT uuid                 Select unit by uuid");
 		srv_send((char *)".");
 	    } else if (strncmp(buf, "LCD", 3) == 0) {
 #ifdef HAVE_WIRINGPI_H
--- a/thermferm/thermferm.c	Thu Jul 24 17:42:34 2014 +0200
+++ b/thermferm/thermferm.c	Thu Jul 24 20:45:15 2014 +0200
@@ -33,7 +33,7 @@
 #include "xutil.h"
 
 
-int			current_unit = -1;
+char			*current_unit = NULL;
 int			tempA = 80;
 int			tempB = 80;
 
--- a/www-thermferm/getprofiles.php	Thu Jul 24 17:42:34 2014 +0200
+++ b/www-thermferm/getprofiles.php	Thu Jul 24 20:45:15 2014 +0200
@@ -53,7 +53,7 @@
 	if ($j > 1)
 	    $row .= ',';
 	$f = explode(",", $arr[$j]);
-	$row .= '{"Id":"'.$f[0].'","UUID":"'.$f[1].'","Name":"'.$f[2].'","Steps":"'.$f[3].'"}';
+	$row .= '{"UUID":"'.$f[0].'","Name":"'.$f[1].'","Steps":"'.$f[2].'"}';
 	$j++;
     }
 }
--- a/www-thermferm/getunits.php	Thu Jul 24 17:42:34 2014 +0200
+++ b/www-thermferm/getunits.php	Thu Jul 24 20:45:15 2014 +0200
@@ -53,7 +53,7 @@
 	if ($j > 1)
 	    $row .= ',';
 	$f = explode(",", $arr[$j]);
-	$row .= '{"Id":"'.$f[0].'","UUID":"'.$f[1].'","Name":"'.$f[2].'","Mode":"'.$f[3].'"}';
+	$row .= '{"UUID":"'.$f[0].'","Name":"'.$f[1].'","Mode":"'.$f[2].'"}';
 	$j++;
     }
 }
--- a/www-thermferm/index.php	Thu Jul 24 17:42:34 2014 +0200
+++ b/www-thermferm/index.php	Thu Jul 24 20:45:15 2014 +0200
@@ -51,6 +51,7 @@
 $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxscrollbar.js"></script>'.PHP_EOL;
 $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxgrid.js"></script>'.PHP_EOL;
 $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxgrid.selection.js"></script>'.PHP_EOL;
+$outstr .= '  <script type="text/javascript" src="jqwidgets/jqxgrid.edit.js"></script>'.PHP_EOL;
 $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxgrid.columnsresize.js"></script>'.PHP_EOL;
 $outstr .= ' </head>'.PHP_EOL;
 
--- a/www-thermferm/js/maintenance_panel.js	Thu Jul 24 17:42:34 2014 +0200
+++ b/www-thermferm/js/maintenance_panel.js	Thu Jul 24 20:45:15 2014 +0200
@@ -54,7 +54,6 @@
     var source_profiles = {
         datatype: "json",
         datafields: [
-            { name: 'Id', type: 'int' },
             { name: 'UUID', type: 'string' },
             { name: 'Name', type: 'string' },
             { name: 'Steps', type: 'int' }
@@ -67,9 +66,9 @@
         source: dataAdapter_profiles,
         theme: 'ui-redmond',
         columnsresize: true,
+	editable: true,
         columns: [
-            { text: 'Id', datafield: 'Id', width: 40 },
-            { text: 'UUID', datafield: 'UUID', width: 280 },
+            { text: 'UUID', editable: false, datafield: 'UUID', width: 280 },
             { text: 'Name', datafield: 'Name', minwidth: 120 },
             { text: 'Steps', datafield: 'Steps', width: 50 }
         ]
@@ -81,7 +80,6 @@
     var source_units = {
         datatype: "json",
         datafields: [
-            { name: 'Id', type: 'int' },
             { name: 'UUID', type: 'string' },
             { name: 'Name', type: 'string' },
             { name: 'Mode', type: 'string' }
@@ -94,9 +92,9 @@
         source: dataAdapter_units,
         theme: 'ui-redmond',
         columnsresize: true,
+	editable: true,
         columns: [
-            { text: 'Id', datafield: 'Id', width: 40 },
-            { text: 'UUID', datafield: 'UUID', width: 280 },
+            { text: 'UUID', editable: false, datafield: 'UUID', width: 280 },
             { text: 'Name', datafield: 'Name', minwidth: 120 },
             { text: 'Mode', datafield: 'Mode', width: 70 }
         ]
--- a/www-thermferm/liveview.php	Thu Jul 24 17:42:34 2014 +0200
+++ b/www-thermferm/liveview.php	Thu Jul 24 20:45:15 2014 +0200
@@ -291,7 +291,7 @@
 		if (strcmp($arr[$i], ".") == 0)
 			break;
 		$parts = explode(",", $arr[$i]);
-		$outstr .= '     <li style="margin-left: 20px;">'.$parts[2].'</li>'.PHP_EOL;
+		$outstr .= '     <li style="margin-left: 20px;">'.$parts[1].'</li>'.PHP_EOL;
 		$i++;
 	    }
 	}
@@ -303,7 +303,7 @@
 		if (strcmp($arr[$i], ".") == 0)
 		    break;
 		$parts = explode(",", $arr[$i]);
-		$outstr .= showunit($parts[1], $i);
+		$outstr .= showunit($parts[0], $i);
 		$i++;
 	    }
 	}

mercurial