Sat, 28 Nov 2015 20:15:38 +0100
Added automate state file.
/***************************************************************************** * Copyright (C) 2015 * * Michiel Broek <mbroek at mbse dot eu> * * This file is part of the mbsePi-apps * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * mbsePi-apps is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ThermFerm; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ #include "brewco.h" #include "rdconfig.h" #include "rdsession.h" #include "futil.h" #include "xutil.h" #include "lcd-pcf8574.h" #include "slcd.h" #include "lock.h" #include "devices.h" #include "keyboard.h" #include "simulator.h" int my_shutdown = FALSE; extern int debug; extern sys_config Config; extern int lcdHandle; extern int slcdHandle; extern int sock; #ifndef HAVE_WIRINGPI_H pthread_t threads[5]; #endif void help(void); void die(int); void help(void) { fprintf(stdout, "mbsePi-apps brewco v%s starting\n\n", VERSION); fprintf(stdout, "Usage: brewco [-d] [-h]\n"); fprintf(stdout, " -d --debug Debug and run in foreground\n"); fprintf(stdout, " -h --help Display this help\n"); } void die(int onsig) { switch (onsig) { case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); break; case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); break; case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); break; case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); my_shutdown = TRUE; exit(SIGSEGV); break; default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } my_shutdown = TRUE; } void prompt(int index) { char message[81]; int line; switch (index) { case 0: #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdClear(lcdHandle); #endif slcdClear(slcdHandle); #ifdef HAVE_WIRINGPI_H piUnlock(LOCK_LCD); #endif return; case 101: snprintf(message, Config.lcd_cols + 1, " Brewco %s ", VERSION); break; case 102: snprintf(message, Config.lcd_cols + 1, " SETUP MENU "); break; case 103: snprintf(message, Config.lcd_cols + 1, " AUTOMATIC MODE "); break; case 104: snprintf(message, Config.lcd_cols + 1, " MANUAL MODE "); break; case 111: snprintf(message, Config.lcd_cols + 1, "AUTO --> Mash In "); break; case 112: snprintf(message, Config.lcd_cols + 1, "AUTO --> Fitasi "); break; case 113: snprintf(message, Config.lcd_cols + 1, "AUTO --> Glucanasi "); break; case 114: snprintf(message, Config.lcd_cols + 1, "AUTO --> Proteasi "); break; case 115: snprintf(message, Config.lcd_cols + 1, "AUTO --> B-amilasi "); break; case 116: snprintf(message, Config.lcd_cols + 1, "AUTO --> A-amilasi 1"); break; case 117: snprintf(message, Config.lcd_cols + 1, "AUTO --> A-amilasi 2"); break; case 118: snprintf(message, Config.lcd_cols + 1, "AUTO --> Mash Out "); break; case 121: snprintf(message, Config.lcd_cols + 1, "AUTO --> Hop xx "); break; case 202: snprintf(message, Config.lcd_cols + 1, " Manage Recipes "); break; case 203: snprintf(message, Config.lcd_cols + 1, " Manage Brewsystems "); break; case 204: snprintf(message, Config.lcd_cols + 1, " Manage Simulator "); break; case 205: snprintf(message, Config.lcd_cols + 1, " System parameters "); break; case 206: snprintf(message, Config.lcd_cols + 1, " Set Automation "); break; case 207: snprintf(message, Config.lcd_cols + 1, " Delay start? "); break; case 208: snprintf(message, Config.lcd_cols + 1, " Resume Process "); break; case 209: snprintf(message, Config.lcd_cols + 1, " Water Added? "); break; case 210: snprintf(message, Config.lcd_cols + 1, " Pump Prime "); break; case 211: snprintf(message, Config.lcd_cols + 1, " Setting Delay "); break; case 212: snprintf(message, Config.lcd_cols + 1, " To be started in "); break; case 213: snprintf(message, Config.lcd_cols + 1, " Iodine test "); break; case 214: snprintf(message, Config.lcd_cols + 1, " START COOLING "); break; case 215: snprintf(message, Config.lcd_cols + 1, " WHIRLPOOL "); break; case 216: snprintf(message, Config.lcd_cols + 1, " Timing Whirlpool "); break; case 217: snprintf(message, Config.lcd_cols + 1, " Brewing Process "); break; case 218: snprintf(message, Config.lcd_cols + 1, " Add Brewsystem? "); break; case 301: snprintf(message, Config.lcd_cols + 1, " Finished "); break; case 302: snprintf(message, Config.lcd_cols + 1, " Shutting down "); break; case 401: snprintf(message, Config.lcd_cols + 1, " --- MAN AUTO SETUP"); break; case 402: snprintf(message, Config.lcd_cols + 1, " --- dwn quit ok "); break; case 403: snprintf(message, Config.lcd_cols + 1, " up dwn quit ok "); break; case 404: snprintf(message, Config.lcd_cols + 1, " up --- quit ok "); break; case 405: snprintf(message, Config.lcd_cols + 1, " UP* *DWN heat pmp "); break; case 406: snprintf(message, Config.lcd_cols + 1, " --- --- No Yes "); break; case 407: snprintf(message, Config.lcd_cols + 1, " --- --- Ok --- "); break; case 408: snprintf(message, Config.lcd_cols + 1, " UP* *DWN Pause --- "); break; case 409: snprintf(message, Config.lcd_cols + 1, " Continue: Yes No "); break; case 410: snprintf(message, Config.lcd_cols + 1, " UP* *DWN --- pmp "); break; case 411: snprintf(message, Config.lcd_cols + 1, " Up Dwn Exit Ok "); break; // 12345678901234567890 default: snprintf(message, Config.lcd_cols + 1, " N/A N/A"); } if (index < 200) line = 0; else if (index < 300) line = 1; else if (index < 400) line = 2; else line = 3; fprintf(stdout, "%d %d '%s'\n", line, Config.lcd_cols, message); #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdPosition(lcdHandle, 0, line); lcdPuts(lcdHandle, message); #endif slcdPosition(slcdHandle, 0, line); slcdPuts(slcdHandle, message); #ifdef HAVE_WIRINGPI_H piUnlock(LOCK_LCD); #endif } void editUnit(units_list *unit) { if (debug) fprintf(stdout, "Start edit brewsystem %d %s\n", unit->number, unit->uuid); if (debug) fprintf(stdout, "End edit brewsystem %d %s\n", unit->number, unit->uuid); } void addUnit(int number) { units_list *tmpu, *unit = (units_list *)malloc(sizeof(units_list)); char name[81]; uuid_t uu; if (debug) fprintf(stdout, "Adding new brewsystem %d\n", number); unit->next = NULL; unit->version = 1; unit->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, unit->uuid); snprintf(name, 20, "System %d", number); unit->name = xstrcpy(name); unit->number = number; if (number == 1) unit->active = 1; else unit->active = 0; unit->hlt_sensor = unit->mlt_sensor = NULL; unit->hlt_heater = unit->mlt_heater = unit->mlt_pump = NULL; unit->hlt_heater_mltfirst = 1; unit->pump_cycle = 7; unit->pump_rest = 2; unit->pump_premash = 1; unit->pump_onmash = 1; unit->pump_mashout = 0; unit->pump_onboil = 0; unit->pump_stop = 90; unit->skip_add = 0; unit->skip_remove = 0; unit->skip_iodine = 0; unit->iodine_time = 90; unit->whirlpool = 1; unit->PID_hlt = (pid_var *)malloc(sizeof(pid_var)); unit->PID_mlt = (pid_var *)malloc(sizeof(pid_var)); InitPID(unit->PID_hlt); InitPID(unit->PID_mlt); editUnit(unit); if (Config.units == NULL) { Config.units = unit; } else { for (tmpu = Config.units; tmpu; tmpu = tmpu->next) { if (tmpu->next == NULL) { tmpu->next = unit; break; } } } syslog(LOG_NOTICE, "Brewsystem %d added to the configuration", number); if (debug) fprintf(stdout, "Brewsystem %d added to the configuration\n", number); } int server(void); int server(void) { int rc = 0, run = 1, key; units_list *unit; brew_session *brew = NULL; #ifndef HAVE_WIRINGPI_H long t = 0; #endif // if (lockprog((char *)"brewco")) { // syslog(LOG_NOTICE, "Can't lock"); // return 1; // } if (debug) fprintf(stdout, "Begin server()\n"); if ((rc = devices_detect())) { syslog(LOG_NOTICE, "Detected %d new devices", rc); if (debug) fprintf(stdout, "Detected %d new devices\n", rc); wrconfig(); } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_devices_loop); #else rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_keyboard_loop); #else rc = pthread_create(&threads[t], NULL, my_keyboard_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_keyboard_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_keyboard_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef USE_SIMULATOR #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_simulator_loop); #else rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #endif /* * Initialize units for processing */ for (unit = Config.units; unit; unit = unit->next) { /* * Safety, turn everything off */ if (unit->active) { if (unit->hlt_heater) unit->hlt_heater->value = 0; if (unit->mlt_heater) unit->mlt_heater->value = 0; if (unit->mlt_pump) unit->mlt_pump->value = 0; } } prompt(101); if (! Config.units) { /* * No brewsystems defined, add the first */ prompt(218); /* Add Brewsystem? */ prompt(407); /* --- --- Ok --- */ do { key = keywait(); } while (key != KEY_RETURN); if (key == KEY_RETURN) { addUnit(1); } } /* * During automation there will be a state file: * ~/.brewco/var/brewing.xml * If this file is present, there has been a crash. */ brew = (brew_session *)malloc(sizeof(brew_session)); if (rdsession(brew) == 0) { } else { /* * No active brew session, make that permanent. */ free(brew); brew = NULL; } do { if (my_shutdown) { run = 0; break; } if (brew) { /* * Automate mode */ } else { /* * Not running. */ prompt(0); prompt(101); prompt(401); key = keywait(); } usleep(100000); } while (run); syslog(LOG_NOTICE, "Out of loop"); if (debug) fprintf(stdout, (char *)"Out of loop\n"); prompt(0); prompt(101); prompt(302); /* * Stop units processing in a neat way */ for (unit = Config.units; unit; unit = unit->next) { } /* * Give threads time to cleanup */ usleep(1500000); prompt(0); // stopLCD(); if (sock != -1) { if (shutdown(sock, SHUT_RDWR)) { syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno)); } sock = -1; } wrconfig(); // ulockprog((char *)"brewco"); return 0; } int main(int argc, char *argv[]) { int rc = 0, c, i; while (1) { int option_index = 0; static struct option long_options[] = { {"debug", 0, 0, 'c'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "dh", long_options, &option_index); if (c == -1) break; switch (c) { case 'd': debug = TRUE; break; case 'h': help(); return 1; } } openlog("brewco", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); syslog(LOG_NOTICE, "mbsePi-apps brewco v%s starting", VERSION); if (debug) fprintf(stdout, "mbsePi-apps brewco v%s starting\n", VERSION); if (rdconfig()) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "Error reading configuration: halted"); return 1; } if (debug) fprintf(stdout, "configuration loaded\n"); /* * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored * but that's live. This daemon should only be stopped by SIGTERM. * Don't catch SIGCHLD. */ for (i = 0; i < NSIG; i++) { if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) signal(i, (void (*))die); } #ifdef HAVE_WIRINGPI_H if (wiringPiSetup () ) return 1; #endif if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) { fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); return 1; } rc = server(); syslog(LOG_NOTICE, "Finished, rc=%d", rc); if (debug) fprintf(stdout, "Finished, rc=%d\n", rc); return rc; }