thermferm/server.c

changeset 362
c92651a54969
parent 358
8b7ef338bf49
child 364
2f9bbbcd2407
equal deleted inserted replaced
361:308f6a436779 362:c92651a54969
105 syslog(LOG_NOTICE, "srv_send failed"); 105 syslog(LOG_NOTICE, "srv_send failed");
106 return -1; 106 return -1;
107 } 107 }
108 108
109 return 0; 109 return 0;
110 }
111
112
113
114 /*
115 * Argument is a buffer of size SS_BUFSIZE.
116 * Return -1 if error, else the number of received
117 * character. \n is line end, ignore \r.
118 */
119 int srv_recv(char *buffer)
120 {
121 int bytesloaded = 0;
122 ssize_t ret;
123 unsigned char buf;
124 socklen_t fromlen;
125
126 memset(buffer, 0, SS_BUFSIZE);
127
128 while(1) {
129 /*
130 * read a single byte
131 */
132 fromlen = sizeof(peeraddr_in);
133 ret = recvfrom(s, &buf, 1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
134 if (ret < 1) {
135 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
136 srv_send((char *)"518 recfrom(): %s", strerror(errno));
137 return -1; /* error */
138 }
139
140 if (buf == '\n')
141 break;
142
143 if (buf != '\r') {
144 buffer[bytesloaded] = buf;
145 bytesloaded++;
146 }
147 }
148
149 if (debug) {
150 syslog(LOG_NOTICE, "recv: %d `%s'", bytesloaded, buffer);
151 fprintf(stdout, "recv: %d `%s'\n", bytesloaded, buffer);
152 }
153 return bytesloaded;
110 } 154 }
111 155
112 156
113 157
114 /* 158 /*
576 */ 620 */
577 int cmd_device(char *buf) 621 int cmd_device(char *buf)
578 { 622 {
579 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 623 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
580 devices_list *device, *tmpd; 624 devices_list *device, *tmpd;
581 socklen_t fromlen;
582 int i, rc, rlen, ival; 625 int i, rc, rlen, ival;
583 uuid_t uu; 626 uuid_t uu;
584 627
585 opt = strtok(buf, " \0"); 628 opt = strtok(buf, " \0");
586 opt = strtok(NULL, " \0"); 629 opt = strtok(NULL, " \0");
709 752
710 if (strcmp(opt, (char *)"PUT") == 0) { 753 if (strcmp(opt, (char *)"PUT") == 0) {
711 for (device = Config.devices; device; device = device->next) { 754 for (device = Config.devices; device; device = device->next) {
712 if (strcmp(device->uuid, param) == 0) { 755 if (strcmp(device->uuid, param) == 0) {
713 while (1) { 756 while (1) {
714 memset((char *)&ibuf, 0, SS_BUFSIZE); 757 rlen = srv_recv(ibuf);
715 fromlen = sizeof(peeraddr_in); 758 if (rlen == -1) {
716 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
717 if (rlen == -1) {
718 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
719 srv_send((char *)"518 recfrom(): %s", strerror(errno));
720 return 1; 759 return 1;
721 } 760 }
722 for (i = 0; i < strlen(ibuf); i++) {
723 if (ibuf[i] == '\n')
724 ibuf[i] = '\0';
725 if (ibuf[i] == '\r')
726 ibuf[i] = '\0';
727 }
728 for (i = strlen(ibuf) -1; i > 0; i--) {
729 if (ibuf[i] == ' ')
730 ibuf[i] = '\0';
731 else
732 break;
733 }
734 if (strlen(ibuf)) { 761 if (strlen(ibuf)) {
735 if (debug) {
736 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
737 fprintf(stdout, "recv: \"%s\"\n", ibuf);
738 }
739 if (strcmp(ibuf, (char *)".") == 0) { 762 if (strcmp(ibuf, (char *)".") == 0) {
740 srv_send((char *)"219 Accepted Device record"); 763 srv_send((char *)"219 Accepted Device record");
741 return 0; 764 return 0;
742 } 765 }
743 kwd = strtok(ibuf, ",\0"); 766 kwd = strtok(ibuf, ",\0");
906 * GLOBAL PUT 929 * GLOBAL PUT
907 */ 930 */
908 int cmd_global(char *buf) 931 int cmd_global(char *buf)
909 { 932 {
910 char *opt, *kwd, *val, ibuf[SS_BUFSIZE]; 933 char *opt, *kwd, *val, ibuf[SS_BUFSIZE];
911 int ival, i, rlen; 934 int ival, rlen;
912 socklen_t fromlen;
913 935
914 opt = strtok(buf, " \0"); 936 opt = strtok(buf, " \0");
915 opt = strtok(NULL, "\0"); 937 opt = strtok(NULL, "\0");
916 938
917 if (opt == NULL) { 939 if (opt == NULL) {
939 return 1; 961 return 1;
940 } 962 }
941 963
942 if (strcmp(opt, (char *)"PUT") == 0) { 964 if (strcmp(opt, (char *)"PUT") == 0) {
943 while (1) { 965 while (1) {
944 memset((char *)&ibuf, 0, SS_BUFSIZE); 966 rlen = srv_recv(ibuf);
945 fromlen = sizeof(peeraddr_in);
946 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
947 if (rlen == -1) { 967 if (rlen == -1) {
948 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
949 srv_send((char *)"518 recfrom(): %s", strerror(errno));
950 return 1; 968 return 1;
951 } 969 }
952 for (i = 0; i < strlen(ibuf); i++) {
953 if (ibuf[i] == '\n')
954 ibuf[i] = '\0';
955 if (ibuf[i] == '\r')
956 ibuf[i] = '\0';
957 }
958 for (i = strlen(ibuf) -1; i > 0; i--) {
959 if (ibuf[i] == ' ')
960 ibuf[i] = '\0';
961 else
962 break;
963 }
964 if (strlen(ibuf)) { 970 if (strlen(ibuf)) {
965 if (debug) {
966 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
967 fprintf(stdout, "recv: \"%s\"\n", ibuf);
968 }
969 if (strcmp(ibuf, (char *)".") == 0) { 971 if (strcmp(ibuf, (char *)".") == 0) {
970 srv_send((char *)"219 Accepted Global record"); 972 srv_send((char *)"219 Accepted Global record");
971 return 0; 973 return 0;
972 } 974 }
973 kwd = strtok(ibuf, ",\0"); 975 kwd = strtok(ibuf, ",\0");
1204 * PROFILE PUTS uuid Put profile steps list 1206 * PROFILE PUTS uuid Put profile steps list
1205 */ 1207 */
1206 int cmd_profile(char *buf) 1208 int cmd_profile(char *buf)
1207 { 1209 {
1208 char ibuf[SS_BUFSIZE], *sstep, *rest, *targ, *param, *kwd, *val; 1210 char ibuf[SS_BUFSIZE], *sstep, *rest, *targ, *param, *kwd, *val;
1209 int i, j, rlen, istep, irest; 1211 int j, rlen, istep, irest;
1210 float ftarg, fval; 1212 float ftarg, fval;
1211 socklen_t fromlen;
1212 char *opt; 1213 char *opt;
1213 profiles_list *profile, *tmpp; 1214 profiles_list *profile, *tmpp;
1214 prof_step *step, *olds; 1215 prof_step *step, *olds;
1215 uuid_t uu; 1216 uuid_t uu;
1216 1217
1296 1297
1297 } else if (strcmp(opt, (char *)"PUT") == 0) { 1298 } else if (strcmp(opt, (char *)"PUT") == 0) {
1298 for (profile = Config.profiles; profile; profile = profile->next) { 1299 for (profile = Config.profiles; profile; profile = profile->next) {
1299 if (strcmp(profile->uuid, param) == 0) { 1300 if (strcmp(profile->uuid, param) == 0) {
1300 while (1) { 1301 while (1) {
1301 memset((char *)&ibuf, 0, SS_BUFSIZE); 1302 rlen = srv_recv(ibuf);
1302 fromlen = sizeof(peeraddr_in);
1303 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
1304 if (rlen == -1) { 1303 if (rlen == -1) {
1305 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
1306 srv_send((char *)"518 recfrom(): %s", strerror(errno));
1307 return 1; 1304 return 1;
1308 } 1305 }
1309 for (i = 0; i < strlen(ibuf); i++) {
1310 if (ibuf[i] == '\n')
1311 ibuf[i] = '\0';
1312 if (ibuf[i] == '\r')
1313 ibuf[i] = '\0';
1314 }
1315 for (i = strlen(ibuf) -1; i > 0; i--) {
1316 if (ibuf[i] == ' ')
1317 ibuf[i] = '\0';
1318 else
1319 break;
1320 }
1321 if (strlen(ibuf)) { 1306 if (strlen(ibuf)) {
1322 if (debug) {
1323 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
1324 fprintf(stdout, "recv: \"%s\"\n", ibuf);
1325 }
1326 if (strcmp(ibuf, (char *)".") == 0) { 1307 if (strcmp(ibuf, (char *)".") == 0) {
1327 srv_send((char *)"219 Accepted Profile record"); 1308 srv_send((char *)"219 Accepted Profile record");
1328 return 0; 1309 return 0;
1329 } 1310 }
1330 kwd = strtok(ibuf, ",\0"); 1311 kwd = strtok(ibuf, ",\0");
1382 profile->steps = NULL; 1363 profile->steps = NULL;
1383 } 1364 }
1384 1365
1385 j = 0; 1366 j = 0;
1386 while (1) { 1367 while (1) {
1387 memset((char *)&ibuf, 0, SS_BUFSIZE); 1368 rlen = srv_recv(ibuf);
1388 fromlen = sizeof(peeraddr_in);
1389 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
1390 if (rlen == -1) { 1369 if (rlen == -1) {
1391 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
1392 srv_send((char *)"518 recfrom(): %s", strerror(errno));
1393 return 1; 1370 return 1;
1394 } else { 1371 } else {
1395 for (i = 0; i < strlen(ibuf); i++) {
1396 if (ibuf[i] == '\n')
1397 ibuf[i] = '\0';
1398 if (ibuf[i] == '\r')
1399 ibuf[i] = '\0';
1400 }
1401 for (i = strlen(ibuf) -1; i > 0; i--) {
1402 if (ibuf[i] == ' ')
1403 ibuf[i] = '\0';
1404 else
1405 break;
1406 }
1407 if (strlen(ibuf)) { 1372 if (strlen(ibuf)) {
1408 if (debug) {
1409 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
1410 fprintf(stdout, "recv: \"%s\"\n", ibuf);
1411 }
1412 if (strcmp(ibuf, (char *)".") == 0) { 1373 if (strcmp(ibuf, (char *)".") == 0) {
1413 1374
1414 srv_send((char *)"219 Accepted Profile steps"); 1375 srv_send((char *)"219 Accepted Profile steps");
1415 return 0; 1376 return 0;
1416 } 1377 }
1504 */ 1465 */
1505 int cmd_simulator(char *buf) 1466 int cmd_simulator(char *buf)
1506 { 1467 {
1507 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 1468 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
1508 simulator_list *simulator, *tmps; 1469 simulator_list *simulator, *tmps;
1509 socklen_t fromlen; 1470 int rc, rlen, ival;
1510 int i, rc, rlen, ival;
1511 float fval; 1471 float fval;
1512 uuid_t uu; 1472 uuid_t uu;
1513 1473
1514 opt = strtok(buf, " \0"); 1474 opt = strtok(buf, " \0");
1515 opt = strtok(NULL, " \0"); 1475 opt = strtok(NULL, " \0");
1622 1582
1623 if (strcmp(opt, (char *)"PUT") == 0) { 1583 if (strcmp(opt, (char *)"PUT") == 0) {
1624 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 1584 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
1625 if (strcmp(simulator->uuid, param) == 0) { 1585 if (strcmp(simulator->uuid, param) == 0) {
1626 while (1) { 1586 while (1) {
1627 memset((char *)&ibuf, 0, SS_BUFSIZE); 1587 rlen = srv_recv(ibuf);
1628 fromlen = sizeof(peeraddr_in);
1629 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
1630 if (rlen == -1) { 1588 if (rlen == -1) {
1631 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
1632 srv_send((char *)"518 recfrom(): %s", strerror(errno));
1633 return 1; 1589 return 1;
1634 } 1590 }
1635 for (i = 0; i < strlen(ibuf); i++) {
1636 if (ibuf[i] == '\n')
1637 ibuf[i] = '\0';
1638 if (ibuf[i] == '\r')
1639 ibuf[i] = '\0';
1640 }
1641 for (i = strlen(ibuf) -1; i > 0; i--) {
1642 if (ibuf[i] == ' ')
1643 ibuf[i] = '\0';
1644 else
1645 break;
1646 }
1647 if (strlen(ibuf)) { 1591 if (strlen(ibuf)) {
1648 if (debug) {
1649 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
1650 fprintf(stdout, "recv: \"%s\"\n", ibuf);
1651 }
1652 if (strcmp(ibuf, (char *)".") == 0) { 1592 if (strcmp(ibuf, (char *)".") == 0) {
1653 srv_send((char *)"219 Accepted Simulator record"); 1593 srv_send((char *)"219 Accepted Simulator record");
1654 return 0; 1594 return 0;
1655 } 1595 }
1656 kwd = strtok(ibuf, ",\0"); 1596 kwd = strtok(ibuf, ",\0");
1818 free(current->psu_address); 1758 free(current->psu_address);
1819 current->psu_address = NULL; 1759 current->psu_address = NULL;
1820 if (current->profile) 1760 if (current->profile)
1821 free(current->profile); 1761 free(current->profile);
1822 current->profile = NULL; 1762 current->profile = NULL;
1763 if (current->PID_cool)
1764 free(current->PID_cool);
1765 current->PID_cool = NULL;
1766 if (current->PID_heat)
1767 free(current->PID_heat);
1768 current->PID_heat = NULL;
1823 free(current); 1769 free(current);
1824 return 1; 1770 return 1;
1825 } else { 1771 } else {
1826 free(current->uuid); 1772 free(current->uuid);
1827 current->uuid = NULL; 1773 current->uuid = NULL;
1852 free(current->psu_address); 1798 free(current->psu_address);
1853 current->psu_address = NULL; 1799 current->psu_address = NULL;
1854 if (current->profile) 1800 if (current->profile)
1855 free(current->profile); 1801 free(current->profile);
1856 current->profile = NULL; 1802 current->profile = NULL;
1803 if (current->PID_cool)
1804 free(current->PID_cool);
1805 current->PID_cool = NULL;
1806 if (current->PID_heat)
1807 free(current->PID_heat);
1808 current->PID_heat = NULL;
1857 previous->next = current->next; 1809 previous->next = current->next;
1858 free(current); 1810 free(current);
1859 current = previous->next; 1811 current = previous->next;
1860 return 1; 1812 return 1;
1861 } 1813 }
1879 int cmd_unit(char *buf) 1831 int cmd_unit(char *buf)
1880 { 1832 {
1881 char *opt, *param = NULL, *kwd, *val, ibuf[SS_BUFSIZE]; 1833 char *opt, *param = NULL, *kwd, *val, ibuf[SS_BUFSIZE];
1882 units_list *unit, *tmpu; 1834 units_list *unit, *tmpu;
1883 uuid_t uu; 1835 uuid_t uu;
1884 socklen_t fromlen;
1885 int ival, i, rc, rlen; 1836 int ival, i, rc, rlen;
1886 float fval; 1837 float fval;
1887 1838
1888 opt = strtok(buf, " \0"); 1839 opt = strtok(buf, " \0");
1889 opt = strtok(NULL, " \0"); 1840 opt = strtok(NULL, " \0");
1929 unit->light_delay = 1; /* 15 seconds delay */ 1880 unit->light_delay = 1; /* 15 seconds delay */
1930 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 1881 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
1931 unit->heater_usage = unit->cooler_usage = unit->fan_usage = unit->light_usage = 0; 1882 unit->heater_usage = unit->cooler_usage = unit->fan_usage = unit->light_usage = 0;
1932 unit->temp_set_min = 1.0; 1883 unit->temp_set_min = 1.0;
1933 unit->temp_set_max = 30.0; 1884 unit->temp_set_max = 30.0;
1934 unit->idle_rangeH = 1.0;
1935 unit->idle_rangeL = -1.0;
1936 unit->prof_started = unit->prof_paused = unit->prof_primary_done = (time_t)0; 1885 unit->prof_started = unit->prof_paused = unit->prof_primary_done = (time_t)0;
1937 unit->prof_percent = 0; 1886 unit->prof_percent = 0;
1938 unit->PID_dState = unit->PID_iState = unit->PID_Kp = unit->PID_Kd = unit->PID_Ki = 0.0; 1887 unit->PID_cool = (pid_var *)malloc(sizeof(pid_var));
1888 unit->PID_heat = (pid_var *)malloc(sizeof(pid_var));
1889 InitPID(unit->PID_cool, PID_TYPE_COOL);
1890 InitPID(unit->PID_heat, PID_TYPE_HEAT);
1939 1891
1940 /* 1892 /*
1941 * Block main process 1893 * Block main process
1942 */ 1894 */
1943 run_pause = TRUE; 1895 run_pause = TRUE;
2009 srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0); 1961 srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
2010 srv_send((char *)"HEATER_ADDRESS,%s", unit->heater_address); 1962 srv_send((char *)"HEATER_ADDRESS,%s", unit->heater_address);
2011 srv_send((char *)"HEATER_STATE,%d", unit->heater_state); 1963 srv_send((char *)"HEATER_STATE,%d", unit->heater_state);
2012 srv_send((char *)"HEATER_DELAY,%d", unit->heater_delay); 1964 srv_send((char *)"HEATER_DELAY,%d", unit->heater_delay);
2013 srv_send((char *)"HEATER_USAGE,%d", unit->heater_usage); 1965 srv_send((char *)"HEATER_USAGE,%d", unit->heater_usage);
1966 if (unit->PID_heat) {
1967 srv_send((char *)"PIDH_IMAX,%.1f", unit->PID_heat->iMax);
1968 srv_send((char *)"PIDH_PGAIN,%.2f", unit->PID_heat->pGain);
1969 srv_send((char *)"PIDH_IGAIN,%.2f", unit->PID_heat->iGain);
1970 srv_send((char *)"PIDH_DGAIN,%.2f", unit->PID_heat->dGain);
1971 srv_send((char *)"PIDH_IDLERANGE,%.2f", unit->PID_heat->idleRange);
1972 }
2014 srv_send((char *)"COOLER_ADDRESS,%s", unit->cooler_address); 1973 srv_send((char *)"COOLER_ADDRESS,%s", unit->cooler_address);
2015 srv_send((char *)"COOLER_STATE,%d", unit->cooler_state); 1974 srv_send((char *)"COOLER_STATE,%d", unit->cooler_state);
2016 srv_send((char *)"COOLER_DELAY,%d", unit->cooler_delay); 1975 srv_send((char *)"COOLER_DELAY,%d", unit->cooler_delay);
2017 srv_send((char *)"COOLER_USAGE,%d", unit->cooler_usage); 1976 srv_send((char *)"COOLER_USAGE,%d", unit->cooler_usage);
1977 if (unit->PID_cool) {
1978 srv_send((char *)"PIDC_IMAX,%.1f", unit->PID_cool->iMax);
1979 srv_send((char *)"PIDC_PGAIN,%.2f", unit->PID_cool->pGain);
1980 srv_send((char *)"PIDC_IGAIN,%.2f", unit->PID_cool->iGain);
1981 srv_send((char *)"PIDC_DGAIN,%.2f", unit->PID_cool->dGain);
1982 srv_send((char *)"PIDC_IDLERANGE,%.2f", unit->PID_cool->idleRange);
1983 }
2018 srv_send((char *)"FAN_ADDRESS,%s", unit->fan_address); 1984 srv_send((char *)"FAN_ADDRESS,%s", unit->fan_address);
2019 srv_send((char *)"FAN_STATE,%d", unit->fan_state); 1985 srv_send((char *)"FAN_STATE,%d", unit->fan_state);
2020 srv_send((char *)"FAN_DELAY,%d", unit->fan_delay); 1986 srv_send((char *)"FAN_DELAY,%d", unit->fan_delay);
2021 srv_send((char *)"FAN_USAGE,%d", unit->fan_usage); 1987 srv_send((char *)"FAN_USAGE,%d", unit->fan_usage);
2022 srv_send((char *)"LIGHT_ADDRESS,%s", unit->light_address); 1988 srv_send((char *)"LIGHT_ADDRESS,%s", unit->light_address);
2041 srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs); 2007 srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs);
2042 srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel); 2008 srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel);
2043 srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done); 2009 srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done);
2044 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); 2010 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min);
2045 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); 2011 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max);
2046 srv_send((char *)"IDLE_RANGE_L,%.2f", unit->idle_rangeL);
2047 srv_send((char *)"IDLE_RANGE_H,%.2f", unit->idle_rangeH);
2048 srv_send((char *)"PID_KP,%.2f", unit->PID_Kp);
2049 srv_send((char *)"PID_KI,%.2f", unit->PID_Ki);
2050 srv_send((char *)"PID_KD,%.2f", unit->PID_Kd);
2051 srv_send((char *)"."); 2012 srv_send((char *)".");
2052 return 1; 2013 return 1;
2053 } 2014 }
2054 } 2015 }
2055 srv_send((char *)"440 No such unit"); 2016 srv_send((char *)"440 No such unit");
2056 return 1; 2017 return 1;
2057 } 2018 }
2058 2019
2059 if (strcmp(opt, (char *)"PUT") == 0) { 2020 if (strcmp(opt, (char *)"PUT") == 0) {
2021 /*
2022 * Block main process
2023 */
2024 run_pause = TRUE;
2025 for (;;) {
2026 usleep(100000);
2027 if (run_hold)
2028 break;
2029 }
2030
2060 for (unit = Config.units ; unit; unit = unit->next) { 2031 for (unit = Config.units ; unit; unit = unit->next) {
2061 if (strcmp(unit->uuid, param) == 0) { 2032 if (strcmp(unit->uuid, param) == 0) {
2062 while (1) { 2033 while (1) {
2063 memset((char *)&ibuf, 0, SS_BUFSIZE); 2034 rlen = srv_recv(ibuf);
2064 fromlen = sizeof(peeraddr_in);
2065 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
2066 if (rlen == -1) { 2035 if (rlen == -1) {
2067 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); 2036 run_pause = FALSE;
2068 srv_send((char *)"518 recfrom(): %s", strerror(errno));
2069 return 1; 2037 return 1;
2070 } 2038 }
2071 for (i = 0; i < strlen(ibuf); i++) {
2072 if (ibuf[i] == '\n')
2073 ibuf[i] = '\0';
2074 if (ibuf[i] == '\r')
2075 ibuf[i] = '\0';
2076 }
2077 for (i = strlen(ibuf) -1; i > 0; i--) {
2078 if (ibuf[i] == ' ')
2079 ibuf[i] = '\0';
2080 else
2081 break;
2082 }
2083 if (strlen(ibuf)) { 2039 if (strlen(ibuf)) {
2084 if (debug) {
2085 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
2086 fprintf(stdout, "recv: \"%s\"\n", ibuf);
2087 }
2088 if (strcmp(ibuf, (char *)".") == 0) { 2040 if (strcmp(ibuf, (char *)".") == 0) {
2089 srv_send((char *)"219 Accepted Unit record"); 2041 srv_send((char *)"219 Accepted Unit record");
2042 run_pause = FALSE;
2090 return 0; 2043 return 0;
2091 } 2044 }
2092 kwd = strtok(ibuf, ",\0"); 2045 kwd = strtok(ibuf, ",\0");
2093 val = strtok(NULL, "\0"); 2046 val = strtok(NULL, "\0");
2094 if (kwd) { 2047 if (kwd) {
2279 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) 2232 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF))
2280 initlog(unit->name); 2233 initlog(unit->name);
2281 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); 2234 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
2282 unit->mode = i; 2235 unit->mode = i;
2283 /* Allways turn everything off after a mode change */ 2236 /* Allways turn everything off after a mode change */
2284 unit->PID_iState = unit->PID_dState = 0.0; 2237 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
2238 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
2285 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; 2239 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0;
2286 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 2240 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2287 device_out(unit->heater_address, unit->heater_state); 2241 device_out(unit->heater_address, unit->heater_state);
2288 device_out(unit->cooler_address, unit->cooler_state); 2242 device_out(unit->cooler_address, unit->cooler_state);
2289 device_out(unit->fan_address, unit->fan_state); 2243 device_out(unit->fan_address, unit->fan_state);
2311 if (unit->beer_set != fval) 2265 if (unit->beer_set != fval)
2312 syslog(LOG_NOTICE, "Fermenter unit %s beer temperature %.1f to %.1f", unit->uuid, unit->beer_set, fval); 2266 syslog(LOG_NOTICE, "Fermenter unit %s beer temperature %.1f to %.1f", unit->uuid, unit->beer_set, fval);
2313 unit->beer_set = fval; 2267 unit->beer_set = fval;
2314 } 2268 }
2315 2269
2316 } else if (val && (strcmp(kwd, (char *)"PID_KP") == 0)) { 2270 } else if (val && (strcmp(kwd, (char *)"PIDC_IMAX") == 0)) {
2317 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) { 2271 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2318 if (unit->PID_Kp != fval) 2272 if (unit->PID_cool->iMax != fval)
2319 syslog(LOG_NOTICE, "Fermenter unit %s PID Kp %.2f to %.2f", unit->uuid, unit->PID_Kp, fval); 2273 syslog(LOG_NOTICE, "Fermenter unit %s PID_cool iGain %.1f to %.1f", unit->uuid, unit->PID_cool->iMax, fval);
2320 unit->PID_Kp = fval; 2274 unit->PID_cool->iMax = fval;
2321 } 2275 }
2322 2276
2323 } else if (val && (strcmp(kwd, (char *)"PID_KD") == 0)) { 2277 } else if (val && (strcmp(kwd, (char *)"PIDC_PGAIN") == 0)) {
2324 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) { 2278 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2325 if (unit->PID_Kd != fval) 2279 if (unit->PID_cool->pGain != fval)
2326 syslog(LOG_NOTICE, "Fermenter unit %s PID Kd %.2f to %.2f", unit->uuid, unit->PID_Kd, fval); 2280 syslog(LOG_NOTICE, "Fermenter unit %s PID_cool pGain %.2f to %.2f", unit->uuid, unit->PID_cool->pGain, fval);
2327 unit->PID_Kd = fval; 2281 unit->PID_cool->pGain = fval;
2282 }
2283
2284 } else if (val && (strcmp(kwd, (char *)"PIDC_DGAIN") == 0)) {
2285 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2286 if (unit->PID_cool->dGain != fval)
2287 syslog(LOG_NOTICE, "Fermenter unit %s PID_cool dGain %.2f to %.2f", unit->uuid, unit->PID_cool->dGain, fval);
2288 unit->PID_cool->dGain = fval;
2328 } 2289 }
2329 2290
2330 } else if (val && (strcmp(kwd, (char *)"PID_KI") == 0)) { 2291 } else if (val && (strcmp(kwd, (char *)"PIDC_IGAIN") == 0)) {
2331 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) { 2292 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2332 if (unit->PID_Ki != fval) 2293 if (unit->PID_cool->iGain != fval)
2333 syslog(LOG_NOTICE, "Fermenter unit %s PID Ki %.2f to %.2f", unit->uuid, unit->PID_Ki, fval); 2294 syslog(LOG_NOTICE, "Fermenter unit %s PID_cool iGain %.2f to %.2f", unit->uuid, unit->PID_cool->iGain, fval);
2334 unit->PID_Ki = fval; 2295 unit->PID_cool->iGain = fval;
2296 }
2297
2298 } else if (val && (strcmp(kwd, (char *)"PIDC_IDLERANGE") == 0)) {
2299 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2300 if (unit->PID_cool->idleRange != fval)
2301 syslog(LOG_NOTICE, "Fermenter unit %s PID_cool idleRange %.2f to %.2f", unit->uuid, unit->PID_cool->idleRange, fval);
2302 unit->PID_cool->idleRange = fval;
2303 }
2304
2305 } else if (val && (strcmp(kwd, (char *)"PIDH_IMAX") == 0)) {
2306 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2307 if (unit->PID_heat->iMax != fval)
2308 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat iGain %.1f to %.1f", unit->uuid, unit->PID_heat->iMax, fval);
2309 unit->PID_heat->iMax = fval;
2310 }
2311
2312 } else if (val && (strcmp(kwd, (char *)"PIDH_PGAIN") == 0)) {
2313 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2314 if (unit->PID_heat->pGain != fval)
2315 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat pGain %.2f to %.2f", unit->uuid, unit->PID_heat->pGain, fval);
2316 unit->PID_heat->pGain = fval;
2317 }
2318
2319 } else if (val && (strcmp(kwd, (char *)"PIDH_DGAIN") == 0)) {
2320 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2321 if (unit->PID_heat->dGain != fval)
2322 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat dGain %.2f to %.2f", unit->uuid, unit->PID_heat->dGain, fval);
2323 unit->PID_heat->dGain = fval;
2324 }
2325
2326 } else if (val && (strcmp(kwd, (char *)"PIDH_IGAIN") == 0)) {
2327 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2328 if (unit->PID_heat->iGain != fval)
2329 syslog(LOG_NOTICE, "Fermenter unit %s PIH_heat iGain %.2f to %.2f", unit->uuid, unit->PID_heat->iGain, fval);
2330 unit->PID_heat->iGain = fval;
2331 }
2332
2333 } else if (val && (strcmp(kwd, (char *)"PIDH_IDLERANGE") == 0)) {
2334 if ((sscanf(val, "%f", &fval) == 1) && (fval >= 0.0)) {
2335 if (unit->PID_heat->idleRange != fval)
2336 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat idleRange %.2f to %.2f", unit->uuid, unit->PID_heat->idleRange, fval);
2337 unit->PID_heat->idleRange = fval;
2335 } 2338 }
2336 2339
2337 } else if (strcmp(kwd, (char *)"PROFILE") == 0) { 2340 } else if (strcmp(kwd, (char *)"PROFILE") == 0) {
2338 if (unit->prof_state == PROFILE_OFF) { 2341 if (unit->prof_state == PROFILE_OFF) {
2339 /* 2342 /*
2349 else 2352 else
2350 unit->profile = NULL; 2353 unit->profile = NULL;
2351 /* 2354 /*
2352 * Reset all output devices 2355 * Reset all output devices
2353 */ 2356 */
2354 unit->PID_iState = unit->PID_dState = 0.0; 2357 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
2358 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
2355 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; 2359 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0;
2356 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 2360 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2357 device_out(unit->heater_address, unit->heater_state); 2361 device_out(unit->heater_address, unit->heater_state);
2358 device_out(unit->cooler_address, unit->cooler_state); 2362 device_out(unit->cooler_address, unit->cooler_state);
2359 device_out(unit->fan_address, unit->fan_state); 2363 device_out(unit->fan_address, unit->fan_state);
2409 if (unit->temp_set_max != fval) 2413 if (unit->temp_set_max != fval)
2410 syslog(LOG_NOTICE, "Fermenter unit %s temperature set maximum %.1f to %.1f", unit->uuid, unit->temp_set_max, fval); 2414 syslog(LOG_NOTICE, "Fermenter unit %s temperature set maximum %.1f to %.1f", unit->uuid, unit->temp_set_max, fval);
2411 unit->temp_set_max = fval; 2415 unit->temp_set_max = fval;
2412 } 2416 }
2413 2417
2414 } else if (val && (strcmp(kwd, (char *)"IDLE_RANGE_L") == 0)) {
2415 if (sscanf(val, "%f", &fval) == 1) {
2416 if (unit->idle_rangeL != fval)
2417 syslog(LOG_NOTICE, "Fermenter unit %s idle range low %.2f to %.2f", unit->uuid, unit->idle_rangeL, fval);
2418 unit->idle_rangeL = fval;
2419 }
2420
2421 } else if (val && (strcmp(kwd, (char *)"IDLE_RANGE_H") == 0)) {
2422 if (sscanf(val, "%f", &fval) == 1) {
2423 if (unit->idle_rangeH != fval)
2424 syslog(LOG_NOTICE, "Fermenter unit %s idle range high %.2f to %.2f", unit->uuid, unit->idle_rangeH, fval);
2425 unit->idle_rangeH = fval;
2426 }
2427
2428 } 2418 }
2429 } 2419 }
2430 } 2420 }
2431 } 2421 }
2432 } 2422 }
2433 } 2423 }
2434 srv_send((char *)"440 No such unit"); 2424 srv_send((char *)"440 No such unit");
2425 run_pause = FALSE;
2435 return 1; 2426 return 1;
2436 } 2427 }
2437 2428
2438 srv_send((char *)"504 Subcommand error"); 2429 srv_send((char *)"504 Subcommand error");
2439 return 1; 2430 return 1;
2442 2433
2443 2434
2444 void cmd_server(void) 2435 void cmd_server(void)
2445 { 2436 {
2446 char buf[SS_BUFSIZE]; 2437 char buf[SS_BUFSIZE];
2447 int i, rlen; 2438 int rlen;
2448 socklen_t fromlen; 2439
2449 2440 rlen = srv_recv(buf);
2450 memset((char *)&buf, 0, SS_BUFSIZE); 2441 if (rlen != -1) {
2451 fromlen = sizeof(peeraddr_in);
2452 rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
2453 if (rlen == -1) {
2454 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
2455 } else {
2456 for (i = 0; i < strlen(buf); i++) {
2457 if (buf[i] == '\n')
2458 buf[i] = '\0';
2459 if (buf[i] == '\r')
2460 buf[i] = '\0';
2461 }
2462 for (i = strlen(buf) -1; i > 0; i--) {
2463 if (buf[i] == ' ')
2464 buf[i] = '\0';
2465 else
2466 break;
2467 }
2468 if (strlen(buf)) { 2442 if (strlen(buf)) {
2469 if (debug) {
2470 syslog(LOG_NOTICE, "recv: \"%s\"", buf);
2471 fprintf(stdout, "recv: \"%s\"\n", buf);
2472 }
2473
2474 /* 2443 /*
2475 * Process commands from the client 2444 * Process commands from the client
2476 */ 2445 */
2477 if (strncmp(buf, "ARCHIVE", 7) == 0) { 2446 if (strncmp(buf, "ARCHIVE", 7) == 0) {
2478 cmd_archive(buf); 2447 cmd_archive(buf);

mercurial