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