Sat, 28 Nov 2015 20:15:38 +0100
Added automate state file.
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" | |
25 | #include "futil.h" | |
26 | #include "xutil.h" | |
27 | ||
28 | extern int debug; | |
29 | ||
30 | ||
31 | ||
32 | #define MY_ENCODING "utf-8" | |
33 | ||
34 | ||
35 | int do_wrsession(brew_session *brew); | |
36 | int do_wrsession(brew_session *brew) | |
37 | { | |
38 | int rc = 0; | |
39 | FILE *fp; | |
40 | char *mypath = NULL; | |
41 | xmlTextWriterPtr writer; | |
42 | xmlBufferPtr buf; | |
43 | ||
44 | /* | |
45 | * Create a new XML buffer, to which the XML document will be written | |
46 | */ | |
47 | if ((buf = xmlBufferCreate()) == NULL) { | |
48 | syslog(LOG_NOTICE, "wrsession: error creating the xml buffer"); | |
49 | return 1; | |
50 | } | |
51 | ||
52 | /* | |
53 | * Create a new XmlWriter for memory, with no compression. | |
54 | */ | |
55 | if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) { | |
56 | syslog(LOG_NOTICE, "wrsession: error creating the xml writer"); | |
57 | return 1; | |
58 | } | |
59 | ||
60 | /* | |
61 | * Use indentation instead of one long line | |
62 | */ | |
63 | if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { | |
64 | syslog(LOG_NOTICE, "wrsession: error setting Indent"); | |
65 | return 1; | |
66 | } | |
67 | ||
68 | /* | |
69 | * Start the document with the xml default for the version, | |
70 | * encoding ISO 8859-1 and the default for the standalone | |
71 | * declaration. | |
72 | */ | |
73 | if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) { | |
74 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartDocument"); | |
75 | return 1; | |
76 | } | |
77 | ||
78 | /* | |
79 | * Start an element named "BREWCO". Since thist is the first | |
80 | * element, this will be the root element of the document. | |
81 | */ | |
82 | if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO_BREW")) < 0) { | |
83 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement"); | |
84 | return 1; | |
85 | } | |
86 | ||
87 | /* | |
88 | * Add an attribute with name "VERSION" and value "1" to BRWCO. | |
89 | */ | |
90 | if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { | |
91 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
92 | return 1; | |
93 | } | |
94 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) { | |
95 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
96 | return 1; | |
97 | } | |
98 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) { | |
99 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
100 | return 1; | |
101 | } | |
102 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PREMASH", "%d", brew->premash)) < 0) { | |
103 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
104 | return 1; | |
105 | } | |
106 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%d", brew->mashstep)) < 0) { | |
107 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
108 | return 1; | |
109 | } | |
110 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTATE", "%d", brew->mashstate)) < 0) { | |
111 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
112 | return 1; | |
113 | } | |
114 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "RESTTIME", "%d", brew->resttime)) < 0) { | |
115 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
116 | return 1; | |
117 | } | |
118 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IODINE", "%d", brew->iodine)) < 0) { | |
119 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
120 | return 1; | |
121 | } | |
122 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHREMOVE", "%d", brew->mashremove)) < 0) { | |
123 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
124 | return 1; | |
125 | } | |
126 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILHEATING", "%d", brew->boilheating)) < 0) { | |
127 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
128 | return 1; | |
129 | } | |
130 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILING", "%d", brew->boiling)) < 0) { | |
131 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
132 | return 1; | |
133 | } | |
134 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) { | |
135 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
136 | return 1; | |
137 | } | |
138 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLING", "%d", brew->cooling)) < 0) { | |
139 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
140 | return 1; | |
141 | } | |
142 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "WHIRLPOOL", "%d", brew->whirlpool)) < 0) { | |
143 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
144 | return 1; | |
145 | } | |
146 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CLEANUP", "%d", brew->cleanup)) < 0) { | |
147 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
148 | return 1; | |
149 | } | |
150 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) { | |
151 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
152 | return 1; | |
153 | } | |
154 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) { | |
155 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
156 | return 1; | |
157 | } | |
158 | ||
159 | /* | |
160 | * All done, close any open elements | |
161 | */ | |
162 | if ((rc = xmlTextWriterEndDocument(writer)) < 0) { | |
163 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterEndDocument"); | |
164 | return 1; | |
165 | } | |
166 | xmlFreeTextWriter(writer); | |
167 | ||
168 | /* | |
169 | * Now write the XML configuration | |
170 | */ | |
171 | if (getenv((char *)"USER") == NULL) { | |
172 | mypath = xstrcpy((char *)"/root"); | |
173 | } else { | |
174 | mypath = xstrcpy(getenv((char *)"HOME")); | |
175 | } | |
176 | mypath = xstrcat(mypath, (char *)"/.brewco/var/"); | |
177 | mkdirs(mypath, 0755); | |
178 | mypath = xstrcat(mypath, (char *)"brewing.xml"); | |
179 | ||
180 | if (debug) | |
181 | fprintf(stdout, "Writing %s\n", mypath); | |
182 | ||
183 | if ((fp = fopen(mypath, "w")) == NULL) { | |
184 | syslog(LOG_NOTICE, "could not rewrite %s", mypath); | |
185 | free(mypath); | |
186 | return 1; | |
187 | } | |
188 | free(mypath); | |
189 | ||
190 | fprintf(fp, "%s", (const char *) buf->content); | |
191 | fclose(fp); | |
192 | xmlBufferFree(buf); | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
197 | ||
198 | ||
199 | int wrsession(brew_session *brew) | |
200 | { | |
201 | int rc; | |
202 | ||
203 | rc = do_wrsession(brew); | |
204 | syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc); | |
205 | return rc; | |
206 | } | |
207 | ||
208 | ||
209 | ||
210 | /* | |
211 | * Returns: | |
212 | * 0 - All is well, session loaded. | |
213 | * 1 - Something went wrong | |
214 | * -1 - No brew session available | |
215 | */ | |
216 | int rdsession(brew_session *brew) | |
217 | { | |
218 | int ival; | |
219 | char *mypath; | |
220 | xmlDocPtr doc; | |
221 | xmlNodePtr cur; | |
222 | xmlChar *key; | |
223 | ||
224 | syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME"))); | |
225 | ||
226 | /* | |
227 | * Search config file | |
228 | */ | |
229 | if (getenv((char *)"USER") == NULL) { | |
230 | mypath = xstrcpy((char *)"/root"); | |
231 | } else { | |
232 | mypath = xstrcpy(getenv((char *)"HOME")); | |
233 | } | |
234 | mypath = xstrcat(mypath, (char *)"/.brewco/var/"); | |
235 | mkdirs(mypath, 0755); | |
236 | mypath = xstrcat(mypath, (char *)"brewing.xml"); | |
237 | ||
238 | /* | |
239 | * See if we have a brewing state file. | |
240 | */ | |
241 | if (file_exist(mypath, W_OK)) { | |
242 | syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath); | |
243 | free(mypath); | |
244 | return -1; | |
245 | } | |
246 | ||
247 | if ((doc = xmlParseFile(mypath)) == NULL) { | |
248 | syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath); | |
249 | free(mypath); | |
250 | return -1; | |
251 | } | |
252 | syslog(LOG_NOTICE, "rdsession: using %s", mypath); | |
253 | ||
254 | if ((cur = xmlDocGetRootElement(doc)) == NULL) { | |
255 | syslog(LOG_NOTICE, "XML file %s empty.", mypath); | |
256 | xmlFreeDoc(doc); | |
257 | return 1; | |
258 | } | |
259 | if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) { | |
260 | syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); | |
261 | xmlFreeDoc(doc); | |
262 | return 1; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Parse session | |
267 | */ | |
268 | cur = cur->xmlChildrenNode; | |
269 | while (cur != NULL) { | |
270 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_RECIPE"))) { | |
271 | brew->uuid_recipe = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
272 | } | |
273 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_UNIT"))) { | |
274 | brew->uuid_unit = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
275 | } | |
276 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"PREMASH"))) { | |
277 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
278 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
279 | brew->premash = ival; | |
280 | xmlFree(key); | |
281 | } | |
282 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTEP"))) { | |
283 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
284 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
285 | brew->mashstep = ival; | |
286 | xmlFree(key); | |
287 | } | |
288 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTATE"))) { | |
289 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
290 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
291 | brew->mashstate = ival; | |
292 | xmlFree(key); | |
293 | } | |
294 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"RESTTIME"))) { | |
295 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
296 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
297 | brew->resttime = ival; | |
298 | xmlFree(key); | |
299 | } | |
300 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"IODINE"))) { | |
301 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
302 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
303 | brew->iodine = ival; | |
304 | xmlFree(key); | |
305 | } | |
306 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHREMOVE"))) { | |
307 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
308 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
309 | brew->mashremove = ival; | |
310 | xmlFree(key); | |
311 | } | |
312 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILHEATING"))) { | |
313 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
314 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
315 | brew->boilheating = ival; | |
316 | xmlFree(key); | |
317 | } | |
318 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILING"))) { | |
319 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
320 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
321 | brew->boiling = ival; | |
322 | xmlFree(key); | |
323 | } | |
324 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILTIMER"))) { | |
325 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
326 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
327 | brew->boiltimer = ival; | |
328 | xmlFree(key); | |
329 | } | |
330 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLING"))) { | |
331 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
332 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
333 | brew->cooling = ival; | |
334 | xmlFree(key); | |
335 | } | |
336 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"WHIRLPOOL"))) { | |
337 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
338 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
339 | brew->whirlpool = ival; | |
340 | xmlFree(key); | |
341 | } | |
342 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"CLEANUP"))) { | |
343 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
344 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
345 | brew->cleanup = ival; | |
346 | xmlFree(key); | |
347 | } | |
348 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"STARTTIME"))) { | |
349 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
350 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
351 | brew->starttime = (time_t)ival; | |
352 | xmlFree(key); | |
353 | } | |
354 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"ENDTIME"))) { | |
355 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
356 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
357 | brew->endtime = (time_t)ival; | |
358 | xmlFree(key); | |
359 | } | |
360 | cur = cur->next; | |
361 | } | |
362 | xmlFreeDoc(doc); | |
363 | ||
364 | free(mypath); | |
365 | mypath = NULL; | |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
370 | ||
371 |