brewco/rdsession.c

changeset 487
d5bc44183aa4
parent 486
5a237a99a793
child 488
bee1f70fb42b
equal deleted inserted replaced
486:5a237a99a793 487:d5bc44183aa4
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 "util.h"
26 #include "xutil.h"
27
28 extern int debug;
29 extern char *etcpath;
30
31
32 const char BREWSTEP[16][11] = { "NA", "BREWINIT", "WATERCHECK", "PUMPPRIME", "WAITSTART",
33 "PREMASH", "MASHING", "REMOVE", "PREBOIL", "BOIL",
34 "BOILDONE", "COOLING", "HOPSTAND", "WHIRLPOOL", "CLEANUP",
35 "BREWDONE" };
36 const char MASHSTEP[8][11] = { "MASH-IN", "PHYTASE", "GLUCANASE", "PROTEASE", "B-AMYLASE", "A-AMYLASE1", "A-AMYLASE2", "MASH-OUT" };
37
38 #define MY_ENCODING "utf-8"
39
40
41 char *brewstep_name(int val)
42 {
43 static char text[41];
44
45 snprintf(text, 20, BREWSTEP[val]);
46 return text;
47 }
48
49
50 int do_wrsession(brew_session *brew);
51 int do_wrsession(brew_session *brew)
52 {
53 int rc = 0;
54 FILE *fp;
55 char *mypath = NULL;
56 xmlTextWriterPtr writer;
57 xmlBufferPtr buf;
58
59 /*
60 * Create a new XML buffer, to which the XML document will be written
61 */
62 if ((buf = xmlBufferCreate()) == NULL) {
63 syslog(LOG_NOTICE, "wrsession: error creating the xml buffer");
64 return 1;
65 }
66
67 /*
68 * Create a new XmlWriter for memory, with no compression.
69 */
70 if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) {
71 syslog(LOG_NOTICE, "wrsession: error creating the xml writer");
72 return 1;
73 }
74
75 /*
76 * Use indentation instead of one long line
77 */
78 if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) {
79 syslog(LOG_NOTICE, "wrsession: error setting Indent");
80 return 1;
81 }
82
83 /*
84 * Start the document with the xml default for the version,
85 * encoding ISO 8859-1 and the default for the standalone
86 * declaration.
87 */
88 if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) {
89 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartDocument");
90 return 1;
91 }
92
93 /*
94 * Start an element named "BREWCO". Since thist is the first
95 * element, this will be the root element of the document.
96 */
97 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO")) < 0) {
98 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement");
99 return 1;
100 }
101 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREW")) < 0) {
102 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement");
103 return 1;
104 }
105
106 /*
107 * Add an attribute with name "VERSION" and value "1" to BRWCO.
108 */
109 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
110 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
111 return 1;
112 }
113 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) {
114 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
115 return 1;
116 }
117 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) {
118 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
119 return 1;
120 }
121 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", brew->name)) < 0) {
122 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
123 return 1;
124 }
125 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BREWSTEP", "%s", BREWSTEP[brew->brewstep])) < 0) {
126 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
127 return 1;
128 }
129 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%s", MASHSTEP[brew->mashstep])) < 0) {
130 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
131 return 1;
132 }
133 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMEOUT", "%d", brew->timeout)) < 0) {
134 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
135 return 1;
136 }
137 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) {
138 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
139 return 1;
140 }
141 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) {
142 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
143 return 1;
144 }
145 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) {
146 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
147 return 1;
148 }
149
150 /*
151 * Close the element named BREW.
152 */
153 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
154 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
155 return 1;
156 }
157
158 /*
159 * All done, close any open elements
160 */
161 if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
162 syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterEndDocument");
163 return 1;
164 }
165 xmlFreeTextWriter(writer);
166
167 /*
168 * Now write the XML configuration
169 */
170 mypath = xstrcpy(etcpath);
171 mypath = xstrcat(mypath, (char *)"brewing.xml");
172
173 if (debug)
174 fprintf(stdout, "Writing %s\n", mypath);
175
176 if ((fp = fopen(mypath, "w")) == NULL) {
177 syslog(LOG_NOTICE, "could not rewrite %s", mypath);
178 free(mypath);
179 return 1;
180 }
181 free(mypath);
182
183 fprintf(fp, "%s", (const char *) buf->content);
184 fclose(fp);
185 xmlBufferFree(buf);
186
187 return 0;
188 }
189
190
191
192 int wrsession(brew_session *brew)
193 {
194 int rc;
195
196 rc = do_wrsession(brew);
197 // syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc);
198 return rc;
199 }
200
201
202
203 /*
204 * Returns:
205 * 0 - All is well, session loaded.
206 * 1 - Something went wrong
207 * -1 - No brew session available
208 */
209 int rdsession(brew_session *brew)
210 {
211 int i, ival;
212 char *mypath;
213 xmlDocPtr doc;
214 xmlNodePtr cur, s1cur;
215 xmlChar *key;
216
217 /*
218 * Search config file
219 */
220 mypath = xstrcpy(etcpath);
221 mypath = xstrcat(mypath, (char *)"brewing.xml");
222
223 /*
224 * See if we have a brewing state file.
225 */
226 if (file_exist(mypath, W_OK)) {
227 syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath);
228 free(mypath);
229 return -1;
230 }
231
232 if ((doc = xmlParseFile(mypath)) == NULL) {
233 syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath);
234 free(mypath);
235 return -1;
236 }
237 syslog(LOG_NOTICE, "rdsession: using %s", mypath);
238
239 if ((cur = xmlDocGetRootElement(doc)) == NULL) {
240 syslog(LOG_NOTICE, "XML file %s empty.", mypath);
241 xmlFreeDoc(doc);
242 return 1;
243 }
244 if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) {
245 syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath);
246 xmlFreeDoc(doc);
247 return 1;
248 }
249
250 /*
251 * Parse session
252 */
253 cur = cur->xmlChildrenNode;
254 while (cur != NULL) {
255
256 if ((! xmlStrcmp(cur->name, (const xmlChar*)"BREW"))) {
257 s1cur = cur->xmlChildrenNode;
258 while (s1cur != NULL) {
259
260 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"UUID_RECIPE"))) {
261 brew->uuid_recipe = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
262 }
263 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"UUID_UNIT"))) {
264 brew->uuid_unit = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
265 }
266 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"NAME"))) {
267 brew->name = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
268 }
269 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"BREWSTEP"))) {
270 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
271 for (i = 0; i < 15; i++) {
272 if (! xmlStrcmp(key, (const xmlChar *)BREWSTEP[i])) {
273 brew->brewstep = i;
274 break;
275 }
276 }
277 xmlFree(key);
278 }
279 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"MASHSTEP"))) {
280 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
281 for (i = 0; i < 8; i++) {
282 if (! xmlStrcmp(key, (const xmlChar *)MASHSTEP[i])) {
283 brew->mashstep = i;
284 break;
285 }
286 }
287 xmlFree(key);
288 }
289 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"TIMEOUT"))) {
290 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
291 if (sscanf((const char *)key, "%d", &ival) == 1)
292 brew->timeout = ival;
293 xmlFree(key);
294 }
295 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"BOILTIMER"))) {
296 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
297 if (sscanf((const char *)key, "%d", &ival) == 1)
298 brew->boiltimer = ival;
299 xmlFree(key);
300 }
301 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"STARTTIME"))) {
302 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
303 if (sscanf((const char *)key, "%d", &ival) == 1)
304 brew->starttime = (time_t)ival;
305 xmlFree(key);
306 }
307 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"ENDTIME"))) {
308 key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
309 if (sscanf((const char *)key, "%d", &ival) == 1)
310 brew->endtime = (time_t)ival;
311 xmlFree(key);
312 }
313 s1cur = s1cur->next;
314 }
315 }
316 cur = cur->next;
317 }
318 xmlFreeDoc(doc);
319
320 free(mypath);
321 mypath = NULL;
322
323 return 0;
324 }
325
326
327

mercurial