thermferm/server.c

changeset 140
1b001de37945
parent 138
e7e7a23af890
child 155
0d86f3c0a07b
--- a/thermferm/server.c	Sat Jul 26 22:26:30 2014 +0200
+++ b/thermferm/server.c	Sun Jul 27 17:06:08 2014 +0200
@@ -499,15 +499,22 @@
 /*
  * 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");
+    opt = strtok(NULL, " \0");
 
     if (opt == NULL) {
 	/*
@@ -536,6 +543,117 @@
 	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) {
+		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);
+		}
+		srv_send((char *)".");
+		return 1;
+	    }
+	}
+
+	srv_send((char *)"440 No such profile");
+	return 1;
+
+    } 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) {
+
+		    fprintf(stdout, "profile found\n");
+		if (profile->steps) {
+		    for (step = profile->steps; step; step = olds) {
+			olds = step->next;
+			free(step);
+		    }
+		    profile->steps = NULL;		    
+		}
+		fprintf(stdout, "profile cleared\n");
+
+            	while (1) {	
+	    	    memset((char *)&ibuf, 0, SS_BUFSIZE);
+            	    fromlen = sizeof(peeraddr_in);
+	    	    rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
+	    	    if (rlen == -1) {
+	    	    	syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
+		    	srv_send((char *)"518 recfrom(): %s", strerror(errno));
+		    	return 1;
+	    	    } else {
+	    	    	for (i = 0; i < strlen(ibuf); i++) {
+		            if (ibuf[i] == '\n')
+		    	    	ibuf[i] = '\0';
+		    	    if (ibuf[i] == '\r')
+		    	    	ibuf[i] = '\0';
+	                }
+	                for (i = strlen(ibuf) -1; i > 0; i--) {
+		    	    if (ibuf[i] == ' ')
+		    	    	ibuf[i] = '\0';
+		    	    else
+		     	    	break;
+	    	    	}
+	    	    	if (strlen(ibuf)) {
+		    	    if (debug) {
+		    	    	syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
+		    	    	fprintf(stdout, "recv: \"%s\"\n", ibuf);
+		    	    }
+		    	    if (strcmp(ibuf, (char *)".") == 0) {
+
+		    	    	srv_send((char *)"219 Accepted profile steps");
+		    	    	return 0;
+		    	    }
+			    sstep = strtok(ibuf, ",\0");
+			    rest = strtok(NULL, ",\0");
+			    targ = strtok(NULL, "\0");
+
+			    if ((sscanf(sstep, "%d", &istep) == 1) &&
+				(sscanf(rest, "%d", &irest) == 1) &&
+				(sscanf(targ, "%f", &ftarg) == 1)) {
+
+				step = (prof_step *)malloc(sizeof(prof_step));
+				step->next = NULL;
+				step->version = 1;
+				step->steptime = istep;
+				step->resttime = irest;
+				step->target = ftarg;
+
+				if (profile->steps == NULL) {
+				    profile->steps = step;
+				} else {
+				    for (olds = profile->steps; olds; olds = olds->next) {
+					if (olds->next == NULL) {
+					    olds->next = step;
+					    break;
+					}
+				    }
+				}
+			    }
+
+			    fprintf(stdout, "this was data\n");
+		    	}
+	    	    }
+		}
+	    }
+	}
+
+	srv_send((char *)"440 No such profile");
+	return 1;
+
     } else {
 	/*
 	 * uuid,name rename profile
@@ -827,6 +945,8 @@
 		srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
 //		srv_send((char *)"PROFILE                       Profile status of current unit");
 		srv_send((char *)"PROFILE uuid,name             Profile rename");
+		srv_send((char *)"PROFILE GETS uuid             Profile get steps list");
+		srv_send((char *)"PROFILE PUTS uuid             Profile put steps list");
 		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)");

mercurial