thermferm/thermferm.c

changeset 550
04c942cded91
parent 548
2924fe4911d9
child 554
ab9f22ab57b5
equal deleted inserted replaced
549:ee1bcad035f0 550:04c942cded91
36 #include "pid.h" 36 #include "pid.h"
37 #include "mqtt.h" 37 #include "mqtt.h"
38 38
39 39
40 int my_shutdown = FALSE; 40 int my_shutdown = FALSE;
41 int my_reboot = FALSE;
41 static pid_t pgrp, mypid; 42 static pid_t pgrp, mypid;
42 int run_pause = FALSE; 43 int run_pause = FALSE;
43 int run_hold = FALSE; 44 int run_hold = FALSE;
44 45
45 extern int debug; 46 extern int debug;
910 911
911 if (debug) { 912 if (debug) {
912 /* 913 /*
913 * For debugging run in foreground. 914 * For debugging run in foreground.
914 */ 915 */
915 rc = server(); 916 do {
917 rc = server();
918 } while (my_reboot == TRUE);
916 } else { 919 } else {
917 /* 920 /*
918 * Server initialization is complete. Now we can fork the 921 * Server initialization is complete. Now we can fork the
919 * daemon and return to the user. We need to do a setpgrp 922 * daemon and return to the user. We need to do a setpgrp
920 * so that the daemon will no longer be assosiated with the 923 * so that the daemon will no longer be assosiated with the
950 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { 953 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
951 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); 954 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
952 _exit(2); 955 _exit(2);
953 } 956 }
954 mypid = getpid(); 957 mypid = getpid();
955 rc = server(); 958 do {
959 rc = server();
960 } while (my_reboot == TRUE);
956 break; 961 break;
957 /* Not reached */ 962 /* Not reached */
958 default: 963 default:
959 /* 964 /*
960 * Here we detach this process and let the child 965 * Here we detach this process and let the child
989 float previous_target_lo, previous_target_hi; 994 float previous_target_lo, previous_target_hi;
990 float LCDair, LCDbeer, LCDspL, LCDspH; 995 float LCDair, LCDbeer, LCDspL, LCDspH;
991 unsigned char LCDstatC, LCDstatH; 996 unsigned char LCDstatC, LCDstatH;
992 int LCDunit; 997 int LCDunit;
993 998
999 syslog(LOG_NOTICE, "Server process started");
1000 my_shutdown = my_reboot = FALSE;
994 if (lockprog((char *)"thermferm")) { 1001 if (lockprog((char *)"thermferm")) {
995 syslog(LOG_NOTICE, "Can't lock"); 1002 syslog(LOG_NOTICE, "Can't lock");
996 return 1; 1003 return 1;
997 } 1004 }
998 #ifdef HAVE_MOSQUITTO_H 1005 #ifdef HAVE_MOSQUITTO_H
1670 * Prevent extreme heating 1677 * Prevent extreme heating
1671 */ 1678 */
1672 if ((unit->mode == UNITMODE_BEER) && ((unit->air_temperature / 1000.0) > (unit->PID_heat->Input + 8.0))) { 1679 if ((unit->mode == UNITMODE_BEER) && ((unit->air_temperature / 1000.0) > (unit->PID_heat->Input + 8.0))) {
1673 unit->PID_heat->OutP = 0.0; 1680 unit->PID_heat->OutP = 0.0;
1674 } 1681 }
1675 if (debug) 1682 // if (debug)
1676 fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", 1683 // fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
1677 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); 1684 // unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP);
1678 if (seconds == 60) { 1685 if (seconds == 60) {
1679 syslog(LOG_NOTICE, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", 1686 syslog(LOG_NOTICE, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f",
1680 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); 1687 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP);
1681 } 1688 }
1682 } else { 1689 } else {
1699 if (seconds == 60) { 1706 if (seconds == 60) {
1700 syslog(LOG_NOTICE, "Cool: Air=%.2f Chiller=%.2f alarm", unit->air_temperature / 1000.0, unit->chiller_temperature / 1000.0); 1707 syslog(LOG_NOTICE, "Cool: Air=%.2f Chiller=%.2f alarm", unit->air_temperature / 1000.0, unit->chiller_temperature / 1000.0);
1701 } 1708 }
1702 } 1709 }
1703 } 1710 }
1704 if (debug) 1711 // if (debug)
1705 fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", 1712 // fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n",
1706 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); 1713 // unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP);
1707 if (seconds == 60) { 1714 if (seconds == 60) {
1708 syslog(LOG_NOTICE, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", 1715 syslog(LOG_NOTICE, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f",
1709 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); 1716 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP);
1710 } 1717 }
1711 } else { 1718 } else {
1795 device_out(unit->cooler_address, unit->cooler_state); 1802 device_out(unit->cooler_address, unit->cooler_state);
1796 } else { 1803 } else {
1797 device_out(unit->cooler_address, 0); 1804 device_out(unit->cooler_address, 0);
1798 } 1805 }
1799 } 1806 }
1800 if (debug) 1807 // if (debug)
1801 fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d PIDcool=%.2f PWRcool=%d\n", 1808 // fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d PIDcool=%.2f PWRcool=%d\n",
1802 unit->PID_heat->OutP, unit->heater_state, unit->PID_cool->OutP, unit->cooler_state); 1809 // unit->PID_heat->OutP, unit->heater_state, unit->PID_cool->OutP, unit->cooler_state);
1803 1810
1804 /* 1811 /*
1805 * If there is a fan, and the unit door is closed, and the unit should be doing 1812 * If there is a fan, and the unit door is closed, and the unit should be doing
1806 * something, then turn on the global fan. 1813 * something, then turn on the global fan.
1807 * But if there is a chiller, do not turn it on if cooling. 1814 * But if there is a chiller, do not turn it on if cooling.
2065 #ifdef HAVE_MOSQUITTO_H 2072 #ifdef HAVE_MOSQUITTO_H
2066 mqtt_disconnect(); 2073 mqtt_disconnect();
2067 #endif 2074 #endif
2068 2075
2069 syslog(LOG_NOTICE, "Out of loop"); 2076 syslog(LOG_NOTICE, "Out of loop");
2070 if (debug)
2071 fprintf(stdout, (char *)"Out of loop\n");
2072 2077
2073 /* 2078 /*
2074 * Give threads time to cleanup 2079 * Give threads time to cleanup
2075 */ 2080 */
2076 usleep(1500000); 2081 usleep(1500000);
2082 } 2087 }
2083 sock = -1; 2088 sock = -1;
2084 } 2089 }
2085 wrconfig(); 2090 wrconfig();
2086 ulockprog((char *)"thermferm"); 2091 ulockprog((char *)"thermferm");
2092
2093 syslog(LOG_NOTICE, "Server process ended");
2087 return 0; 2094 return 0;
2088 } 2095 }
2089 2096

mercurial