# HG changeset patch # User Michiel Broek # Date 1399667382 -7200 # Node ID 2357e8636a6077aae203dbcd5220fb094be05e88 # Parent 3bac8fd4173d1c5bc1b13d605ffa3819ea148f48 Added user log with temperatures and cooler state diff -r 3bac8fd4173d -r 2357e8636a60 coolers/coolers.c --- a/coolers/coolers.c Fri May 09 20:39:01 2014 +0200 +++ b/coolers/coolers.c Fri May 09 22:29:42 2014 +0200 @@ -28,8 +28,8 @@ #ifdef HAVE_WIRINGPI_H -int tempA = 170; -int tempB = 250; +int tempA = 80; +int tempB = 80; int coolerA = 0; int coolerB = 0; @@ -239,7 +239,7 @@ char buf[1024]; w1_therm *tmp1, *old1; rc_switch *tmp2, *old2; - int rc, run = 0; + int rc, run = 0, temp; my_mosquitto_init(); @@ -249,6 +249,9 @@ syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); } + snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); + logger((char *)"coolers.log", (char *)"coolers", buf); + do { lcdupdate = FALSE; @@ -260,11 +263,13 @@ 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; @@ -274,11 +279,13 @@ 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) { @@ -286,11 +293,15 @@ tmp1 = Config.w1therms; snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias); 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); 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); diff -r 3bac8fd4173d -r 2357e8636a60 lib/logger.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/logger.c Fri May 09 22:29:42 2014 +0200 @@ -0,0 +1,65 @@ +/***************************************************************************** + * Copyright (C) 2014 + * + * Michiel Broek + * + * 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 "../config.h" +#include "mbselib.h" + +static int lastmin = 70; + +void logger(char *filename, char *progname, char *data) +{ + struct timeval now; + struct tm ptm; + char *outstr = NULL, *name = NULL; + FILE *logfile; + + name = xstrcpy((char *)"/var/local/log/"); + name = xstrcat(name, progname); + name = xstrcat(name, (char *)"/"); + name = xstrcat(name, filename); + + gettimeofday(&now, NULL); + localtime_r(&now.tv_sec, &ptm); + + /* + * Log no more then once per minute + */ + if (lastmin != ptm.tm_min) { + if ((logfile = fopen(name, "a+"))) { + outstr = calloc(10240, sizeof(char)); + snprintf(outstr, 10239, "%04d-%02d-%02d %02d:%02d,%s\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, data); + + fprintf(logfile, outstr); + fclose(logfile); + free(outstr); + outstr = NULL; + } else { + syslog(LOG_NOTICE, "logger: cannot open %s for writing", name); + } + lastmin = ptm.tm_min; + } + + free(name); + name = NULL; +} + +