thermferm/rdconfig.c

changeset 91
901ca9858a7a
parent 90
28ee293654fd
child 92
116226a8c70a
equal deleted inserted replaced
90:28ee293654fd 91:901ca9858a7a
35 35
36 36
37 37
38 void killconfig(void) 38 void killconfig(void)
39 { 39 {
40 w1_therm *tmp1, *old1; 40 w1_therm *tmp1;
41 units_list *tmp2; 41 units_list *tmp2;
42 profiles_list *tmp3;
43 prof_step *tmp4;
42 44
43 if (Config.name) 45 if (Config.name)
44 free(Config.name); 46 free(Config.name);
45 Config.name = NULL; 47 Config.name = NULL;
46 48
47 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { 49 for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) {
48 old1 = tmp1->next;
49 if (tmp1->master) 50 if (tmp1->master)
50 free(tmp1->master); 51 free(tmp1->master);
51 if (tmp1->name) 52 if (tmp1->name)
52 free(tmp1->name); 53 free(tmp1->name);
53 if (tmp1->alias) 54 if (tmp1->alias)
75 free(tmp2->profile); 76 free(tmp2->profile);
76 free(tmp2); 77 free(tmp2);
77 } 78 }
78 Config.units = NULL; 79 Config.units = NULL;
79 80
81 for (tmp3 = Config.profiles; tmp3; tmp3 = tmp3->next) {
82 if (tmp3->uuid)
83 free(tmp3->uuid);
84 if (tmp3->name)
85 free(tmp3->name);
86 if (tmp3->steps) {
87 for (tmp4 = tmp3->steps; tmp4; tmp4 = tmp4->next) {
88 free(tmp4);
89 }
90 }
91 free(tmp3);
92 }
93 Config.profiles = NULL;
94
80 #ifdef HAVE_WIRINGPI_H 95 #ifdef HAVE_WIRINGPI_H
81 Config.lcd_cols = 16; 96 Config.lcd_cols = 16;
82 Config.lcd_rows = 2; 97 Config.lcd_rows = 2;
83 #endif 98 #endif
84 99
94 FILE *fp; 109 FILE *fp;
95 xmlTextWriterPtr writer; 110 xmlTextWriterPtr writer;
96 xmlBufferPtr buf; 111 xmlBufferPtr buf;
97 w1_therm *tmp1; 112 w1_therm *tmp1;
98 units_list *tmp3; 113 units_list *tmp3;
114 profiles_list *tmp4;
115 prof_step *tmp5;
99 116
100 /* 117 /*
101 * Create a new XML buffer, to which the XML document will be written 118 * Create a new XML buffer, to which the XML document will be written
102 */ 119 */
103 if ((buf = xmlBufferCreate()) == NULL) { 120 if ((buf = xmlBufferCreate()) == NULL) {
361 return 1; 378 return 1;
362 } 379 }
363 } 380 }
364 381
365 /* 382 /*
383 * Fermenting profiles
384 */
385 if (Config.profiles) {
386 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "PROFILES")) < 0) {
387 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
388 return 1;
389 }
390 for (tmp4 = Config.profiles; tmp4; tmp4 = tmp4->next) {
391 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "PROFILE")) < 0) {
392 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
393 return 1;
394 }
395 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
396 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
397 return 1;
398 }
399 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", tmp4->uuid)) < 0) {
400 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
401 return 1;
402 }
403 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp4->name)) < 0) {
404 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
405 return 1;
406 }
407 if (tmp4->steps) {
408 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "STEPS")) < 0) {
409 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
410 return 1;
411 }
412 for (tmp5 = tmp4->steps; tmp5; tmp5 = tmp5->next) {
413 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "STEP")) < 0) {
414 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
415 return 1;
416 }
417 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
418 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
419 return 1;
420 }
421 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DURATION", "%d", tmp5->duration)) < 0) {
422 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
423 return 1;
424 }
425 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TARGET", "%.1f", tmp5->target)) < 0) {
426 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
427 return 1;
428 }
429 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
430 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
431 return 1;
432 }
433 }
434 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
435 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
436 return 1;
437 }
438 }
439 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
440 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
441 return 1;
442 }
443 }
444 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
445 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
446 return 1;
447 }
448 }
449
450 /*
366 * All done, close any open elements 451 * All done, close any open elements
367 */ 452 */
368 if ((rc = xmlTextWriterEndDocument(writer)) < 0) { 453 if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
369 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument"); 454 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument");
370 return 1; 455 return 1;
684 769
685 int parseFermenters(xmlDocPtr doc, xmlNodePtr cur) 770 int parseFermenters(xmlDocPtr doc, xmlNodePtr cur)
686 { 771 {
687 cur = cur->xmlChildrenNode; 772 cur = cur->xmlChildrenNode;
688 while (cur != NULL) { 773 while (cur != NULL) {
689 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) { 774 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) {
690 parseUnit(doc, cur); 775 parseUnit(doc, cur);
776 }
777 cur = cur->next;
778 }
779 return 0;
780 }
781
782
783
784 int parseStep(xmlDocPtr doc, xmlNodePtr cur, prof_step **profstep)
785 {
786 xmlChar *key;
787 int ival;
788 float val;
789 prof_step *step, *tmp;
790
791 step = (prof_step *)malloc(sizeof(prof_step));
792 step->next = NULL;
793 step->version = 1;
794 step->duration = 0;
795 step->target = 20.0;
796
797 cur = cur->xmlChildrenNode;
798 while (cur != NULL) {
799 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) {
800 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
801 if (xmlStrcmp(key, (const xmlChar *)"1")) {
802 xmlFree(key);
803 return 1;
804 }
805 step->version = 1;
806 xmlFree(key);
807 }
808 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DURATION"))) {
809 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
810 if (sscanf((const char *)key, "%d", &ival) == 1)
811 step->duration = ival;
812 xmlFree(key);
813 }
814 if ((!xmlStrcmp(cur->name, (const xmlChar *)"TARGET"))) {
815 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
816 if (sscanf((const char *)key, "%f", &val) == 1)
817 step->target = val;
818 xmlFree(key);
819 }
820 cur = cur->next;
821 }
822
823 if (*profstep == NULL) {
824 *profstep = step;
825 } else {
826 for (tmp = *profstep; tmp; tmp = tmp->next) {
827 if (tmp->next == NULL) {
828 tmp->next = step;
829 break;
830 }
831 }
832 }
833 return 0;
834 }
835
836
837
838 int parseSteps(xmlDocPtr doc, xmlNodePtr cur, prof_step **step)
839 {
840 cur = cur->xmlChildrenNode;
841 while (cur != NULL) {
842 if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEP"))) {
843 parseStep(doc, cur, step);
844 }
845 cur = cur->next;
846 }
847 return 0;
848 }
849
850
851
852 int parseProfile(xmlDocPtr doc, xmlNodePtr cur)
853 {
854 xmlChar *key;
855 profiles_list *profile, *tmp;
856
857 profile = (profiles_list *)malloc(sizeof(profiles_list));
858 profile->next = NULL;
859 profile->version = 1;
860 profile->uuid = profile->name = NULL;
861 profile->steps = NULL;
862
863 cur = cur->xmlChildrenNode;
864 while (cur != NULL) {
865 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) {
866 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
867 if (xmlStrcmp(key, (const xmlChar *)"1")) {
868 xmlFree(key);
869 return 1;
870 }
871 profile->version = 1;
872 xmlFree(key);
873 }
874 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
875 profile->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
876 }
877 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
878 profile->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
879 }
880 if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEPS"))) {
881 parseSteps(doc, cur, &(profile)->steps);
882 }
883 cur = cur->next;
884 }
885
886 if (Config.profiles == NULL) {
887 Config.profiles = profile;
888 } else {
889 for (tmp = Config.profiles; tmp; tmp = tmp->next) {
890 if (tmp->next == NULL) {
891 tmp->next = profile;
892 break;
893 }
894 }
895 }
896
897 return 0;
898 }
899
900
901
902 int parseProfiles(xmlDocPtr doc, xmlNodePtr cur)
903 {
904 cur = cur->xmlChildrenNode;
905 while (cur != NULL) {
906 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILE"))) {
907 parseProfile(doc, cur);
691 } 908 }
692 cur = cur->next; 909 cur = cur->next;
693 } 910 }
694 return 0; 911 return 0;
695 } 912 }
782 parseW1therms(doc, cur); 999 parseW1therms(doc, cur);
783 } 1000 }
784 if ((!xmlStrcmp(cur->name, (const xmlChar *)"FERMENTERS"))) { 1001 if ((!xmlStrcmp(cur->name, (const xmlChar *)"FERMENTERS"))) {
785 parseFermenters(doc, cur); 1002 parseFermenters(doc, cur);
786 } 1003 }
1004 if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILES"))) {
1005 parseProfiles(doc, cur);
1006 }
787 cur = cur->next; 1007 cur = cur->next;
788 } 1008 }
789 xmlFreeDoc(doc); 1009 xmlFreeDoc(doc);
790 1010
791 free(mypath); 1011 free(mypath);

mercurial