Sun, 20 Dec 2015 20:37:40 +0100
Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.
441 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2015 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "brewco.h" | |
24 | #include "rdsession.h" | |
446
78e9d5234d15
Switched to PID code from Arduino
Michiel Broek <mbroek@mbse.eu>
parents:
444
diff
changeset
|
25 | #include "util.h" |
441 | 26 | #include "xutil.h" |
27 | ||
28 | extern int debug; | |
464
4a624c071ca9
Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.
Michiel Broek <mbroek@mbse.eu>
parents:
446
diff
changeset
|
29 | extern char *etcpath; |
441 | 30 | |
31 | ||
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
32 | const char BREWSTEP[14][10] = { "NA", "INIT", "WAITSTART", "PREMASH", "MASHING", "IODINE", "REMOVE", "PREBOIL", "BOIL", "BOILDONE", "COOLING", "WHIRLPOOL", "CLEANUP", "DONE" }; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
33 | const char MASHSTEP[5][8] = { "NA", "PROMPT", "HEATING", "REST", "DONE" }; |
441 | 34 | |
35 | #define MY_ENCODING "utf-8" | |
36 | ||
37 | ||
38 | int do_wrsession(brew_session *brew); | |
39 | int do_wrsession(brew_session *brew) | |
40 | { | |
41 | int rc = 0; | |
42 | FILE *fp; | |
43 | char *mypath = NULL; | |
44 | xmlTextWriterPtr writer; | |
45 | xmlBufferPtr buf; | |
46 | ||
47 | /* | |
48 | * Create a new XML buffer, to which the XML document will be written | |
49 | */ | |
50 | if ((buf = xmlBufferCreate()) == NULL) { | |
51 | syslog(LOG_NOTICE, "wrsession: error creating the xml buffer"); | |
52 | return 1; | |
53 | } | |
54 | ||
55 | /* | |
56 | * Create a new XmlWriter for memory, with no compression. | |
57 | */ | |
58 | if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) { | |
59 | syslog(LOG_NOTICE, "wrsession: error creating the xml writer"); | |
60 | return 1; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Use indentation instead of one long line | |
65 | */ | |
66 | if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { | |
67 | syslog(LOG_NOTICE, "wrsession: error setting Indent"); | |
68 | return 1; | |
69 | } | |
70 | ||
71 | /* | |
72 | * Start the document with the xml default for the version, | |
73 | * encoding ISO 8859-1 and the default for the standalone | |
74 | * declaration. | |
75 | */ | |
76 | if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) { | |
77 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartDocument"); | |
78 | return 1; | |
79 | } | |
80 | ||
81 | /* | |
82 | * Start an element named "BREWCO". Since thist is the first | |
83 | * element, this will be the root element of the document. | |
84 | */ | |
85 | if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO_BREW")) < 0) { | |
86 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement"); | |
87 | return 1; | |
88 | } | |
89 | ||
90 | /* | |
91 | * Add an attribute with name "VERSION" and value "1" to BRWCO. | |
92 | */ | |
93 | if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { | |
94 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
95 | return 1; | |
96 | } | |
97 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) { | |
98 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
99 | return 1; | |
100 | } | |
101 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) { | |
102 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
103 | return 1; | |
104 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
105 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BREWSTEP", "%s", BREWSTEP[brew->brewstep])) < 0) { |
441 | 106 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
107 | return 1; | |
108 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
109 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%s", MASHSTEP[brew->mashstep])) < 0) { |
441 | 110 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
111 | return 1; | |
112 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
113 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMEOUT", "%d", brew->timeout)) < 0) { |
441 | 114 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
115 | return 1; | |
116 | } | |
117 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) { | |
118 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
119 | return 1; | |
120 | } | |
121 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) { | |
122 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
123 | return 1; | |
124 | } | |
125 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) { | |
126 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | /* | |
131 | * All done, close any open elements | |
132 | */ | |
133 | if ((rc = xmlTextWriterEndDocument(writer)) < 0) { | |
134 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterEndDocument"); | |
135 | return 1; | |
136 | } | |
137 | xmlFreeTextWriter(writer); | |
138 | ||
139 | /* | |
140 | * Now write the XML configuration | |
141 | */ | |
464
4a624c071ca9
Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.
Michiel Broek <mbroek@mbse.eu>
parents:
446
diff
changeset
|
142 | mypath = xstrcpy(etcpath); |
441 | 143 | mypath = xstrcat(mypath, (char *)"brewing.xml"); |
144 | ||
145 | if (debug) | |
146 | fprintf(stdout, "Writing %s\n", mypath); | |
147 | ||
148 | if ((fp = fopen(mypath, "w")) == NULL) { | |
149 | syslog(LOG_NOTICE, "could not rewrite %s", mypath); | |
150 | free(mypath); | |
151 | return 1; | |
152 | } | |
153 | free(mypath); | |
154 | ||
155 | fprintf(fp, "%s", (const char *) buf->content); | |
156 | fclose(fp); | |
157 | xmlBufferFree(buf); | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | ||
163 | ||
164 | int wrsession(brew_session *brew) | |
165 | { | |
166 | int rc; | |
167 | ||
168 | rc = do_wrsession(brew); | |
169 | syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc); | |
170 | return rc; | |
171 | } | |
172 | ||
173 | ||
174 | ||
175 | /* | |
176 | * Returns: | |
177 | * 0 - All is well, session loaded. | |
178 | * 1 - Something went wrong | |
179 | * -1 - No brew session available | |
180 | */ | |
181 | int rdsession(brew_session *brew) | |
182 | { | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
183 | int i, ival; |
441 | 184 | char *mypath; |
185 | xmlDocPtr doc; | |
186 | xmlNodePtr cur; | |
187 | xmlChar *key; | |
188 | ||
189 | /* | |
190 | * Search config file | |
191 | */ | |
464
4a624c071ca9
Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.
Michiel Broek <mbroek@mbse.eu>
parents:
446
diff
changeset
|
192 | mypath = xstrcpy(etcpath); |
441 | 193 | mypath = xstrcat(mypath, (char *)"brewing.xml"); |
194 | ||
195 | /* | |
196 | * See if we have a brewing state file. | |
197 | */ | |
198 | if (file_exist(mypath, W_OK)) { | |
199 | syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath); | |
200 | free(mypath); | |
201 | return -1; | |
202 | } | |
203 | ||
204 | if ((doc = xmlParseFile(mypath)) == NULL) { | |
205 | syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath); | |
206 | free(mypath); | |
207 | return -1; | |
208 | } | |
209 | syslog(LOG_NOTICE, "rdsession: using %s", mypath); | |
210 | ||
211 | if ((cur = xmlDocGetRootElement(doc)) == NULL) { | |
212 | syslog(LOG_NOTICE, "XML file %s empty.", mypath); | |
213 | xmlFreeDoc(doc); | |
214 | return 1; | |
215 | } | |
216 | if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) { | |
217 | syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); | |
218 | xmlFreeDoc(doc); | |
219 | return 1; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Parse session | |
224 | */ | |
225 | cur = cur->xmlChildrenNode; | |
226 | while (cur != NULL) { | |
227 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_RECIPE"))) { | |
228 | brew->uuid_recipe = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
229 | } | |
230 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_UNIT"))) { | |
231 | brew->uuid_unit = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
232 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
233 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BREWSTEP"))) { |
441 | 234 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
235 | for (i = 0; i < 14; i++) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
236 | if (! xmlStrcmp(key, (const xmlChar *)BREWSTEP[i])) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
237 | brew->brewstep = i; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
238 | break; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
239 | } |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
240 | } |
441 | 241 | xmlFree(key); |
242 | } | |
243 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTEP"))) { | |
244 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
245 | for (i = 0; i < 5; i++) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
246 | if (! xmlStrcmp(key, (const xmlChar *)MASHSTEP[i])) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
247 | brew->mashstep = i; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
248 | break; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
249 | } |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
250 | } |
441 | 251 | xmlFree(key); |
252 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
253 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"TIMEOUT"))) { |
441 | 254 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
255 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
256 | brew->timeout = ival; |
441 | 257 | xmlFree(key); |
258 | } | |
259 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILTIMER"))) { | |
260 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
261 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
262 | brew->boiltimer = ival; | |
263 | xmlFree(key); | |
264 | } | |
265 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"STARTTIME"))) { | |
266 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
267 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
268 | brew->starttime = (time_t)ival; | |
269 | xmlFree(key); | |
270 | } | |
271 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"ENDTIME"))) { | |
272 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
273 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
274 | brew->endtime = (time_t)ival; | |
275 | xmlFree(key); | |
276 | } | |
277 | cur = cur->next; | |
278 | } | |
279 | xmlFreeDoc(doc); | |
280 | ||
281 | free(mypath); | |
282 | mypath = NULL; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | ||
288 |