Sat, 23 Feb 2019 22:56:35 +0100
Fixed save empty dates
0 | 1 | /***************************************************************************** |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
2 | * Copyright (C) 2017-2019 |
0 | 3 | * |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the bms (Brewery Management System) | |
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 | * bms 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 "bms.h" | |
24 | #include "xutil.h" | |
25 | #include "futil.h" | |
26 | #include "rdconfig.h" | |
27 | ||
28 | ||
29 | static char *mypath; | |
30 | static char *k, *v; | |
31 | static int linecnt = 0; | |
32 | ||
33 | extern int debug; | |
34 | extern char *Private_Path; | |
35 | ||
36 | ||
37 | sys_config Config; | |
38 | ||
39 | ||
40 | static int getstr(char **); | |
41 | static int getint(char **); | |
42 | ||
43 | ||
44 | key_list keytab[] = { | |
45 | {(char *)"bms_name", getstr, (char **)&Config.bms_name}, | |
46 | {(char *)"bms_uuid", getstr, (char **)&Config.bms_uuid}, | |
47 | {(char *)"mysql_host", getstr, (char **)&Config.mysql_host}, | |
48 | {(char *)"mysql_port", getint, (char **)&Config.mysql_port}, | |
49 | {(char *)"mysql_user", getstr, (char **)&Config.mysql_user}, | |
50 | {(char *)"mysql_pass", getstr, (char **)&Config.mysql_pass}, | |
51 | {(char *)"mysql_database", getstr, (char **)&Config.mysql_database}, | |
52 | {(char *)"mqtt_host", getstr, (char **)&Config.mqtt_host}, | |
53 | {(char *)"mqtt_port", getint, (char **)&Config.mqtt_port}, | |
54 | {(char *)"mqtt_user", getstr, (char **)&Config.mqtt_user}, | |
55 | {(char *)"mqtt_pass", getstr, (char **)&Config.mqtt_pass}, | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
56 | {(char *)"web_root", getstr, (char **)&Config.web_root}, |
0 | 57 | {NULL, NULL, NULL} |
58 | }; | |
59 | ||
60 | ||
61 | ||
62 | /* | |
63 | * Kill configuration data | |
64 | */ | |
65 | void killconfig(void) | |
66 | { | |
67 | if (Config.bms_name) | |
68 | free(Config.bms_name); | |
69 | Config.bms_name = NULL; | |
70 | if (Config.bms_uuid) | |
71 | free(Config.bms_uuid); | |
72 | Config.bms_uuid = NULL; | |
73 | ||
74 | if (Config.mysql_host) | |
75 | free(Config.mysql_host); | |
76 | Config.mysql_host = NULL; | |
77 | Config.mysql_port = 3306; | |
78 | if (Config.mysql_user) | |
79 | free(Config.mysql_user); | |
80 | Config.mysql_user = NULL; | |
81 | if (Config.mysql_pass) | |
82 | free(Config.mysql_pass); | |
83 | Config.mysql_pass = NULL; | |
84 | if (Config.mysql_database) | |
85 | free(Config.mysql_database); | |
86 | Config.mysql_database = NULL; | |
87 | ||
88 | if (Config.mqtt_host) | |
89 | free(Config.mqtt_host); | |
90 | Config.mqtt_host = NULL; | |
91 | Config.mqtt_port = 1883; | |
92 | if (Config.mqtt_user) | |
93 | free(Config.mqtt_user); | |
94 | Config.mqtt_user = NULL; | |
95 | if (Config.mqtt_pass) | |
96 | free(Config.mqtt_pass); | |
97 | Config.mqtt_pass = NULL; | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
98 | |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
99 | if (Config.web_root) |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
100 | free(Config.web_root); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
101 | Config.web_root = NULL; |
0 | 102 | } |
103 | ||
104 | ||
105 | ||
106 | int wrconfig(void) | |
107 | { | |
108 | FILE *fp; | |
109 | ||
110 | if (mypath) | |
111 | free(mypath); | |
112 | mypath = xstrcpy(Private_Path); | |
113 | mypath = xstrcat(mypath, (char *)"/bms.config"); | |
114 | if (mkdirs(mypath, 0755) == FALSE) | |
115 | return 1; | |
116 | ||
117 | if ((fp = fopen(mypath, "w")) == NULL) { | |
118 | syslog(LOG_NOTICE, "[rdconfig] could not rewrite %s", mypath); | |
119 | return 1; | |
120 | } | |
121 | ||
122 | fprintf(fp, "# Configuration file for BMS %s\n", VERSION); | |
123 | fprintf(fp, "#\n"); | |
124 | if (Config.bms_name) | |
125 | fprintf(fp, "bms_name %s\n", Config.bms_name); | |
126 | if (Config.bms_uuid) | |
127 | fprintf(fp, "bms_uuid %s\n", Config.bms_uuid); | |
128 | fprintf(fp, "#\n"); | |
129 | ||
130 | if (Config.mysql_host) | |
131 | fprintf(fp, "mysql_host %s\n", Config.mysql_host); | |
132 | fprintf(fp, "mysql_port %d\n", Config.mysql_port); | |
133 | if (Config.mysql_user) | |
134 | fprintf(fp, "mysql_user %s\n", Config.mysql_user); | |
135 | else | |
136 | fprintf(fp, "#mysql_user <username>\n"); | |
137 | if (Config.mysql_pass) | |
138 | fprintf(fp, "mysql_pass %s\n", Config.mysql_pass); | |
139 | else | |
140 | fprintf(fp, "#mysql_pass <passwd>\n"); | |
141 | if (Config.mysql_database) | |
142 | fprintf(fp, "mysql_database %s\n", Config.mysql_database); | |
143 | else | |
144 | fprintf(fp, "#mysql_database <dbname>\n"); | |
145 | fprintf(fp, "#\n"); | |
146 | ||
147 | if (Config.mqtt_host) | |
148 | fprintf(fp, "mqtt_host %s\n", Config.mqtt_host); | |
149 | fprintf(fp, "mqtt_port %d\n", Config.mqtt_port); | |
150 | if (Config.mqtt_user) | |
151 | fprintf(fp, "mqtt_user %s\n", Config.mqtt_user); | |
152 | else | |
153 | fprintf(fp, "#mqtt_user <username>\n"); | |
154 | if (Config.mqtt_pass) | |
155 | fprintf(fp, "mqtt_pass %s\n", Config.mqtt_pass); | |
156 | else | |
157 | fprintf(fp, "#mqtt_pwd <passwd>\n"); | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
158 | fprintf(fp, "#\n"); |
0 | 159 | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
160 | if (Config.web_root) |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
161 | fprintf(fp, "web_root %s\n", Config.web_root); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
162 | else |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
163 | fprintf(fp, "#web_root </path/to/webroot>\n"); |
0 | 164 | fprintf(fp, "#\n"); |
165 | fprintf(fp, "# End of generated configuration\n"); | |
166 | fclose(fp); | |
167 | free(mypath); | |
168 | mypath = NULL; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | ||
173 | ||
174 | int rdconfig(void) | |
175 | { | |
176 | FILE *fp; | |
177 | uuid_t uu; | |
178 | char buf[256], *p; | |
179 | int i, rc = 0; | |
180 | ||
181 | if (mypath) | |
182 | free(mypath); | |
183 | mypath = xstrcpy(Private_Path); | |
184 | mypath = xstrcat(mypath, (char *)"/bms.config"); | |
185 | ||
186 | killconfig(); | |
187 | ||
188 | if (debug) | |
189 | fprintf(stdout, "[rdconfig] reading %s\n", mypath); | |
190 | ||
191 | if ((fp = fopen(mypath, "r")) == NULL) { | |
192 | syslog(LOG_NOTICE, "[rdconfig] cannot open %s", mypath); | |
193 | Config.bms_name = xstrcpy((char *)"My Brewery"); | |
194 | Config.bms_uuid = malloc(37); | |
195 | uuid_generate(uu); | |
196 | uuid_unparse(uu, Config.bms_uuid); | |
197 | Config.mysql_port = 3306; | |
198 | Config.mysql_host = xstrcpy((char *)"localhost"); | |
199 | Config.mqtt_port = 1883; | |
200 | Config.mqtt_host = xstrcpy((char *)"localhost"); | |
201 | return wrconfig(); | |
202 | } | |
203 | ||
204 | linecnt = 0; | |
205 | while (fgets(buf, sizeof(buf) -1, fp)) { | |
206 | linecnt++; | |
207 | if (*(p = buf + strlen(buf) -1) != '\n') { | |
208 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf); | |
209 | rc = 1; | |
210 | break; | |
211 | } | |
212 | *p-- = '\0'; | |
213 | while ((p >= buf) && isspace(*p)) | |
214 | *p-- = '\0'; | |
215 | k = buf; | |
216 | while (*k && isspace(*k)) | |
217 | k++; | |
218 | p = k; | |
219 | while (*p && !isspace(*p)) | |
220 | p++; | |
221 | *p++='\0'; | |
222 | v = p; | |
223 | while (*v && isspace(*v)) | |
224 | v++; | |
225 | ||
226 | if ((*k == '\0') || (*k == '#')) { | |
227 | continue; | |
228 | } | |
229 | ||
230 | for (i = 0; keytab[i].key; i++) | |
231 | if (strcasecmp(k,keytab[i].key) == 0) | |
232 | break; | |
233 | ||
234 | if (keytab[i].key == NULL) { | |
235 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v)); | |
236 | rc = 1; | |
237 | break; | |
238 | } else if ((keytab[i].prc(keytab[i].dest))) { | |
239 | rc = 1; | |
240 | break; | |
241 | } | |
242 | } | |
243 | fclose(fp); | |
244 | ||
245 | free(mypath); | |
246 | mypath = NULL; | |
247 | ||
248 | return rc; | |
249 | } | |
250 | ||
251 | ||
252 | ||
253 | static int getstr(char **dest) | |
254 | { | |
255 | if (debug) | |
256 | fprintf(stdout, "[rdconfig] getstr: %s(%d): %s %s\n", mypath, linecnt, S(k), S(v)); | |
257 | ||
258 | *dest = xstrcpy(v); | |
259 | return 0; | |
260 | } | |
261 | ||
262 | ||
263 | ||
264 | static int getint(char **dest) | |
265 | { | |
266 | if (debug) | |
267 | fprintf(stdout, "[rdconfig] getint: %s(%d): %s %s\n", mypath, linecnt, k, v); | |
268 | ||
269 | if (strspn(v,"0123456789") != strlen(v)) | |
270 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); | |
271 | else | |
272 | *((int*)dest)=atoi(v); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | ||
277 |