|
@@ -22,15 +22,19 @@
|
|
|
#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;
|
|
|
+coffee_status_t lastState;
|
|
|
+
|
|
|
|
|
|
-const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
|
|
|
+const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
|
|
|
|
|
|
/**
|
|
|
* Thread for the finite state machine
|
|
@@ -53,6 +57,22 @@ void *coffeeThread(void *threadid) {
|
|
|
brewTimer.stop();
|
|
|
brewTime = 0;
|
|
|
|
|
|
+ lastState = STATE_OFF;
|
|
|
+
|
|
|
+ //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);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+ if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
|
|
|
+ logger_error("coffee.cpp: Couldn't read the brew counter from the database");
|
|
|
+ //pthread_exit(EXIT_SUCCESS);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+
|
|
|
+ event_subscribe("terminate", &coffeeTerminate);
|
|
|
+
|
|
|
logger(V_BREW, "Determining inital state\n");
|
|
|
//determine inital state
|
|
|
if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
|
|
@@ -82,7 +102,7 @@ void *coffeeThread(void *threadid) {
|
|
|
if (SigValueEmpty())
|
|
|
pause();
|
|
|
if (getSigValue() == SigInt0Rls) {
|
|
|
- if (halProxSensorCovered()) { //Check Waterlevel
|
|
|
+ if (!halProxSensorCovered()) { //Check waterlevel in gray water tank
|
|
|
//turn machine on
|
|
|
halMachineOn();
|
|
|
sleep(1);
|
|
@@ -92,12 +112,38 @@ void *coffeeThread(void *threadid) {
|
|
|
changeState(STATE_IDLE);
|
|
|
}
|
|
|
} else {
|
|
|
+ logger_error("Empty Tank please!\n");
|
|
|
changeState(STATE_ERROR);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ /*
|
|
|
+ *
|
|
|
+ */
|
|
|
+ case STATE_WAIT_OFF:
|
|
|
+ if (halIsHeating()) {
|
|
|
+ halMachineOff();
|
|
|
+ coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
+ writeBackCache();
|
|
|
+ changeState(STATE_OFF);
|
|
|
+ }
|
|
|
+ if (SigValueEmpty())
|
|
|
+ pause();
|
|
|
+ switch (getSigValue()) {
|
|
|
+ case SigPressOpn:
|
|
|
+ halMachineOff();
|
|
|
+ coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
+ writeBackCache();
|
|
|
+ changeState(STATE_OFF);
|
|
|
+ break;
|
|
|
|
|
|
+ case SigInt0Psh:
|
|
|
+ case SigInt1Psh:
|
|
|
+ changeState(STATE_INITALHEATING);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
/*
|
|
|
*
|
|
|
*/
|
|
@@ -109,8 +155,14 @@ void *coffeeThread(void *threadid) {
|
|
|
//Turn machine off again
|
|
|
halMachineOff();
|
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
+ writeBackCache();
|
|
|
changeState(STATE_OFF);
|
|
|
break;
|
|
|
+
|
|
|
+ case SigInt1Rls:
|
|
|
+ changeState(STATE_WAIT_OFF);
|
|
|
+ break;
|
|
|
+
|
|
|
case SigPressOpn:
|
|
|
//Inital heating finished
|
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
@@ -127,19 +179,28 @@ void *coffeeThread(void *threadid) {
|
|
|
pause();
|
|
|
switch (getSigValue()) {
|
|
|
case SigInt1RlsLong:
|
|
|
- //Turn machine off again
|
|
|
+ //Turn machine _immediately_ off again
|
|
|
halMachineOff();
|
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
+ writeBackCache();
|
|
|
changeState(STATE_OFF);
|
|
|
break;
|
|
|
+
|
|
|
+ case SigInt1Rls:
|
|
|
+ //turn machine off when heating is finished
|
|
|
+ changeState(STATE_WAIT_OFF);
|
|
|
+ break;
|
|
|
+
|
|
|
case SigPressOpn:
|
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
changeState(STATE_IDLE);
|
|
|
break;
|
|
|
+
|
|
|
case SigInt0Psh:
|
|
|
//start to brew a delicious coffee
|
|
|
changeState(STATE_BREW);
|
|
|
break;
|
|
|
+
|
|
|
case SigBrewOn:
|
|
|
//someone brews manually
|
|
|
changeState(STATE_BREWMANUAL);
|
|
@@ -154,17 +215,31 @@ void *coffeeThread(void *threadid) {
|
|
|
pause();
|
|
|
switch (getSigValue()) {
|
|
|
case SigInt1RlsLong:
|
|
|
- //Turn machine off again
|
|
|
+ //turn machine _immediately_ off
|
|
|
halMachineOff();
|
|
|
+ writeBackCache();
|
|
|
changeState(STATE_OFF);
|
|
|
break;
|
|
|
+
|
|
|
+ case SigInt1Rls:
|
|
|
+ //turn machine off when heating is finished
|
|
|
+ changeState(STATE_WAIT_OFF);
|
|
|
+ break;
|
|
|
+
|
|
|
case SigPressCls:
|
|
|
changeState(STATE_HEATING);
|
|
|
break;
|
|
|
+
|
|
|
case SigInt0Psh:
|
|
|
//start to brew a delicious coffee
|
|
|
- changeState(STATE_BREW);
|
|
|
+ if(!halProxSensorCovered()){
|
|
|
+ changeState(STATE_BREW);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ changeState(STATE_ERROR);
|
|
|
+ }
|
|
|
break;
|
|
|
+
|
|
|
case SigBrewOn:
|
|
|
//someone brews manually
|
|
|
changeState(STATE_BREWMANUAL);
|
|
@@ -177,7 +252,12 @@ void *coffeeThread(void *threadid) {
|
|
|
case STATE_BREW:
|
|
|
coffeeBrew();
|
|
|
logger(V_BREW, "Finishing brewing\n");
|
|
|
- changeState(STATE_IDLE);
|
|
|
+ if(!halProxSensorCovered()){
|
|
|
+ changeState(STATE_IDLE);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ changeState(STATE_ERROR);
|
|
|
+ }
|
|
|
break;
|
|
|
|
|
|
/*
|
|
@@ -202,8 +282,13 @@ void *coffeeThread(void *threadid) {
|
|
|
case STATE_ERROR:
|
|
|
if (SigValueEmpty())
|
|
|
pause();
|
|
|
+ switch (getSigValue()) {
|
|
|
+ case SigInt1Psh:
|
|
|
+ case SigInt0Psh:
|
|
|
+ changeState(RETURN_STATE);
|
|
|
break;
|
|
|
}
|
|
|
+ }
|
|
|
}
|
|
|
pthread_exit(EXIT_SUCCESS);
|
|
|
}
|
|
@@ -218,7 +303,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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -244,9 +329,12 @@ bool SigValueEmpty(void) {
|
|
|
* @param newState
|
|
|
*/
|
|
|
void changeState(coffee_status_t newState) {
|
|
|
+ if(newState == RETURN_STATE)
|
|
|
+ newState = lastState;
|
|
|
+
|
|
|
logger(V_BREW, "Changing state to %s\n", StateName[newState]);
|
|
|
+ lastState = state;
|
|
|
state = newState;
|
|
|
-
|
|
|
event_trigger("statechange", &state, sizeof(state));
|
|
|
}
|
|
|
|
|
@@ -268,11 +356,28 @@ void brewTimeHandler(void) {
|
|
|
/**
|
|
|
* handles program termination
|
|
|
*/
|
|
|
-void coffeeTerminate(void) {
|
|
|
- logger_error("Coffee thread terminated");
|
|
|
+void coffeeTerminate(event_t *event) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -326,7 +431,6 @@ void coffeeBrew(void) {
|
|
|
halResetFlow();
|
|
|
brewTimer.stop();
|
|
|
brewTime = 0;
|
|
|
- changeState(STATE_IDLE);
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -334,24 +438,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;
|
|
|
}
|