mash/mash.c

changeset 538
6d139c21e22c
parent 537
4eebab50993e
child 539
300b5c4cd977
equal deleted inserted replaced
537:4eebab50993e 538:6d139c21e22c
1 /*****************************************************************************
2 * Copyright (C) 2014
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with EC-65K; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "mash.h"
24 #include "lock.h"
25 #include "logger.h"
26 #include "xutil.h"
27 #include "beerxml.h"
28 #include "lcd-pcf8574.h"
29 #include "sensors.h"
30
31
32 #ifdef HAVE_WIRINGPI_H
33
34 /* wiringPi */
35 #include <wiringPi.h>
36 #include <pcf8574.h>
37 #include <lcd.h>
38
39 int tempA = 80;
40 int tempB = 80;
41 int coolerA = 0;
42 int coolerB = 0;
43
44 int my_shutdown = FALSE;
45 static pid_t pgrp, mypid;
46
47 extern int debug;
48 extern sys_config Config;
49 extern int lcdHandle;
50 extern unsigned char lcdbuf[MAX_LCDS][20][4];
51 int lcdupdate;
52
53
54 void help(void);
55 void die(int);
56 void stopLCD(void);
57 int server(void);
58
59
60 void help(void)
61 {
62 fprintf(stdout, "mbsePi-apps mash v%s starting\n\n", VERSION);
63 fprintf(stdout, "Usage: mash [-d] [-h]\n");
64 fprintf(stdout, " -d --debug Debug and run in foreground\n");
65 fprintf(stdout, " -h --help Display this help\n");
66 }
67
68
69
70 void die(int onsig)
71 {
72 switch (onsig) {
73 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
74 break;
75 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
76 break;
77 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
78 break;
79 default: syslog(LOG_NOTICE, "die() on signal %d", onsig);
80 }
81
82 my_shutdown = TRUE;
83 }
84
85
86
87 void stopLCD(void)
88 {
89 mb_lcdClear(lcdHandle);
90 setBacklight(0);
91 }
92
93
94
95 int main(int argc, char *argv[])
96 {
97 int rc, c, i;
98 pid_t frk;
99 char buf[80];
100
101 while (1) {
102 int option_index = 0;
103 static struct option long_options[] = {
104 {"debug", 0, 0, 'c'},
105 {"help", 0, 0, 'h'},
106 {0, 0, 0, 0}
107 };
108
109 c = getopt_long(argc, argv, "dh", long_options, &option_index);
110 if (c == -1)
111 break;
112
113 switch (c) {
114 case 'd': debug = TRUE;
115 break;
116 case 'h': help();
117 return 1;
118 }
119 }
120
121
122 parseBeerXML((char *)"/home/mbroek/Downloads/SalmoGold.xml");
123
124 return 0;
125
126 openlog("mash", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
127 syslog(LOG_NOTICE, "mbsePi-apps mash v%s starting", VERSION);
128 if (debug)
129 fprintf(stdout, "mbsePi-apps mash v%s starting\n", VERSION);
130
131 if (rdconfig((char *)"mash.conf")) {
132 fprintf(stderr, "Error reading configuration\n");
133 syslog(LOG_NOTICE, "halted");
134 return 1;
135 }
136
137 /*
138 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
139 * but that's live. This daemon should only be stopped by SIGTERM.
140 * Don't catch SIGCHLD.
141 */
142 for (i = 0; i < NSIG; i++) {
143 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
144 signal(i, (void (*))die);
145 }
146
147 if (wiringPiSetup () )
148 return 1;
149
150 if ((rc = initLCD (16, 2))) {
151 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
152 return 1;
153 }
154
155 lcdPosition(lcdHandle, 0, 0);
156 sprintf(buf, " Mash");
157 mb_lcdPuts(lcdHandle, buf);
158 lcdPosition(lcdHandle, 0, 1);
159 sprintf(buf, " Version %s", VERSION);
160 mb_lcdPuts(lcdHandle, buf);
161
162 if (debug) {
163 /*
164 * For debugging run in foreground.
165 */
166 rc = server();
167 } else {
168 /*
169 * Server initialization is complete. Now we can fork the
170 * daemon and return to the user. We need to do a setpgrp
171 * so that the daemon will no longer be assosiated with the
172 * users control terminal. This is done before the fork, so
173 * that the child will not be a process group leader. Otherwise,
174 * if the child were to open a terminal, it would become
175 * associated with that terminal as its control terminal.
176 */
177 if ((pgrp = setpgid(0, 0)) == -1) {
178 syslog(LOG_NOTICE, "setpgpid failed");
179 }
180
181 frk = fork();
182 switch (frk) {
183 case -1:
184 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
185 syslog(LOG_NOTICE, "Finished, rc=1");
186 stopLCD();
187 exit(1);
188 case 0: /*
189 * Run the daemon
190 */
191 fclose(stdin);
192 if (open("/dev/null", O_RDONLY) != 0) {
193 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
194 _exit(2);
195 }
196 fclose(stdout);
197 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
198 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
199 _exit(2);
200 }
201 fclose(stderr);
202 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
203 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
204 _exit(2);
205 }
206 mypid = getpid();
207 rc = server();
208 break;
209 /* Not reached */
210 default:
211 /*
212 * Here we detach this process and let the child
213 * run the deamon process.
214 */
215 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
216 exit(0);
217 }
218 }
219
220 syslog(LOG_NOTICE, "Finished, rc=%d", rc);
221 return rc;
222 }
223
224
225
226 int server(void)
227 {
228 char buf[1024];
229 w1_therm *tmp1;
230 int rc, run = 1, temp;
231
232 if (lockprog((char *)"mash")) {
233 syslog(LOG_NOTICE, "Can't lock");
234 return 1;
235 }
236
237 rc = piThreadCreate(my_sensors_loop);
238 if (rc) {
239 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
240 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc);
241 }
242
243 // rc = piThreadCreate(my_server_loop);
244 // if (rc) {
245 // fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
246 // syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
247 // }
248
249 snprintf(buf, 1023, "temp,step,description");
250 logger((char *)"mash.log", (char *)"mash", buf);
251
252 do {
253 lcdupdate = FALSE;
254
255 if (my_shutdown)
256 run = 0;
257
258 tmp1 = Config.w1therms;
259 // if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
260 // syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
261 // lcdupdate = TRUE;
262 // }
263 // if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
264 // syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
265 // lcdupdate = TRUE;
266 // }
267 if (tmp1->update) {
268 tmp1->update = FALSE;
269 lcdupdate = TRUE;
270 }
271 // old1 = tmp1->next;
272 // tmp1 = old1;
273 // if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
274 // syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
275 // lcdupdate = TRUE;
276 // }
277 // if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
278 // syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
279 // lcdupdate = TRUE;
280 // }
281 // if (tmp1->update) {
282 // tmp1->update = FALSE;
283 // lcdupdate = TRUE;
284 // }
285
286 if (run && lcdupdate) {
287 lcdPosition(lcdHandle, 0, 0);
288 tmp1 = Config.w1therms;
289 snprintf(buf, 16, "%4.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
290 mb_lcdPuts(lcdHandle, buf);
291 temp = tmp1->lastval;
292 // old1 = tmp1->next;
293 // tmp1 = old1;
294 lcdPosition(lcdHandle, 0, 1);
295 snprintf(buf, 16, " ");
296 mb_lcdPuts(lcdHandle, buf);
297 snprintf(buf, 1023, "%.1f,%d,%s", temp / 1000.0, 1, (char *)"step description");
298 logger((char *)"mash.log", (char *)"mash", buf);
299 }
300 usleep(100000);
301
302 } while (run);
303
304 if (debug)
305 fprintf(stdout, (char *)"Out of loop\n");
306
307 /*
308 * Give threads time to cleanup
309 */
310 usleep(1500000);
311
312 stopLCD();
313
314 // wrconfig((char *)"mash.conf");
315
316 ulockprog((char *)"mash");
317
318 if (debug)
319 fprintf(stdout, "Goodbye\n");
320
321 return 0;
322 }
323
324 #else
325
326
327 int main(int argc, char *argv[])
328 {
329 parseBeerXML((char *)"/home/mbroek/Downloads/beerxml.xml");
330 printBeerXML();
331 // fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
332 // fprintf(stderr, "This program is useless and will do nothing.\n");
333 return 0;
334 }
335
336
337 #endif

mercurial