thermferm/server.c

changeset 693
3518c07737d8
parent 689
f94b525f7563
child 696
fe042f9484ac
equal deleted inserted replaced
692:6d97eb820cc1 693:3518c07737d8
34 extern int run_pause; 34 extern int run_pause;
35 extern int run_hold; 35 extern int run_hold;
36 extern sys_config Config; 36 extern sys_config Config;
37 extern const char UNITMODE[5][8]; 37 extern const char UNITMODE[5][8];
38 extern const char UNITSTAGE[4][12]; 38 extern const char UNITSTAGE[4][12];
39 extern const char TEMPSTATE[3][8];
40 extern const char DEVTYPE[8][6]; 39 extern const char DEVTYPE[8][6];
41 extern const char DEVPRESENT[4][6]; 40 extern const char DEVPRESENT[4][6];
42 extern const char DEVDIR[7][11]; 41 extern const char DEVDIR[7][11];
43 extern const char PROFSTATE[5][6]; 42 extern const char PROFSTATE[5][6];
44 extern pthread_mutex_t mutexes[5]; 43 extern pthread_mutex_t mutexes[5];
650 649
651 if (strcmp(opt, (char *)"GET") == 0) { 650 if (strcmp(opt, (char *)"GET") == 0) {
652 srv_send(s, (char *)"213 Global Settings record follows:"); 651 srv_send(s, (char *)"213 Global Settings record follows:");
653 srv_send(s, (char *)"RELEASE,%s", VERSION); 652 srv_send(s, (char *)"RELEASE,%s", VERSION);
654 srv_send(s, (char *)"NAME,%s", Config.name); 653 srv_send(s, (char *)"NAME,%s", Config.name);
655 srv_send(s, (char *)"PORT,%d", Config.my_port); 654 srv_send(s, (char *)"PORT,%d", Config.server_port);
656 srv_send(s, (char *)"TEMPFORMAT,%c", Config.tempFormat); 655 srv_send(s, (char *)"TEMP_UUID,%s", Config.temp_uuid);
657 srv_send(s, (char *)"TEMP_ADDRESS,%s", Config.temp_address); 656 srv_send(s, (char *)"TEMP_STATE,%s", DEVPRESENT[Config.temp_state]);
658 srv_send(s, (char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]);
659 srv_send(s, (char *)"TEMP_VALUE,%.1f", Config.temp_value / 1000.0); 657 srv_send(s, (char *)"TEMP_VALUE,%.1f", Config.temp_value / 1000.0);
660 srv_send(s, (char *)"HUM_ADDRESS,%s", Config.hum_address); 658 srv_send(s, (char *)"HUM_UUID,%s", Config.hum_uuid);
661 srv_send(s, (char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]); 659 srv_send(s, (char *)"HUM_STATE,%s", DEVPRESENT[Config.hum_state]);
662 srv_send(s, (char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0); 660 srv_send(s, (char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0);
663 srv_send(s, (char *)"TEMP_HUM_IDX,%d", Config.temp_hum_idx); 661 srv_send(s, (char *)"TEMP_HUM_IDX,%d", Config.temp_hum_idx);
664 srv_send(s, (char *)"LCD_COLS,%d", Config.lcd_cols); 662 srv_send(s, (char *)"LCD_COLS,%d", Config.lcd_cols);
665 srv_send(s, (char *)"LCD_ROWS,%d", Config.lcd_rows); 663 srv_send(s, (char *)"LCD_ROWS,%d", Config.lcd_rows);
666 srv_send(s, (char *)"NEXT_UNIT,%d", Config.next_unit); 664 srv_send(s, (char *)"NEXT_UNIT,%d", Config.next_unit);
712 else 710 else
713 Config.name = NULL; 711 Config.name = NULL;
714 712
715 } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) { 713 } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) {
716 if (sscanf(val, "%d", &ival) == 1) { 714 if (sscanf(val, "%d", &ival) == 1) {
717 if (Config.my_port != ival) 715 if (Config.server_port != ival)
718 syslog(LOG_NOTICE, "Global port %d to %d", Config.my_port, ival); 716 syslog(LOG_NOTICE, "Global server port %d to %d", Config.server_port, ival);
719 Config.my_port = ival; 717 Config.server_port = ival;
720 } 718 }
721 719
722 } else if (val && (strcmp(kwd, (char *)"TEMPFORMAT") == 0)) { 720 } else if (strcmp(kwd, (char *)"TEMP_UUID") == 0) {
723 if ((val[0] == 'C') || (val[0] == 'F')) { 721 if (val && Config.temp_uuid && (strcmp(val, Config.temp_uuid)))
724 if (Config.tempFormat != val[0]) 722 syslog(LOG_NOTICE, "Global temperature uuid `%s' to `%s'", Config.temp_uuid, val);
725 syslog(LOG_NOTICE, "Global port %c to %c", Config.tempFormat, val[0]); 723 if (Config.temp_uuid) {
726 Config.tempFormat = val[0]; 724 device_count(FALSE, Config.temp_uuid);
727 } 725 free(Config.temp_uuid);
728
729 } else if (strcmp(kwd, (char *)"TEMP_ADDRESS") == 0) {
730 if (val && Config.temp_address && (strcmp(val, Config.temp_address)))
731 syslog(LOG_NOTICE, "Global temperature address `%s' to `%s'", Config.temp_address, val);
732 if (Config.temp_address) {
733 device_count(FALSE, Config.temp_address);
734 free(Config.temp_address);
735 } 726 }
736 if (val) { 727 if (val) {
737 Config.temp_address = xstrcpy(val); 728 Config.temp_uuid = xstrcpy(val);
738 device_count(TRUE, Config.temp_address); 729 device_count(TRUE, Config.temp_uuid);
739 } else 730 } else
740 Config.temp_address = NULL; 731 Config.temp_uuid = NULL;
741 732
742 } else if (strcmp(kwd, (char *)"HUM_ADDRESS") == 0) { 733 } else if (strcmp(kwd, (char *)"HUM_UUID") == 0) {
743 if (val && Config.hum_address && (strcmp(val, Config.hum_address))) 734 if (val && Config.hum_uuid && (strcmp(val, Config.hum_uuid)))
744 syslog(LOG_NOTICE, "Global humidity address `%s' to `%s'", Config.hum_address, val); 735 syslog(LOG_NOTICE, "Global humidity uuid `%s' to `%s'", Config.hum_uuid, val);
745 if (Config.hum_address) { 736 if (Config.hum_uuid) {
746 device_count(FALSE, Config.hum_address); 737 device_count(FALSE, Config.hum_uuid);
747 free(Config.hum_address); 738 free(Config.hum_uuid);
748 } 739 }
749 if (val) { 740 if (val) {
750 Config.hum_address = xstrcpy(val); 741 Config.hum_uuid = xstrcpy(val);
751 device_count(TRUE, Config.hum_address); 742 device_count(TRUE, Config.hum_uuid);
752 } else 743 } else
753 Config.hum_address = NULL; 744 Config.hum_uuid = NULL;
754 745
755 } else if (val && (strcmp(kwd, (char *)"TEMP_HUM_IDX") == 0)) { 746 } else if (val && (strcmp(kwd, (char *)"TEMP_HUM_IDX") == 0)) {
756 if (sscanf(val, "%d", &ival) == 1) { 747 if (sscanf(val, "%d", &ival) == 1) {
757 if (Config.temp_hum_idx != ival) 748 if (Config.temp_hum_idx != ival)
758 syslog(LOG_NOTICE, "Global Temp/Humidity idx %d to %d", Config.temp_hum_idx, ival); 749 syslog(LOG_NOTICE, "Global Temp/Humidity idx %d to %d", Config.temp_hum_idx, ival);
1400 } 1391 }
1401 srv_send(s, (char *)"."); 1392 srv_send(s, (char *)".");
1402 return 0; 1393 return 0;
1403 } 1394 }
1404 1395
1396 if (strcmp(opt, (char *)"JSON") == 0) {
1397 char *payload = NULL, *payloadu = NULL;
1398 bool comma = false;
1399
1400 if (param == NULL) {
1401 srv_send(s, (char *)"212 Units json list follows:");
1402 payload = xstrcpy((char *)"[");
1403 for (unit = Config.units; unit; unit = unit->next) {
1404 if (comma)
1405 payload = xstrcat(payload, (char *)",");
1406 payloadu = unit_data(unit, true);
1407 payload = xstrcat(payload, payloadu);
1408 comma = true;
1409 free(payloadu);
1410 payloadu = NULL;
1411 }
1412 payload = xstrcat(payload, (char *)"]");
1413 srv_send(s, payload);
1414 srv_send(s, (char *)".");
1415 free(payload);
1416 payload = NULL;
1417 return 0;
1418 } else {
1419 syslog(LOG_NOTICE, "UNIT JSON %s", param);
1420 for (unit = Config.units; unit; unit = unit->next) {
1421 if (strcmp(param, unit->uuid) == 0) {
1422 srv_send(s, (char *)"213 Unit json data follows:");
1423 payload = xstrcpy((char *)"{\"type\":\"fermenter\",\"unit\":\"");
1424 payload = xstrcat(payload, unit->alias);
1425 payload = xstrcat(payload, (char *)"\",\"metric\":");
1426 payloadu = unit_data(unit, false);
1427 payload = xstrcat(payload, payloadu);
1428 payload = xstrcat(payload, (char *)"}");
1429 srv_send(s, payload);
1430 free(payload);
1431 free(payloadu);
1432 payload = payloadu = NULL;
1433 srv_send(s, (char *)".");
1434 return 0;
1435 }
1436 }
1437 srv_send(s, (char *)"440 No such unit");
1438 return 0;
1439 }
1440 }
1441
1405 if (param == NULL) { 1442 if (param == NULL) {
1406 srv_send(s, (char *)"502 Parameter missing"); 1443 srv_send(s, (char *)"502 Parameter missing");
1407 return 0; 1444 return 0;
1408 } 1445 }
1409 1446
1427 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \ 1464 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \
1428 unit->door_idx = unit->light_idx = unit->psu_idx = unit->profile_fridge_mode = \ 1465 unit->door_idx = unit->light_idx = unit->psu_idx = unit->profile_fridge_mode = \
1429 unit->profile_duration = unit->profile_totalsteps = 0; 1466 unit->profile_duration = unit->profile_totalsteps = 0;
1430 unit->profile_steps = NULL; 1467 unit->profile_steps = NULL;
1431 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0; 1468 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
1432 unit->air_state = unit->beer_state = unit->chiller_state = 1; 1469 unit->air_state = unit->beer_state = unit->chiller_state = DEVPRESENT_NO;
1433 unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000; 1470 unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000;
1434 unit->beer_set_lo = unit->beer_set_hi = unit->fridge_set_lo = unit->fridge_set_hi = unit->profile_inittemp_lo = unit->profile_inittemp_hi =20.0; 1471 unit->beer_set_lo = unit->beer_set_hi = unit->fridge_set_lo = unit->fridge_set_hi = unit->profile_inittemp_lo = unit->profile_inittemp_hi =20.0;
1435 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \ 1472 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \
1436 unit->light_state = unit->light_timer = unit->psu_state = unit->prof_state = unit->stage = 0; 1473 unit->light_state = unit->light_timer = unit->psu_state = unit->prof_state = unit->stage = 0;
1437 unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20; /* 5 minutes delay */ 1474 unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20; /* 5 minutes delay */
1510 srv_send(s, (char *)"PRODUCT_CODE,%s", unit->product_code); 1547 srv_send(s, (char *)"PRODUCT_CODE,%s", unit->product_code);
1511 srv_send(s, (char *)"MODE,%s", UNITMODE[unit->mode]); 1548 srv_send(s, (char *)"MODE,%s", UNITMODE[unit->mode]);
1512 srv_send(s, (char *)"STAGE,%s", UNITSTAGE[unit->stage]); 1549 srv_send(s, (char *)"STAGE,%s", UNITSTAGE[unit->stage]);
1513 srv_send(s, (char *)"VOLUME,%2f", unit->volume); 1550 srv_send(s, (char *)"VOLUME,%2f", unit->volume);
1514 srv_send(s, (char *)"AIR_ADDRESS,%s", unit->air_address); 1551 srv_send(s, (char *)"AIR_ADDRESS,%s", unit->air_address);
1515 srv_send(s, (char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]); 1552 srv_send(s, (char *)"AIR_STATE,%s", DEVPRESENT[unit->air_state]);
1516 srv_send(s, (char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0); 1553 srv_send(s, (char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
1517 srv_send(s, (char *)"AIR_IDX,%d", unit->air_idx); 1554 srv_send(s, (char *)"AIR_IDX,%d", unit->air_idx);
1518 srv_send(s, (char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address)); 1555 srv_send(s, (char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address));
1519 srv_send(s, (char *)"BEER_ADDRESS2,%s", MBSE_SS(unit->beer_address2)); 1556 srv_send(s, (char *)"BEER_ADDRESS2,%s", MBSE_SS(unit->beer_address2));
1520 srv_send(s, (char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]); 1557 srv_send(s, (char *)"BEER_STATE,%s", DEVPRESENT[unit->beer_state]);
1521 srv_send(s, (char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0); 1558 srv_send(s, (char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
1522 srv_send(s, (char *)"BEER_IDX,%d", unit->beer_idx); 1559 srv_send(s, (char *)"BEER_IDX,%d", unit->beer_idx);
1523 srv_send(s, (char *)"CHILLER_ADDRESS,%s", MBSE_SS(unit->chiller_address)); 1560 srv_send(s, (char *)"CHILLER_ADDRESS,%s", MBSE_SS(unit->chiller_address));
1524 srv_send(s, (char *)"CHILLER_STATE,%s", TEMPSTATE[unit->chiller_state]); 1561 srv_send(s, (char *)"CHILLER_STATE,%s", DEVPRESENT[unit->chiller_state]);
1525 srv_send(s, (char *)"CHILLER_TEMPERATURE,%.3f", unit->chiller_temperature / 1000.0); 1562 srv_send(s, (char *)"CHILLER_TEMPERATURE,%.3f", unit->chiller_temperature / 1000.0);
1526 srv_send(s, (char *)"CHILLER_IDX,%d", unit->chiller_idx); 1563 srv_send(s, (char *)"CHILLER_IDX,%d", unit->chiller_idx);
1527 srv_send(s, (char *)"HEATER_ADDRESS,%s", unit->heater_address); 1564 srv_send(s, (char *)"HEATER_ADDRESS,%s", unit->heater_address);
1528 srv_send(s, (char *)"HEATER_STATE,%d", unit->heater_state); 1565 srv_send(s, (char *)"HEATER_STATE,%d", unit->heater_state);
1529 srv_send(s, (char *)"HEATER_DELAY,%d", unit->heater_delay); 1566 srv_send(s, (char *)"HEATER_DELAY,%d", unit->heater_delay);
1600 } 1637 }
1601 srv_send(s, (char *)"440 No such unit"); 1638 srv_send(s, (char *)"440 No such unit");
1602 return 0; 1639 return 0;
1603 } 1640 }
1604 1641
1605 if (strcmp(opt, (char *)"JSON") == 0) {
1606 syslog(LOG_NOTICE, "UNIT JSON %s", param);
1607 for (unit = Config.units; unit; unit = unit->next) {
1608 if (strcmp(param, unit->uuid) == 0) {
1609 char *payload, *payloadu;
1610
1611 srv_send(s, (char *)"213 Unit json data follows:");
1612
1613 payload = xstrcpy((char *)"{\"type\":\"fermenter\",\"unit\":\"");
1614 payload = xstrcat(payload, unit->alias);
1615 payload = xstrcat(payload, (char *)"\",\"metric\":");
1616 payloadu = unit_data(unit, false);
1617 payload = xstrcat(payload, payloadu);
1618 payload = xstrcat(payload, (char *)"}");
1619 srv_send(s, payload);
1620 free(payload);
1621 free(payloadu);
1622 payload = payloadu = NULL;
1623 srv_send(s, (char *)".");
1624 return 0;
1625 }
1626 }
1627 srv_send(s, (char *)"440 No such unit");
1628 return 0;
1629 }
1630
1631 if (strcmp(opt, (char *)"PUT") == 0) { 1642 if (strcmp(opt, (char *)"PUT") == 0) {
1632 /* 1643 /*
1633 * Block main process 1644 * Block main process
1634 */ 1645 */
1635 run_pause = TRUE; 1646 run_pause = TRUE;
2270 2281
2271 memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); 2282 memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
2272 memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); 2283 memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
2273 myaddr_in.sin_family = AF_INET; 2284 myaddr_in.sin_family = AF_INET;
2274 myaddr_in.sin_addr.s_addr = INADDR_ANY; 2285 myaddr_in.sin_addr.s_addr = INADDR_ANY;
2275 myaddr_in.sin_port = htons(Config.my_port); 2286 myaddr_in.sin_port = htons(Config.server_port);
2276 2287
2277 ls = socket(AF_INET, SOCK_STREAM, 0); 2288 ls = socket(AF_INET, SOCK_STREAM, 0);
2278 if (ls == -1) { 2289 if (ls == -1) {
2279 syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); 2290 syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno));
2280 fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); 2291 fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno));

mercurial