Ver código fonte

added local copies of database entries now. Writeback now only when program terminates or machine gets turned off

Sebastian 7 anos atrás
pai
commit
65b4c2e8e3
3 arquivos alterados com 49 adições e 18 exclusões
  1. 1 1
      CoffeeCode/buildno
  2. 44 16
      CoffeeCode/coffee.cpp
  3. 4 1
      CoffeeCode/coffee.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-211
+213

+ 44 - 16
CoffeeCode/coffee.cpp

@@ -22,13 +22,16 @@
 #include "logger.h"
 #include "timer.h"
 #include "database.h"
-#include "events.h"
+
 
 coffee_status_t state;
 int sigValue;
 int brewTime; //Brew time in ms
 timer brewTimer(&brewTimeHandler);
 
+uint64_t totalHeatingTime; //local copies of the corresponding database entries
+uint16_t brewCounter;
+
 
 const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
 
@@ -53,6 +56,18 @@ void *coffeeThread(void *threadid) {
 	brewTimer.stop();
 	brewTime = 0;
 
+	//read the database values
+	if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
+		logger_error("coffee.cpp: Couldn't read the heating time from the database");
+		pthread_exit(EXIT_SUCCESS);
+	}
+	if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
+		logger_error("coffee.cpp: Couldn't read the brew counter from the database");
+		pthread_exit(EXIT_SUCCESS);
+	}
+
+	event_subscribe("terminate", &coffeeTerminate);
+
 	logger(V_BREW, "Determining inital state\n");
 	//determine inital state
 	if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
@@ -109,6 +124,7 @@ void *coffeeThread(void *threadid) {
 				//Turn machine off again
 				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
@@ -130,6 +146,7 @@ void *coffeeThread(void *threadid) {
 				//Turn machine off again
 				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
@@ -156,6 +173,7 @@ void *coffeeThread(void *threadid) {
 			case SigInt1RlsLong:
 				//Turn machine off again
 				halMachineOff();
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 			case SigPressCls:
@@ -268,11 +286,32 @@ void brewTimeHandler(void) {
 /**
  * handles program termination
  */
-void coffeeTerminate(void) {
-	logger_error("Coffee thread terminated");
+void coffeeTerminate(event_t *event) {
+	if (event->len != sizeof(coffee_status_t)) {
+		logger_error("Invalid use of event %s\n", event->event);
+		return;
+	}
+	logger(V_BREW, "Coffee.cpp thread terminating");
 	//stop brewing
 	halRelaisOff(RELAIS_PUMP);
 	brewTimer.stop();
+	writeBackCache();
+}
+
+/**
+ * Function to write back the values of the local copies
+ * brewCounter and totalHeatingTime
+ *
+ */
+void writeBackCache(void){
+	if (sqlSetConf(CFGbrewcounter, brewCounter)) {
+		logger_error("coffee.cpp: Couldn't write brewcounter to database");
+		return;
+	}
+	if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
+		logger_error("coffee.cpp: Couldn't write heating time to database");
+		return;
+	}
 }
 
 /**
@@ -334,24 +373,13 @@ void coffeeBrew(void) {
  *
  */
 void coffeeIncreaseBrewCounter(void) {
-	uint64_t brewcounter = sqlGetConf(CFGbrewcounter);
-	if (sqlSetConf(CFGbrewcounter, ++brewcounter)) {
-		logger_error("Couldn't write brewcounter to database");
-	}
+	brewCounter++;
 }
-/**
- *
- */
-void coffeeIncreaseHeatingTime(clock_t begin, clock_t end){
 
-}
 
 /**
  *
  */
 void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
-	uint64_t totalHeatingTime = sqlGetConf(CFGHeatingTime);
-	if (sqlSetConf(CFGHeatingTime, (totalHeatingTime + heatingTime))) {
-		logger_error("Couldn't write heating time to database");
-	}
+	totalHeatingTime += heatingTime;
 }

+ 4 - 1
CoffeeCode/coffee.h

@@ -7,7 +7,9 @@
 
 #ifndef COFFEE_H_
 #define COFFEE_H_
+
 #include <csignal>
+#include "events.h"
 
 //define status
 typedef enum {
@@ -35,7 +37,8 @@ bool SigValueEmpty(void);
 void changeState(coffee_status_t newState);
 coffee_status_t getState(void);
 void brewTimeHandler(void);
-void coffeeTerminate(void);
+void writeBackCache(void);
+void coffeeTerminate(event_t *event);
 void coffeeBrew(void);
 void coffeeIncreaseBrewCounter(void);
 void coffeeIncreaseHeatingTime(uint64_t heatingTime);