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