Tue, 13 Apr 2021 16:55:25 +0200
Use mariadb JSON support for mash and fermentation profiles.
0 | 1 | /***************************************************************************** |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
2 | * Copyright (C) 2017-2020 |
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" | |
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
|
25 | #include "futil.h" |
0 | 26 | #include "rdconfig.h" |
27 | #include "lock.h" | |
28 | #include "mqtt.h" | |
29 | #include "mysql.h" | |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
30 | #include "nodes.h" |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
31 | #include "websocket.h" |
0 | 32 | |
33 | ||
34 | int my_shutdown = FALSE; | |
35 | int debug = FALSE; | |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
36 | static pid_t mypid; |
0 | 37 | char *Private_Path = NULL; /* Users data path */ |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
38 | pthread_t ws_thread; |
0 | 39 | |
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
|
40 | extern sys_config Config; |
0 | 41 | |
42 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
43 | |
0 | 44 | void help(void) |
45 | { | |
46 | fprintf(stdout, "bmsd v%s starting\n\n", VERSION); | |
47 | fprintf(stdout, "Usage: bmsd [-d] [-h]\n"); | |
48 | fprintf(stdout, " -d --debug Debug and run in foreground\n"); | |
49 | fprintf(stdout, " -h --help Display this help\n"); | |
50 | } | |
51 | ||
52 | ||
53 | ||
54 | void die(int onsig) | |
55 | { | |
56 | switch (onsig) { | |
57 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); | |
58 | break; | |
59 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); | |
60 | break; | |
61 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); | |
62 | break; | |
63 | case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); | |
64 | my_shutdown = TRUE; | |
65 | exit(SIGSEGV); | |
66 | break; | |
67 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); | |
68 | } | |
69 | my_shutdown = TRUE; | |
70 | } | |
71 | ||
72 | ||
73 | ||
74 | int server(void) { | |
75 | uid_t myuid; | |
76 | struct passwd *mypwd; | |
77 | int rc = 0; | |
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
|
78 | char *tmppath = NULL; |
0 | 79 | |
80 | myuid = getuid(); | |
81 | mypwd = getpwuid(myuid); | |
82 | if (mypwd == NULL) { | |
83 | fprintf(stderr, "[main] Could not find passwd entry\n"); | |
84 | return 1; | |
85 | } | |
86 | ||
87 | Private_Path = xstrcpy(mypwd->pw_dir); | |
88 | Private_Path = xstrcat(Private_Path, (char *)"/.bms"); | |
89 | ||
90 | if (lockprog((char *)"bmsd")) { | |
91 | syslog(LOG_NOTICE, "Can't lock"); | |
92 | return 1; | |
93 | } | |
94 | ||
95 | if (rdconfig()) { | |
96 | rc = 2; | |
97 | goto endit1; | |
98 | } | |
99 | ||
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
|
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 | rc = 5; |
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 | syslog(LOG_NOTICE, "Configuration `web_root' is not set"); |
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
|
103 | goto endit1; |
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
|
104 | } |
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
|
105 | |
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
|
106 | /* |
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
|
107 | * Make sure the needed log paths exist. |
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
|
108 | */ |
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
|
109 | tmppath = xstrcpy(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
|
110 | tmppath = xstrcat(tmppath, (char *)"/log/fermentation/"); |
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
|
111 | if (! mkdirs(tmppath, 0755)) { |
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
|
112 | rc = 5; |
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
|
113 | goto endit1; |
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
|
114 | } |
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
|
115 | free(tmppath); |
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
|
116 | tmppath = xstrcpy(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
|
117 | tmppath = xstrcat(tmppath, (char *)"/log/brews/"); |
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
|
118 | if (! mkdirs(tmppath, 0755)) { |
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
|
119 | rc = 5; |
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
|
120 | goto endit1; |
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
|
121 | } |
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
|
122 | free(tmppath); |
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
|
123 | tmppath = NULL; |
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
|
124 | |
0 | 125 | if (bms_mysql_init()) { |
126 | rc = 3; | |
127 | goto endit1; | |
128 | } | |
129 | if (mqtt_connect()) { | |
130 | rc = 4; | |
131 | goto endit2; | |
132 | } | |
133 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
134 | rc = pthread_create(&ws_thread, NULL, ws_loop, NULL); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
135 | if (rc) { |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
136 | fprintf(stderr, "ws_loop thread didn't start rc=%d\n", rc); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
137 | syslog(LOG_NOTICE, "ws_loop thread didn't start rc=%d", rc); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
138 | rc = 5; |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
139 | goto endit3; |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
140 | } |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
141 | |
0 | 142 | if (debug) |
143 | fprintf(stdout, "[main] Entering main loop\n"); | |
144 | ||
145 | while (my_shutdown == FALSE) { | |
146 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
147 | usleep(3000000); |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
148 | nodes_check_online(); |
680
0bb48333d133
Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.
Michiel Broek <mbroek@mbse.eu>
parents:
671
diff
changeset
|
149 | usleep(2000000); |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
150 | ws_check(); |
0 | 151 | } |
152 | if (debug) | |
153 | fprintf(stdout, "[main] Exit from main loop\n"); | |
154 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
155 | endit3: |
0 | 156 | /* |
157 | * Remove our topics and close MQTT connection. | |
158 | */ | |
159 | mqtt_disconnect(); | |
160 | ||
161 | endit2: | |
162 | bms_mysql_end(); | |
163 | ||
164 | endit1: | |
165 | ||
166 | killconfig(); | |
167 | ||
168 | ulockprog((char *)"bmsd"); | |
169 | ||
170 | free(Private_Path); | |
171 | Private_Path = NULL; | |
172 | ||
173 | if (debug) | |
174 | fprintf(stdout, "[main] Server shutdown complete\n"); | |
175 | return rc; | |
176 | } | |
177 | ||
178 | ||
179 | ||
180 | int main(int argc, char *argv[]) { | |
181 | int rc, c, i; | |
182 | pid_t frk; | |
183 | ||
184 | while (1) { | |
185 | int option_index = 0; | |
186 | static struct option long_options[] = { | |
187 | {"debug", 0, 0, 'c'}, | |
188 | {"help", 0, 0, 'h'}, | |
189 | {0, 0, 0, 0} | |
190 | }; | |
191 | ||
192 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
193 | if (c == -1) | |
194 | break; | |
195 | switch (c) { | |
196 | case 'd': debug = TRUE; | |
197 | break; | |
198 | case 'h': help(); | |
199 | return 1; | |
200 | } | |
201 | } | |
202 | ||
203 | openlog("bmsd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); | |
204 | syslog(LOG_NOTICE, "bmsd v%s starting", VERSION); | |
205 | if (debug) | |
206 | fprintf(stdout, "bmsd v%s starting\n", VERSION); | |
207 | ||
208 | ||
209 | /* | |
210 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
211 | * but that's live. This daemon should only be stopped by SIGTERM. | |
212 | * Don't catch SIGCHLD. | |
213 | */ | |
214 | for (i = 0; i < NSIG; i++) { | |
215 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
216 | signal(i, (void (*))die); | |
217 | } | |
218 | ||
219 | if (debug) { | |
220 | /* | |
221 | * For debugging run in foreground. | |
222 | */ | |
223 | rc = server(); | |
224 | } else { | |
225 | /* | |
226 | * Server initialization is complete. Now we can fork the | |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
227 | * daemon and return to the user. |
0 | 228 | */ |
229 | frk = fork(); | |
230 | switch (frk) { | |
231 | case -1: | |
232 | syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); | |
233 | exit(1); | |
234 | case 0: /* | |
235 | * Run the daemon | |
236 | */ | |
237 | fclose(stdin); | |
238 | if (open("/dev/null", O_RDONLY) != 0) { | |
239 | syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); | |
240 | _exit(2); | |
241 | } | |
242 | fclose(stdout); | |
243 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { | |
244 | syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); | |
245 | _exit(2); | |
246 | } | |
247 | fclose(stderr); | |
248 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { | |
249 | syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); | |
250 | _exit(2); | |
251 | } | |
252 | mypid = getpid(); | |
253 | rc = server(); | |
254 | break; | |
255 | /* Not reached */ | |
256 | default: | |
257 | /* | |
258 | * Here we detach this process and let the child | |
259 | * run the deamon process. | |
260 | */ | |
261 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); | |
262 | exit(0); | |
263 | } | |
264 | } | |
265 | ||
266 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); | |
267 | return rc; | |
268 | } | |
269 | ||
270 |