Tue, 22 Dec 2015 21:07:14 +0100
Added initial part of the brew automation.
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 | ||
470 | 32 | const char BREWSTEP[23][11] = { "NA", "INIT", "WATEROK", "PRIME", "WAITSTART", |
469 | 33 | "PREMASH", "MASHING", "IODINE", "REMOVE", "PREBOIL", |
470 | 34 | "BOIL", "BOILDONE", "HOPSTAND1", "COOLING1", "WHIRLPOOL1", |
35 | "COOLING2", "HOPSTAND2", "COOLING3", "HOPSTAND3", "COOLING", | |
36 | "WHIRLPOOL", "CLEANUP", "DONE" }; | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
37 | const char MASHSTEP[5][8] = { "NA", "PROMPT", "HEATING", "REST", "DONE" }; |
441 | 38 | |
39 | #define MY_ENCODING "utf-8" | |
40 | ||
41 | ||
42 | int do_wrsession(brew_session *brew); | |
43 | int do_wrsession(brew_session *brew) | |
44 | { | |
45 | int rc = 0; | |
46 | FILE *fp; | |
47 | char *mypath = NULL; | |
48 | xmlTextWriterPtr writer; | |
49 | xmlBufferPtr buf; | |
50 | ||
51 | /* | |
52 | * Create a new XML buffer, to which the XML document will be written | |
53 | */ | |
54 | if ((buf = xmlBufferCreate()) == NULL) { | |
55 | syslog(LOG_NOTICE, "wrsession: error creating the xml buffer"); | |
56 | return 1; | |
57 | } | |
58 | ||
59 | /* | |
60 | * Create a new XmlWriter for memory, with no compression. | |
61 | */ | |
62 | if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) { | |
63 | syslog(LOG_NOTICE, "wrsession: error creating the xml writer"); | |
64 | return 1; | |
65 | } | |
66 | ||
67 | /* | |
68 | * Use indentation instead of one long line | |
69 | */ | |
70 | if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { | |
71 | syslog(LOG_NOTICE, "wrsession: error setting Indent"); | |
72 | return 1; | |
73 | } | |
74 | ||
75 | /* | |
76 | * Start the document with the xml default for the version, | |
77 | * encoding ISO 8859-1 and the default for the standalone | |
78 | * declaration. | |
79 | */ | |
80 | if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) { | |
81 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartDocument"); | |
82 | return 1; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Start an element named "BREWCO". Since thist is the first | |
87 | * element, this will be the root element of the document. | |
88 | */ | |
89 | if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO_BREW")) < 0) { | |
90 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement"); | |
91 | return 1; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Add an attribute with name "VERSION" and value "1" to BRWCO. | |
96 | */ | |
97 | if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { | |
98 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
99 | return 1; | |
100 | } | |
101 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) { | |
102 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
103 | return 1; | |
104 | } | |
105 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) { | |
106 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); | |
107 | return 1; | |
108 | } | |
472
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
109 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", brew->name)) < 0) { |
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
110 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); |
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
111 | return 1; |
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
112 | } |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
113 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BREWSTEP", "%s", BREWSTEP[brew->brewstep])) < 0) { |
441 | 114 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
115 | return 1; | |
116 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
117 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%s", MASHSTEP[brew->mashstep])) < 0) { |
441 | 118 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
119 | return 1; | |
120 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
121 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMEOUT", "%d", brew->timeout)) < 0) { |
441 | 122 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); |
123 | return 1; | |
124 | } | |
125 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) { | |
126 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
127 | return 1; | |
128 | } | |
129 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) { | |
130 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
131 | return 1; | |
132 | } | |
133 | if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) { | |
134 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); | |
135 | return 1; | |
136 | } | |
137 | ||
138 | /* | |
139 | * All done, close any open elements | |
140 | */ | |
141 | if ((rc = xmlTextWriterEndDocument(writer)) < 0) { | |
142 | syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterEndDocument"); | |
143 | return 1; | |
144 | } | |
145 | xmlFreeTextWriter(writer); | |
146 | ||
147 | /* | |
148 | * Now write the XML configuration | |
149 | */ | |
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
|
150 | mypath = xstrcpy(etcpath); |
441 | 151 | mypath = xstrcat(mypath, (char *)"brewing.xml"); |
152 | ||
153 | if (debug) | |
154 | fprintf(stdout, "Writing %s\n", mypath); | |
155 | ||
156 | if ((fp = fopen(mypath, "w")) == NULL) { | |
157 | syslog(LOG_NOTICE, "could not rewrite %s", mypath); | |
158 | free(mypath); | |
159 | return 1; | |
160 | } | |
161 | free(mypath); | |
162 | ||
163 | fprintf(fp, "%s", (const char *) buf->content); | |
164 | fclose(fp); | |
165 | xmlBufferFree(buf); | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | ||
171 | ||
172 | int wrsession(brew_session *brew) | |
173 | { | |
174 | int rc; | |
175 | ||
176 | rc = do_wrsession(brew); | |
177 | syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc); | |
178 | return rc; | |
179 | } | |
180 | ||
181 | ||
182 | ||
183 | /* | |
184 | * Returns: | |
185 | * 0 - All is well, session loaded. | |
186 | * 1 - Something went wrong | |
187 | * -1 - No brew session available | |
188 | */ | |
189 | int rdsession(brew_session *brew) | |
190 | { | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
191 | int i, ival; |
441 | 192 | char *mypath; |
193 | xmlDocPtr doc; | |
194 | xmlNodePtr cur; | |
195 | xmlChar *key; | |
196 | ||
197 | /* | |
198 | * Search config file | |
199 | */ | |
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
|
200 | mypath = xstrcpy(etcpath); |
441 | 201 | mypath = xstrcat(mypath, (char *)"brewing.xml"); |
202 | ||
203 | /* | |
204 | * See if we have a brewing state file. | |
205 | */ | |
206 | if (file_exist(mypath, W_OK)) { | |
207 | syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath); | |
208 | free(mypath); | |
209 | return -1; | |
210 | } | |
211 | ||
212 | if ((doc = xmlParseFile(mypath)) == NULL) { | |
213 | syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath); | |
214 | free(mypath); | |
215 | return -1; | |
216 | } | |
217 | syslog(LOG_NOTICE, "rdsession: using %s", mypath); | |
218 | ||
219 | if ((cur = xmlDocGetRootElement(doc)) == NULL) { | |
220 | syslog(LOG_NOTICE, "XML file %s empty.", mypath); | |
221 | xmlFreeDoc(doc); | |
222 | return 1; | |
223 | } | |
224 | if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) { | |
225 | syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); | |
226 | xmlFreeDoc(doc); | |
227 | return 1; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Parse session | |
232 | */ | |
233 | cur = cur->xmlChildrenNode; | |
234 | while (cur != NULL) { | |
235 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_RECIPE"))) { | |
236 | brew->uuid_recipe = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
237 | } | |
238 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_UNIT"))) { | |
239 | brew->uuid_unit = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
240 | } | |
472
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
241 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { |
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
242 | brew->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
55bcbf92ecab
Added initial part of the brew automation.
Michiel Broek <mbroek@mbse.eu>
parents:
470
diff
changeset
|
243 | } |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
244 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BREWSTEP"))) { |
441 | 245 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
470 | 246 | for (i = 0; i < 23; i++) { |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
247 | if (! xmlStrcmp(key, (const xmlChar *)BREWSTEP[i])) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
248 | brew->brewstep = i; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
249 | break; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
250 | } |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
251 | } |
441 | 252 | xmlFree(key); |
253 | } | |
254 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTEP"))) { | |
255 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
256 | for (i = 0; i < 5; i++) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
257 | if (! xmlStrcmp(key, (const xmlChar *)MASHSTEP[i])) { |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
258 | brew->mashstep = i; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
259 | break; |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
260 | } |
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
261 | } |
441 | 262 | xmlFree(key); |
263 | } | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
264 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"TIMEOUT"))) { |
441 | 265 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
266 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
444
eafb140a1ddd
Better brew state field names.
Michiel Broek <mbroek@mbse.eu>
parents:
441
diff
changeset
|
267 | brew->timeout = ival; |
441 | 268 | xmlFree(key); |
269 | } | |
270 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILTIMER"))) { | |
271 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
272 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
273 | brew->boiltimer = ival; | |
274 | xmlFree(key); | |
275 | } | |
276 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"STARTTIME"))) { | |
277 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
278 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
279 | brew->starttime = (time_t)ival; | |
280 | xmlFree(key); | |
281 | } | |
282 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"ENDTIME"))) { | |
283 | key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); | |
284 | if (sscanf((const char *)key, "%d", &ival) == 1) | |
285 | brew->endtime = (time_t)ival; | |
286 | xmlFree(key); | |
287 | } | |
288 | cur = cur->next; | |
289 | } | |
290 | xmlFreeDoc(doc); | |
291 | ||
292 | free(mypath); | |
293 | mypath = NULL; | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | ||
299 |