thermferm/server.c

changeset 570
1e0192b295b9
parent 566
776a605befa5
child 575
86496d2bc4bb
equal deleted inserted replaced
569:9c69d43bfb06 570:1e0192b295b9
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2008-2018 2 * Copyright (C) 2008-2019
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
179 #ifdef HAVE_WIRINGPI_H 179 #ifdef HAVE_WIRINGPI_H
180 piUnlock(LOCK_DEVICES); 180 piUnlock(LOCK_DEVICES);
181 #endif 181 #endif
182 } 182 }
183 } 183 }
184 }
185
186
187
188 int delete_Profile(char *uuid)
189 {
190 profiles_list *current = Config.profiles;
191 profiles_list *previous = NULL;
192 prof_step *step, *olds;
193
194 while (current) {
195 if (strcmp(current->uuid, uuid) == 0) {
196 if (previous == NULL) {
197 Config.profiles = current->next;
198 free(current->uuid);
199 current->uuid = NULL;
200 free(current->name);
201 current->name = NULL;
202 if (current->steps) {
203 for (step = current->steps; step; step = olds) {
204 olds = step->next;
205 free(step);
206 }
207 current->steps = NULL;
208 }
209 free(current);
210 return 1;
211 } else {
212 free(current->uuid);
213 current->uuid = NULL;
214 free(current->name);
215 current->name = NULL;
216 if (current->steps) {
217 for (step = current->steps; step; step = olds) {
218 olds = step->next;
219 free(step);
220 }
221 current->steps = NULL;
222 }
223 previous->next = current->next;
224 free(current);
225 current = previous->next;
226 return 1;
227 }
228 } else {
229 previous = current;
230 current = current->next;
231 }
232 }
233
234 return 0;
235 } 184 }
236 185
237 186
238 187
239 void tidy_lslist(ls_list **lap) 188 void tidy_lslist(ls_list **lap)
697 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) || 646 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) ||
698 (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) { 647 (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) {
699 648
700 device = (devices_list *)malloc(sizeof(devices_list)); 649 device = (devices_list *)malloc(sizeof(devices_list));
701 device->next = NULL; 650 device->next = NULL;
702 device->version = 1;
703 device->uuid = malloc(37); 651 device->uuid = malloc(37);
704 uuid_generate(uu); 652 uuid_generate(uu);
705 uuid_unparse(uu, device->uuid); 653 uuid_unparse(uu, device->uuid);
706 for (i = 0; i < 8; i++) { 654 for (i = 0; i < 8; i++) {
707 if (strcmp(param, DEVTYPE[i]) == 0) { 655 if (strcmp(param, DEVTYPE[i]) == 0) {
1198 return 0; 1146 return 0;
1199 } 1147 }
1200 1148
1201 1149
1202 1150
1203 /*
1204 * PROFILE ADD name Add a new profile
1205 * PROFILE DEL uuid Delete profile with uuid
1206 * PROFILE LIST List available profiles
1207 * PROFILE GET uuid Get profile record
1208 * PROFILE PUT uuid Put profile record
1209 * PROFILE GETS uuid Get profile steps list
1210 * PROFILE PUTS uuid Put profile steps list
1211 */
1212 int cmd_profile(char *buf)
1213 {
1214 char ibuf[SS_BUFSIZE], *sstep, *rest, *tlarg, *tharg, *frarg, *param, *kwd, *val;
1215 int j, ival, rlen, istep, irest, ifrarg;
1216 float ftlarg, ftharg, fval;
1217 char *opt;
1218 profiles_list *profile, *tmpp;
1219 prof_step *step, *olds;
1220 uuid_t uu;
1221
1222 opt = strtok(buf, " \0");
1223 opt = strtok(NULL, " \0");
1224
1225 if (opt == NULL) {
1226 srv_send((char *)"501 Subcommand missing");
1227 return 0;
1228 }
1229
1230 if (strcmp(opt, (char *)"HELP") == 0) {
1231 srv_send((char *)"100 Help text follows:");
1232 srv_send((char *)"Recognized commands:");
1233 srv_send((char *)"PROFILE uuid,name Profile rename");
1234 srv_send((char *)"PROFILE ADD name Add new Profile with name");
1235 srv_send((char *)"PROFILE DEL uuid Delete Profile by uuid");
1236 srv_send((char *)"PROFILE LIST List available profiles");
1237 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid");
1238 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid");
1239 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
1240 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
1241 srv_send((char *)".");
1242 return 0;
1243 }
1244
1245 if (strcmp(opt, (char *)"LIST") == 0) {
1246 /*
1247 * Fermenting profiles
1248 */
1249 srv_send((char *)"212 Profiles list follows:");
1250 for (profile = Config.profiles; profile; profile = profile->next) {
1251 j = 0;
1252 for (step = profile->steps; step; step = step->next)
1253 j++;
1254 srv_send((char *)"%s,%s,%d,%d", profile->uuid, profile->name, j, profile->busy);
1255 }
1256 srv_send((char *)".");
1257 return 0;
1258 }
1259
1260 param = strtok(NULL, "\0");
1261 if (param == NULL) {
1262 srv_send((char *)"502 Parameter missing");
1263 return 0;
1264 }
1265
1266 if (strcmp(opt, (char *)"ADD") == 0) {
1267 profile = (profiles_list *)malloc(sizeof(profiles_list));
1268 profile->next = NULL;
1269 profile->version = 1;
1270 profile->uuid = malloc(37);
1271 uuid_generate(uu);
1272 uuid_unparse(uu, profile->uuid);
1273 profile->name = xstrcpy(param);
1274 profile->busy = profile->fridge_mode = 0;
1275 profile->inittemp_lo = 19.8;
1276 profile->inittemp_hi = 20.2;
1277 profile->steps = NULL;
1278 if (Config.profiles == NULL) {
1279 Config.profiles = profile;
1280 } else {
1281 for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) {
1282 if (tmpp->next == NULL) {
1283 tmpp->next = profile;
1284 break;
1285 }
1286 }
1287 }
1288
1289 syslog(LOG_NOTICE, "Profile %s added", profile->uuid);
1290 srv_send((char *)"211 Profile %s added", profile->uuid);
1291 return 1;
1292
1293
1294 } else if (strcmp(opt, (char *)"DEL") == 0) {
1295 if (delete_Profile(param)) {
1296 syslog(LOG_NOTICE, "Profile %s deleted", param);
1297 srv_send((char *)"211 Profile %s deleted", param);
1298 return 1;
1299 } else {
1300 srv_send((char *)"440 No such profile");
1301 return 0;
1302 }
1303
1304 } else if (strcmp(opt, (char *)"GET") == 0) {
1305 for (profile = Config.profiles; profile; profile = profile->next) {
1306 if (strcmp(profile->uuid, param) == 0) {
1307 srv_send((char *)"213 Profile record follows:");
1308 srv_send((char *)"UUID,%s", profile->uuid);
1309 srv_send((char *)"NAME,%s", profile->name);
1310 srv_send((char *)"INITTEMP_LO,%.1f", profile->inittemp_lo);
1311 srv_send((char *)"INITTEMP_HI,%.1f", profile->inittemp_hi);
1312 srv_send((char *)"FRIDGE_MODE,%d", profile->fridge_mode);
1313 srv_send((char *)".");
1314 return 0;
1315 }
1316 }
1317 srv_send((char *)"440 No such profile");
1318 return 0;
1319
1320 } else if (strcmp(opt, (char *)"PUT") == 0) {
1321 for (profile = Config.profiles; profile; profile = profile->next) {
1322 if (strcmp(profile->uuid, param) == 0) {
1323 while (1) {
1324 rlen = srv_recv(ibuf);
1325 if (rlen == -1) {
1326 return 0;
1327 }
1328 if (strlen(ibuf)) {
1329 if (strcmp(ibuf, (char *)".") == 0) {
1330 srv_send((char *)"219 Accepted Profile record");
1331 return 1;
1332 }
1333 kwd = strtok(ibuf, ",\0");
1334 val = strtok(NULL, "\0");
1335 if (kwd && val) {
1336 if (strcmp(kwd, (char *)"NAME") == 0) {
1337 if (profile->name) {
1338 if (strcmp(profile->name, val))
1339 syslog(LOG_NOTICE, "Profile %s name `%s' to `%s'", profile->uuid, profile->name, val);
1340 free(profile->name);
1341 }
1342 profile->name = xstrcpy(val);
1343 } else if (strcmp(kwd, (char *)"INITTEMP_LO") == 0) {
1344 if (sscanf(val, "%f", &fval) == 1) {
1345 if (profile->inittemp_lo != fval)
1346 syslog(LOG_NOTICE, "Profile %s initial temperature low %.1f to %.1f", profile->uuid, profile->inittemp_lo, fval);
1347 profile->inittemp_lo = fval;
1348 }
1349 } else if (strcmp(kwd, (char *)"INITTEMP_HI") == 0) {
1350 if (sscanf(val, "%f", &fval) == 1) {
1351 if (profile->inittemp_hi != fval)
1352 syslog(LOG_NOTICE, "Profile %s initial temperature high %.1f to %.1f", profile->uuid, profile->inittemp_hi, fval);
1353 profile->inittemp_hi = fval;
1354 }
1355 } else if (strcmp(kwd, (char *)"FRIDGE_MODE") == 0) {
1356 if (sscanf(val, "%d", &ival) == 1) {
1357 if (profile->fridge_mode != ival)
1358 syslog(LOG_NOTICE, "Profile %s fridge mode %d to %d", profile->uuid, profile->fridge_mode, ival);
1359 profile->fridge_mode = ival;
1360 }
1361 }
1362 }
1363 }
1364 }
1365 }
1366 }
1367 srv_send((char *)"440 No such profile");
1368 return 0;
1369
1370 } else if (strcmp(opt, (char *)"GETS") == 0) {
1371
1372 for (profile = Config.profiles; profile; profile = profile->next) {
1373 if (strcmp(profile->uuid, param) == 0) {
1374 srv_send((char *)"215 Profile steps follow:");
1375 for (step = profile->steps; step; step = step->next) {
1376 srv_send((char *)"%d,%d,%.1f,%.1f,%d", step->steptime, step->resttime, step->target_lo, step->target_hi, step->fridge_mode);
1377 }
1378 srv_send((char *)".");
1379 return 0;
1380 }
1381 }
1382
1383 srv_send((char *)"440 No such profile");
1384 return 0;
1385
1386 } else if (strcmp(opt, (char *)"PUTS") == 0) {
1387
1388 for (profile = Config.profiles; profile; profile = profile->next) {
1389 if (strcmp(profile->uuid, param) == 0) {
1390
1391 if (profile->steps) {
1392 syslog(LOG_NOTICE, "PROFILE PUTS %s erased all old steps", profile->uuid);
1393 for (step = profile->steps; step; step = olds) {
1394 olds = step->next;
1395 free(step);
1396 }
1397 profile->steps = NULL;
1398 }
1399
1400 j = 0;
1401 while (1) {
1402 rlen = srv_recv(ibuf);
1403 if (rlen == -1) {
1404 return 0;
1405 } else {
1406 if (strlen(ibuf)) {
1407 if (strcmp(ibuf, (char *)".") == 0) {
1408
1409 srv_send((char *)"219 Accepted Profile steps");
1410 return 1;
1411 }
1412 sstep = strtok(ibuf, ",\0");
1413 rest = strtok(NULL, ",\0");
1414 tlarg = strtok(NULL, ",\0");
1415 tharg = strtok(NULL, ",\0");
1416 frarg = strtok(NULL, "\0");
1417
1418 if ((sscanf(sstep, "%d", &istep) == 1) &&
1419 (sscanf(rest, "%d", &irest) == 1) &&
1420 (sscanf(tlarg, "%f", &ftlarg) == 1) &&
1421 (sscanf(tharg, "%f", &ftharg) == 1) &&
1422 (sscanf(frarg, "%d", &ifrarg) == 1)) {
1423
1424 j++;
1425 syslog(LOG_NOTICE, "PROFILE PUTS %s add step %d: steptime=%d resttime=%d target=%.1f..%.1f fridge_mode=%d",
1426 profile->uuid, j, istep, irest, ftlarg, ftharg, ifrarg);
1427 step = (prof_step *)malloc(sizeof(prof_step));
1428 step->next = NULL;
1429 step->version = 1;
1430 step->steptime = istep;
1431 step->resttime = irest;
1432 step->target_lo = ftlarg;
1433 step->target_hi = ftharg;
1434 step->fridge_mode = ifrarg;
1435
1436 if (profile->steps == NULL) {
1437 profile->steps = step;
1438 } else {
1439 for (olds = profile->steps; olds; olds = olds->next) {
1440 if (olds->next == NULL) {
1441 olds->next = step;
1442 break;
1443 }
1444 }
1445 }
1446 }
1447 }
1448 }
1449 }
1450 }
1451 }
1452
1453 srv_send((char *)"440 No such profile");
1454 return 0;
1455 }
1456
1457 srv_send((char *)"504 Subcommand error");
1458 return 0;
1459 }
1460
1461
1462
1463 #ifdef USE_SIMULATOR 1151 #ifdef USE_SIMULATOR
1464 int delete_Simulator(char *uuid) 1152 int delete_Simulator(char *uuid)
1465 { 1153 {
1466 simulator_list *current = Config.simulators; 1154 simulator_list *current = Config.simulators;
1467 simulator_list *previous = NULL; 1155 simulator_list *previous = NULL;
1556 return 0; 1244 return 0;
1557 } 1245 }
1558 1246
1559 simulator = (simulator_list *)malloc(sizeof(simulator_list)); 1247 simulator = (simulator_list *)malloc(sizeof(simulator_list));
1560 simulator->next = NULL; 1248 simulator->next = NULL;
1561 simulator->version = 1;
1562 simulator->uuid = malloc(37); 1249 simulator->uuid = malloc(37);
1563 uuid_generate(uu); 1250 uuid_generate(uu);
1564 uuid_unparse(uu, simulator->uuid); 1251 uuid_unparse(uu, simulator->uuid);
1565 simulator->name = xstrcpy(param); 1252 simulator->name = xstrcpy(param);
1566 simulator->volume_air = 150; 1253 simulator->volume_air = 150;
1792 1479
1793 int delete_Unit(char *uuid) 1480 int delete_Unit(char *uuid)
1794 { 1481 {
1795 units_list *current = Config.units; 1482 units_list *current = Config.units;
1796 units_list *previous = NULL; 1483 units_list *previous = NULL;
1484 prof_step *step, *olds;
1797 1485
1798 while (current) { 1486 while (current) {
1799 if (strcmp(current->uuid, uuid) == 0) { 1487 if (strcmp(current->uuid, uuid) == 0) {
1800 if (previous == NULL) { 1488 if (previous == NULL) {
1801 Config.units = current->next; 1489 Config.units = current->next;
1835 free(current->door_address); 1523 free(current->door_address);
1836 current->door_address = NULL; 1524 current->door_address = NULL;
1837 if (current->psu_address) 1525 if (current->psu_address)
1838 free(current->psu_address); 1526 free(current->psu_address);
1839 current->psu_address = NULL; 1527 current->psu_address = NULL;
1840 if (current->profile) 1528 if (current->profile_uuid)
1841 free(current->profile); 1529 free(current->profile_uuid);
1842 current->profile = NULL; 1530 current->profile_uuid = NULL;
1531 if (current->profile_name)
1532 free(current->profile_name);
1533 current->profile_name = NULL;
1843 if (current->PID_cool) 1534 if (current->PID_cool)
1844 free(current->PID_cool); 1535 free(current->PID_cool);
1845 current->PID_cool = NULL; 1536 current->PID_cool = NULL;
1846 if (current->PID_heat) 1537 if (current->PID_heat)
1847 free(current->PID_heat); 1538 free(current->PID_heat);
1848 current->PID_heat = NULL; 1539 current->PID_heat = NULL;
1540 if (current->profile_steps) {
1541 for (step = current->profile_steps; step; step = olds) {
1542 olds = step->next;
1543 free(step);
1544 }
1545 current->profile_steps = NULL;
1546 }
1849 free(current); 1547 free(current);
1850 return 1; 1548 return 1;
1851 } else { 1549 } else {
1852 free(current->uuid); 1550 free(current->uuid);
1853 current->uuid = NULL; 1551 current->uuid = NULL;
1885 free(current->light_address); 1583 free(current->light_address);
1886 current->light_address = NULL; 1584 current->light_address = NULL;
1887 if (current->psu_address) 1585 if (current->psu_address)
1888 free(current->psu_address); 1586 free(current->psu_address);
1889 current->psu_address = NULL; 1587 current->psu_address = NULL;
1890 if (current->profile) 1588 if (current->profile_uuid)
1891 free(current->profile); 1589 free(current->profile_uuid);
1892 current->profile = NULL; 1590 current->profile_uuid = NULL;
1591 if (current->profile_name)
1592 free(current->profile_name);
1593 current->profile_name = NULL;
1893 if (current->PID_cool) 1594 if (current->PID_cool)
1894 free(current->PID_cool); 1595 free(current->PID_cool);
1895 current->PID_cool = NULL; 1596 current->PID_cool = NULL;
1896 if (current->PID_heat) 1597 if (current->PID_heat)
1897 free(current->PID_heat); 1598 free(current->PID_heat);
1898 current->PID_heat = NULL; 1599 current->PID_heat = NULL;
1600 if (current->profile_steps) {
1601 for (step = current->profile_steps; step; step = olds) {
1602 olds = step->next;
1603 free(step);
1604 }
1605 current->profile_steps = NULL;
1606 }
1899 previous->next = current->next; 1607 previous->next = current->next;
1900 free(current); 1608 free(current);
1901 current = previous->next; 1609 current = previous->next;
1902 return 1; 1610 return 1;
1903 } 1611 }
1970 1678
1971 sprintf(an, "unit%d", Config.next_unit); 1679 sprintf(an, "unit%d", Config.next_unit);
1972 Config.next_unit++; 1680 Config.next_unit++;
1973 unit = (units_list *)malloc(sizeof(units_list)); 1681 unit = (units_list *)malloc(sizeof(units_list));
1974 unit->next = NULL; 1682 unit->next = NULL;
1975 unit->version = 1;
1976 unit->uuid = malloc(37); 1683 unit->uuid = malloc(37);
1977 uuid_generate(uu); 1684 uuid_generate(uu);
1978 uuid_unparse(uu, unit->uuid); 1685 uuid_unparse(uu, unit->uuid);
1979 unit->product_uuid = NULL; 1686 unit->product_uuid = NULL;
1980 unit->product_code = xstrcpy((char *)"FAKE0000"); 1687 unit->product_code = xstrcpy((char *)"FAKE0000");
1981 unit->product_name = xstrcpy(param); 1688 unit->product_name = xstrcpy(param);
1982 unit->alias = xstrcpy(an); 1689 unit->alias = xstrcpy(an);
1983 unit->air_address = unit->beer_address = unit->chiller_address = unit->heater_address = unit->cooler_address = \ 1690 unit->air_address = unit->beer_address = unit->chiller_address = unit->heater_address = unit->cooler_address = \
1984 unit->fan_address = unit->door_address = unit->light_address = \ 1691 unit->fan_address = unit->door_address = unit->light_address = \
1985 unit->psu_address = unit->profile = NULL; 1692 unit->psu_address = unit->profile_uuid = unit->profile_name = NULL;
1986 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \ 1693 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \
1987 unit->door_idx = unit->light_idx = unit->psu_idx = 0; 1694 unit->door_idx = unit->light_idx = unit->psu_idx = unit->profile_fridge_mode = \
1695 unit->profile_duration = unit->profile_totalsteps = 0;
1696 unit->profile_steps = NULL;
1988 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0; 1697 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
1989 unit->air_state = unit->beer_state = unit->chiller_state = 1; 1698 unit->air_state = unit->beer_state = unit->chiller_state = 1;
1990 unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000; 1699 unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000;
1991 unit->beer_set = unit->fridge_set = 20.0; 1700 unit->beer_set = unit->fridge_set = unit->profile_inittemp_lo = unit->profile_inittemp_hi =20.0;
1992 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \ 1701 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \
1993 unit->light_state = unit->psu_state = unit->prof_state = unit->stage = 0; 1702 unit->light_state = unit->psu_state = unit->prof_state = unit->stage = 0;
1994 unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20; /* 5 minutes delay */ 1703 unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20; /* 5 minutes delay */
1995 unit->light_delay = 1; /* 15 seconds delay */ 1704 unit->light_delay = 1; /* 15 seconds delay */
1996 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 1705 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2122 srv_send((char *)"PSU_ADDRESS,%s", unit->psu_address); 1831 srv_send((char *)"PSU_ADDRESS,%s", unit->psu_address);
2123 srv_send((char *)"PSU_STATE,%d", unit->psu_state); 1832 srv_send((char *)"PSU_STATE,%d", unit->psu_state);
2124 srv_send((char *)"PSU_IDX,%d", unit->psu_idx); 1833 srv_send((char *)"PSU_IDX,%d", unit->psu_idx);
2125 srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set); 1834 srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set);
2126 srv_send((char *)"BEER_SET,%.1f", unit->beer_set); 1835 srv_send((char *)"BEER_SET,%.1f", unit->beer_set);
2127 srv_send((char *)"PROFILE,%s", unit->profile); 1836 if (unit->profile_uuid) {
2128 srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started); 1837 srv_send((char *)"PROFILE_UUID,%s", unit->profile_uuid);
2129 if (unit->prof_state == PROFILE_RUN) { 1838 srv_send((char *)"PROFILE_NAME,%s", unit->profile_name);
2130 srv_send((char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent); 1839 srv_send((char *)"PROFILE_INITTEMP_LO,%.1f", unit->profile_inittemp_lo);
2131 } else { 1840 srv_send((char *)"PROFILE_INITTEMP_HI,%.1f", unit->profile_inittemp_hi);
2132 srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]); 1841 srv_send((char *)"PROFILE_FRIDGE_MODE,%d", unit->profile_fridge_mode);
1842 srv_send((char *)"PROFILE_DURATION,%d", unit->profile_duration);
1843 srv_send((char *)"PROFILE_TOTALSTEPS,%d", unit->profile_totalsteps);
1844 srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started);
1845 if (unit->prof_state == PROFILE_RUN) {
1846 srv_send((char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent);
1847 } else {
1848 srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]);
1849 }
1850 srv_send((char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo);
1851 srv_send((char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi);
1852 srv_send((char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode);
1853 srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs);
1854 srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel);
1855 srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done);
2133 } 1856 }
2134 srv_send((char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo);
2135 srv_send((char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi);
2136 srv_send((char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode);
2137 srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs);
2138 srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel);
2139 srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done);
2140 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); 1857 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min);
2141 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); 1858 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max);
2142 srv_send((char *)"ALARM,%d", unit->alarm_flag); 1859 srv_send((char *)"ALARM,%d", unit->alarm_flag);
2143 srv_send((char *)"."); 1860 srv_send((char *)".");
2144 return 0; 1861 return 0;
2497 * Set a sane default until it will be overruled by the 2214 * Set a sane default until it will be overruled by the
2498 * main processing loop. 2215 * main processing loop.
2499 */ 2216 */
2500 unit->prof_target_lo = unit->prof_target_hi = 20.0; 2217 unit->prof_target_lo = unit->prof_target_hi = 20.0;
2501 unit->prof_fridge_mode = 0; 2218 unit->prof_fridge_mode = 0;
2502 if (unit->profile) { 2219 if (unit->profile_uuid) {
2503 unit->mqtt_flag |= MQTT_FLAG_DATA; 2220 unit->mqtt_flag |= MQTT_FLAG_DATA;
2504 } 2221 }
2505 } 2222 }
2506 break; 2223 break;
2507 } 2224 }
2591 if (unit->PID_heat->idleRange != fval) 2308 if (unit->PID_heat->idleRange != fval)
2592 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat idleRange %.2f to %.2f", unit->uuid, unit->PID_heat->idleRange, fval); 2309 syslog(LOG_NOTICE, "Fermenter unit %s PID_heat idleRange %.2f to %.2f", unit->uuid, unit->PID_heat->idleRange, fval);
2593 unit->PID_heat->idleRange = fval; 2310 unit->PID_heat->idleRange = fval;
2594 } 2311 }
2595 2312
2596 } else if (strcmp(kwd, (char *)"PROFILE") == 0) { 2313 } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0) && unit->profile_uuid) {
2597 if (unit->prof_state == PROFILE_OFF) {
2598 /*
2599 * Only change profile if it is not active, else drop this one.
2600 */
2601 if (unit->profile && val && strcmp(unit->profile, val))
2602 syslog(LOG_NOTICE, "Fermenter unit %s profile name `%s' to `%s'", unit->uuid, unit->profile, val);
2603 if (unit->profile)
2604 free(unit->profile);
2605
2606 if (val)
2607 unit->profile = xstrcpy(val);
2608 else
2609 unit->profile = NULL;
2610 /*
2611 * Reset all output devices
2612 */
2613 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
2614 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
2615 unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0;
2616 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2617 device_out(unit->heater_address, unit->heater_state);
2618 device_out(unit->cooler_address, unit->cooler_state);
2619 device_out(unit->fan_address, unit->fan_state);
2620 device_out(unit->light_address, unit->light_state);
2621 unit->mqtt_flag |= MQTT_FLAG_DATA;
2622 }
2623
2624 } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) {
2625 for (i = 0; i < 5; i++) { 2314 for (i = 0; i < 5; i++) {
2626 if (strcmp(val, PROFSTATE[i]) == 0) { 2315 if (strcmp(val, PROFSTATE[i]) == 0) {
2627 switch (i) { 2316 switch (i) {
2628 case PROFILE_OFF: if (unit->prof_state == PROFILE_DONE) { 2317 case PROFILE_OFF: if (unit->prof_state == PROFILE_DONE) {
2629 unit->prof_state = PROFILE_OFF; 2318 unit->prof_state = PROFILE_OFF;
2647 } 2336 }
2648 break; 2337 break;
2649 case PROFILE_DONE: break; /* Command is illegal */ 2338 case PROFILE_DONE: break; /* Command is illegal */
2650 case PROFILE_ABORT: if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) { 2339 case PROFILE_ABORT: if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) {
2651 unit->prof_state = PROFILE_OFF; 2340 unit->prof_state = PROFILE_OFF;
2652 unit->prof_started = 0; 2341 unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0;
2342 unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
2653 syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid); 2343 syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid);
2654 } 2344 }
2655 break; 2345 break;
2656 } 2346 }
2657 unit->mqtt_flag |= MQTT_FLAG_DATA; 2347 unit->mqtt_flag |= MQTT_FLAG_DATA;
2737 srv_send((char *)"GLOBAL <CMD> [parameters] Global commands"); 2427 srv_send((char *)"GLOBAL <CMD> [parameters] Global commands");
2738 srv_send((char *)"GLOBAL HELP Global help screen"); 2428 srv_send((char *)"GLOBAL HELP Global help screen");
2739 srv_send((char *)"LIST <CMD> [parameters] List commands"); 2429 srv_send((char *)"LIST <CMD> [parameters] List commands");
2740 srv_send((char *)"LIST HELP List help screen"); 2430 srv_send((char *)"LIST HELP List help screen");
2741 srv_send((char *)"PING Check if server is alive"); 2431 srv_send((char *)"PING Check if server is alive");
2742 srv_send((char *)"PROFILE <CMD> [parameters] Profile commands");
2743 srv_send((char *)"PROFILE HELP Profile help screen");
2744 #ifdef USE_SIMULATOR 2432 #ifdef USE_SIMULATOR
2745 srv_send((char *)"SIMULATOR <CMD> [parameters] Simulator commands"); 2433 srv_send((char *)"SIMULATOR <CMD> [parameters] Simulator commands");
2746 srv_send((char *)"SIMULATOR HELP Simulator help screen"); 2434 srv_send((char *)"SIMULATOR HELP Simulator help screen");
2747 #endif 2435 #endif
2748 srv_send((char *)"UNIT <CMD> [parameters] Unit commands"); 2436 srv_send((char *)"UNIT <CMD> [parameters] Unit commands");
2752 } else if (strncmp(buf, "LIST", 4) == 0) { 2440 } else if (strncmp(buf, "LIST", 4) == 0) {
2753 cmd_list(buf); 2441 cmd_list(buf);
2754 2442
2755 } else if (strncmp(buf, "PING", 4) == 0) { 2443 } else if (strncmp(buf, "PING", 4) == 0) {
2756 srv_send((char *)"101 PONG"); 2444 srv_send((char *)"101 PONG");
2757
2758 } else if (strncmp(buf, "PROFILE", 7) == 0) {
2759 if (cmd_profile(buf))
2760 wrconfig();
2761 2445
2762 #ifdef USE_SIMULATOR 2446 #ifdef USE_SIMULATOR
2763 } else if (strncmp(buf, "SIMULATOR", 9) == 0) { 2447 } else if (strncmp(buf, "SIMULATOR", 9) == 0) {
2764 if (cmd_simulator(buf)) 2448 if (cmd_simulator(buf))
2765 wrconfig(); 2449 wrconfig();

mercurial