Client/Server communication is now almost final

Thu, 07 Aug 2014 19:38:30 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 07 Aug 2014 19:38:30 +0200
changeset 191
c74bbc24a1c8
parent 190
6512a8256407
child 192
5d013b4a9138

Client/Server communication is now almost final

thermferm/rdconfig.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
www-thermferm/getlog.php file | annotate | diff | comparison | revisions
www-thermferm/profiles.php file | annotate | diff | comparison | revisions
--- a/thermferm/rdconfig.c	Thu Aug 07 16:44:53 2014 +0200
+++ b/thermferm/rdconfig.c	Thu Aug 07 19:38:30 2014 +0200
@@ -434,6 +434,10 @@
 		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 		return 1;
 	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "INITTEMP", "%.1f", tmp4->inittemp)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
 	    if (tmp4->steps) {
 		if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "STEPS")) < 0) {
 		    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
@@ -924,12 +928,14 @@
     xmlChar     	*key;
     profiles_list	*profile, *tmp;
     int			ival;
+    float		fval;
 
     profile = (profiles_list *)malloc(sizeof(profiles_list));
     profile->next = NULL;
     profile->version = 1;
     profile->uuid = profile->name = NULL;
     profile->busy = 0;
+    profile->inittemp = 20.0;
     profile->steps = NULL;
 
     cur = cur->xmlChildrenNode;
@@ -955,6 +961,12 @@
 		profile->busy = ival;
 	    xmlFree(key);
 	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"INITTEMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%f", &fval) == 1)
+		profile->inittemp = fval;
+	    xmlFree(key);
+	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEPS"))) {
 	    parseSteps(doc, cur, &(profile)->steps);
 	}
--- a/thermferm/server.c	Thu Aug 07 16:44:53 2014 +0200
+++ b/thermferm/server.c	Thu Aug 07 19:38:30 2014 +0200
@@ -114,66 +114,7 @@
 
 
 
-/*
- * ADD PROFILE name
- */
-int unit_add(char *buf)
-{
-    profiles_list	*profile, *tmpp;
-    uuid_t		uu;
-    char		*opt, *param;
-
-    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;
-    }
-
-    if (debug)
-	fprintf(stdout, "opt: '%s' param: '%s'\n", MBSE_SS(opt), MBSE_SS(param));
-
-    if (strcmp(opt, (char *)"PROFILE") == 0) {
-	profile = (profiles_list *)malloc(sizeof(profiles_list));
-	profile->next = NULL;
-	profile->version = 1;
-	profile->uuid = malloc(37);
-	uuid_generate(uu);
-	uuid_unparse(uu, profile->uuid);
-	profile->name = xstrcpy(param);
-	profile->busy = 0;
-	profile->steps = NULL;
-
-	if (Config.profiles == NULL) {
-	    Config.profiles = profile;
-	} else {
-	    for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) {
-		if (tmpp->next == NULL) {
-		    tmpp->next = profile;
-		    break;
-		}
-	    }
-	}
-
-	syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid);
-	srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid);
-	return 0;
-    }
-
-    srv_send((char *)"502 Unknown command option");
-    return 1;
-}
-
-
-
-
-void delete_Profile(char *uuid)
+int delete_Profile(char *uuid)
 {
     profiles_list	*current = Config.profiles;
     profiles_list	*previous = NULL;
@@ -195,7 +136,7 @@
 		    current->steps = NULL;
 		}
 		free(current);
-		return;
+		return 1;
 	    } else {
 		free(current->uuid);
 		current->uuid = NULL;
@@ -211,48 +152,15 @@
 		previous->next = current->next;
 		free(current);
 		current = previous->next;
-		return;
+		return 1;
 	    }
 	} else {
 	    previous = current;
 	    current = current->next;
 	}
-    }    
-}
-
-
-
-/*
- * DEL PROFILE
- */
-int cmd_del(char *buf)
-{
-    char                *opt, *param;
-
-    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;
-    }
-
-    if (debug)
-	fprintf(stdout, "opt: '%s' param: '%s'\n", MBSE_SS(opt), MBSE_SS(param));
-
-    if (strcmp(opt, (char *)"PROFILE") == 0) {
-	delete_Profile(param);
-	srv_send((char *)"211 Profile %s deleted", param);
-	return 0;
-    }
-
-    srv_send((char *)"502 Unknown command option");
-    return 1;
+    return 0;
 }
 
 
@@ -523,19 +431,15 @@
 /*
  * LIST
  * LIST LOG
- * LIST PROFILES
  */
 int cmd_list(char *buf)
 {
-    char		*opt, *filename, *p, q[2], buffer[256];
+    char		*opt, *param, *filename, *p, q[2], buffer[256];
     units_list		*unit;
-    profiles_list	*profile;
-    prof_step		*step;
-    int			j;
     FILE		*fp;
 
     opt = strtok(buf, " \0");
-    opt = strtok(NULL, "\0");
+    opt = strtok(NULL, " \0");
 
     if (opt == NULL) {
 	/*
@@ -549,17 +453,16 @@
 	return 0;
 
     } else if (strcmp(opt, (char *)"LOG") == 0) {
-	/*
-	 * Get the logfile data and emit only one line per hour.
-	 */
-	if (current_unit == NULL) {
-	    srv_send((char *)"401 No fermenter unit selected");
+
+	param = strtok(NULL, "\0");
+	if (param == NULL) {
+	    srv_send((char *)"501 Parameter missing");
 	    return 1;
 	}
 
 	q[0] = q[1] = 'a';
 	for (unit = Config.units; unit; unit = unit->next) {
-	    if (strcmp(current_unit, unit->uuid) == 0)
+	    if (strcmp(param, unit->uuid) == 0)
 		break;
 	}
 
@@ -589,8 +492,41 @@
 	filename = NULL;
 	srv_send((char *)".");
 	return 0;
+    }
 
-    } else if (strcmp(opt, (char *)"PROFILES") == 0) {
+    srv_send((char *)"502 Unknown command option");
+    return 1;
+}
+
+
+
+/*
+ * PROFILE ADD name	Add a new profile
+ * PROFILE DEL uuid	Delete profile with uuid
+ * PROFILE LIST		List available profiles
+ * PROFILE GETS uuid	Get profile steps list
+ * PROFILE PUTS uuid	Put profile steps list
+ */
+int cmd_profile(char *buf)
+{
+    char                ibuf[SS_BUFSIZE], *sstep, *rest, *targ, *param;
+    int                 i, j, rlen, istep, irest;
+    float		ftarg;
+    socklen_t           fromlen;
+    char		*opt;
+    profiles_list	*profile, *tmpp;
+    prof_step		*step, *olds;
+    uuid_t		uu;
+
+    opt = strtok(buf, " \0");
+    opt = strtok(NULL, " \0");
+
+    if (opt == NULL) {
+	srv_send((char *)"501 Subcommand missing");
+	return 1;
+    }
+
+    if (strcmp(opt, (char *)"LIST") == 0) {
 	/*
 	 * Fermenting profiles
 	 */
@@ -602,72 +538,56 @@
 	    srv_send((char *)"%s,%s,%d,%d", profile->uuid, profile->name, j, profile->busy);
 	}
 	srv_send((char *)".");
-	return 0;
+	return 1;
+    }
+
+    param = strtok(NULL, "\0");
+    if (param == NULL) {
+	srv_send((char *)"501 Parameter missing");
+	return 1;
     }
 
-    srv_send((char *)"502 Unknown command option");
-    return 1;
-}
+    if (strcmp(opt, (char *)"ADD") == 0) {
+	profile = (profiles_list *)malloc(sizeof(profiles_list));
+	profile->next = NULL;
+	profile->version = 1;
+	profile->uuid = malloc(37);
+	uuid_generate(uu);
+	uuid_unparse(uu, profile->uuid);
+	profile->name = xstrcpy(param);
+	profile->busy = 0;
+	profile->inittemp = 20.0;
+	profile->steps = NULL;
+	if (Config.profiles == NULL) {
+	    Config.profiles = profile;
+	} else {
+	    for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) {
+		if (tmpp->next == NULL) {
+		    tmpp->next = profile;
+		    break;
+		}
+	    }
+	}
 
+	syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid);
+	srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid);
+	return 0;
 
 
-/*
- * PROFILE		List profile status of current unit
- * PROFILE uuid,name	Rename profile name
- * PROFILE GETS uuid	Get profile steps list
- * PROFILE PUTS uuid	Put profile steps list
- */
-int cmd_profile(char *buf)
-{
-    char                ibuf[SS_BUFSIZE], *sstep, *rest, *targ;
-    int                 i, rlen, istep, irest;
-    float		ftarg;
-    socklen_t           fromlen;
-    char		*opt, *uuid, *param;
-    profiles_list	*profile;
-    prof_step		*step, *olds;
-    units_list		*unit;
-
-    opt = strtok(buf, " \0");
-    opt = strtok(NULL, " \0");
-
-    if (opt == NULL) {
-	/*
-	 * Profile status of current unit
-	 */
-	if (current_unit == NULL) {
-	    srv_send((char *)"401 No fermenter unit selected");
+    } else if (strcmp(opt, (char *)"DEL") == 0) {
+	if (delete_Profile(param)) {
+	    syslog(LOG_NOTICE, "Deleted Profile with %s", param);
+	    srv_send((char *)"211 Profile %s deleted", param);
+	    return 0;
+	} else {
+	    srv_send((char *)"440 Delete Profile: No such profile%s", param);
 	    return 1;
 	}
-	for (unit = Config.units; unit; unit = unit->next) {
-	    if (strcmp(current_unit, unit->uuid) == 0)
-		break;
-	}
-
-	if (unit->profile == NULL) {
-	    srv_send((char *)"441 Unit has no profile");
-	    return 1;
-	}
-	if (unit->mode != UNITMODE_PROFILE) {
-	    srv_send((char *)"442 Unit is not using a profile");
-	    return 1;
-	}
-	srv_send((char *)"241 Profile status follows:");
-
-
-	srv_send((char *)".");
-	return 1;
-
+    
     } else if (strcmp(opt, (char *)"GETS") == 0) {
 
-	uuid = strtok(NULL, "\0\n\r");
-	if (uuid == NULL) {
-	    srv_send((char *)"502 Unknown command option");
-	    return 1;
-	}
-
 	for (profile = Config.profiles; profile; profile = profile->next) {
-	    if (strcmp(profile->uuid, uuid) == 0) {
+	    if (strcmp(profile->uuid, param) == 0) {
 		srv_send((char *)"215 Profile steps follow:");
 		for (step = profile->steps; step; step = step->next) {
 		    srv_send((char *)"%d,%d,%.1f", step->steptime, step->resttime, step->target);
@@ -682,16 +602,9 @@
 
     } else if (strcmp(opt, (char *)"PUTS") == 0) {
 
-        uuid = strtok(NULL, "\0\n\r");
-	if (uuid == NULL) {
-	    srv_send((char *)"502 Unknown command option");
-	    return 1;
-	}
- 
 	for (profile = Config.profiles; profile; profile = profile->next) {
-	    if (strcmp(profile->uuid, uuid) == 0) {
+	    if (strcmp(profile->uuid, param) == 0) {
 
-		    fprintf(stdout, "profile found\n");
 		if (profile->steps) {
 		    for (step = profile->steps; step; step = olds) {
 			olds = step->next;
@@ -699,7 +612,6 @@
 		    }
 		    profile->steps = NULL;		    
 		}
-		fprintf(stdout, "profile cleared\n");
 
             	while (1) {	
 	    	    memset((char *)&ibuf, 0, SS_BUFSIZE);
@@ -758,8 +670,6 @@
 				    }
 				}
 			    }
-
-			    fprintf(stdout, "this was data\n");
 		    	}
 	    	    }
 		}
@@ -768,99 +678,6 @@
 
 	srv_send((char *)"440 No such profile");
 	return 1;
-
-    } else {
-	/*
-	 * uuid,name rename profile
-	 */
-	uuid = strtok(opt, ",");
-	param = strtok(NULL, "\0");
-	fprintf(stdout, "uuid: '%s' param: '%s'\n", uuid, param);
-	for (profile = Config.profiles; profile; profile = profile->next) {
-	    if (strcmp(profile->uuid, uuid) == 0) {
-		syslog(LOG_NOTICE, "Profile %s rename from '%s' to '%s'", uuid, profile->name, param);
-		if (profile->name)
-		    free(profile->name);
-		profile->name = xstrcpy(param);
-		srv_send((char *)"240 Profile updated");
-		return 0;
-	    }
-	}
-	srv_send((char *)"440 No such profile");
-	return 1;
-    }
-
-    srv_send((char *)"502 Unknown command option");
-    return 1;
-}
-
-
-
-/*
- * SET PROFILE string
- */
-int cmd_set(char *buf)
-{
-    char                *opt, *param;
-    units_list          *unit;
-    profiles_list	*profile;
-    int                 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 == NULL) {
-	srv_send((char *)"401 No fermenter unit selected");
-	return 1;
-    }
-
-    for (unit = Config.units; unit; unit = unit->next) {
-	if (strcmp(current_unit, unit->uuid) == 0)
-	    break;
-    }
-
-    if (strcmp(opt, (char *)"PROFILE") == 0) {
-	/*
-	 * Check for active profile, already selected etc.
-	 */
-	if (unit->profile && (unit->mode == UNITMODE_PROFILE)) {
-	    srv_send((char *)"541 Cannot change profile while a profile is active");
-	    return 1;
-	}
-	if (unit->profile && (strcmp(unit->profile, param) == 0)) {
-	    srv_send((char *)"542 Profile already set");
-	    return 1;
-	}
-	for (profile = Config.profiles; profile; profile = profile->next) {
-	    if (strcmp(profile->uuid, param) == 0) {
-		if (unit->profile)
-		    free(unit->profile);
-		unit->profile = xstrcpy(param);
-		srv_send((char *)"242 Unit profile changed to %s", param);
-		return 0;
-	    }
-	}
-	srv_send((char *)"543 Invalid profile");
-	return 1;
     }
 
     srv_send((char *)"502 Unknown command option");
@@ -1252,12 +1069,17 @@
 				    unit->beer_set = fval;
 
 			    } else if (strcmp(kwd, (char *)"PROFILE") == 0) {
-				if (unit->profile)
-				    free(unit->profile);
-				if (val)
-				    unit->profile = xstrcpy(val);
-				else
-				    unit->profile = NULL;
+				if (unit->prof_state == PROFILE_OFF) {
+				    /*
+				     * Only change profile if it is not active, else drop this one.
+				     */
+				    if (unit->profile)
+				    	free(unit->profile);
+				    if (val)
+				    	unit->profile = xstrcpy(val);
+				    else
+				    	unit->profile = NULL;
+				}
 
 			    } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) {
 				for (i = 0; i < 4; i++) {
@@ -1337,13 +1159,7 @@
 	    /*
 	     * Process commands from the client
 	     */
-	    if (strncmp(buf, "ADD", 3) == 0) {
-		if (unit_add(buf) == 0)
-		    wrconfig();
-	    } else if (strncmp(buf, "DEL", 3) == 0) {
-		if (cmd_del(buf) == 0)
-		    wrconfig();
-	    } else if (strncmp(buf, "DEVICE", 6) == 0) {
+	    if (strncmp(buf, "DEVICE", 6) == 0) {
 		if (cmd_device(buf) == 0)
 		    wrconfig();
 	    } else if (strncmp(buf, "HELP", 4) == 0) {
@@ -1351,8 +1167,6 @@
 		srv_send((char *)"Recognized commands:");
 		srv_send((char *)"");
 //                                12345678901234567890123456789012345678901234567890123456789012345678901234567890
-		srv_send((char *)"ADD PROFILE name              Add a new profile with \"name\"");
-		srv_send((char *)"DEL PROFILE uuid              Delete profile with uuid");
 		srv_send((char *)"DEVICE ADD type               Add new Device type");
 		srv_send((char *)"DEVICE DEL uuid               Delete Device by uuid");
 		srv_send((char *)"DEVICE LIST                   List Devices");
@@ -1360,15 +1174,13 @@
 		srv_send((char *)"DEVICE PUT uuid               Put Device record by uuid");
 		srv_send((char *)"LCD                           Get LCD screen (allways 4 rows of 20 characters)");
 		srv_send((char *)"LIST                          List all fermenter units");
-		srv_send((char *)"LIST LOG                      List logfile data in 1 hour lines");
-		srv_send((char *)"LIST PROFILES                 List available profiles");
-//		srv_send((char *)"PROFILE                       Profile status of current unit");
+		srv_send((char *)"LIST LOG uuid                 List logfile data in 1 hour lines");
 		srv_send((char *)"PROFILE uuid,name             Profile rename");
+		srv_send((char *)"PROFILE ADD name              Add new profile with name");
+		srv_send((char *)"PROFILE DEL uuid              Delete profile by uuid");
+		srv_send((char *)"PROFILE LIST                  List available profiles");
 		srv_send((char *)"PROFILE GETS uuid             Profile get steps list");
 		srv_send((char *)"PROFILE PUTS uuid             Profile put steps list");
-		srv_send((char *)"SET PROFILE uuid              Set unit profile");
-//		srv_send((char *)"SET PROFILE start|stop|pause  Profile start, stop or pause");
-		srv_send((char *)"SET VOLUME val                Set unit volume");
 		srv_send((char *)"UNIT uuid                     Select unit by uuid");
 		srv_send((char *)"UNIT ADD name                 Add a new unit with name");
 		srv_send((char *)"UNIT DEL uuid                 Delete Unit by uuid");
@@ -1395,9 +1207,6 @@
 	    } else if (strncmp(buf, "PROFILE", 7) == 0) {
 		if (cmd_profile(buf) == 0)
 		    wrconfig();
-	    } else if (strncmp(buf, "SET", 3) == 0) {
-		if (cmd_set(buf) == 0)
-		    wrconfig();
 	    } else if (strncmp(buf, "UNIT", 4) == 0) {
 		if (cmd_unit(buf) == 0)
 		    wrconfig();
--- a/thermferm/thermferm.h	Thu Aug 07 16:44:53 2014 +0200
+++ b/thermferm/thermferm.h	Thu Aug 07 19:38:30 2014 +0200
@@ -122,6 +122,7 @@
     char		*uuid;			/* Profile uuid			*/
     char		*name;			/* Profile name			*/
     int			busy;			/* Profile busy == 1, free == 0	*/
+    float		inittemp;		/* Temp target before start	*/
     prof_step		*steps;			/* Profile steps		*/
 } profiles_list;
 
--- a/www-thermferm/getlog.php	Thu Aug 07 16:44:53 2014 +0200
+++ b/www-thermferm/getlog.php	Thu Aug 07 19:38:30 2014 +0200
@@ -23,7 +23,6 @@
 
 require_once('utilities.php');
 
-$unit = $_GET["unit"];
 
 $sock = open_socket();
 if ($sock == false) {
@@ -31,23 +30,7 @@
     return;
 }
 
-socket_write($sock, "UNIT " . $unit, 4096);
-$answer = "";
-while (1) {
-    $line = socket_read($sock, 4096);
-    if ($line === '')
-	break;
-    $answer .= $line;
-}
-socket_close($sock);
-
-$sock = open_socket();
-if ($sock == false) {
-    echo "";
-    return;
-}
-
-socket_write($sock, "LIST LOG", 4096);
+socket_write($sock, "LIST LOG ".$_GET["unit"], 4096);
 $answer = "";
 while (1) {
     $line = socket_read($sock, 4096);
--- a/www-thermferm/profiles.php	Thu Aug 07 16:44:53 2014 +0200
+++ b/www-thermferm/profiles.php	Thu Aug 07 19:38:30 2014 +0200
@@ -31,7 +31,7 @@
     load('index.php');
 }
 
-socket_write($sock, "LIST PROFILES", 4096);
+socket_write($sock, "PROFILE LIST", 4096);
 $answer = "";
 while (1) {
     $line = socket_read($sock, 4096);
@@ -41,7 +41,7 @@
 }
 socket_close($sock);
 /*
- * $arr contains the complete reply of he LIST PROFILES command.
+ * $arr contains the complete reply of he PROFILE LIST command.
  */
 $arr = explode("\r\n", $answer);
 
@@ -98,7 +98,7 @@
 	load('profiles.php');
     }
 
-    socket_write($sock, "PROFILES GETS ".$UUID, 4096);
+    socket_write($sock, "PROFILE GETS ".$UUID, 4096);
     $answer = "";
     while (1) {
 	$line = socket_read($sock, 4096);

mercurial