Versie 0.8.3. Enkele debug console berichten verwijderd. De MQTT NCMD reboot en rebirth commando's geimplementeerd.

Mon, 23 Jul 2018 14:41:21 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 23 Jul 2018 14:41:21 +0200
changeset 550
04c942cded91
parent 549
ee1bcad035f0
child 551
3721f9c08843

Versie 0.8.3. Enkele debug console berichten verwijderd. De MQTT NCMD reboot en rebirth commando's geimplementeerd.

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/simulator.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/configure	Sat Jul 21 20:40:02 2018 +0200
+++ b/configure	Mon Jul 23 14:41:21 2018 +0200
@@ -2035,7 +2035,7 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.8.2"
+VERSION="0.8.3"
 COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2018"
 
@@ -3570,6 +3570,66 @@
 
 fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for json_object_iter_init_default in -ljson-c" >&5
+$as_echo_n "checking for json_object_iter_init_default in -ljson-c... " >&6; }
+if ${ac_cv_lib_json_c_json_object_iter_init_default+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-ljson-c  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char json_object_iter_init_default ();
+int
+main ()
+{
+return json_object_iter_init_default ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_json_c_json_object_iter_init_default=yes
+else
+  ac_cv_lib_json_c_json_object_iter_init_default=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_json_c_json_object_iter_init_default" >&5
+$as_echo "$ac_cv_lib_json_c_json_object_iter_init_default" >&6; }
+if test "x$ac_cv_lib_json_c_json_object_iter_init_default" = xyes; then :
+  result=yes
+else
+  result=no
+fi
+
+if test "$result" = "yes"; then
+  LIBS="$LIBS -ljson-c"
+  for ac_header in json-c/json.h
+do :
+  ac_fn_c_check_header_mongrel "$LINENO" "json-c/json.h" "ac_cv_header_json_c_json_h" "$ac_includes_default"
+if test "x$ac_cv_header_json_c_json_h" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_JSON_C_JSON_H 1
+_ACEOF
+
+fi
+
+done
+
+else
+  as_fn_error $? "json-c not found" "$LINENO" 5
+fi
+
 #
 # Additional commandline switches
 #
--- a/configure.ac	Sat Jul 21 20:40:02 2018 +0200
+++ b/configure.ac	Mon Jul 23 14:41:21 2018 +0200
@@ -8,7 +8,7 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.8.2"
+VERSION="0.8.3"
 COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2018"
 AC_SUBST(PACKAGE)
@@ -46,6 +46,14 @@
   AC_CHECK_HEADERS(wiringPi.h,WIRINGPI=Yes,WIRINGPI=No)
 fi
 
+AC_CHECK_LIB(json-c,json_object_iter_init_default,result=yes,result=no)
+if test "$result" = "yes"; then
+  LIBS="$LIBS -ljson-c"
+  AC_CHECK_HEADERS(json-c/json.h)
+else
+  AC_MSG_ERROR(json-c not found)
+fi
+
 #
 # Additional commandline switches
 #
--- a/thermferm/mqtt.c	Sat Jul 21 20:40:02 2018 +0200
+++ b/thermferm/mqtt.c	Mon Jul 23 14:41:21 2018 +0200
@@ -26,6 +26,8 @@
 
 extern sys_config       Config;
 extern int		debug;
+extern int		my_shutdown;
+extern int		my_reboot;
 extern const char	UNITMODE[5][8];
 extern const char	PROFSTATE[5][6];
 extern const char	TEMPSTATE[3][8];
@@ -157,10 +159,58 @@
 
 void my_message_callback(struct mosquitto *my_mosq, void *userdata, const struct mosquitto_message *message)
 {
+    char		*message_type;
+    struct json_object	*jobj, *metric, *val;
+    time_t		timestamp;
+    int			timediff;
+
     if (message->payloadlen) {
+	/*
+	 * Process received commands
+	 */
+	strtok(message->topic, "/");	// Ignore mbv1.0
+	strtok(NULL, "/");		// Ignore group_id
+	message_type = strtok(NULL, "/");
+
+	jobj = json_tokener_parse(message->payload);
+	if (json_object_object_get_ex(jobj, "timestamp", &val)) {
+	    timestamp = json_object_get_int(val);
+	    timediff = (int)timestamp - time(NULL);
+	    if ((timediff < 61) && (timediff > -61)) {
+		if (json_object_object_get_ex(jobj, "metric", &metric)) {
+		    if ((json_object_object_get_ex(metric, "Node Control/Reboot", &val)) && (strcmp(message_type, "NCMD") == 0)) {
+			if (json_object_get_boolean(val) == true) {
+			    syslog(LOG_NOTICE, "MQTT: `Node Control/Reboot' command");
+			    /*
+			     * Reboot. The erver process will restart which is handled
+			     * in the main thread loop.
+			     */
+			    my_reboot = my_shutdown = TRUE;
+			    return;
+			}
+		    }
+		    if ((json_object_object_get_ex(metric, "Node Control/Rebirth", &val)) && (strcmp(message_type, "NCMD") == 0)) {
+			if (json_object_get_boolean(val) == true) {
+			    /*
+			     * Resend all birth certificates.
+			     */
+			    syslog(LOG_NOTICE, "MQTT: `Node Control/Rebirth' command");
+			    publishNData(true, 0);
+			    publishDBirthAll();
+			    return;
+			}
+		    }
+		    printf("metric: %s\n", (char *)json_object_get_string(metric));
+		    syslog(LOG_NOTICE, "MQTT: %s payload not understood\n", (char *)message->payload);
+		    return;
+		}
+	    } else {
+		syslog(LOG_NOTICE, "MQTT: got payload with timestamp %d seconds error", timediff);
+		return;
+	    }
+	}
+
 	syslog(LOG_NOTICE, "MQTT: message callback %s :: %d", message->topic, message->payloadlen);
-        // TODO: process subscribed topics here.
-
     } else {
 	syslog(LOG_NOTICE, "MQTT: message callback %s (null)", message->topic);
     }
--- a/thermferm/simulator.c	Sat Jul 21 20:40:02 2018 +0200
+++ b/thermferm/simulator.c	Mon Jul 23 14:41:21 2018 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2008-2014
+ * Copyright (C) 2008-2018
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -42,8 +42,8 @@
     simulator_list	*simulator;
     time_t		now, last = (time_t)0;
     int			seconds = 0;
-    double		k_room_air, k_beer_air, sqm_room_air, sqm_beer_air, thick_room_air, thick_beer_air, air_heat_transfer, beer_heat_transfer;
-    double		air_change, /* beer_change, */ vhc_air = 0.00121 /*, vhc_water = 4.1796 */;
+    double		k_room_air, sqm_room_air, thick_room_air, air_heat_transfer;
+    double		air_change, vhc_air = 0.00121;
 
     syslog(LOG_NOTICE, "Thread my_simulator_loop started");
     if (debug)
@@ -119,17 +119,6 @@
 		}
 
 	    	/*
-	    	 * Calculate the extra beer temperatur rise to simulate the heat produced by the
-	    	 * fermentation process. Peak about one day after start and slowly decrease after
-	    	 * that.
-	    	 */
-		sqm_beer_air = (cbrtl(simulator->volume_beer) * cbrtl(simulator->volume_beer) * 6) / 100; /* Simple, the beer is in a cubic box */
-		thick_beer_air = 0.001;
-		k_beer_air = 0.5;	/* HDPE */
-		beer_heat_transfer=(k_beer_air * sqm_beer_air * (simulator->air_temperature - simulator->beer_temperature)) / thick_beer_air;
-		/* beer_change = 0; */
-
-	    	/*
 	    	 * Calculate final temperature of the beer and the air.
 	    	 */
 		// Cheap trick, just follow slowly the air temp.
@@ -139,10 +128,9 @@
 		syslog(LOG_NOTICE, "air=%.3f beer=%.3f heater=%.3f cooler=%.3f", simulator->air_temperature, simulator->beer_temperature,
 				simulator->s_heat_temp, simulator->s_cool_temp);
 
-		if (debug)
-		    fprintf(stdout, "sqm_room_air=%f air=%f air_heat_transfer=%f air_change=%f  sqm_beer_air=%f beer=%f beer_heat_transfer=%f\n",
-			sqm_room_air, simulator->air_temperature, air_heat_transfer, air_change,
-			sqm_beer_air, simulator->beer_temperature, beer_heat_transfer);
+//		if (debug)
+//		    fprintf(stdout, "sqm_room_air=%f air=%f air_heat_transfer=%f air_change=%f beer=%f\n",
+//			sqm_room_air, simulator->air_temperature, air_heat_transfer, air_change, simulator->beer_temperature);
 	    }
 	    usleep(100000);
 	}
--- a/thermferm/thermferm.c	Sat Jul 21 20:40:02 2018 +0200
+++ b/thermferm/thermferm.c	Mon Jul 23 14:41:21 2018 +0200
@@ -38,6 +38,7 @@
 
 
 int			my_shutdown = FALSE;
+int			my_reboot = FALSE;
 static pid_t		pgrp, mypid;
 int			run_pause = FALSE;
 int			run_hold = FALSE;
@@ -912,7 +913,9 @@
 	/*
 	 * For debugging run in foreground.
 	 */
-	rc = server();
+	do {
+	    rc = server();
+	} while (my_reboot == TRUE);
     } else {
 	/*
 	 * Server initialization is complete. Now we can fork the 
@@ -952,7 +955,9 @@
 			    _exit(2);
 			}
 			mypid = getpid();
-			rc = server();
+			do {
+			    rc = server();
+			} while (my_reboot == TRUE);
 			break;
 			/* Not reached */
 	    default:
@@ -991,6 +996,8 @@
     unsigned char	LCDstatC, LCDstatH;
     int			LCDunit;
 
+    syslog(LOG_NOTICE, "Server process started");
+    my_shutdown = my_reboot = FALSE;
     if (lockprog((char *)"thermferm")) {
 	syslog(LOG_NOTICE, "Can't lock");
 	return 1;
@@ -1672,9 +1679,9 @@
 			if ((unit->mode == UNITMODE_BEER) && ((unit->air_temperature / 1000.0) > (unit->PID_heat->Input + 8.0))) {
 			    unit->PID_heat->OutP = 0.0;
 			}
-			if (debug)
-			    fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
-				unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP);
+//			if (debug)
+//			    fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
+//				unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP);
 			if (seconds == 60) {
 			    syslog(LOG_NOTICE, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f",
 				unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP);
@@ -1701,9 +1708,9 @@
 				}
 			    }
 			}
-		    	if (debug)
-			    fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
-				unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP);
+//		    	if (debug)
+//			    fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
+//				unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP);
 		    	if (seconds == 60) {
 			    syslog(LOG_NOTICE, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f",
 				unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP);
@@ -1797,9 +1804,9 @@
 			    device_out(unit->cooler_address, 0);
 			}
 		    }
-		    if (debug)
-			fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d  PIDcool=%.2f PWRcool=%d\n", 
-					unit->PID_heat->OutP, unit->heater_state, unit->PID_cool->OutP, unit->cooler_state);
+//		    if (debug)
+//			fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d  PIDcool=%.2f PWRcool=%d\n", 
+//					unit->PID_heat->OutP, unit->heater_state, unit->PID_cool->OutP, unit->cooler_state);
 
 		    /*
 		     * If there is a fan, and the unit door is closed, and the unit should be doing
@@ -2067,8 +2074,6 @@
 #endif
 
     syslog(LOG_NOTICE, "Out of loop");
-    if (debug)
-	fprintf(stdout, (char *)"Out of loop\n");
 
     /*
      * Give threads time to cleanup
@@ -2084,6 +2089,8 @@
     }
     wrconfig();
     ulockprog((char *)"thermferm");
+
+    syslog(LOG_NOTICE, "Server process ended");
     return 0;
 }
 
--- a/thermferm/thermferm.h	Sat Jul 21 20:40:02 2018 +0200
+++ b/thermferm/thermferm.h	Mon Jul 23 14:41:21 2018 +0200
@@ -36,6 +36,7 @@
 #include <dirent.h>
 #include <uuid/uuid.h>
 #include <math.h>
+#include <json-c/json.h>
 #ifndef HAVE_WIRINGPI_H
 #include <pthread.h>
 #endif

mercurial