1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027 |
- /*
- * coffee.cpp
- *
- * Created on: Sep 25, 2017
- * Author: sebastian
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <stdint.h>
- #include <wiringPi.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <iostream>
- #include <csignal>
- #include <time.h>
- #include <ctime>
- #include "coffee.h"
- #include "hal.h"
- #include "logger.h"
- #include "timer.h"
- #include "database.h"
- coffee_status_t state;
- coffee_menuPage_t page;
- coffee_mode_t mode;
- int sigValue;
- int menuTimeout;
- timer menuTimer(&menuTimerHandler);
- timer brewTimer(&brewTimerHandler);
- uint64_t totalHeatingTime; //local copies of the corresponding database entries
- uint16_t brewCounter;
- uint16_t brewTime;
- uint16_t currentCleanCycle;
- bool initalHeating;
- bool descaling; //flag to indicate descaling and cleaning
- uint16_t descBrewcount;
- time_t descRawTimestamp;
- const char* PageName[] = { "SoftOff", "Kill", "Stats", "Stats2", "Descaling", "Temp", "Clean", "Restart",
- "Exit" };
- const char* StateName[] = { "OFF", "HEATING", "INITHEAT", "IDLE", "BREW",
- "BREWMAN", "CLEAN", "ERROR", "WAITOFF" };
- /**
- * Thread for the finite state machine
- * It represents the current state of the machine and handles signals coming from
- * the pressure control, buttons, the brew switch and the proximity sensor
- * @param threadid the ID of the thread
- */
- void *coffeeThread(void *threadid) {
- logger(V_BASIC, "Initializing coffee thread...\n");
- //installing new Signal handler for coffeethread
- struct sigaction action;
- sigemptyset(&action.sa_mask);
- action.sa_flags = SA_SIGINFO;
- action.sa_sigaction = coffeeHandler;
- sigaction(SIGUSR2, &action, NULL);
- currentCleanCycle = 0;
- menuTimer.setDivider(4);
- menuTimer.stop();
- menuTimeout = 0;
- brewTimer.setDivider(2);
- brewTimer.stop();
- brewTime = 0;
- initalHeating = true;
- mode = MODE_STATE; //Unless we enter the menu we start in state mode
- page = PAGE_SOFTOFF;
- descaling = false;
- //read the database values
- if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
- logger_error("coffee.cpp: Couldn't read the heating time from the database\n");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
- logger_error("coffee.cpp: Couldn't read the brew counter from the database\n");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(descRawTimestamp = (time_t) sqlGetConf(CFGDescTimestamp))) {
- logger_error("coffee.cpp: Couldn't read the descaling time from the database\n");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(descBrewcount = sqlGetConf(CFGDescBrewCount))) {
- logger_error("coffee.cpp: Couldn't read the descaling brewcount time from the database\n");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- checkDescaling();
- event_subscribe("terminate", &coffeeTerminate, "coffee.cpp");
- logger(V_BREW, "Determining inital state\n");
- //determine inital state
- if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
- && !halGetRelaisState(RELAIS_PUMP)) {
- //wait for heat relais to switch
- coffeeNap(1,0);
- if (halIsHeating()) { //Heating is on
- changeState(STATE_INITALHEATING);
- } else {
- initalHeating = false;
- changeState(STATE_IDLE);
- }
- } else if (halGetRelaisState(RELAIS_PUMP)) {
- logger_error("Whoops, why is the pump running...\n");
- changeState(STATE_ERROR);
- } else {
- changeState(STATE_OFF);
- }
- logger(V_BREW, "Starting Coffee FSM\n");
- //begin FSM
- while (1) {
- /*
- * Menue FSM
- */
- switch (page) {
- case PAGE_NULL:
- break;
- case PAGE_SOFTOFF: //this page is only available when the machine is on
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- if(halIsHeating()){
- changeState(STATE_WAIT_OFF);
- }
- else {
- changeState(STATE_OFF);
- }
- leaveMenu();
- break;
- case SigRotCW:
- changePage(PAGE_KILL);
- break;
- case SigRotCCW:
- changePage(PAGE_EXIT);
- break;
- }
- break;
- case PAGE_KILL: //this page is only available when the machine is on
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- if (halIsHeating()) {
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- }
- changeState(STATE_OFF);
- leaveMenu();
- break;
- case SigRotCW:
- if (state == STATE_IDLE || state == STATE_HEATING) {
- changePage(PAGE_CLEAN);
- } else {
- changePage(PAGE_RESTART);
- }
- break;
- case SigRotCCW:
- changePage(PAGE_SOFTOFF);
- break;
- }
- break;
- case PAGE_CLEAN: //this page is only be available when the machine is hot
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- changeMode(MODE_STATE);
- if (!halProxSensorCovered()) {
- changeState(STATE_CLEANING);
- leaveMenu();
- } else {
- changeState(STATE_FULLTANK);
- leaveMenu();
- }
- break;
- case SigRotCW:
- changePage(PAGE_RESTART);
- break;
- case SigRotCCW:
- changePage(PAGE_KILL);
- break;
- }
- break;
- case PAGE_RESTART:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- event_trigger("terminate");
- coffeeNap(4, 0);
- exit(EXIT_SUCCESS);
- break;
- case SigRotCW:
- changePage(PAGE_TEMP);
- break;
- case SigRotCCW:
- if(state == STATE_IDLE || state == STATE_HEATING){
- changePage(PAGE_CLEAN);
- } else if (state == STATE_ERROR || state == STATE_INITALHEATING) {
- changePage(PAGE_KILL);
- } else {
- changePage(PAGE_EXIT);
- }
- break;
- }
- break;
- case PAGE_TEMP:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigRotCW:
- changePage(PAGE_STATS);
- break;
- case SigRotCCW:
- changePage(PAGE_RESTART);
- break;
- }
- break;
- case PAGE_STATS:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigRotCW:
- changePage(PAGE_STATS2);
- break;
- case SigRotCCW:
- changePage(PAGE_TEMP);
- break;
- }
- break;
- case PAGE_STATS2:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigRotCW:
- changePage(PAGE_DESCALING);
- break;
- case SigRotCCW:
- changePage(PAGE_STATS);
- break;
- }
- break;
- case PAGE_DESCALING:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigRotCW:
- changePage(PAGE_EXIT);
- break;
- case SigRotCCW:
- changePage(PAGE_STATS2);
- break;
- }
- break;
- case PAGE_EXIT:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- leaveMenu();
- break;
- case SigRotCW:
- if (state == STATE_HEATING || state == STATE_ERROR
- || state == STATE_IDLE
- || state == STATE_INITALHEATING) {
- changePage(PAGE_SOFTOFF);
- } else {
- changePage(PAGE_RESTART);
- }
- break;
- case SigRotCCW:
- changePage(PAGE_DESCALING);
- break;
- }
- break;
- } //end switch (page)
- /*
- * Hardware FSM
- */
- switch (state) {
- case STATE_NULL:
- break;
- /*
- *
- */
- case STATE_OFF:
- if (mode == MODE_STATE) {
- halMachineOff();
- writeBackCache();
- //TODO this might be a bit confusing to change the page here even if the menu isnt actually displayed
- changePage(PAGE_RESTART);
- if (SigValueEmpty())
- pause();
- }
- switch (getSigValue(MODE_STATE)) {
- case SigInt0Rls:
- case SigPowerUp:
- //Check waterlevel in gray water tank
- //turn machine on
- halMachineOn();
- coffeeNap(1,0);
- if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
- changeState(STATE_INITALHEATING);
- } else if (!halIsHeating() && !halProxSensorCovered()) {
- changeState(STATE_IDLE);
- } else if (halProxSensorCovered()) {
- logger_error("Empty Tank please!\n");
- changeState(STATE_FULLTANK);
- }
- if (page != PAGE_SOFTOFF)
- changePage(PAGE_SOFTOFF); //the machine is on, the menu starts with the turning off page
- break;
- case SigRotCCW:
- case SigRotCW:
- case SigInt1Rls:
- //Enter the menu
- /* This should be not necessary!
- * if (page != PAGE_DEMO)
- changePage(PAGE_DEMO); //machine is off, the menu starts with the demo page*/
- enterMenu();
- break;
- }
- break;
- /*
- *
- */
- case STATE_WAIT_OFF:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigPressOpn:
- coffeeNap(1,0); //wait so no load will be switched
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- changeState(STATE_OFF);
- break;
- case SigInt0Rls:
- case SigInt1Rls:
- case SigPowerUp:
- if (halProxSensorCovered()) {
- changeState(STATE_FULLTANK);
- } else if (initalHeating) {
- changeState(STATE_INITALHEATING);
- } else {
- changeState(STATE_HEATING);
- }
- break;
- }
- break;
- /*
- *
- */
- case STATE_INITALHEATING:
- initalHeating = true;
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt0RlsLong:
- // //Turn machine off again
- // coffeeIncreaseHeatingTime(halgetHeatingTime());
- // changeState(STATE_OFF);
- // break;
- //
- // case SigInt0Rls:
- // changeState(STATE_WAIT_OFF);
- // break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigPressOpn:
- //Inital heating finished
- initalHeating = false;
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- changeState(STATE_IDLE);
- break;
- case SigPowerDown:
- changeState(STATE_WAIT_OFF);
- break;
- case SigRotCCW:
- case SigRotCW:
- case SigInt1Rls:
- enterMenu();
- break;
- }
- break;
- /*
- *
- */
- case STATE_HEATING:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt1RlsLong:
- // //Turn machine _immediately_ off again
- // coffeeIncreaseHeatingTime(halgetHeatingTime());
- // 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 SigInt0Rls:
- //start to brew a delicious coffee
- changeState(STATE_BREW);
- break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigBrewOn:
- //someone brews manually
- coffeeManualBrewStart();
- changeState(STATE_BREWMANUAL);
- break;
- case SigPowerDown:
- changeState(STATE_WAIT_OFF);
- break;
- case SigRotCCW:
- case SigRotCW:
- case SigInt1Rls:
- //Enter the menu
- enterMenu();
- break;
- }
- break;
- /*
- *
- */
- case STATE_IDLE:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt1RlsLong:
- // //turn machine _immediately_ off
- // changeState(STATE_OFF);
- // break;
- //
- // case SigInt1Rls:
- // changeState(STATE_OFF);
- // break;
- case SigPressCls:
- changeState(STATE_HEATING);
- break;
- case SigInt0Rls:
- changeState(STATE_BREW);
- break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigBrewOn:
- //someone brews manually
- coffeeManualBrewStart();
- changeState(STATE_BREWMANUAL);
- break;
- case SigPowerDown:
- changeState(STATE_OFF);
- break;
- case SigRotCCW:
- case SigRotCW:
- case SigInt1Rls:
- //Enter the menu
- enterMenu();
- break;
- }
- break;
- /*
- *
- */
- case STATE_BREW:
- //make sure the tank is not full
- if (halProxSensorCovered()) {
- changeState(STATE_FULLTANK);
- logger_error("coffee.cpp: Full tank detection failed..\n");
- } else {
- coffeeBrew();
- checkDescaling();
- logger(V_BREW, "Finishing brewing\n");
- if (!halProxSensorCovered()) {
- if (halIsHeating()) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- } else {
- changeState(STATE_FULLTANK);
- }
- }
- break;
- /*
- *
- */
- case STATE_BREWMANUAL:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigBrewOff:
- if (halIsHeating()) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- break;
- }
- break;
- /*
- *
- */
- case STATE_CLEANING: //this can only be executed once the machine is hot!
- if (!halProxSensorCovered()) {
- //execute the cleaning procedure
- coffeeClean();
- if (halIsHeating()) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- } else {
- changeState(STATE_FULLTANK);
- }
- break;
- /*
- * Full tank is detected at the beginning and the end of a brewing process, during
- * idle times, initial heating and heating
- */
- case STATE_FULLTANK:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigInt1Rls:
- case SigInt0Rls:
- if (halIsHeating() && initalHeating) {
- changeState(STATE_INITALHEATING);
- } else if (halIsHeating() && !initalHeating) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- break;
- }
- break;
- /*
- *
- */
- case STATE_ERROR:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigInt0RlsLong:
- if (halIsHeating()) {
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- }
- changeState(STATE_OFF);
- break;
- }
- }
- }
- pthread_exit(EXIT_SUCCESS);
- }
- /**
- * Handler for the Signal send to this thread
- * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
- * The time is stored in the HalEvent variable when a release event is received
- * @param signum
- * @param siginfo
- * @param context
- */
- void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
- sigval_t sigVal = (siginfo->si_value);
- sigValue = sigVal.sival_int;
- logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %s\n",
- SigName[sigValue]);
- }
- /**
- * returns the Signal value from the last received Signal for the given mode and clears the variable
- * @return value sent with the last signal
- */
- int getSigValue(coffee_mode_t mode) {
- int tmp = sigValue;
- if (mode == MODE_MENU) {
- switch (sigValue) {
- case SigInt0Psh:
- case SigInt0Rls:
- case SigInt0RlsLong:
- case SigInt1Psh:
- case SigInt1Rls:
- case SigRotCCW:
- case SigRotCW:
- sigValue = 0;
- menuTimeout = 0;
- return tmp;
- break;
- default:
- break;
- }
- } else { //State Mode
- sigValue = 0;
- return tmp;
- }
- //int tmp = sigValue;
- //sigValue = 0;
- //return tmp;
- return 0;
- }
- bool SigValueEmpty(void) {
- if (sigValue == 0)
- return true;
- else
- return false;
- }
- /**
- * Changes the state of the machine to newState
- * prints the change to the logger
- * @param newState
- */
- void changeState(coffee_status_t newState) {
- logger(V_BREW, "Changing state to %s\n", StateName[newState]);
- state = newState;
- event_trigger("statechange", &state, sizeof(state));
- }
- /*
- * Change Page to new menu page
- */
- void changePage(coffee_menuPage_t newPage) {
- logger(V_BREW, "Change Page to %s\n", PageName[newPage]);
- event_trigger("pagechange", &newPage, sizeof(newPage));
- page = newPage;
- }
- /*
- * Changes the mode of the machine to the given mode
- */
- void changeMode(coffee_mode_t newMode) {
- if (newMode == MODE_MENU)
- logger(V_BREW, "Changing to menu mode\n");
- else
- logger(V_BREW, "Changing to state mode\n");
- event_trigger("modechange", &newMode, sizeof(newMode));
- mode = newMode;
- }
- /*
- * leaving the menu
- * sets the start page for the next menu call to softoff
- */
- void leaveMenu(void) {
- logger(V_BREW, "Leaving the menu again\n");
- //leave the menu again
- changeMode(MODE_STATE);
- //change page to initial page
- changePage(PAGE_SOFTOFF);
- //stop the timeout counter
- menuTimeout = 0;
- menuTimer.stop();
- }
- /**
- * entering the menu
- * starts the timeoutcounter and changes the mode to the Menu Mode
- */
- void enterMenu(void) {
- logger(V_BREW, "Entering the menu\n");
- changeMode(MODE_MENU);
- menuTimeout = 0;
- menuTimer.start();
- }
- /**
- * Returns the current cleaning cycle
- */
- uint16_t getCurrentCleanCycle(void) {
- return currentCleanCycle;
- }
- /**
- * Returns the current state of the FSM
- */
- coffee_status_t getState(void) {
- return state;
- }
- /**
- * Returns the local up-to-date brewcounter
- */
- uint16_t getBrewCounter(void) {
- return brewCounter;
- }
- /**
- * Returns the total heating time in seconds
- */
- uint64_t getTotalHeatingTime(void) {
- return totalHeatingTime;
- }
- /**
- * Returns the value of the brewcounter when the last descaling happened
- */
- uint16_t getDescBrewCounter (void) {
- return descBrewcount;
- }
- /**
- * Returns the raw time stamp when the last descaling happened
- */
- time_t * getDescTimestamp (void) {
- return &descRawTimestamp;
- }
- /**
- * Counter for the menu timeout
- * When no input is coming from the user the machine leaves the menu automatically after MENUTIMEOUT seconds
- */
- void menuTimerHandler(void){
- menuTimeout += 200;
- if((menuTimeout/1000) >= MENUTIMEOUT) {
- leaveMenu();
- }
- }
- /**
- * handles program termination
- */
- void coffeeTerminate(event_t *event) {
- logger(V_BREW, "Coffee.cpp: Terminating\n");
- //stop brewing
- halRelaisOff(RELAIS_PUMP);
- writeBackCache();
- }
- /**
- * coffeeNap does almost the same as sleep() but resumes to sleep after a signal got delivered
- * sec: seconds to sleep
- * nanosec: nanoseconds to sleep
- */
- void coffeeNap (uint64_t sec, uint64_t nanosec){
- struct timespec sleepTime;
- clock_gettime(CLOCK_REALTIME, &sleepTime);
- sleepTime.tv_sec += sec;
- sleepTime.tv_nsec += nanosec;
- //overflow
- if(sleepTime.tv_nsec >= 1000000000) {
- sleepTime.tv_nsec -= 1000000000;
- sleepTime.tv_sec++;
- }
- int errval = 0;
- do{
- errval = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &sleepTime, NULL);
- }
- while(errval == EINTR);
- if(errval) logger_error("coffee.cpp: suspending thread failed with error %d\n", errval);
- }
- /**
- * Function to write back the values of the local copies
- * brewCounter and totalHeatingTime
- *
- */
- void writeBackCache(void) {
- logger(V_BREW, "Writing back cache...\n");
- logger(V_BREW, "Writing back brewCounter with %d\n", brewCounter);
- if (sqlSetConf(CFGbrewcounter, brewCounter)) {
- logger_error("coffee.cpp: Couldn't write brewcounter to database");
- return;
- }
- logger(V_BREW, "Writing back total heating Time with %lld sec\n", totalHeatingTime);
- if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
- logger_error("coffee.cpp: Couldn't write heating time to database");
- return;
- }
- logger(V_BREW, "Writing back descaling brew counter %d\n", descBrewcount);
- if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
- logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
- return;
- }
- struct tm * timeinfo;
- timeinfo = localtime(&descRawTimestamp);
- logger(V_BREW, "Writing back descaling timestamp %d-%d-%d %d:%d\n", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min);
- if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
- logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
- return;
- }
- }
- /*
- * Procedure for cleaning the machine
- */
- void coffeeClean(void) {
- logger(V_BREW, "Cleaning...\n");
- for (int i = 0; i < CLEANING_CYCLES; i++) {
- currentCleanCycle++;
- halPumpOn();
- coffeeNap(3,0);
- halPumpOff();
- coffeeNap(15,0);
- }
- updateDescaling();
- descaling = false;
- currentCleanCycle = 0;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- /*
- *
- */
- void coffeeManualBrewStart (void) {
- logger(V_BREW, "Starting manual brewing...\n");
- coffeeIncreaseBrewCounter();
- }
- /**
- * Brewing process
- */
- void coffeeBrew(void) {
- coffeeIncreaseBrewCounter();
- /*
- * Preinfusion
- */
- logger(V_BREW, "Starting preinfusion...\n");
- halPumpOn();
- while (halGetFlow() < AMOUNT_PREINFUSION && halGetFlowTime() < TIME_PREINFUSION) {
- //TODO don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
- coffeeNap(0, 50000000);
- if (getSigValue(MODE_STATE) == SigInt0Rls){
- halPumpOff();
- return;
- }
- }
- halPumpOff();
- /*
- * Wait for coffee to soak in infused water
- */
- brewTimer.start();
- while (brewTime < TIME_SOAK) {
- coffeeNap(1, 100000000);
- if (getSigValue(MODE_STATE) == SigInt0Rls) {
- brewTimer.stop();
- brewTime = 0;
- return;
- }
- }
- brewTimer.stop();
- brewTime = 0;
- /*
- * Brewing the actual espresso
- */
- logger(V_BREW, "Starting infusion...\n");
- halPumpOn();
- while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
- coffeeNap(1, 100000000);
- if (getSigValue(MODE_STATE) == SigInt0Rls){
- halPumpOff();
- return;
- }
- }
- halPumpOff();
- return;
- }
- void brewTimerHandler(){
- brewTime += 200;
- }
- /**
- *
- */
- void coffeeIncreaseBrewCounter(void) {
- brewCounter++;
- }
- /**
- *
- */
- void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
- totalHeatingTime += heatingTime;
- }
- /**
- * Checks if the descaling is necessary
- * uses descBrewcount and descTimestamp
- */
- void checkDescaling(){
- int16_t dirtyEspresso = checkDirtyEspresso ();
- int16_t dirtyTime = checkDirtyTime ();
- if(dirtyEspresso <= 0) {
- logger(V_BREW, "Descaling necessary due to quantity: %d\n", dirtyEspresso);
- descaling = true;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- if(dirtyTime <= 0) {
- logger(V_BREW, "Descaling necessary due to time in days: %d\n", dirtyTime);
- descaling = true;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- }
- /**
- * this function returns the remaining espressi to be brewed before the descaling event is fired
- * returns a positive integer when there are cups remaining
- * and a negative when the number of cups are exceeded
- * Number of cups after a descaling is defined with DIRTY_ESPRESSO
- */
- int16_t checkDirtyEspresso (void) {
- return descBrewcount + DIRTY_ESPRESSO - brewCounter;
- }
- /**
- * Returns the remaining days before the next descaling event is fired
- * returns a positive integer if there is time left and a negative one if the descaling time was exceeded
- */
- int16_t checkDirtyTime (void) {
- time_t rawtime;
- time(&rawtime);
- double diffseconds = difftime(rawtime, descRawTimestamp);
- diffseconds /= 24*60*60; //calculate the days
- return DIRTY_TIME - diffseconds;
- }
- /**
- * updates the corresponding variables after a descaling process
- */
- void updateDescaling(){
- descBrewcount = brewCounter;
- time_t newDesTimestamp;
- time(&newDesTimestamp);
- if(newDesTimestamp == -1){
- logger(V_BREW, "Whoops, couldn't retrieve new descaling timestamp\n");
- }
- else {
- descRawTimestamp = newDesTimestamp;
- }
- }
|