Wed, 23 Jul 2014 21:34:02 +0200
Removed sorting of 1-Wire devices because the busy indicator doesn't dissapear
/***************************************************************************** * Copyright (C) 2014 * * 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 EC-65K; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ #include "lock.h" #include "logger.h" #include "rdconfig.h" #include "sensors.h" #include "server.h" #include "thermferm.h" #include "lcd-pcf8574.h" #include "lcd-buffer.h" #include "futil.h" #include "units.h" #include "xutil.h" int current_unit = -1; int tempA = 80; int tempB = 80; key_t key = 5680; /* key to be passed to shmget() */ int shmid; int my_shutdown = FALSE; static pid_t pgrp, mypid; extern int debug; extern sys_config Config; #ifdef HAVE_WIRINGPI_H extern int lcdHandle; extern unsigned char lcdbuf[MAX_LCDS][20][4]; #endif int lcdupdate; #ifndef HAVE_WIRINGPI_H pthread_t threads[3]; #endif extern const char UNITMODE[5][8]; int server(void); void help(void); void die(int); #ifdef HAVE_WIRINGPI_H void sendRCswitch(char *, int); void stopLCD(void); void stopRCswitch(void); #endif void help(void) { fprintf(stdout, "mbsePi-apps thermferm v%s starting\n\n", VERSION); fprintf(stdout, "Usage: thermferm [-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; default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } my_shutdown = TRUE; } #ifdef HAVE_WIRINGPI_H void stopLCD(void) { mb_lcdClear(lcdHandle); setBacklight(0); } #endif int main(int argc, char *argv[]) { int rc, c, i; pid_t frk; 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("thermferm", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); syslog(LOG_NOTICE, "mbsePi-apps thermferm v%s starting", VERSION); if (debug) fprintf(stdout, "mbsePi-apps thermferm v%s starting\n", VERSION); if (rdconfig()) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "halted"); return 1; } /* * 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; if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) { fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); return 1; } #endif if (debug) { /* * For debugging run in foreground. */ rc = server(); } else { /* * Server initialization is complete. Now we can fork the * daemon and return to the user. We need to do a setpgrp * so that the daemon will no longer be assosiated with the * users control terminal. This is done before the fork, so * that the child will not be a process group leader. Otherwise, * if the child were to open a terminal, it would become * associated with that terminal as its control terminal. */ if ((pgrp = setpgid(0, 0)) == -1) { syslog(LOG_NOTICE, "setpgpid failed"); } frk = fork(); switch (frk) { case -1: syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); syslog(LOG_NOTICE, "Finished, rc=1"); #ifdef HAVE_WIRINGPI_H stopLCD(); #endif exit(1); case 0: /* * Run the daemon */ fclose(stdin); if (open("/dev/null", O_RDONLY) != 0) { syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); _exit(2); } fclose(stdout); if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); _exit(2); } fclose(stderr); if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); _exit(2); } mypid = getpid(); rc = server(); break; /* Not reached */ default: /* * Here we detach this process and let the child * run the deamon process. */ syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); exit(0); } } syslog(LOG_NOTICE, "Finished, rc=%d", rc); return rc; } int server(void) { char buf[1024], *filename, target[40], heater[40], cooler[40], fan[40], door[40]; time_t now, last = (time_t)0; w1_therm *tmp1; units_list *unit; int rc, run = 1, temp, seconds = 0; #ifdef HAVE_WIRINGPI_H struct tm *tm; int row; #else long t = 0; #endif if (lockprog((char *)"thermferm")) { syslog(LOG_NOTICE, "Can't lock"); return 1; } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_sensors_loop); #else rc = pthread_create(&threads[t], NULL, my_sensors_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_server_loop); #else rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_units_loop); #else rc = pthread_create(&threads[t], NULL, my_units_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_units_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_units_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef HAVE_WIRINGPI_H lcd_buf_write(1, (char *)" ThermFerm "); lcd_buf_write(2, (char *)" Version %s ", VERSION); #endif snprintf(buf, 1023, "tempA,tempB"); logger((char *)"thermferm.log", buf); for (unit = Config.units; unit; unit = unit->next) { if (unit->mode != UNITMODE_OFF) { snprintf(buf, 1023, "Mode,Air,Beer,Target,Heater,Cooler,Fan,Door"); filename = xstrcpy(unit->name); filename = xstrcat(filename, (char *)".log"); logger(filename, buf); free(filename); filename = NULL; } } do { lcdupdate = FALSE; if (my_shutdown) run = 0; for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) { if (tmp1->update) { tmp1->update = FALSE; lcdupdate = TRUE; } } /* * Timed schedulers */ now = time(NULL); if (now != last) { last = now; seconds++; #ifdef HAVE_WIRINGPI_H row = 3; tm = localtime(&now); lcd_buf_write(row++, " %02d-%02d-%04d ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900); lcd_buf_write(row++, " %02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec); for (unit = Config.units; unit; unit = unit->next) { if (unit->mode != UNITMODE_OFF) { lcd_buf_write(row++, "Unit %s ", unit->name); lcd_buf_write(row++, "Mode %s ", UNITMODE[unit->mode]); if (unit->air_address) { lcd_buf_write(row++, " Air %.3f %cC ", unit->air_temperature / 1000.0, 0xdf); } if (unit->beer_address) { lcd_buf_write(row++, "Beer %.3f %cC ", unit->beer_temperature / 1000.0, 0xdf); } } } lcd_buf_show(); #endif if (seconds == 60) { seconds = 0; if (Config.w1therms) { tmp1 = Config.w1therms; temp = tmp1->lastval; tmp1 = tmp1->next; if (temp && tmp1->lastval && run) { snprintf(buf, 1023, "%.2f,%.2f", temp / 1000.0, tmp1->lastval / 1000.0); logger((char *)"thermferm.log", buf); } } for (unit = Config.units; unit; unit = unit->next) { if (unit->mode != UNITMODE_OFF) { snprintf(target, 39, "NA"); snprintf(heater, 39, "NA"); snprintf(cooler, 39, "NA"); snprintf(fan, 39, "NA"); snprintf(door, 39, "NA"); if (unit->mode == UNITMODE_BEER) snprintf(target, 39, "%.1f", unit->beer_set); else if (unit->mode == UNITMODE_FRIDGE) snprintf(target, 39, "%.1f", unit->fridge_set); if (unit->io1_address) { if (unit->heater_available) { } if (unit->cooler_available) { } } if (unit->io2_address) { if (unit->fan_available) { } } snprintf(buf, 1023, "%s,%.3f,%.3f,%s,%s,%s,%s,%s", UNITMODE[unit->mode], unit->air_temperature / 1000.0, unit->beer_temperature / 1000.0, target, heater, cooler, fan, door); filename = xstrcpy(unit->name); filename = xstrcat(filename, (char *)".log"); logger(filename, buf); free(filename); filename = NULL; } } } } usleep(100000); } while (run); if (debug) fprintf(stdout, (char *)"Out of loop\n"); /* * Give threads time to cleanup */ usleep(1500000); #ifdef HAVE_WIRINGPI_H stopLCD(); #endif wrconfig(); ulockprog((char *)"thermferm"); if (debug) fprintf(stdout, "Goodbye\n"); return 0; }