18 * You should have received a copy of the GNU General Public License |
18 * You should have received a copy of the GNU General Public License |
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
21 *****************************************************************************/ |
21 *****************************************************************************/ |
22 |
22 |
|
23 #include "brewco.h" |
23 #include "rdconfig.h" |
24 #include "rdconfig.h" |
24 #include "brewco.h" |
25 #include "rdsession.h" |
25 #include "futil.h" |
26 #include "futil.h" |
26 #include "xutil.h" |
27 #include "xutil.h" |
27 #include "lcd-pcf8574.h" |
28 #include "lcd-pcf8574.h" |
28 #include "slcd.h" |
29 #include "slcd.h" |
29 #include "lock.h" |
30 #include "lock.h" |
209 piUnlock(LOCK_LCD); |
210 piUnlock(LOCK_LCD); |
210 #endif |
211 #endif |
211 } |
212 } |
212 |
213 |
213 |
214 |
|
215 |
|
216 void editUnit(units_list *unit) |
|
217 { |
|
218 if (debug) |
|
219 fprintf(stdout, "Start edit brewsystem %d %s\n", unit->number, unit->uuid); |
|
220 |
|
221 if (debug) |
|
222 fprintf(stdout, "End edit brewsystem %d %s\n", unit->number, unit->uuid); |
|
223 } |
|
224 |
|
225 |
|
226 |
|
227 void addUnit(int number) |
|
228 { |
|
229 units_list *tmpu, *unit = (units_list *)malloc(sizeof(units_list)); |
|
230 char name[81]; |
|
231 uuid_t uu; |
|
232 |
|
233 if (debug) |
|
234 fprintf(stdout, "Adding new brewsystem %d\n", number); |
|
235 unit->next = NULL; |
|
236 unit->version = 1; |
|
237 unit->uuid = malloc(37); |
|
238 uuid_generate(uu); |
|
239 uuid_unparse(uu, unit->uuid); |
|
240 snprintf(name, 20, "System %d", number); |
|
241 unit->name = xstrcpy(name); |
|
242 unit->number = number; |
|
243 if (number == 1) |
|
244 unit->active = 1; |
|
245 else |
|
246 unit->active = 0; |
|
247 unit->hlt_sensor = unit->mlt_sensor = NULL; |
|
248 unit->hlt_heater = unit->mlt_heater = unit->mlt_pump = NULL; |
|
249 unit->hlt_heater_mltfirst = 1; |
|
250 unit->pump_cycle = 7; |
|
251 unit->pump_rest = 2; |
|
252 unit->pump_premash = 1; |
|
253 unit->pump_onmash = 1; |
|
254 unit->pump_mashout = 0; |
|
255 unit->pump_onboil = 0; |
|
256 unit->pump_stop = 90; |
|
257 unit->skip_add = 0; |
|
258 unit->skip_remove = 0; |
|
259 unit->skip_iodine = 0; |
|
260 unit->iodine_time = 90; |
|
261 unit->whirlpool = 1; |
|
262 unit->PID_hlt = (pid_var *)malloc(sizeof(pid_var)); |
|
263 unit->PID_mlt = (pid_var *)malloc(sizeof(pid_var)); |
|
264 InitPID(unit->PID_hlt); |
|
265 InitPID(unit->PID_mlt); |
|
266 |
|
267 editUnit(unit); |
|
268 |
|
269 if (Config.units == NULL) { |
|
270 Config.units = unit; |
|
271 } else { |
|
272 for (tmpu = Config.units; tmpu; tmpu = tmpu->next) { |
|
273 if (tmpu->next == NULL) { |
|
274 tmpu->next = unit; |
|
275 break; |
|
276 } |
|
277 } |
|
278 } |
|
279 syslog(LOG_NOTICE, "Brewsystem %d added to the configuration", number); |
|
280 if (debug) |
|
281 fprintf(stdout, "Brewsystem %d added to the configuration\n", number); |
|
282 } |
|
283 |
|
284 |
|
285 |
214 int server(void); |
286 int server(void); |
215 int server(void) |
287 int server(void) |
216 { |
288 { |
217 int rc = 0, run = 1, key; |
289 int rc = 0, run = 1, key; |
218 units_list *unit; |
290 units_list *unit; |
|
291 brew_session *brew = NULL; |
219 #ifndef HAVE_WIRINGPI_H |
292 #ifndef HAVE_WIRINGPI_H |
220 long t = 0; |
293 long t = 0; |
221 #endif |
294 #endif |
222 |
295 |
223 // if (lockprog((char *)"brewco")) { |
296 // if (lockprog((char *)"brewco")) { |
224 // syslog(LOG_NOTICE, "Can't lock"); |
297 // syslog(LOG_NOTICE, "Can't lock"); |
225 // return 1; |
298 // return 1; |
226 // } |
299 // } |
227 |
300 |
|
301 if (debug) |
|
302 fprintf(stdout, "Begin server()\n"); |
|
303 |
228 if ((rc = devices_detect())) { |
304 if ((rc = devices_detect())) { |
229 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
305 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
|
306 if (debug) |
|
307 fprintf(stdout, "Detected %d new devices\n", rc); |
230 wrconfig(); |
308 wrconfig(); |
231 } |
309 } |
232 |
310 |
233 #ifdef HAVE_WIRINGPI_H |
311 #ifdef HAVE_WIRINGPI_H |
234 rc = piThreadCreate(my_devices_loop); |
312 rc = piThreadCreate(my_devices_loop); |
243 t++; |
321 t++; |
244 #endif |
322 #endif |
245 } |
323 } |
246 |
324 |
247 #ifdef HAVE_WIRINGPI_H |
325 #ifdef HAVE_WIRINGPI_H |
248 rc = piThreadCreate(my_panel_loop); |
326 rc = piThreadCreate(my_keyboard_loop); |
249 #else |
327 #else |
250 rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t ); |
328 rc = pthread_create(&threads[t], NULL, my_keyboard_loop, (void *)t ); |
251 #endif |
329 #endif |
252 if (rc) { |
330 if (rc) { |
253 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
331 fprintf(stderr, "my_keyboard_loop thread didn't start rc=%d\n", rc); |
254 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
332 syslog(LOG_NOTICE, "my_keyboard_loop thread didn't start rc=%d", rc); |
255 #ifndef HAVE_WIRINGPI_H |
333 #ifndef HAVE_WIRINGPI_H |
256 } else { |
334 } else { |
257 t++; |
335 t++; |
258 #endif |
336 #endif |
259 } |
337 } |
275 #endif |
353 #endif |
276 |
354 |
277 /* |
355 /* |
278 * Initialize units for processing |
356 * Initialize units for processing |
279 */ |
357 */ |
280 // for (unit = Config.units; unit; unit = unit->next) { |
358 for (unit = Config.units; unit; unit = unit->next) { |
281 /* |
359 /* |
282 * Safety, turn everything off |
360 * Safety, turn everything off |
283 */ |
361 */ |
284 // unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; |
362 if (unit->active) { |
285 // unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; |
363 if (unit->hlt_heater) |
286 // } |
364 unit->hlt_heater->value = 0; |
|
365 if (unit->mlt_heater) |
|
366 unit->mlt_heater->value = 0; |
|
367 if (unit->mlt_pump) |
|
368 unit->mlt_pump->value = 0; |
|
369 } |
|
370 } |
287 |
371 |
288 prompt(101); |
372 prompt(101); |
289 if (! Config.units) { |
373 if (! Config.units) { |
290 /* |
374 /* |
291 * No brewsystems defined, add the first |
375 * No brewsystems defined, add the first |
292 */ |
376 */ |
293 prompt(218); /* Add Brewsystem? */ |
377 prompt(218); /* Add Brewsystem? */ |
294 prompt(407); /* --- --- Ok --- */ |
378 prompt(407); /* --- --- Ok --- */ |
295 |
379 |
296 do { |
380 do { |
297 usleep(10000); |
381 key = keywait(); |
298 slcdDummy(slcdHandle); |
|
299 key = keycheck(); |
|
300 } while (key != KEY_RETURN); |
382 } while (key != KEY_RETURN); |
301 } |
383 |
302 |
384 if (key == KEY_RETURN) { |
303 /* Check for a crash and recover */ |
385 addUnit(1); |
304 |
386 } |
305 prompt(0); |
387 } |
306 prompt(401); |
388 |
|
389 /* |
|
390 * During automation there will be a state file: |
|
391 * ~/.brewco/var/brewing.xml |
|
392 * If this file is present, there has been a crash. |
|
393 */ |
|
394 brew = (brew_session *)malloc(sizeof(brew_session)); |
|
395 if (rdsession(brew) == 0) { |
|
396 } else { |
|
397 /* |
|
398 * No active brew session, make that permanent. |
|
399 */ |
|
400 free(brew); |
|
401 brew = NULL; |
|
402 } |
307 |
403 |
308 do { |
404 do { |
309 if (my_shutdown) |
405 if (my_shutdown) { |
310 run = 0; |
406 run = 0; |
311 |
407 break; |
312 /* |
408 } |
313 * If there is no unit, setup a unit |
409 |
314 */ |
410 if (brew) { |
315 if (! Config.units) { |
411 /* |
316 } |
412 * Automate mode |
317 |
413 */ |
318 /* |
414 |
319 * Select the active unit |
415 } else { |
320 */ |
416 /* |
321 |
417 * Not running. |
322 /* |
418 */ |
323 * If running, do the things |
419 prompt(0); |
324 */ |
420 prompt(101); |
325 |
421 prompt(401); |
326 /* |
422 key = keywait(); |
327 * Not running, do manual, setup etc |
423 } |
328 */ |
|
329 |
|
330 |
424 |
331 usleep(100000); |
425 usleep(100000); |
332 |
426 |
333 } while (run); |
427 } while (run); |
334 |
428 |
399 if (rdconfig()) { |
493 if (rdconfig()) { |
400 fprintf(stderr, "Error reading configuration\n"); |
494 fprintf(stderr, "Error reading configuration\n"); |
401 syslog(LOG_NOTICE, "Error reading configuration: halted"); |
495 syslog(LOG_NOTICE, "Error reading configuration: halted"); |
402 return 1; |
496 return 1; |
403 } |
497 } |
|
498 if (debug) |
|
499 fprintf(stdout, "configuration loaded\n"); |
404 |
500 |
405 /* |
501 /* |
406 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
502 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
407 * but that's live. This daemon should only be stopped by SIGTERM. |
503 * but that's live. This daemon should only be stopped by SIGTERM. |
408 * Don't catch SIGCHLD. |
504 * Don't catch SIGCHLD. |