Tue, 17 Oct 2023 11:54:38 +0200
Documentation update
0 | 1 | /***************************************************************************** |
748 | 2 | * Copyright (C) 2017-2021 |
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}, |
696
f4de55f587c1
Added websocket_port to the bms.conf file. Log the used port number.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
57 | {(char *)"websocket_port", getint, (char **)&Config.websocket_port}, |
0 | 58 | {NULL, NULL, NULL} |
59 | }; | |
60 | ||
61 | ||
62 | ||
63 | /* | |
64 | * Kill configuration data | |
65 | */ | |
66 | void killconfig(void) | |
67 | { | |
68 | if (Config.bms_name) | |
69 | free(Config.bms_name); | |
70 | Config.bms_name = NULL; | |
71 | if (Config.bms_uuid) | |
72 | free(Config.bms_uuid); | |
73 | Config.bms_uuid = NULL; | |
74 | ||
75 | if (Config.mysql_host) | |
76 | free(Config.mysql_host); | |
77 | Config.mysql_host = NULL; | |
78 | Config.mysql_port = 3306; | |
79 | if (Config.mysql_user) | |
80 | free(Config.mysql_user); | |
81 | Config.mysql_user = NULL; | |
82 | if (Config.mysql_pass) | |
83 | free(Config.mysql_pass); | |
84 | Config.mysql_pass = NULL; | |
85 | if (Config.mysql_database) | |
86 | free(Config.mysql_database); | |
87 | Config.mysql_database = NULL; | |
88 | ||
89 | if (Config.mqtt_host) | |
90 | free(Config.mqtt_host); | |
91 | Config.mqtt_host = NULL; | |
92 | Config.mqtt_port = 1883; | |
93 | if (Config.mqtt_user) | |
94 | free(Config.mqtt_user); | |
95 | Config.mqtt_user = NULL; | |
96 | if (Config.mqtt_pass) | |
97 | free(Config.mqtt_pass); | |
98 | 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
|
99 | |
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 | 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
|
101 | 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
|
102 | Config.web_root = NULL; |
696
f4de55f587c1
Added websocket_port to the bms.conf file. Log the used port number.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
103 | Config.websocket_port = 8010; |
0 | 104 | } |
105 | ||
106 | ||
107 | ||
108 | int wrconfig(void) | |
109 | { | |
110 | FILE *fp; | |
111 | ||
112 | if (mypath) | |
113 | free(mypath); | |
114 | mypath = xstrcpy(Private_Path); | |
115 | mypath = xstrcat(mypath, (char *)"/bms.config"); | |
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
696
diff
changeset
|
116 | if (mkdirs(mypath, 0755) == false) |
0 | 117 | return 1; |
118 | ||
119 | if ((fp = fopen(mypath, "w")) == NULL) { | |
120 | syslog(LOG_NOTICE, "[rdconfig] could not rewrite %s", mypath); | |
121 | return 1; | |
122 | } | |
123 | ||
124 | fprintf(fp, "# Configuration file for BMS %s\n", VERSION); | |
125 | fprintf(fp, "#\n"); | |
126 | if (Config.bms_name) | |
127 | fprintf(fp, "bms_name %s\n", Config.bms_name); | |
128 | if (Config.bms_uuid) | |
129 | fprintf(fp, "bms_uuid %s\n", Config.bms_uuid); | |
130 | fprintf(fp, "#\n"); | |
131 | ||
132 | if (Config.mysql_host) | |
133 | fprintf(fp, "mysql_host %s\n", Config.mysql_host); | |
134 | fprintf(fp, "mysql_port %d\n", Config.mysql_port); | |
135 | if (Config.mysql_user) | |
136 | fprintf(fp, "mysql_user %s\n", Config.mysql_user); | |
137 | else | |
138 | fprintf(fp, "#mysql_user <username>\n"); | |
139 | if (Config.mysql_pass) | |
140 | fprintf(fp, "mysql_pass %s\n", Config.mysql_pass); | |
141 | else | |
142 | fprintf(fp, "#mysql_pass <passwd>\n"); | |
143 | if (Config.mysql_database) | |
144 | fprintf(fp, "mysql_database %s\n", Config.mysql_database); | |
145 | else | |
146 | fprintf(fp, "#mysql_database <dbname>\n"); | |
147 | fprintf(fp, "#\n"); | |
148 | ||
149 | if (Config.mqtt_host) | |
150 | fprintf(fp, "mqtt_host %s\n", Config.mqtt_host); | |
151 | fprintf(fp, "mqtt_port %d\n", Config.mqtt_port); | |
152 | if (Config.mqtt_user) | |
153 | fprintf(fp, "mqtt_user %s\n", Config.mqtt_user); | |
154 | else | |
155 | fprintf(fp, "#mqtt_user <username>\n"); | |
156 | if (Config.mqtt_pass) | |
157 | fprintf(fp, "mqtt_pass %s\n", Config.mqtt_pass); | |
158 | else | |
159 | 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
|
160 | fprintf(fp, "#\n"); |
0 | 161 | |
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
|
162 | 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
|
163 | 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
|
164 | 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
|
165 | fprintf(fp, "#web_root </path/to/webroot>\n"); |
696
f4de55f587c1
Added websocket_port to the bms.conf file. Log the used port number.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
166 | fprintf(fp, "websocket_port %d\n", Config.websocket_port); |
0 | 167 | fprintf(fp, "#\n"); |
168 | fprintf(fp, "# End of generated configuration\n"); | |
169 | fclose(fp); | |
170 | free(mypath); | |
171 | mypath = NULL; | |
172 | return 0; | |
173 | } | |
174 | ||
175 | ||
176 | ||
177 | int rdconfig(void) | |
178 | { | |
179 | FILE *fp; | |
180 | uuid_t uu; | |
181 | char buf[256], *p; | |
182 | int i, rc = 0; | |
183 | ||
184 | if (mypath) | |
185 | free(mypath); | |
186 | mypath = xstrcpy(Private_Path); | |
187 | mypath = xstrcat(mypath, (char *)"/bms.config"); | |
188 | ||
189 | killconfig(); | |
190 | ||
191 | if (debug) | |
192 | fprintf(stdout, "[rdconfig] reading %s\n", mypath); | |
193 | ||
194 | if ((fp = fopen(mypath, "r")) == NULL) { | |
195 | syslog(LOG_NOTICE, "[rdconfig] cannot open %s", mypath); | |
196 | Config.bms_name = xstrcpy((char *)"My Brewery"); | |
197 | Config.bms_uuid = malloc(37); | |
198 | uuid_generate(uu); | |
199 | uuid_unparse(uu, Config.bms_uuid); | |
200 | Config.mysql_port = 3306; | |
201 | Config.mysql_host = xstrcpy((char *)"localhost"); | |
202 | Config.mqtt_port = 1883; | |
203 | Config.mqtt_host = xstrcpy((char *)"localhost"); | |
696
f4de55f587c1
Added websocket_port to the bms.conf file. Log the used port number.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
204 | Config.websocket_port = 8010; |
0 | 205 | return wrconfig(); |
206 | } | |
207 | ||
208 | linecnt = 0; | |
209 | while (fgets(buf, sizeof(buf) -1, fp)) { | |
210 | linecnt++; | |
211 | if (*(p = buf + strlen(buf) -1) != '\n') { | |
212 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf); | |
213 | rc = 1; | |
214 | break; | |
215 | } | |
216 | *p-- = '\0'; | |
217 | while ((p >= buf) && isspace(*p)) | |
218 | *p-- = '\0'; | |
219 | k = buf; | |
220 | while (*k && isspace(*k)) | |
221 | k++; | |
222 | p = k; | |
223 | while (*p && !isspace(*p)) | |
224 | p++; | |
225 | *p++='\0'; | |
226 | v = p; | |
227 | while (*v && isspace(*v)) | |
228 | v++; | |
229 | ||
230 | if ((*k == '\0') || (*k == '#')) { | |
231 | continue; | |
232 | } | |
233 | ||
234 | for (i = 0; keytab[i].key; i++) | |
235 | if (strcasecmp(k,keytab[i].key) == 0) | |
236 | break; | |
237 | ||
238 | if (keytab[i].key == NULL) { | |
239 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v)); | |
240 | rc = 1; | |
241 | break; | |
242 | } else if ((keytab[i].prc(keytab[i].dest))) { | |
243 | rc = 1; | |
244 | break; | |
245 | } | |
246 | } | |
247 | fclose(fp); | |
248 | ||
249 | free(mypath); | |
250 | mypath = NULL; | |
251 | ||
252 | return rc; | |
253 | } | |
254 | ||
255 | ||
256 | ||
257 | static int getstr(char **dest) | |
258 | { | |
259 | if (debug) | |
260 | fprintf(stdout, "[rdconfig] getstr: %s(%d): %s %s\n", mypath, linecnt, S(k), S(v)); | |
261 | ||
262 | *dest = xstrcpy(v); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | ||
267 | ||
268 | static int getint(char **dest) | |
269 | { | |
270 | if (debug) | |
271 | fprintf(stdout, "[rdconfig] getint: %s(%d): %s %s\n", mypath, linecnt, k, v); | |
272 | ||
273 | if (strspn(v,"0123456789") != strlen(v)) | |
274 | syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); | |
275 | else | |
276 | *((int*)dest)=atoi(v); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | ||
281 |