bmsd/bms.c

Sun, 05 Jan 2020 17:04:56 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 05 Jan 2020 17:04:56 +0100
changeset 579
1253a237b620
parent 573
c4c891d5c4f8
child 671
4b54d6f79d25
permissions
-rw-r--r--

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.

0
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the bms (Brewery Management System)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * bms is distributed in the hope that it will be useful, but
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 #include "bms.h"
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 #include "rdconfig.h"
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 #include "lock.h"
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 #include "mqtt.h"
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 int my_shutdown = FALSE;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 char *Private_Path = NULL; /* Users data path */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 void help(void)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 fprintf(stdout, "bmsd v%s starting\n\n", VERSION);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 fprintf(stdout, "Usage: bmsd [-d] [-h]\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 fprintf(stdout, " -d --debug Debug and run in foreground\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 fprintf(stdout, " -h --help Display this help\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 void die(int onsig)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 switch (onsig) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 my_shutdown = TRUE;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 exit(SIGSEGV);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 default: syslog(LOG_NOTICE, "die() on signal %d", onsig);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 my_shutdown = TRUE;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 int server(void) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 uid_t myuid;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 struct passwd *mypwd;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 myuid = getuid();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 mypwd = getpwuid(myuid);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 if (mypwd == NULL) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 fprintf(stderr, "[main] Could not find passwd entry\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 return 1;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 Private_Path = xstrcpy(mypwd->pw_dir);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 Private_Path = xstrcat(Private_Path, (char *)"/.bms");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 if (lockprog((char *)"bmsd")) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 syslog(LOG_NOTICE, "Can't lock");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 return 1;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 if (rdconfig()) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 rc = 2;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 goto endit1;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 if (bms_mysql_init()) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 rc = 3;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 goto endit1;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 if (mqtt_connect()) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 rc = 4;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 goto endit2;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 if (debug)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 fprintf(stdout, "[main] Entering main loop\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 while (my_shutdown == FALSE) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 if (debug)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 fprintf(stdout, "[main] Exit from main loop\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 * Remove our topics and close MQTT connection.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 mqtt_disconnect();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 endit2:
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 bms_mysql_end();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 endit1:
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 killconfig();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 ulockprog((char *)"bmsd");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
157
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
158 free(Private_Path);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 Private_Path = NULL;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 if (debug)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 fprintf(stdout, "[main] Server shutdown complete\n");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163 return rc;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 int main(int argc, char *argv[]) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 int rc, c, i;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 pid_t frk;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 while (1) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 int option_index = 0;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 static struct option long_options[] = {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 {"debug", 0, 0, 'c'},
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 {"help", 0, 0, 'h'},
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 {0, 0, 0, 0}
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 };
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 c = getopt_long(argc, argv, "dh", long_options, &option_index);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 if (c == -1)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 switch (c) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 case 'd': debug = TRUE;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 case 'h': help();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 return 1;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 openlog("bmsd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192 syslog(LOG_NOTICE, "bmsd v%s starting", VERSION);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 if (debug)
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194 fprintf(stdout, "bmsd v%s starting\n", VERSION);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
198 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199 * but that's live. This daemon should only be stopped by SIGTERM.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200 * Don't catch SIGCHLD.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
202 for (i = 0; i < NSIG; i++) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204 signal(i, (void (*))die);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207 if (debug) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208 /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209 * For debugging run in foreground.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 rc = server();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 } else {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214 * Server initialization is complete. Now we can fork the
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 * daemon and return to the user. We need to do a setpgrp
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216 * so that the daemon will no longer be assosiated with the
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 * users control terminal. This is done before the fork, so
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 * that the child will not be a process group leader. Otherwise,
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 * if the child were to open a terminal, it would become
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220 * associated with that terminal as its control terminal.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226 frk = fork();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 switch (frk) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228 case -1:
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230 exit(1);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231 case 0: /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 * Run the daemon
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 fclose(stdin);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 if (open("/dev/null", O_RDONLY) != 0) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237 _exit(2);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239 fclose(stdout);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 _exit(2);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
244 fclose(stderr);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
247 _exit(2);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
248 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249 mypid = getpid();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
250 rc = server();
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
251 break;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
252 /* Not reached */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
253 default:
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
254 /*
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
255 * Here we detach this process and let the child
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256 * run the deamon process.
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
257 */
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
258 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
259 exit(0);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
260 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
261 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
262
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263 syslog(LOG_NOTICE, "Finished, rc=%d", rc);
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
264 return rc;
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265 }
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
266
033898178630 Versio 0.0.2 initial check in
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267

mercurial