36 bool my_shutdown = false; |
34 bool my_shutdown = false; |
37 static pid_t pgrp, mypid; |
35 static pid_t pgrp, mypid; |
38 |
36 |
39 extern bool debug; |
37 extern bool debug; |
40 extern sys_config Config; |
38 extern sys_config Config; |
|
39 #ifdef HAVE_WIRINGPI_H |
41 extern int lcdHandle; |
40 extern int lcdHandle; |
42 extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
41 extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
|
42 #endif |
43 int lcdupdate; |
43 int lcdupdate; |
44 |
44 |
45 |
45 |
46 int server(void); |
46 int server(void); |
47 void help(void); |
47 void help(void); |
48 void die(int); |
48 void die(int); |
|
49 #ifdef HAVE_WIRINGPI_H |
49 void sendRCswitch(char *, int); |
50 void sendRCswitch(char *, int); |
50 void stopLCD(void); |
51 void stopLCD(void); |
51 void stopRCswitch(void); |
52 void stopRCswitch(void); |
|
53 #endif |
52 |
54 |
53 |
55 |
54 |
56 |
55 void help(void) |
57 void help(void) |
56 { |
58 { |
122 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); |
126 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); |
123 free(cmd); |
127 free(cmd); |
124 cmd = NULL; |
128 cmd = NULL; |
125 } |
129 } |
126 } |
130 } |
|
131 #endif |
127 |
132 |
128 |
133 |
129 |
134 |
130 int main(int argc, char *argv[]) |
135 int main(int argc, char *argv[]) |
131 { |
136 { |
132 int rc, c, i; |
137 int rc, c, i; |
133 pid_t frk; |
138 pid_t frk; |
|
139 #ifdef HAVE_WIRINGPI_H |
134 char buf[80]; |
140 char buf[80]; |
|
141 #endif |
135 |
142 |
136 while (1) { |
143 while (1) { |
137 int option_index = 0; |
144 int option_index = 0; |
138 static struct option long_options[] = { |
145 static struct option long_options[] = { |
139 {"debug", 0, 0, 'c'}, |
146 {"debug", 0, 0, 'c'}, |
172 for (i = 0; i < NSIG; i++) { |
179 for (i = 0; i < NSIG; i++) { |
173 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
180 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
174 signal(i, (void (*))die); |
181 signal(i, (void (*))die); |
175 } |
182 } |
176 |
183 |
|
184 #ifdef HAVE_WIRINGPI_H |
177 if (wiringPiSetup () ) |
185 if (wiringPiSetup () ) |
178 return 1; |
186 return 1; |
179 |
187 |
180 if ((rc = initLCD (16, 2))) { |
188 if ((rc = initLCD (16, 2))) { |
181 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
189 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
263 int server(void) |
274 int server(void) |
264 { |
275 { |
265 char buf[1024]; |
276 char buf[1024]; |
266 time_t now, last = (time_t)0; |
277 time_t now, last = (time_t)0; |
267 w1_therm *tmp1; |
278 w1_therm *tmp1; |
|
279 #ifdef HAVE_WIRINGPI_H |
268 rc_switch *tmp2; |
280 rc_switch *tmp2; |
269 int rc, run = 1, temp; |
281 int rc; |
|
282 #endif |
|
283 int run = 1, temp; |
270 |
284 |
271 if (lockprog((char *)"thermferm")) { |
285 if (lockprog((char *)"thermferm")) { |
272 syslog(LOG_NOTICE, "Can't lock"); |
286 syslog(LOG_NOTICE, "Can't lock"); |
273 return 1; |
287 return 1; |
274 } |
288 } |
275 |
289 |
|
290 #ifdef HAVE_WIRINGPI_H |
276 rc = piThreadCreate(my_sensors_loop); |
291 rc = piThreadCreate(my_sensors_loop); |
277 if (rc) { |
292 if (rc) { |
278 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); |
293 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); |
279 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); |
294 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); |
280 } |
295 } |
282 rc = piThreadCreate(my_server_loop); |
297 rc = piThreadCreate(my_server_loop); |
283 if (rc) { |
298 if (rc) { |
284 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
299 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
285 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
300 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
286 } |
301 } |
|
302 #endif |
287 |
303 |
288 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); |
304 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); |
289 logger((char *)"thermferm.log", (char *)"thermferm", buf); |
305 logger((char *)"thermferm.log", (char *)"thermferm", buf); |
290 |
306 |
291 do { |
307 do { |
293 |
309 |
294 if (my_shutdown) |
310 if (my_shutdown) |
295 run = 0; |
311 run = 0; |
296 |
312 |
297 tmp1 = Config.w1therms; |
313 tmp1 = Config.w1therms; |
|
314 #ifdef HAVE_WIRINGPI_H |
298 tmp2 = Config.rcswitch; |
315 tmp2 = Config.rcswitch; |
|
316 #endif |
299 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { |
317 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { |
300 coolerA = 0; |
318 coolerA = 0; |
301 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
319 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
320 #ifdef HAVE_WIRINGPI_H |
302 sendRCswitch(tmp2->address, 0); |
321 sendRCswitch(tmp2->address, 0); |
|
322 #endif |
303 lcdupdate = TRUE; |
323 lcdupdate = TRUE; |
304 } |
324 } |
305 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { |
325 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { |
306 coolerA = 1; |
326 coolerA = 1; |
307 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
327 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
328 #ifdef HAVE_WIRINGPI_H |
308 sendRCswitch(tmp2->address, 1); |
329 sendRCswitch(tmp2->address, 1); |
|
330 #endif |
309 lcdupdate = TRUE; |
331 lcdupdate = TRUE; |
310 } |
332 } |
311 if (tmp1->update) { |
333 if (tmp1->update) { |
312 tmp1->update = FALSE; |
334 tmp1->update = FALSE; |
313 lcdupdate = TRUE; |
335 lcdupdate = TRUE; |
314 } |
336 } |
315 tmp1 = tmp1->next; |
337 tmp1 = tmp1->next; |
|
338 #ifdef HAVE_WIRINGPI_H |
316 tmp2 = tmp2->next; |
339 tmp2 = tmp2->next; |
|
340 #endif |
317 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { |
341 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { |
318 coolerB = 0; |
342 coolerB = 0; |
319 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
343 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
344 #ifdef HAVE_WIRINGPI_H |
320 sendRCswitch(tmp2->address, 0); |
345 sendRCswitch(tmp2->address, 0); |
|
346 #endif |
321 lcdupdate = TRUE; |
347 lcdupdate = TRUE; |
322 } |
348 } |
323 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { |
349 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { |
324 coolerB = 1; |
350 coolerB = 1; |
325 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
351 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
352 #ifdef HAVE_WIRINGPI_H |
326 sendRCswitch(tmp2->address, 1); |
353 sendRCswitch(tmp2->address, 1); |
|
354 #endif |
327 lcdupdate = TRUE; |
355 lcdupdate = TRUE; |
328 } |
356 } |
329 if (tmp1->update) { |
357 if (tmp1->update) { |
330 tmp1->update = FALSE; |
358 tmp1->update = FALSE; |
331 lcdupdate = TRUE; |
359 lcdupdate = TRUE; |
332 } |
360 } |
333 |
361 |
|
362 #ifdef HAVE_WIRINGPI_H |
334 if (run && lcdupdate) { |
363 if (run && lcdupdate) { |
335 lcdPosition(lcdHandle, 0, 0); |
364 lcdPosition(lcdHandle, 0, 0); |
336 tmp1 = Config.w1therms; |
365 tmp1 = Config.w1therms; |
337 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '-' : ' ', tmp1->alias); |
366 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '-' : ' ', tmp1->alias); |
338 mb_lcdPuts(lcdHandle, buf); |
367 mb_lcdPuts(lcdHandle, buf); |
340 tmp1 = tmp1->next; |
369 tmp1 = tmp1->next; |
341 lcdPosition(lcdHandle, 0, 1); |
370 lcdPosition(lcdHandle, 0, 1); |
342 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias); |
371 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias); |
343 mb_lcdPuts(lcdHandle, buf); |
372 mb_lcdPuts(lcdHandle, buf); |
344 } |
373 } |
|
374 #endif |
345 |
375 |
346 now = time(NULL); |
376 now = time(NULL); |
347 if (((int)now - (int)last) > 60) { |
377 if (((int)now - (int)last) > 60) { |
348 last = now; |
378 last = now; |
349 tmp1 = Config.w1therms; |
379 tmp1 = Config.w1therms; |