thermferm/rdconfig.c

changeset 500
5aa914eb644e
parent 497
18ace27338e5
child 506
cdcd07bbee30
equal deleted inserted replaced
499:602d9968960f 500:5aa914eb644e
63 Config.temp_address = Config.hum_address = NULL; 63 Config.temp_address = Config.hum_address = NULL;
64 Config.temp_value = 20000; 64 Config.temp_value = 20000;
65 Config.temp_state = Config.hum_state = 1; // missing 65 Config.temp_state = Config.hum_state = 1; // missing
66 Config.hum_value = 50000; 66 Config.hum_value = 50000;
67 #ifdef HAVE_MOSQUITTO_H 67 #ifdef HAVE_MOSQUITTO_H
68 if (Config.mosq_host) 68 if (Config.mqtt_host)
69 free(Config.mosq_host); 69 free(Config.mqtt_host);
70 Config.mosq_host = NULL; 70 Config.mqtt_host = NULL;
71 Config.mosq_port = 1883; 71 Config.mqtt_port = 1883;
72 #endif 72 #endif
73 73
74 for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) { 74 for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) {
75 if (tmp2->uuid) 75 if (tmp2->uuid)
76 free(tmp2->uuid); 76 free(tmp2->uuid);
242 } 242 }
243 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HUM_VALUE", "%d", Config.hum_value)) < 0) { 243 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HUM_VALUE", "%d", Config.hum_value)) < 0) {
244 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 244 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
245 return 1; 245 return 1;
246 } 246 }
247 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NEXT_UNIT", "%d", Config.next_unit)) < 0) {
248 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
249 return 1;
250 }
251 #ifdef HAVE_MOSQUITTO_H
252 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_HOST", "%s", Config.mqtt_host)) < 0) {
253 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
254 return 1;
255 }
256 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_PORT", "%d", Config.mqtt_port)) < 0) {
257 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
258 return 1;
259 }
260 #endif
247 261
248 /* 262 /*
249 * Start an element named "LCDS" as child of THERMFERM. 263 * Start an element named "LCDS" as child of THERMFERM.
250 */ 264 */
251 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) { 265 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) {
315 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", tmp3->uuid)) < 0) { 329 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", tmp3->uuid)) < 0) {
316 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 330 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
317 return 1; 331 return 1;
318 } 332 }
319 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp3->name)) < 0) { 333 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp3->name)) < 0) {
334 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
335 return 1;
336 }
337 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ALIAS", "%s", tmp3->alias)) < 0) {
320 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 338 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
321 return 1; 339 return 1;
322 } 340 }
323 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME", "%.1f", tmp3->volume)) < 0) { 341 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME", "%.1f", tmp3->volume)) < 0) {
324 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 342 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
1002 1020
1003 1021
1004 /* 1022 /*
1005 * Parse a fermenter unit 1023 * Parse a fermenter unit
1006 */ 1024 */
1007 int parseUnit(xmlDocPtr doc, xmlNodePtr cur) 1025 int parseUnit(xmlDocPtr doc, xmlNodePtr cur, int number)
1008 { 1026 {
1009 xmlChar *key; 1027 xmlChar *key;
1010 int i, ival; 1028 int i, ival;
1011 float val; 1029 float val;
1012 units_list *unit, *tmp; 1030 units_list *unit, *tmp;
1013 1031
1014 unit = (units_list *)malloc(sizeof(units_list)); 1032 unit = (units_list *)malloc(sizeof(units_list));
1015 unit->next = NULL; 1033 unit->next = NULL;
1016 unit->version = 1; 1034 unit->version = 1;
1017 unit->uuid = unit->name = unit->air_address = unit->beer_address = unit->heater_address = \ 1035 unit->uuid = unit->name = unit->alias = unit->air_address = unit->beer_address = unit->heater_address = \
1018 unit->cooler_address = unit->fan_address = unit->door_address = \ 1036 unit->cooler_address = unit->fan_address = unit->door_address = \
1019 unit->light_address = unit->psu_address = unit->profile = NULL; 1037 unit->light_address = unit->psu_address = unit->profile = NULL;
1020 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0; 1038 unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
1021 unit->air_temperature = unit->beer_temperature = unit->beer_set = unit->fridge_set = 20.0; 1039 unit->air_temperature = unit->beer_temperature = unit->beer_set = unit->fridge_set = 20.0;
1022 unit->air_state = unit->beer_state = 1; // missing 1040 unit->air_state = unit->beer_state = 1; // missing
1050 unit->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1068 unit->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1051 } 1069 }
1052 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { 1070 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
1053 unit->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1071 unit->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1054 } 1072 }
1073 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ALIAS"))) {
1074 unit->alias = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1075 }
1055 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME"))) { 1076 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME"))) {
1056 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1077 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1057 if (sscanf((const char *)key, "%f", &val) == 1) 1078 if (sscanf((const char *)key, "%f", &val) == 1)
1058 unit->volume = val; 1079 unit->volume = val;
1059 xmlFree(key); 1080 xmlFree(key);
1388 if (sscanf((const char *)key, "%d", &ival) == 1) 1409 if (sscanf((const char *)key, "%d", &ival) == 1)
1389 unit->prof_primary_done = ival; 1410 unit->prof_primary_done = ival;
1390 xmlFree(key); 1411 xmlFree(key);
1391 } 1412 }
1392 cur = cur->next; 1413 cur = cur->next;
1414 }
1415
1416 /*
1417 * If there is no alias name, create it.
1418 */
1419 if (unit->alias == NULL) {
1420 char an[128];
1421 sprintf(an, "unit%d", number);
1422 unit->alias = xstrcpy(an);
1393 } 1423 }
1394 1424
1395 if (Config.units == NULL) { 1425 if (Config.units == NULL) {
1396 Config.units = unit; 1426 Config.units = unit;
1397 } else { 1427 } else {
1411 int parseFermenters(xmlDocPtr doc, xmlNodePtr cur) 1441 int parseFermenters(xmlDocPtr doc, xmlNodePtr cur)
1412 { 1442 {
1413 cur = cur->xmlChildrenNode; 1443 cur = cur->xmlChildrenNode;
1414 while (cur != NULL) { 1444 while (cur != NULL) {
1415 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) { 1445 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) {
1416 parseUnit(doc, cur); 1446 parseUnit(doc, cur, Config.next_unit);
1447 Config.next_unit++;
1417 } 1448 }
1418 cur = cur->next; 1449 cur = cur->next;
1419 } 1450 }
1420 return 0; 1451 return 0;
1421 } 1452 }
1981 } 2012 }
1982 } 2013 }
1983 syslog(LOG_NOTICE, "rdconfig: using %s", mypath); 2014 syslog(LOG_NOTICE, "rdconfig: using %s", mypath);
1984 2015
1985 #ifdef HAVE_MOSQUITTO_H 2016 #ifdef HAVE_MOSQUITTO_H
1986 Config.mosq_host = xstrcpy((char *)"localhost"); 2017 Config.mqtt_host = xstrcpy((char *)"localhost");
1987 Config.mosq_port = 1883; 2018 Config.mqtt_port = 1883;
1988 #endif 2019 #endif
2020 Config.next_unit = 1;
1989 2021
1990 if ((cur = xmlDocGetRootElement(doc)) == NULL) { 2022 if ((cur = xmlDocGetRootElement(doc)) == NULL) {
1991 syslog(LOG_NOTICE, "XML file %s empty.", mypath); 2023 syslog(LOG_NOTICE, "XML file %s empty.", mypath);
1992 xmlFreeDoc(doc); 2024 xmlFreeDoc(doc);
1993 return 1; 2025 return 1;
2030 Config.temp_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 2062 Config.temp_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
2031 } 2063 }
2032 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HUM_ADDRESS"))) { 2064 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HUM_ADDRESS"))) {
2033 Config.hum_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 2065 Config.hum_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
2034 } 2066 }
2067 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NEXT_UNIT"))) {
2068 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
2069 if (sscanf((const char *)key, "%d", &ival) == 1)
2070 Config.next_unit = ival;
2071 xmlFree(key);
2072 }
2073 #ifdef HAVE_MOSQUITTO_H
2074 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_HOST"))) {
2075 Config.mqtt_host = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
2076 }
2077 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_PORT"))) {
2078 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
2079 if (sscanf((const char *)key, "%d", &ival) == 1)
2080 Config.mqtt_port = ival;
2081 xmlFree(key);
2082 }
2083 #endif
2035 if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) { 2084 if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) {
2036 parseLCDs(doc, cur); 2085 parseLCDs(doc, cur);
2037 } 2086 }
2038 if ((!xmlStrcmp(cur->name, (const xmlChar *)"FERMENTERS"))) { 2087 if ((!xmlStrcmp(cur->name, (const xmlChar *)"FERMENTERS"))) {
2039 parseFermenters(doc, cur); 2088 parseFermenters(doc, cur);

mercurial