Tue, 12 May 2020 14:24:45 +0200
Prevent repeated fermenter offline broadcasts
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(); |
579
1253a237b620
Added check for database changes for iSpindels such as a new beer, or changing mode. Added denity in Plato and SG to the iSpindel web screen. The iSpindels must be calibrated in Plato! A bit of documentation updated.
Michiel Broek <mbroek@mbse.eu>
parents:
573
diff
changeset
|
149 | usleep(1000000); |
1253a237b620
Added check for database changes for iSpindels such as a new beer, or changing mode. Added denity in Plato and SG to the iSpindel web screen. The iSpindels must be calibrated in Plato! A bit of documentation updated.
Michiel Broek <mbroek@mbse.eu>
parents:
573
diff
changeset
|
150 | ispindel_mysql_check(); |
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
|
151 | usleep(1000000); |
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
|
152 | ws_check(); |
0 | 153 | } |
154 | if (debug) | |
155 | fprintf(stdout, "[main] Exit from main loop\n"); | |
156 | ||
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
|
157 | endit3: |
0 | 158 | /* |
159 | * Remove our topics and close MQTT connection. | |
160 | */ | |
161 | mqtt_disconnect(); | |
162 | ||
163 | endit2: | |
164 | bms_mysql_end(); | |
165 | ||
166 | endit1: | |
167 | ||
168 | killconfig(); | |
169 | ||
170 | ulockprog((char *)"bmsd"); | |
171 | ||
172 | free(Private_Path); | |
173 | Private_Path = NULL; | |
174 | ||
175 | if (debug) | |
176 | fprintf(stdout, "[main] Server shutdown complete\n"); | |
177 | return rc; | |
178 | } | |
179 | ||
180 | ||
181 | ||
182 | int main(int argc, char *argv[]) { | |
183 | int rc, c, i; | |
184 | pid_t frk; | |
185 | ||
186 | while (1) { | |
187 | int option_index = 0; | |
188 | static struct option long_options[] = { | |
189 | {"debug", 0, 0, 'c'}, | |
190 | {"help", 0, 0, 'h'}, | |
191 | {0, 0, 0, 0} | |
192 | }; | |
193 | ||
194 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
195 | if (c == -1) | |
196 | break; | |
197 | switch (c) { | |
198 | case 'd': debug = TRUE; | |
199 | break; | |
200 | case 'h': help(); | |
201 | return 1; | |
202 | } | |
203 | } | |
204 | ||
205 | openlog("bmsd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); | |
206 | syslog(LOG_NOTICE, "bmsd v%s starting", VERSION); | |
207 | if (debug) | |
208 | fprintf(stdout, "bmsd v%s starting\n", VERSION); | |
209 | ||
210 | ||
211 | /* | |
212 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
213 | * but that's live. This daemon should only be stopped by SIGTERM. | |
214 | * Don't catch SIGCHLD. | |
215 | */ | |
216 | for (i = 0; i < NSIG; i++) { | |
217 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
218 | signal(i, (void (*))die); | |
219 | } | |
220 | ||
221 | if (debug) { | |
222 | /* | |
223 | * For debugging run in foreground. | |
224 | */ | |
225 | rc = server(); | |
226 | } else { | |
227 | /* | |
228 | * 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
|
229 | * daemon and return to the user. |
0 | 230 | */ |
231 | frk = fork(); | |
232 | switch (frk) { | |
233 | case -1: | |
234 | syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); | |
235 | exit(1); | |
236 | case 0: /* | |
237 | * Run the daemon | |
238 | */ | |
239 | fclose(stdin); | |
240 | if (open("/dev/null", O_RDONLY) != 0) { | |
241 | syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); | |
242 | _exit(2); | |
243 | } | |
244 | fclose(stdout); | |
245 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { | |
246 | syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); | |
247 | _exit(2); | |
248 | } | |
249 | fclose(stderr); | |
250 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { | |
251 | syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); | |
252 | _exit(2); | |
253 | } | |
254 | mypid = getpid(); | |
255 | rc = server(); | |
256 | break; | |
257 | /* Not reached */ | |
258 | default: | |
259 | /* | |
260 | * Here we detach this process and let the child | |
261 | * run the deamon process. | |
262 | */ | |
263 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); | |
264 | exit(0); | |
265 | } | |
266 | } | |
267 | ||
268 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); | |
269 | return rc; | |
270 | } | |
271 | ||
272 |