|
@@ -22,13 +22,16 @@
|
|
#include "logger.h"
|
|
#include "logger.h"
|
|
#include "timer.h"
|
|
#include "timer.h"
|
|
#include "database.h"
|
|
#include "database.h"
|
|
-#include "events.h"
|
|
|
|
|
|
+
|
|
|
|
|
|
coffee_status_t state;
|
|
coffee_status_t state;
|
|
int sigValue;
|
|
int sigValue;
|
|
int brewTime; //Brew time in ms
|
|
int brewTime; //Brew time in ms
|
|
timer brewTimer(&brewTimeHandler);
|
|
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"};
|
|
const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
|
|
|
|
|
|
@@ -53,6 +56,18 @@ void *coffeeThread(void *threadid) {
|
|
brewTimer.stop();
|
|
brewTimer.stop();
|
|
brewTime = 0;
|
|
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");
|
|
logger(V_BREW, "Determining inital state\n");
|
|
//determine inital state
|
|
//determine inital state
|
|
if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
|
|
if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
|
|
@@ -109,6 +124,7 @@ void *coffeeThread(void *threadid) {
|
|
//Turn machine off again
|
|
//Turn machine off again
|
|
halMachineOff();
|
|
halMachineOff();
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
|
+ writeBackCache();
|
|
changeState(STATE_OFF);
|
|
changeState(STATE_OFF);
|
|
break;
|
|
break;
|
|
case SigPressOpn:
|
|
case SigPressOpn:
|
|
@@ -130,6 +146,7 @@ void *coffeeThread(void *threadid) {
|
|
//Turn machine off again
|
|
//Turn machine off again
|
|
halMachineOff();
|
|
halMachineOff();
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
coffeeIncreaseHeatingTime(halgetHeatingTime());
|
|
|
|
+ writeBackCache();
|
|
changeState(STATE_OFF);
|
|
changeState(STATE_OFF);
|
|
break;
|
|
break;
|
|
case SigPressOpn:
|
|
case SigPressOpn:
|
|
@@ -156,6 +173,7 @@ void *coffeeThread(void *threadid) {
|
|
case SigInt1RlsLong:
|
|
case SigInt1RlsLong:
|
|
//Turn machine off again
|
|
//Turn machine off again
|
|
halMachineOff();
|
|
halMachineOff();
|
|
|
|
+ writeBackCache();
|
|
changeState(STATE_OFF);
|
|
changeState(STATE_OFF);
|
|
break;
|
|
break;
|
|
case SigPressCls:
|
|
case SigPressCls:
|
|
@@ -268,11 +286,32 @@ void brewTimeHandler(void) {
|
|
/**
|
|
/**
|
|
* handles program termination
|
|
* 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
|
|
//stop brewing
|
|
halRelaisOff(RELAIS_PUMP);
|
|
halRelaisOff(RELAIS_PUMP);
|
|
brewTimer.stop();
|
|
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) {
|
|
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) {
|
|
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;
|
|
}
|
|
}
|