Thu, 05 Mar 2020 21:19:14 +0100
Added private yeastlab pdf list
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" | |
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" |
0 | 31 | |
32 | ||
33 | int my_shutdown = FALSE; | |
34 | int debug = FALSE; | |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
35 | static pid_t /*pgrp, */mypid; |
0 | 36 | char *Private_Path = NULL; /* Users data path */ |
37 | ||
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
|
38 | extern sys_config Config; |
0 | 39 | |
40 | ||
41 | void help(void) | |
42 | { | |
43 | fprintf(stdout, "bmsd v%s starting\n\n", VERSION); | |
44 | fprintf(stdout, "Usage: bmsd [-d] [-h]\n"); | |
45 | fprintf(stdout, " -d --debug Debug and run in foreground\n"); | |
46 | fprintf(stdout, " -h --help Display this help\n"); | |
47 | } | |
48 | ||
49 | ||
50 | ||
51 | void die(int onsig) | |
52 | { | |
53 | switch (onsig) { | |
54 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); | |
55 | break; | |
56 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); | |
57 | break; | |
58 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); | |
59 | break; | |
60 | case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); | |
61 | my_shutdown = TRUE; | |
62 | exit(SIGSEGV); | |
63 | break; | |
64 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); | |
65 | } | |
66 | my_shutdown = TRUE; | |
67 | } | |
68 | ||
69 | ||
70 | ||
71 | int server(void) { | |
72 | uid_t myuid; | |
73 | struct passwd *mypwd; | |
74 | 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
|
75 | char *tmppath = NULL; |
0 | 76 | |
77 | myuid = getuid(); | |
78 | mypwd = getpwuid(myuid); | |
79 | if (mypwd == NULL) { | |
80 | fprintf(stderr, "[main] Could not find passwd entry\n"); | |
81 | return 1; | |
82 | } | |
83 | ||
84 | Private_Path = xstrcpy(mypwd->pw_dir); | |
85 | Private_Path = xstrcat(Private_Path, (char *)"/.bms"); | |
86 | ||
87 | if (lockprog((char *)"bmsd")) { | |
88 | syslog(LOG_NOTICE, "Can't lock"); | |
89 | return 1; | |
90 | } | |
91 | ||
92 | if (rdconfig()) { | |
93 | rc = 2; | |
94 | goto endit1; | |
95 | } | |
96 | ||
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
|
97 | 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
|
98 | 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
|
99 | 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
|
100 | 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
|
101 | } |
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 | |
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 | /* |
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 | * 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
|
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 | 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
|
107 | 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
|
108 | 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
|
109 | 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
|
110 | 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
|
111 | } |
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 | 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
|
113 | 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
|
114 | 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
|
115 | 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
|
116 | 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
|
117 | 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
|
118 | } |
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 | 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
|
120 | 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
|
121 | |
0 | 122 | if (bms_mysql_init()) { |
123 | rc = 3; | |
124 | goto endit1; | |
125 | } | |
126 | if (mqtt_connect()) { | |
127 | rc = 4; | |
128 | goto endit2; | |
129 | } | |
130 | ||
131 | if (debug) | |
132 | fprintf(stdout, "[main] Entering main loop\n"); | |
133 | ||
134 | while (my_shutdown == FALSE) { | |
135 | ||
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
|
136 | usleep(4000000); |
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
|
137 | 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
|
138 | 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
|
139 | ispindel_mysql_check(); |
0 | 140 | } |
141 | if (debug) | |
142 | fprintf(stdout, "[main] Exit from main loop\n"); | |
143 | ||
144 | /* | |
145 | * Remove our topics and close MQTT connection. | |
146 | */ | |
147 | mqtt_disconnect(); | |
148 | ||
149 | endit2: | |
150 | bms_mysql_end(); | |
151 | ||
152 | endit1: | |
153 | ||
154 | killconfig(); | |
155 | ||
156 | ulockprog((char *)"bmsd"); | |
157 | ||
158 | free(Private_Path); | |
159 | Private_Path = NULL; | |
160 | ||
161 | if (debug) | |
162 | fprintf(stdout, "[main] Server shutdown complete\n"); | |
163 | return rc; | |
164 | } | |
165 | ||
166 | ||
167 | ||
168 | int main(int argc, char *argv[]) { | |
169 | int rc, c, i; | |
170 | pid_t frk; | |
171 | ||
172 | while (1) { | |
173 | int option_index = 0; | |
174 | static struct option long_options[] = { | |
175 | {"debug", 0, 0, 'c'}, | |
176 | {"help", 0, 0, 'h'}, | |
177 | {0, 0, 0, 0} | |
178 | }; | |
179 | ||
180 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
181 | if (c == -1) | |
182 | break; | |
183 | switch (c) { | |
184 | case 'd': debug = TRUE; | |
185 | break; | |
186 | case 'h': help(); | |
187 | return 1; | |
188 | } | |
189 | } | |
190 | ||
191 | openlog("bmsd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); | |
192 | syslog(LOG_NOTICE, "bmsd v%s starting", VERSION); | |
193 | if (debug) | |
194 | fprintf(stdout, "bmsd v%s starting\n", VERSION); | |
195 | ||
196 | ||
197 | /* | |
198 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
199 | * but that's live. This daemon should only be stopped by SIGTERM. | |
200 | * Don't catch SIGCHLD. | |
201 | */ | |
202 | for (i = 0; i < NSIG; i++) { | |
203 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
204 | signal(i, (void (*))die); | |
205 | } | |
206 | ||
207 | if (debug) { | |
208 | /* | |
209 | * For debugging run in foreground. | |
210 | */ | |
211 | rc = server(); | |
212 | } else { | |
213 | /* | |
214 | * Server initialization is complete. Now we can fork the | |
215 | * daemon and return to the user. We need to do a setpgrp | |
216 | * so that the daemon will no longer be assosiated with the | |
217 | * users control terminal. This is done before the fork, so | |
218 | * that the child will not be a process group leader. Otherwise, | |
219 | * if the child were to open a terminal, it would become | |
220 | * associated with that terminal as its control terminal. | |
221 | */ | |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
222 | // if ((pgrp = setpgid(0, 0)) == -1) { |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
223 | // syslog(LOG_NOTICE, "setpgid failed: %s", strerror(errno)); |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
224 | // } |
0 | 225 | |
226 | frk = fork(); | |
227 | switch (frk) { | |
228 | case -1: | |
229 | syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); | |
230 | exit(1); | |
231 | case 0: /* | |
232 | * Run the daemon | |
233 | */ | |
234 | fclose(stdin); | |
235 | if (open("/dev/null", O_RDONLY) != 0) { | |
236 | syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); | |
237 | _exit(2); | |
238 | } | |
239 | fclose(stdout); | |
240 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { | |
241 | syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); | |
242 | _exit(2); | |
243 | } | |
244 | fclose(stderr); | |
245 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { | |
246 | syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); | |
247 | _exit(2); | |
248 | } | |
249 | mypid = getpid(); | |
250 | rc = server(); | |
251 | break; | |
252 | /* Not reached */ | |
253 | default: | |
254 | /* | |
255 | * Here we detach this process and let the child | |
256 | * run the deamon process. | |
257 | */ | |
258 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); | |
259 | exit(0); | |
260 | } | |
261 | } | |
262 | ||
263 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); | |
264 | return rc; | |
265 | } | |
266 | ||
267 |