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 "../lib/mbselib.h" |
|
24 #include "coolers.h" |
|
25 #include "mosquitto.h" |
|
26 #include "sensors.h" |
|
27 |
|
28 #ifdef HAVE_WIRINGPI_H |
|
29 |
|
30 |
|
31 int tempA = 80; |
|
32 int tempB = 80; |
|
33 int coolerA = 0; |
|
34 int coolerB = 0; |
|
35 |
|
36 bool my_shutdown = false; |
|
37 static pid_t pgrp, mypid; |
|
38 |
|
39 extern bool debug; |
|
40 extern sys_config Config; |
|
41 extern int lcdHandle; |
|
42 int lcdupdate; |
|
43 |
|
44 int sock = -1; /* Datagram socket */ |
|
45 struct sockaddr_un servaddr; /* Server address */ |
|
46 struct sockaddr_un from; /* From address */ |
|
47 static char spath[PATH_MAX]; /* Socket path */ |
|
48 socklen_t fromlen; |
|
49 |
|
50 |
|
51 int server(void); |
|
52 void help(void); |
|
53 void die(int); |
|
54 |
|
55 |
|
56 void help(void) |
|
57 { |
|
58 fprintf(stdout, "mbsePi-apps coolers v%s starting\n\n", VERSION); |
|
59 fprintf(stdout, "Usage: coolers [-d] [-h]\n"); |
|
60 fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
|
61 fprintf(stdout, " -h --help Display this help\n"); |
|
62 } |
|
63 |
|
64 |
|
65 |
|
66 void die(int onsig) |
|
67 { |
|
68 switch (onsig) { |
|
69 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
|
70 break; |
|
71 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
|
72 break; |
|
73 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
|
74 break; |
|
75 default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
|
76 } |
|
77 |
|
78 my_shutdown = true; |
|
79 } |
|
80 |
|
81 |
|
82 |
|
83 void stopLCD(void) |
|
84 { |
|
85 mb_lcdClear(lcdHandle); |
|
86 setBacklight(0); |
|
87 } |
|
88 |
|
89 |
|
90 |
|
91 void stopRCswitch(void) |
|
92 { |
|
93 rc_switch *tmp, *old; |
|
94 char *cmd = NULL; |
|
95 int rc; |
|
96 |
|
97 for (tmp = Config.rcswitch; tmp; tmp = old) { |
|
98 old = tmp->next; |
|
99 cmd = xstrcpy(tmp->address); |
|
100 cmd = xstrcat(cmd, (char *)",0"); |
|
101 rc = toggleSwitch(cmd); |
|
102 if (debug) |
|
103 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc); |
|
104 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); |
|
105 free(cmd); |
|
106 cmd = NULL; |
|
107 } |
|
108 } |
|
109 |
|
110 |
|
111 |
|
112 int main(int argc, char *argv[]) |
|
113 { |
|
114 int rc, c, i; |
|
115 pid_t frk; |
|
116 char buf[80]; |
|
117 |
|
118 while (1) { |
|
119 int option_index = 0; |
|
120 static struct option long_options[] = { |
|
121 {"debug", 0, 0, 'c'}, |
|
122 {"help", 0, 0, 'h'}, |
|
123 {0, 0, 0, 0} |
|
124 }; |
|
125 |
|
126 c = getopt_long(argc, argv, "dh", long_options, &option_index); |
|
127 if (c == -1) |
|
128 break; |
|
129 |
|
130 switch (c) { |
|
131 case 'd': debug = true; |
|
132 break; |
|
133 case 'h': help(); |
|
134 return 1; |
|
135 } |
|
136 } |
|
137 |
|
138 openlog("coolers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
|
139 syslog(LOG_NOTICE, "mbsePi-apps coolers v%s starting", VERSION); |
|
140 if (debug) |
|
141 fprintf(stdout, "mbsePi-apps coolers v%s starting\n", VERSION); |
|
142 |
|
143 if (rdconfig((char *)"coolers.conf")) { |
|
144 fprintf(stderr, "Error reading configuration\n"); |
|
145 syslog(LOG_NOTICE, "halted"); |
|
146 return 1; |
|
147 } |
|
148 |
|
149 /* |
|
150 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
|
151 * but that's live. This daemon should only be stopped by SIGTERM. |
|
152 * Don't catch SIGCHLD. |
|
153 */ |
|
154 for (i = 0; i < NSIG; i++) { |
|
155 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
|
156 signal(i, (void (*))die); |
|
157 } |
|
158 |
|
159 if (wiringPiSetup () ) |
|
160 return 1; |
|
161 |
|
162 if ((rc = initLCD (16, 2))) { |
|
163 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
|
164 return 1; |
|
165 } |
|
166 |
|
167 lcdPosition(lcdHandle, 0, 0); |
|
168 sprintf(buf, "Coolers"); |
|
169 mb_lcdPuts(lcdHandle, buf); |
|
170 lcdPosition(lcdHandle, 0, 1); |
|
171 sprintf(buf, "Version %s", VERSION); |
|
172 mb_lcdPuts(lcdHandle, buf); |
|
173 |
|
174 if (Config.tx433 != -1) { |
|
175 if (debug) |
|
176 fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433); |
|
177 syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433); |
|
178 enableTransmit(Config.tx433); |
|
179 } |
|
180 |
|
181 if (debug) { |
|
182 /* |
|
183 * For debugging run in foreground. |
|
184 */ |
|
185 rc = server(); |
|
186 } else { |
|
187 /* |
|
188 * Server initialization is complete. Now we can fork the |
|
189 * daemon and return to the user. We need to do a setpgrp |
|
190 * so that the daemon will no longer be assosiated with the |
|
191 * users control terminal. This is done before the fork, so |
|
192 * that the child will not be a process group leader. Otherwise, |
|
193 * if the child were to open a terminal, it would become |
|
194 * associated with that terminal as its control terminal. |
|
195 */ |
|
196 if ((pgrp = setpgid(0, 0)) == -1) { |
|
197 syslog(LOG_NOTICE, "setpgpid failed"); |
|
198 } |
|
199 |
|
200 frk = fork(); |
|
201 switch (frk) { |
|
202 case -1: |
|
203 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
|
204 syslog(LOG_NOTICE, "Finished, rc=1"); |
|
205 stopLCD(); |
|
206 exit(1); |
|
207 case 0: /* |
|
208 * Run the daemon |
|
209 */ |
|
210 fclose(stdin); |
|
211 if (open("/dev/null", O_RDONLY) != 0) { |
|
212 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); |
|
213 _exit(2); |
|
214 } |
|
215 fclose(stdout); |
|
216 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
|
217 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); |
|
218 _exit(2); |
|
219 } |
|
220 fclose(stderr); |
|
221 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
|
222 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); |
|
223 _exit(2); |
|
224 } |
|
225 mypid = getpid(); |
|
226 rc = server(); |
|
227 break; |
|
228 /* Not reached */ |
|
229 default: |
|
230 /* |
|
231 * Here we detach this process and let the child |
|
232 * run the deamon process. |
|
233 */ |
|
234 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); |
|
235 exit(0); |
|
236 } |
|
237 } |
|
238 |
|
239 syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
|
240 return rc; |
|
241 } |
|
242 |
|
243 |
|
244 |
|
245 int server(void) |
|
246 { |
|
247 char buf[1024]; |
|
248 w1_therm *tmp1, *old1; |
|
249 rc_switch *tmp2, *old2; |
|
250 int rc, run = 0, temp, rlen; |
|
251 struct pollfd pfd[1]; |
|
252 |
|
253 |
|
254 if (lockprog((char *)"coolers")) { |
|
255 syslog(LOG_NOTICE, "Can't lock"); |
|
256 return 1; |
|
257 } |
|
258 |
|
259 my_mosquitto_init(); |
|
260 |
|
261 rc = piThreadCreate(my_sensors_loop); |
|
262 if (rc) { |
|
263 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); |
|
264 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); |
|
265 } |
|
266 |
|
267 /* |
|
268 * Setup socket so that clients can communicate with this server. |
|
269 */ |
|
270 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { |
|
271 fprintf(stderr, "Can't create socket\n"); |
|
272 return 1; |
|
273 } |
|
274 |
|
275 snprintf(spath, PATH_MAX, "/var/run/coolers.sock"); |
|
276 memset(&servaddr, 0, sizeof(servaddr)); |
|
277 servaddr.sun_family = AF_UNIX; |
|
278 strcpy(servaddr.sun_path, spath); |
|
279 |
|
280 if (bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { |
|
281 close(sock); |
|
282 sock = -1; |
|
283 fprintf(stderr, "Can't bind socket %s\n", spath); |
|
284 return 1; |
|
285 } |
|
286 if (chmod(spath, 0666)) { |
|
287 fprintf(stderr, "Can't chmod 0666 %s\n", spath); |
|
288 close(sock); |
|
289 sock = -1; |
|
290 unlink(spath); |
|
291 return 1; |
|
292 } |
|
293 if (debug) |
|
294 fprintf(stdout, "Unix socket created\n"); |
|
295 |
|
296 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); |
|
297 logger((char *)"coolers.log", (char *)"coolers", buf); |
|
298 |
|
299 do { |
|
300 /* |
|
301 * Poll UNIX Datagram socket the defined timeout of one second. |
|
302 * This means we listen if a client program has something to tell us. |
|
303 * Timeout is one second, after the timeout the rest of the mainloop is executed. |
|
304 */ |
|
305 pfd[0].fd = sock; |
|
306 pfd[0].events = POLLIN; |
|
307 pfd[0].revents = 0; |
|
308 rc = poll(pfd, 1, 1000); |
|
309 |
|
310 if (rc == -1) { |
|
311 /* |
|
312 * Poll can be interrupted by a finished child so that's not a real error. |
|
313 */ |
|
314 if (errno != EINTR) { |
|
315 syslog(LOG_NOTICE, "poll() rc=%d sock=%d, events=%04x", rc, sock, pfd[0].revents); |
|
316 } |
|
317 } else if (rc) { |
|
318 if (pfd[0].revents & POLLIN) { |
|
319 /* |
|
320 * Process the clients request for mbtask commands. |
|
321 */ |
|
322 memset(&buf, 0, sizeof(buf)); |
|
323 fromlen = sizeof(from); |
|
324 rlen = recvfrom(sock, buf, sizeof(buf) -1, 0, (struct sockaddr *)&from, &fromlen); |
|
325 if (rlen == -1) { |
|
326 syslog(LOG_NOTICE, "recvfrom() for socket receiver"); |
|
327 } else { |
|
328 // do_cmd(buf); |
|
329 } |
|
330 } |
|
331 } |
|
332 |
|
333 lcdupdate = FALSE; |
|
334 |
|
335 run = my_mosquitto_loop(); |
|
336 |
|
337 tmp1 = Config.w1therms; |
|
338 tmp2 = Config.rcswitch; |
|
339 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { |
|
340 my_mosquitto_switch(tmp2->address, 0); |
|
341 coolerA = 0; |
|
342 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
343 lcdupdate = TRUE; |
|
344 } |
|
345 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { |
|
346 my_mosquitto_switch(tmp2->address, 1); |
|
347 coolerA = 1; |
|
348 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
349 lcdupdate = TRUE; |
|
350 } |
|
351 old1 = tmp1->next; |
|
352 tmp1 = old1; |
|
353 old2 = tmp2->next; |
|
354 tmp2 = old2; |
|
355 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { |
|
356 my_mosquitto_switch(tmp2->address, 0); |
|
357 coolerB = 0; |
|
358 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
359 lcdupdate = TRUE; |
|
360 } |
|
361 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { |
|
362 my_mosquitto_switch(tmp2->address, 1); |
|
363 coolerB = 1; |
|
364 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
365 lcdupdate = TRUE; |
|
366 } |
|
367 |
|
368 if (run && lcdupdate) { |
|
369 lcdPosition(lcdHandle, 0, 0); |
|
370 tmp1 = Config.w1therms; |
|
371 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias); |
|
372 mb_lcdPuts(lcdHandle, buf); |
|
373 temp = tmp1->lastval; |
|
374 old1 = tmp1->next; |
|
375 tmp1 = old1; |
|
376 lcdPosition(lcdHandle, 0, 1); |
|
377 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '+' : ' ', tmp1->alias); |
|
378 mb_lcdPuts(lcdHandle, buf); |
|
379 snprintf(buf, 1023, "%.1f,%s,%.1f,%s", temp / 1000.0, coolerA ? (char *)"on" : (char *)"off", |
|
380 tmp1->lastval / 1000.0, coolerB ? (char *)"on" : (char *)"off"); |
|
381 logger((char *)"coolers.log", (char *)"coolers", buf); |
|
382 } |
|
383 usleep(100000); |
|
384 |
|
385 } while (run); |
|
386 |
|
387 if (debug) |
|
388 fprintf(stdout, (char *)"Out of loop\n"); |
|
389 |
|
390 /* |
|
391 * Give threads time to cleanup |
|
392 */ |
|
393 usleep(1500000); |
|
394 |
|
395 if (Config.tx433 != -1) { |
|
396 stopRCswitch(); |
|
397 } |
|
398 |
|
399 my_mosquitto_exit(); |
|
400 stopLCD(); |
|
401 disableTransmit(); |
|
402 |
|
403 ulockprog((char *)"coolers"); |
|
404 unlink(spath); |
|
405 |
|
406 if (debug) |
|
407 fprintf(stdout, "Goodbye\n"); |
|
408 |
|
409 return 0; |
|
410 } |
|
411 |
|
412 #else |
|
413 |
|
414 |
|
415 int main(int argc, char *argv[]) |
|
416 { |
|
417 fprintf(stderr, "Compiled on a system without a wiringPi library.\n"); |
|
418 fprintf(stderr, "This program is useless and will do nothing.\n"); |
|
419 return 0; |
|
420 } |
|
421 |
|
422 |
|
423 #endif |
|