coolers/coolers.c

changeset 41
f534ace74eea
parent 40
dafbbd5e9922
child 42
01b96a24ae7c
--- a/coolers/coolers.c	Sat May 17 23:06:39 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,423 +0,0 @@
-/*****************************************************************************
- * 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 "../lib/mbselib.h"
-#include "coolers.h"
-#include "mosquitto.h"
-#include "sensors.h"
-
-#ifdef HAVE_WIRINGPI_H
-
-
-int			tempA = 80;
-int			tempB = 80;
-int			coolerA = 0;
-int			coolerB = 0;
-
-bool			my_shutdown = false;
-static pid_t		pgrp, mypid;
-
-extern bool		debug;
-extern sys_config	Config;
-extern int		lcdHandle;
-int			lcdupdate;
-
-int			sock = -1;		/* Datagram socket	*/
-struct sockaddr_un	servaddr;		/* Server address	*/
-struct sockaddr_un	from;			/* From address		*/
-static char		spath[PATH_MAX];	/* Socket path		*/
-socklen_t		fromlen;
-
-
-int server(void);
-void help(void);
-void die(int);
-
-
-void help(void)
-{
-    fprintf(stdout, "mbsePi-apps coolers v%s starting\n\n", VERSION);
-    fprintf(stdout, "Usage: coolers [-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;
-}
-
-
-
-void stopLCD(void)
-{
-    mb_lcdClear(lcdHandle);
-    setBacklight(0);
-}
-
-
-
-void stopRCswitch(void)
-{
-    rc_switch   *tmp, *old;
-    char	*cmd = NULL;
-    int		rc;
-
-    for (tmp = Config.rcswitch; tmp; tmp = old) {
-	old = tmp->next;
-	cmd = xstrcpy(tmp->address);
-	cmd = xstrcat(cmd, (char *)",0");
-	rc = toggleSwitch(cmd);
-	if (debug)
-	    fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
-	syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
-	free(cmd);
-	cmd = NULL;
-    }
-}
-
-
-
-int main(int argc, char *argv[])
-{
-    int		rc, c, i;
-    pid_t	frk;
-    char	buf[80];
-
-    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("coolers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
-    syslog(LOG_NOTICE, "mbsePi-apps coolers v%s starting", VERSION);
-    if (debug)
-	fprintf(stdout, "mbsePi-apps coolers v%s starting\n", VERSION);
-
-    if (rdconfig((char *)"coolers.conf")) {
-	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);
-    }
-
-    if (wiringPiSetup () )
-	return 1;
-
-    if ((rc = initLCD (16, 2))) {
-	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
-	return 1;
-    }
-
-    lcdPosition(lcdHandle, 0, 0);
-    sprintf(buf, "Coolers");
-    mb_lcdPuts(lcdHandle, buf);
-    lcdPosition(lcdHandle, 0, 1);
-    sprintf(buf, "Version %s", VERSION);
-    mb_lcdPuts(lcdHandle, buf);
-
-    if (Config.tx433 != -1) {
-	if (debug)
-	    fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433);
-	syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433);
-    	enableTransmit(Config.tx433);
-    }
-
-    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");
-			stopLCD();
-			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];
-    w1_therm		*tmp1, *old1;
-    rc_switch		*tmp2, *old2;
-    int			rc, run = 0, temp, rlen;
-    struct pollfd	pfd[1];
-
-
-    if (lockprog((char *)"coolers")) {
-	syslog(LOG_NOTICE, "Can't lock");
-	return 1;
-    }
-
-    my_mosquitto_init();
-
-    rc = piThreadCreate(my_sensors_loop);
-    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);
-    }
-
-    /*
-     * Setup socket so that clients can communicate with this server.
-     */
-    if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
-	fprintf(stderr, "Can't create socket\n");
-	return 1;
-    }
-
-    snprintf(spath, PATH_MAX, "/var/run/coolers.sock");
-    memset(&servaddr, 0, sizeof(servaddr));
-    servaddr.sun_family = AF_UNIX;
-    strcpy(servaddr.sun_path, spath);
-
-    if (bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
-	close(sock);
-	sock = -1;
-	fprintf(stderr, "Can't bind socket %s\n", spath);
-	return 1;
-    }
-    if (chmod(spath, 0666)) {
-	fprintf(stderr, "Can't chmod 0666 %s\n", spath);
-	close(sock);
-	sock = -1;
-	unlink(spath);
-	return 1;
-    }
-    if (debug)
-	fprintf(stdout, "Unix socket created\n");
-
-    snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB");
-    logger((char *)"coolers.log", (char *)"coolers", buf);
-
-    do {
-	/*
-	 * Poll UNIX Datagram socket the defined timeout of one second.
-	 * This means we listen if a client program has something to tell us.
-	 * Timeout is one second, after the timeout the rest of the mainloop is executed.
-	 */
-	pfd[0].fd = sock;
-	pfd[0].events = POLLIN;
-	pfd[0].revents = 0;
-	rc = poll(pfd, 1, 1000);
-
-	if (rc == -1) {
-	    /* 
-	     *  Poll can be interrupted by a finished child so that's not a real error.
-	     */
-	    if (errno != EINTR) {
-		syslog(LOG_NOTICE, "poll() rc=%d sock=%d, events=%04x", rc, sock, pfd[0].revents);
-	    }
-	} else if (rc) {
-	    if (pfd[0].revents & POLLIN) {
-		/*
-		 * Process the clients request for mbtask commands.
-		 */
-		memset(&buf, 0, sizeof(buf));
-		fromlen = sizeof(from);
-		rlen = recvfrom(sock, buf, sizeof(buf) -1, 0, (struct sockaddr *)&from, &fromlen);
-		if (rlen == -1) {
-		    syslog(LOG_NOTICE, "recvfrom() for socket receiver");
-		} else {
-//		    do_cmd(buf);
-		}
-	    }
-	}
-
-	lcdupdate = FALSE;
-
-	run = my_mosquitto_loop();
-
-	tmp1 = Config.w1therms;
-	tmp2 = Config.rcswitch;
-	if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
-	    my_mosquitto_switch(tmp2->address, 0);
-	    coolerA = 0;
-	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
-	    lcdupdate = TRUE;
-	}
-	if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
-	    my_mosquitto_switch(tmp2->address, 1);
-	    coolerA = 1;
-	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
-	    lcdupdate = TRUE;
-	}
-	old1 = tmp1->next;
-	tmp1 = old1;
-	old2 = tmp2->next;
-	tmp2 = old2;
-	if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
-	    my_mosquitto_switch(tmp2->address, 0);
-	    coolerB = 0;
-	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
-	    lcdupdate = TRUE;
-	}
-	if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
-	    my_mosquitto_switch(tmp2->address, 1);
-	    coolerB = 1;
-	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
-	    lcdupdate = TRUE;
-	}
-
-	if (run && lcdupdate) {
-	    lcdPosition(lcdHandle, 0, 0);
-	    tmp1 = Config.w1therms;
-	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias);
-	    mb_lcdPuts(lcdHandle, buf);
-	    temp = tmp1->lastval;
-	    old1 = tmp1->next;
-	    tmp1 = old1;
-	    lcdPosition(lcdHandle, 0, 1);
-	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '+' : ' ', tmp1->alias);
-	    mb_lcdPuts(lcdHandle, buf);
-	    snprintf(buf, 1023, "%.1f,%s,%.1f,%s", temp / 1000.0, coolerA ? (char *)"on" : (char *)"off",
-			    			 tmp1->lastval / 1000.0, coolerB ? (char *)"on" : (char *)"off");
-	    logger((char *)"coolers.log", (char *)"coolers", buf);
-	}
-	usleep(100000);
-
-    } while (run);
-
-    if (debug)
-	fprintf(stdout, (char *)"Out of loop\n");
-
-    /*
-     * Give threads time to cleanup
-     */
-    usleep(1500000);
-
-    if (Config.tx433 != -1) {
-	stopRCswitch();
-    }
-
-    my_mosquitto_exit();
-    stopLCD();
-    disableTransmit();
-
-    ulockprog((char *)"coolers");
-    unlink(spath);
-
-    if (debug)
-	fprintf(stdout, "Goodbye\n");
-
-    return 0;
-}
-
-#else
-
-
-int main(int argc, char *argv[])
-{
-    fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
-    fprintf(stderr, "This program is useless and will do nothing.\n");
-    return 0;
-}
-
-
-#endif

mercurial