Browse Source

modified state Wait_off, now program exits on database read failure

Sebastian 7 năm trước cách đây
mục cha
commit
69e0633a64
4 tập tin đã thay đổi với 31 bổ sung18 xóa
  1. 1 1
      CoffeeCode/buildno
  2. 17 7
      CoffeeCode/coffee.cpp
  3. 8 7
      CoffeeCode/hal.cpp
  4. 5 3
      CoffeeCode/main.cpp

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-213
+216

+ 17 - 7
CoffeeCode/coffee.cpp

@@ -59,11 +59,13 @@ void *coffeeThread(void *threadid) {
 	//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);
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
 	}
 	if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
 		logger_error("coffee.cpp: Couldn't read the brew counter from the database");
-		pthread_exit(EXIT_SUCCESS);
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
 	}
 
 	event_subscribe("terminate", &coffeeTerminate);
@@ -107,6 +109,7 @@ void *coffeeThread(void *threadid) {
 						changeState(STATE_IDLE);
 					}
 				} else {
+					logger_error("Not enough Water in tank detected\n");
 					changeState(STATE_ERROR);
 				}
 				break;
@@ -116,6 +119,12 @@ void *coffeeThread(void *threadid) {
 			 *
 			*/
 		case STATE_WAIT_OFF:
+			if (halIsHeating()) {
+				halMachineOff();
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
+				changeState(STATE_OFF);
+			}
 			if (SigValueEmpty())
 				pause();
 			switch (getSigValue()) {
@@ -209,6 +218,11 @@ void *coffeeThread(void *threadid) {
 				changeState(STATE_OFF);
 				break;
 
+			case SigInt1Rls:
+				//turn machine off when heating is finished
+				changeState(STATE_WAIT_OFF);
+				break;
+
 			case SigPressCls:
 				changeState(STATE_HEATING);
 				break;
@@ -271,7 +285,7 @@ void *coffeeThread(void *threadid) {
 void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
 	sigval_t sigVal = (siginfo->si_value);
 	sigValue = sigVal.sival_int;
-	logger(V_BREW, "CoffeeHandler called with %d\n", sigValue);
+	logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n", sigValue);
 }
 
 /**
@@ -322,10 +336,6 @@ void brewTimeHandler(void) {
  * handles program termination
  */
 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);

+ 8 - 7
CoffeeCode/hal.cpp

@@ -239,7 +239,7 @@ double halgetHeatingTime(void){
  * Method to handle toggle of the proximity sensor
  */
 void halIntProximity(void) {
-	logger(V_HAL, "IntProximity triggered\n");
+	//logger(V_HAL, "IntProximity triggered\n");
 	return;
 	if (halProxSensorCovered()) {
 		halSendSignal(SigProxCvrd);
@@ -280,11 +280,12 @@ bool halIsHeating(void) {
  * @return 1 if the proximity switch is covered and 0 if uncovered
  */
 bool halProxSensorCovered(void) {
-	if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
-	 return false;
-	 } else {
-	 return true;
-	 }
+//	if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
+//	 return false;
+//	 } else {
+//	 return true;
+//	 }
+	return true;
 }
 
 /**
@@ -313,7 +314,7 @@ void halSendSignal(HalSig val) {
 
 	try {
 		if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
-			logger_error("Failed to queue signal %d %s\n", val,
+			logger_error("hal.cpp: Failed to queue signal %d %s\n", val,
 					strerror(errno));
 			//No Signals reach the state machine anymore...
 			exit(EXIT_FAILURE);

+ 5 - 3
CoffeeCode/main.cpp

@@ -128,6 +128,9 @@ int main(int argc, char *argv[]) {
 	displayInit();
 	//temperatur_init();
 	DS18B20_init();
+	sqlOpen();
+	sqlSetup();
+
 
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
@@ -160,9 +163,10 @@ int main(int argc, char *argv[]) {
 			exit(-1);
 		}
 	}
-
+	//hal might send signals to processes which must exist!
 	halInit();
 
+
 	// free attribute and wait for the other threads
 	pthread_attr_destroy(&attr);
 
@@ -235,9 +239,7 @@ void killThread(int threadid, int sig) {
  */
 
 void *mainLoop(void *threadid) {
-	sqlOpen();
 
-	sqlSetup();
 
 	// Do more stuff here
 	while (1){