Added GLOBAL GET and GLOBAL PUT commands.

Sun, 10 Aug 2014 14:45:11 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 10 Aug 2014 14:45:11 +0200
changeset 206
78fb6f99e473
parent 205
ca18ff45deba
child 207
8d2ba4126519

Added GLOBAL GET and GLOBAL PUT commands.

thermferm/rdconfig.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/thermferm/rdconfig.c	Sun Aug 10 13:26:35 2014 +0200
+++ b/thermferm/rdconfig.c	Sun Aug 10 14:45:11 2014 +0200
@@ -54,11 +54,14 @@
 
     Config.my_port = 6554;
     Config.tempFormat = 'C';
-    if (Config.air_address)
-	free(Config.air_address);
-    Config.air_address = NULL;
-    Config.air_temperature = 20000;
-    Config.air_state = 1;	// missing
+    if (Config.temp_address)
+	free(Config.temp_address);
+    if (Config.hum_address)
+	free(Config.hum_address);
+    Config.temp_address = Config.hum_address = NULL;
+    Config.temp_value = 20000;
+    Config.temp_state = Config.hum_state = 1;	// missing
+    Config.hum_value = 50000;
 
     for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) {
 	if (tmp2->uuid)
@@ -188,19 +191,29 @@
 	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 	return 1;
     }
-    if (Config.air_address) {
-    	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_ADDRESS", "%s", Config.air_address)) < 0) {
-	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-	    return 1;
-    	}
-	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_STATE", "%d", Config.air_state)) < 0) {
-	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-	    return 1;                           
-	}
-	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%d", Config.air_temperature)) < 0) {
-	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
-	    return 1;
-	}
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMP_ADDRESS", "%s", Config.temp_address)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMP_STATE", "%d", Config.temp_state)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;                           
+    }
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMP_VALUE", "%d", Config.temp_value)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HUM_ADDRESS", "%s", Config.hum_address)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }   
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HUM_STATE", "%d", Config.hum_state)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;    
+    }   
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HUM_VALUE", "%d", Config.hum_value)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
     }
 
 #ifdef HAVE_WIRINGPI_H
@@ -1221,8 +1234,11 @@
 	    Config.tempFormat = key[0];
 	    xmlFree(key);
 	}
-	if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_ADDRESS"))) {
-	    Config.air_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"TEMP_ADDRESS"))) {
+	    Config.temp_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HUM_ADDRESS"))) {
+	    Config.hum_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	}
 #ifdef HAVE_WIRINGPI_H
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) {
--- a/thermferm/server.c	Sun Aug 10 13:26:35 2014 +0200
+++ b/thermferm/server.c	Sun Aug 10 14:45:11 2014 +0200
@@ -30,7 +30,6 @@
 
 extern int		my_shutdown;
 extern int		debug;
-extern char		*current_unit;
 #ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
 extern unsigned char	lcdbuf[MAX_LCDS][20][4];
@@ -429,6 +428,136 @@
 
 
 /*
+ * GLOBAL GET
+ * GLOBAL PUT
+ */
+int cmd_global(char *buf)
+{
+    char	*opt, *kwd, *val, ibuf[SS_BUFSIZE];
+    int		ival, i, rlen;
+    socklen_t	fromlen;
+
+    opt = strtok(buf, " \0");
+    opt = strtok(NULL, "\0");
+
+    if (opt == NULL) {
+	srv_send((char *)"502 Missing command option");
+	return 1;
+    }
+
+    if (strcmp(opt, (char *)"GET") == 0) {
+	srv_send((char *)"213 Global Settings record follows:");
+	srv_send((char *)"NAME,%s", Config.name);
+	srv_send((char *)"PORT,%d", Config.my_port);
+	srv_send((char *)"TEMPFORMAT,%c", Config.tempFormat);
+	srv_send((char *)"TEMP_ADDRESS,%s", Config.temp_address);
+	srv_send((char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]);
+	srv_send((char *)"TEMP_VALUE,%.3f", Config.temp_value / 1000.0);
+	srv_send((char *)"HUM_ADDRESS,%s", Config.hum_address);
+	srv_send((char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]);
+	srv_send((char *)"HUM_VALUE,%.3f", Config.hum_value / 1000.0);
+#ifdef HAVE_WIRINGPI_H
+	srv_send((char *)"LCD_COLS,%d", Config.lcd_cols);
+	srv_send((char *)"LCD_ROWS,%d", Config.lcd_rows);
+#endif
+	srv_send((char *)".");
+	return 1;
+    }
+
+    if (strcmp(opt, (char *)"PUT") == 0) {
+	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;
+	    }
+	    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 Global record");
+		    return 0;
+		}
+		kwd = strtok(ibuf, ",\0");
+		val = strtok(NULL, "\0");
+		if (kwd) {
+		    if (strcmp(kwd, (char *)"NAME") == 0) {
+			if (Config.name)
+			    free(Config.name);
+			if (val)
+			    Config.name = xstrcpy(val);
+			else
+			    Config.name = NULL;
+
+		    } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) {
+			if (sscanf(val, "%d", &ival) == 1)
+			    Config.my_port = ival;
+
+		    } else if (val && (strcmp(kwd, (char *)"TEMPFORMAT") == 0)) {
+			if ((val[0] == 'C') || (val[0] == 'F'))
+			    Config.tempFormat = val[0];
+
+		    } else if (strcmp(kwd, (char *)"TEMP_ADDRESS") == 0) {
+		        if (Config.temp_address) {
+			    device_count(FALSE, Config.temp_address);
+			    free(Config.temp_address);
+			}
+			if (val) {
+			    Config.temp_address = xstrcpy(val);
+			    device_count(TRUE, Config.temp_address);
+			} else
+			    Config.temp_address = NULL;
+
+		    } else if (strcmp(kwd, (char *)"HUM_ADDRESS") == 0) {
+			if (Config.hum_address) {
+			    device_count(FALSE, Config.hum_address);
+			    free(Config.hum_address);
+			}
+			if (val) {
+			    Config.hum_address = xstrcpy(val);
+			    device_count(TRUE, Config.hum_address);
+			} else
+			    Config.hum_address = NULL;
+
+#ifdef HAVE_WIRINGPI_H
+		    } else if (val && (strcmp(kwd, (char *)"LCD_COLS") == 0)) {
+			if (sscanf(val, "%d", &ival) == 1)
+			    Config.lcd_cols = ival;
+
+		    } else if (val && (strcmp(kwd, (char *)"LCD_ROWS") == 0)) {
+			if (sscanf(val, "%d", &ival) == 1)
+			    Config.lcd_rows = ival;
+#endif
+		    }
+		}
+	    }
+	}
+    }
+
+    srv_send((char *)"502 Unknown command option");
+    return 1;
+}
+
+
+
+/*
  * LIST
  * LIST LOG
  */
@@ -829,7 +958,6 @@
 
 
 /*
- * UNIT uuid
  * UNIT ADD name
  * UNIT DEL uuid
  * UNIT LIST
@@ -854,26 +982,6 @@
     }
     param = strtok(NULL, "\0");
 
-    /*
-     * UNIT uuid
-     */
-    if ((strlen(opt) == 36) && (param == NULL)) {
-	/*
-	 * Search using uuid
-	 */
-	for (unit = Config.units; unit; unit = unit->next) {
-	    if (strcmp(opt, unit->uuid) == 0) {
-		srv_send((char *)"210 Unit %s selected", unit->uuid);
-		if (current_unit)
-		    free(current_unit);
-		current_unit = xstrcpy(unit->uuid);;
-		return 1;
-	    }
-	}
-	srv_send((char *)"410 No such unit");
-	return 1;
-    }
-
     if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
 	srv_send((char *)"212 Fermenter Units list follows:");
 	for (unit = Config.units; unit; unit = unit->next) {
@@ -895,9 +1003,6 @@
 	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->heater_address = unit->cooler_address = \
 			    unit->fan_address = unit->door_address = unit->profile = NULL;
@@ -1238,6 +1343,9 @@
 	    if (strncmp(buf, "DEVICE", 6) == 0) {
 		if (cmd_device(buf) == 0)
 		    wrconfig();
+	    } else if (strncmp(buf, "GLOBAL", 6) == 0) {
+		if (cmd_global(buf) == 0)
+		    wrconfig();
 	    } else if (strncmp(buf, "HELP", 4) == 0) {
 		srv_send((char *)"100 Help text follows");
 		srv_send((char *)"Recognized commands:");
@@ -1248,6 +1356,8 @@
 		srv_send((char *)"DEVICE LIST                   List Devices");
 		srv_send((char *)"DEVICE GET uuid               Get Device record by uuid");
 		srv_send((char *)"DEVICE PUT uuid               Put Device record by uuid");
+		srv_send((char *)"GLOBAL GET                    Get global settings");
+		srv_send((char *)"GLOBAL PUT                    Put global settings");
 		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 uuid                 List logfile data in 1 hour lines");
@@ -1259,10 +1369,9 @@
 		srv_send((char *)"PROFILE PUT uuid              Put Profile record by uuid");
 		srv_send((char *)"PROFILE GETS uuid             Profile get steps list");
 		srv_send((char *)"PROFILE PUTS uuid             Profile put steps list");
-		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");
-		srv_send((char *)"UNIT LIST                     List Units");
+		srv_send((char *)"UNIT LIST                     List all Units");
 		srv_send((char *)"UNIT GET uuid                 Get Unit record by uuid");
 		srv_send((char *)"UNIT PUT uuid                 Put Unit record by uuid");
 		srv_send((char *)".");
--- a/thermferm/thermferm.c	Sun Aug 10 13:26:35 2014 +0200
+++ b/thermferm/thermferm.c	Sun Aug 10 14:45:11 2014 +0200
@@ -33,9 +33,8 @@
 #include "xutil.h"
 
 
-char			*current_unit = NULL;
-int			tempA = 80;
-int			tempB = 80;
+//int			tempA = 80;
+//int			tempB = 80;
 
 int			my_shutdown = FALSE;
 static pid_t		pgrp, mypid;
--- a/thermferm/thermferm.h	Sun Aug 10 13:26:35 2014 +0200
+++ b/thermferm/thermferm.h	Sun Aug 10 14:45:11 2014 +0200
@@ -182,9 +182,12 @@
     char		*name;			/* Configuration name		*/
     int			my_port;		/* my client/server port	*/
     unsigned char	tempFormat;		/* Temperature format, C or F	*/
-    char		*air_address;		/* 1-wire environment sensor	*/
-    int			air_state;		/* 0=ok, 1=missing, 2=error	*/
-    int			air_temperature;	/* Air temperature in C * 1000	*/
+    char		*temp_address;		/* Environment temperature	*/
+    int			temp_state;		/* 0=ok, 1=missing, 2=error	*/
+    int			temp_value;		/* Air temperature in C * 1000	*/
+    char		*hum_address;		/* Environment huminity		*/
+    int			hum_state;		/* 0=ok, 1=missing, 2=error	*/
+    int			hum_value;		/* Huminity in % * 1000		*/
 #ifdef HAVE_WIRINGPI_H
     int			lcd_cols;		/* LCD display columns		*/
     int			lcd_rows;		/* LCD display rows		*/

mercurial