thermferm/rdconfig.c

changeset 158
f1b7e2ef90be
parent 136
264e5ee5abfc
child 161
493e39bb0a08
equal deleted inserted replaced
157:259a018758f9 158:f1b7e2ef90be
33 33
34 const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" }; 34 const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" };
35 const char UNITMODE[5][8] = { "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" }; 35 const char UNITMODE[5][8] = { "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" };
36 const char UNITmode[5] = { 'o', 'n', 'f', 'b', 'p' }; 36 const char UNITmode[5] = { 'o', 'n', 'f', 'b', 'p' };
37 const char PROFSTATE[4][6] = { "OFF", "PAUSE", "RUN", "DONE" }; 37 const char PROFSTATE[4][6] = { "OFF", "PAUSE", "RUN", "DONE" };
38 38 const char DEVTYPE[7][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI" };
39 const char DEVPRESENT[4][6] = { "UNDEF", "NO", "YES", "ERROR" };
40 const char DEVDIR[6][11] = { "UNDEF", "IN_BIN", "OUT_BIN", "IN_ANALOG", "OUT_ANALOG", "OUT_PWM" };
39 41
40 42
41 43
42 void killconfig(void) 44 void killconfig(void)
43 { 45 {
117 xmlBufferPtr buf; 119 xmlBufferPtr buf;
118 w1_therm *tmp1; 120 w1_therm *tmp1;
119 units_list *tmp3; 121 units_list *tmp3;
120 profiles_list *tmp4; 122 profiles_list *tmp4;
121 prof_step *tmp5; 123 prof_step *tmp5;
124 devices_list *device;
122 125
123 /* 126 /*
124 * Create a new XML buffer, to which the XML document will be written 127 * Create a new XML buffer, to which the XML document will be written
125 */ 128 */
126 if ((buf = xmlBufferCreate()) == NULL) { 129 if ((buf = xmlBufferCreate()) == NULL) {
493 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); 496 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
494 return 1; 497 return 1;
495 } 498 }
496 } 499 }
497 500
501 if (Config.devices) {
502 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "DEVICES")) < 0) {
503 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
504 return 1;
505 }
506 for (device = Config.devices; device; device = device->next) {
507 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "DEVICE")) < 0) {
508 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
509 return 1;
510 }
511 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VERSION", "%d", device->version)) < 0) {
512 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
513 return 1;
514 }
515 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", device->uuid)) < 0) {
516 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
517 return 1;
518 }
519 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TYPE", "%s", DEVTYPE[device->type])) < 0) {
520 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
521 return 1;
522 }
523 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DIRECTION", "%s", DEVDIR[device->direction])) < 0) {
524 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
525 return 1;
526 }
527 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VALUE", "%d", device->value)) < 0) {
528 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
529 return 1;
530 }
531 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PRESENT", "%s", DEVPRESENT[device->present])) < 0) {
532 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
533 return 1;
534 }
535 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ADDRESS", "%s", device->address)) < 0) {
536 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
537 return 1;
538 }
539 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SUBDEVICE", "%d", device->subdevice)) < 0) {
540 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
541 return 1;
542 }
543 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "GPIOPIN", "%d", device->gpiopin)) < 0) {
544 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
545 return 1;
546 }
547 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DESCRIPTION", "%s", device->description)) < 0) {
548 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
549 return 1;
550 }
551 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "INUSE", "%d", device->inuse)) < 0) {
552 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
553 return 1;
554 }
555 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COMMENT", "%s", device->comment)) < 0) {
556 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
557 return 1;
558 }
559 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMESTAMP", "%d", (int)device->timestamp)) < 0) {
560 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
561 return 1;
562 }
563 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
564 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
565 return 1;
566 }
567 }
568
569 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
570 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
571 return 1;
572 }
573 }
574
498 /* 575 /*
499 * All done, close any open elements 576 * All done, close any open elements
500 */ 577 */
501 if ((rc = xmlTextWriterEndDocument(writer)) < 0) { 578 if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
502 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument"); 579 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument");
990 { 1067 {
991 cur = cur->xmlChildrenNode; 1068 cur = cur->xmlChildrenNode;
992 while (cur != NULL) { 1069 while (cur != NULL) {
993 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILE"))) { 1070 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILE"))) {
994 parseProfile(doc, cur); 1071 parseProfile(doc, cur);
1072 }
1073 cur = cur->next;
1074 }
1075 return 0;
1076 }
1077
1078
1079
1080 int parseDevice(xmlDocPtr doc, xmlNodePtr cur)
1081 {
1082 xmlChar *key;
1083 devices_list *device, *tmp;
1084 int i, ival;
1085
1086 device = (devices_list *)malloc(sizeof(devices_list));
1087 device->next = NULL;
1088 device->version = 1;
1089 device->uuid = device->address = device->description = device->comment = NULL;
1090 device->type = device->direction = device->present = device->subdevice = device->inuse = 0;
1091 device->gpiopin = -1;
1092 device->timestamp = (time_t)0;
1093
1094 cur = cur->xmlChildrenNode;
1095 while (cur != NULL) {
1096 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) {
1097 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1098 if (xmlStrcmp(key, (const xmlChar *)"1")) {
1099 xmlFree(key);
1100 return 1;
1101 }
1102 device->version = 1;
1103 xmlFree(key);
1104 }
1105 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
1106 device->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1107 }
1108 if ((!xmlStrcmp(cur->name, (const xmlChar *)"TYPE"))) {
1109 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1110 for (i = 0; i < 7; i++) {
1111 if (! xmlStrcmp(key, (const xmlChar *)DEVTYPE[i])) {
1112 device->type = i;
1113 break;
1114 }
1115 }
1116 xmlFree(key);
1117 }
1118 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DIRECTION"))) {
1119 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1120 for (i = 0; i < 6; i++) {
1121 if (! xmlStrcmp(key, (const xmlChar *)DEVDIR[i])) {
1122 device->direction = i;
1123 break;
1124 }
1125 }
1126 xmlFree(key);
1127 }
1128 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VALUE"))) {
1129 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1130 if (sscanf((const char *)key, "%d", &ival) == 1)
1131 device->value = ival;
1132 xmlFree(key);
1133 }
1134 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PRESENT"))) {
1135 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1136 for (i = 0; i < 4; i++) {
1137 if (! xmlStrcmp(key, (const xmlChar *)DEVDIR[i])) {
1138 device->present = i;
1139 break;
1140 }
1141 }
1142 xmlFree(key);
1143 }
1144 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ADDRESS"))) {
1145 device->address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1146 }
1147 if ((!xmlStrcmp(cur->name, (const xmlChar *)"SUBDEVICE"))) {
1148 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1149 if (sscanf((const char *)key, "%d", &ival) == 1)
1150 device->subdevice = ival;
1151 xmlFree(key);
1152 }
1153 if ((!xmlStrcmp(cur->name, (const xmlChar *)"GPIOPIN"))) {
1154 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1155 if (sscanf((const char *)key, "%d", &ival) == 1)
1156 device->gpiopin = ival;
1157 xmlFree(key);
1158 }
1159 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DESCRPTION"))) {
1160 device->description = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1161 }
1162 if ((!xmlStrcmp(cur->name, (const xmlChar *)"INUSE"))) {
1163 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1164 if (sscanf((const char *)key, "%d", &ival) == 1)
1165 device->inuse = ival;
1166 xmlFree(key);
1167 }
1168 if ((!xmlStrcmp(cur->name, (const xmlChar *)"COMMENT"))) {
1169 device->comment = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1170 }
1171 if ((!xmlStrcmp(cur->name, (const xmlChar *)"TIMESTAMP"))) {
1172 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1173 if (sscanf((const char *)key, "%d", &ival) == 1)
1174 device->timestamp = (time_t)ival;
1175 xmlFree(key);
1176 }
1177
1178 cur = cur->next;
1179 }
1180
1181 if (Config.devices == NULL) {
1182 Config.devices = device;
1183 } else {
1184 for (tmp = Config.devices; tmp; tmp = tmp->next) {
1185 if (tmp->next == NULL) {
1186 tmp->next = device;
1187 break;
1188 }
1189 }
1190 }
1191
1192 return 0;
1193 }
1194
1195
1196
1197 int parseDevices(xmlDocPtr doc, xmlNodePtr cur)
1198 {
1199 cur = cur->xmlChildrenNode;
1200 while (cur != NULL) {
1201 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICE"))) {
1202 parseDevice(doc, cur);
995 } 1203 }
996 cur = cur->next; 1204 cur = cur->next;
997 } 1205 }
998 return 0; 1206 return 0;
999 } 1207 }
1092 parseFermenters(doc, cur); 1300 parseFermenters(doc, cur);
1093 } 1301 }
1094 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILES"))) { 1302 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILES"))) {
1095 parseProfiles(doc, cur); 1303 parseProfiles(doc, cur);
1096 } 1304 }
1305 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICES"))) {
1306 parseDevices(doc, cur);
1307 }
1097 cur = cur->next; 1308 cur = cur->next;
1098 } 1309 }
1099 xmlFreeDoc(doc); 1310 xmlFreeDoc(doc);
1100 1311
1101 free(mypath); 1312 free(mypath);

mercurial