Sun, 01 Dec 2019 13:35:32 +0100
In calcWater() the optimum SO4:Cl ratio is taken from the target profile if set, else it's calculated from the BUGU value. Swapped Cl:SO4 to SO4:Cl ratio presentation. Added remark text next to the Optimal SO4:Cl ratio.
0 | 1 | /** |
2 | * @file bms.h | |
3 | */ | |
4 | ||
5 | #ifndef _BMS_H | |
6 | #define _BMS_H | |
7 | ||
8 | #define TRUE 1 | |
9 | #define FALSE 0 | |
10 | ||
11 | #include "../config.h" | |
12 | ||
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <stdint.h> | |
16 | #include <stdarg.h> | |
17 | #include <string.h> | |
18 | #include <ctype.h> | |
19 | #include <sys/types.h> | |
20 | #include <sys/socket.h> | |
21 | #include <sys/stat.h> | |
22 | #include <sys/time.h> | |
23 | #include <sys/un.h> | |
24 | #include <sys/utsname.h> | |
25 | #include <pwd.h> | |
26 | #include <time.h> | |
27 | #include <fcntl.h> | |
28 | #include <syslog.h> | |
29 | #include <unistd.h> | |
30 | #include <errno.h> | |
31 | #include <signal.h> | |
32 | #include <getopt.h> | |
33 | #include <limits.h> | |
34 | #include <sys/socket.h> | |
35 | #include <arpa/inet.h> | |
36 | #include <netdb.h> | |
37 | #include <poll.h> | |
38 | #include <dirent.h> | |
39 | #include <uuid/uuid.h> | |
40 | #include <math.h> | |
41 | #include <assert.h> | |
42 | #include <libgen.h> | |
43 | #include <libxml/xmlmemory.h> | |
44 | #include <libxml/parser.h> | |
45 | #include <libxml/encoding.h> | |
46 | #include <libxml/xmlwriter.h> | |
47 | #include <mosquitto.h> | |
551
57cb71496a9a
Compile fix for Debian Buster.
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
48 | #ifdef HAVE_MYSQL_MYSQL_H |
0 | 49 | #include <mysql/mysql.h> |
551
57cb71496a9a
Compile fix for Debian Buster.
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
50 | #endif |
57cb71496a9a
Compile fix for Debian Buster.
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
51 | #ifdef HAVE_MARIADB_MYSQL_H |
57cb71496a9a
Compile fix for Debian Buster.
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
52 | #include <mariadb/mysql.h> |
57cb71496a9a
Compile fix for Debian Buster.
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
53 | #endif |
0 | 54 | #include <json-c/json.h> |
55 | ||
56 | ||
57 | #define MBSE_SS(x) (x)?(x):"(null)" | |
58 | ||
59 | ||
60 | #define MQTT_NODE_CONTROL 0x0001 ///< Show node control | |
61 | ||
62 | ||
63 | ||
64 | /** | |
65 | * @brief Configuration structure. Stored in a flat ascii file ~/.bms/bms.config | |
66 | */ | |
67 | typedef struct _sys_config { | |
68 | char *bms_name; ///< Brewery name | |
69 | char *bms_uuid; ///< Brewery uuid | |
70 | char *mysql_host; ///< MySQL host | |
71 | int mysql_port; ///< MySQL port | |
72 | char *mysql_user; ///< MySQL user name | |
73 | char *mysql_pass; ///< MySQL password | |
74 | char *mysql_database; ///< MySQL database | |
75 | char *mqtt_host; ///< MQTT host | |
76 | int mqtt_port; ///< MQTT port | |
77 | char *mqtt_user; ///< MQTT username of NULL | |
78 | char *mqtt_pass; ///< MQTT password of NULL | |
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:
192
diff
changeset
|
79 | char *web_root; ///< WWW root directory |
0 | 80 | } sys_config; |
81 | ||
82 | ||
83 | /** | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
84 | * @brief Structure for the nodes. Database table 'mon_nodes'. |
0 | 85 | */ |
86 | typedef struct _sys_node_list { | |
87 | struct _sys_node_list *next; | |
88 | char *uuid; ///< uuid string | |
89 | char *node; ///< nodename | |
90 | bool online; ///< on-line state | |
91 | char *group_id; ///< group type | |
92 | char *hardwaremake; ///< hardware make | |
93 | char *hardwaremodel; ///< hardware model | |
94 | char *os; ///< os type | |
95 | char *os_version; ///< os version | |
96 | char *firmware; ///< application version | |
97 | time_t firstseen; ///< First seen this session | |
98 | time_t lastseen; ///< Last seen this session | |
99 | float temperature; ///< environment temperature | |
100 | float humidity; ///< environment humidity | |
101 | float barometer; ///< environment barometer pressure | |
102 | float gps_latitude; ///< GPS latitude | |
103 | float gps_longitude; ///< GPS longitude | |
104 | float gps_altitude; ///< GPS altitude | |
105 | char *net_address; ///< IPv4 or IPv6 address | |
106 | char *net_ifname; ///< Interface name | |
107 | int net_rssi; ///< RSSI value if wireless. | |
108 | } sys_node_list; | |
109 | ||
110 | ||
111 | /** | |
112 | * @brief Fermenting profile steps | |
113 | */ | |
114 | typedef struct _prof_step { | |
115 | struct _prof_step *next; | |
116 | int steptime; ///< Step time to target in hours | |
117 | int resttime; ///< Rest time on target in hours | |
118 | float target_lo; ///< Low Target temperature | |
119 | float target_hi; ///< High target temperature | |
120 | int fridge_mode; ///< Fridge or beer mode | |
121 | } prof_step; | |
122 | ||
123 | ||
124 | typedef enum | |
125 | { | |
126 | FERMENTER_MODE_OFF = 0, ///< Fermenter is off. | |
127 | FERMENTER_MODE_NONE, ///< Fermenter on but does nothing. | |
128 | FERMENTER_MODE_FRIDGE, ///< Fermenter acts as a fridge. | |
129 | FERMENTER_MODE_BEER, ///< Fermenter acts as beer cooler. | |
130 | FERMENTER_MODE_PROFILE, ///< Fermenter runs in profile mode. | |
131 | } FERMENTER_MODE; | |
132 | ||
133 | typedef enum | |
134 | { | |
135 | FERMENTER_STAGE_PRIMARY = 0, ///< Fermentation primage stage. | |
136 | FERMENTER_STAGE_SECONDARY, ///< Fermenter secondary stage. | |
137 | FERMENTER_STAGE_TERTIARY, ///< Fermenter tertiary stage. | |
34 | 138 | FERMENTER_STAGE_CARBONATION, ///< Fermenter carbonation stage. |
0 | 139 | } FERMENTER_STAGE; |
140 | ||
141 | typedef enum | |
142 | { | |
143 | DEVPRESENT_UNDEF = 0, ///< Precence not detectable. | |
144 | DEVPRESENT_NO, ///< Device is missing. | |
145 | DEVPRESENT_YES, ///< Device is present and Ok. | |
146 | DEVPRESENT_ERROR, ///< Device is present but in error state. | |
147 | } DEVPRESENT_STATE; | |
148 | ||
149 | ||
150 | typedef enum | |
151 | { | |
152 | ALARM_FLAG_DOOR = 0x0001, ///< Door open | |
153 | ALARM_FLAG_PSU = 0x0002, ///< PSU problem | |
154 | ALARM_FLAG_CHILLER = 0x0100, ///< Chiller too warm | |
155 | } ALARM_FLAGS; | |
156 | ||
157 | ||
158 | /** | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
159 | * @brief Structure for the fermenters. Stored in database table 'mon_fermenters'. |
0 | 160 | */ |
161 | typedef struct _fermenter_list { | |
162 | struct _fermenter_list *next; | |
163 | char *uuid; ///< Fixed uuid string | |
164 | char *alias; ///< Fixed short name | |
165 | char *node; ///< Fixed node name | |
166 | bool online; ///< Online status | |
167 | char *beercode; ///< Beer unique code | |
168 | char *beername; ///< Beer name in fermenter | |
192
7f69b43e6084
Implemented beer uuid product in monitor fermenters.
Michiel Broek <mbroek@mbse.eu>
parents:
81
diff
changeset
|
169 | char *beeruuid; ///< Beer uuid in fermenter |
0 | 170 | char *air_address; ///< Air sensor address |
171 | char *air_state; ///< Air sensor state | |
172 | float air_temperature; ///< Air temperature | |
173 | char *beer_address; ///< Beer sensor address | |
174 | char *beer_state; ///< Beer sensor state | |
175 | float beer_temperature; ///< Beer temperature | |
176 | char *chiller_address; ///< Chiller sensor address | |
177 | char *chiller_state; ///< Chiller sensor state | |
178 | float chiller_temperature; ///< Chiller temperature | |
179 | char *heater_address; ///< Heater address | |
180 | int heater_state; ///< Heater state | |
181 | uint64_t heater_usage; ///< Heater usage | |
182 | char *cooler_address; ///< Cooler address | |
183 | int cooler_state; ///< Cooler state | |
184 | uint64_t cooler_usage; ///> Cooler usage | |
185 | char *fan_address; ///< Fan address | |
186 | int fan_state; ///< Fan state | |
187 | uint64_t fan_usage; ///< Fan usage | |
188 | char *light_address; ///< Light address | |
189 | int light_state; ///< Light state | |
190 | uint64_t light_usage; ///< Light usage | |
191 | char *door_address; ///< Door address | |
192 | int door_state; ///< Door state | |
193 | char *psu_address; ///< PSU address | |
194 | int psu_state; ///< PSU state | |
195 | char *mode; ///< Fermenter mode | |
196 | char *stage; ///< Fermentation stage | |
197 | uint32_t alarm; ///< Alarm flag | |
198 | float setpoint_high; ///< Setpoint high | |
199 | float setpoint_low; ///< Setpoint low | |
200 | char *profile_uuid; ///< Profile uuid | |
201 | char *profile_name; ///< Profile name | |
202 | char *profile_state; ///< Profile state | |
203 | int profile_percent; ///< Profile percent done | |
204 | float profile_inittemp_high; ///< Profile init temp high | |
205 | float profile_inittemp_low; ///< Profile init temp low | |
206 | char *profile_steps; ///< Profile steps in JSON | |
299
047ead629d4a
Accept yeast temperature limits from thermferm via MQTT. Send yeat temperature ranges to thermferm together with the beer parameters. Store yeast temperature limits in the mon_fermenters database table. The monitor fermenters screen adjusts the temperature color ranges.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
207 | float yeast_lo; ///< Yeast minimum temperature |
047ead629d4a
Accept yeast temperature limits from thermferm via MQTT. Send yeat temperature ranges to thermferm together with the beer parameters. Store yeast temperature limits in the mon_fermenters database table. The monitor fermenters screen adjusts the temperature color ranges.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
208 | float yeast_hi; ///< Yeast maximum temperature |
334
26c6f8e5d58e
Added webcam_url and webcam_light fields to the mon_fermenters database table. Currently these fields need to be set using phpMyAdmin or another tool. If the webcam_url field is set, then on the mon_fermenter webpage a button is added to show a webcam page.
Michiel Broek <mbroek@mbse.eu>
parents:
299
diff
changeset
|
209 | char *webcam_url; ///< Webcam url to the stream |
26c6f8e5d58e
Added webcam_url and webcam_light fields to the mon_fermenters database table. Currently these fields need to be set using phpMyAdmin or another tool. If the webcam_url field is set, then on the mon_fermenter webpage a button is added to show a webcam page.
Michiel Broek <mbroek@mbse.eu>
parents:
299
diff
changeset
|
210 | int webcam_light; ///< Webcam control lights |
0 | 211 | } sys_fermenter_list; |
212 | ||
213 | ||
214 | ||
215 | typedef enum | |
216 | { | |
217 | PROFILE_OFF = 0, ///< Profile not active | |
218 | PROFILE_PAUSE, ///< Profile pause | |
219 | PROFILE_RUN, ///< Profile is running | |
220 | PROFILE_DONE, ///< Profile is finished | |
221 | PROFILE_ABORT, ///< Profile abort | |
222 | } PROFILE_STATE; | |
223 | ||
224 | ||
225 | ||
226 | /** | |
227 | * @brief Strcuture holding a fermentation log entry. | |
228 | */ | |
229 | typedef struct fermenting_log { | |
230 | char *datetime; ///< Date/time stamp | |
231 | char *product_uuid; ///< Product uuid | |
232 | char *product_code; ///< Product code | |
233 | char *product_name; ///< Product name | |
234 | float temperature_air; ///< Temperature air | |
235 | float temperature_beer; ///< Temperature beer | |
236 | float temperature_chiller; ///< Temperature chiller (if pressent) | |
237 | float temperature_room; ///< Temperature room | |
238 | int heater_power; ///< Heater power 0..100 | |
239 | uint64_t heater_usage; ///< Heater usage counter in seconds. | |
240 | int cooler_power; ///< Cooler power 0..100 | |
241 | uint64_t cooler_usage; ///< Cooler usage counter in seconds. | |
242 | int fan_power; ///< Fan power 0 or 100 | |
243 | uint64_t fan_usage; ///< Fan usage counter in seconds | |
244 | float setpoint_low; ///< Target temperature low | |
245 | float setpoint_high; ///< Tarhet temperature high | |
246 | char *mode; ///< Working mode. | |
247 | char *stage; ///< Fermentation stage | |
248 | char *event; ///< Optional event | |
249 | char *fermenter_uuid; ///< Fermenter in use | |
250 | char *fermenter_node; ///< Fermenter node | |
251 | char *fermenter_alias; ///< Fermenter_alias | |
252 | } fermentation_log; | |
253 | ||
254 | ||
255 | ||
256 | /** | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
257 | * @brief Structure for the co2meters. Stored in database table 'mon_co2meters'. |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
258 | */ |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
259 | typedef struct _co2meter_list { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
260 | struct _co2meter_list *next; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
261 | char *uuid; ///< Fixed uuid string |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
262 | char *alias; ///< Fixed short name |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
263 | char *node; ///< Fixed node name |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
264 | bool online; ///< Online status |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
265 | char *mode; ///< Working mode |
505
c09b67fd8323
Added CO2 meter units alarm led.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
266 | uint32_t alarm; ///< Alarm bits |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
267 | char *beercode; ///< Beer unique code |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
268 | char *beername; ///< Beer name in fermenter |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
269 | char *beeruuid; ///< Beer uuid in fermenter |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
270 | char *temperature_state; ///< Temperature sensor state |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
271 | char *temperature_address; ///< Temperature sensor address |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
272 | float temperature; ///< Temperature value |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
273 | char *pressure_state; ///< Pressure sensor state |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
274 | uint8_t pressure_channel; ///< Pressure sensor channel |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
275 | float pressure_voltage; ///< Pressure voltage reading |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
276 | float pressure_zero; ///< Pressure zero offset voltage |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
277 | float pressure_bar; ///< Pressure in bar |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
278 | } sys_co2meter_list; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
279 | |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
280 | |
506
8ab0e87d579e
Added beer selection in the CO2 meter screen. Added CO2 pressure logging.
Michiel Broek <mbroek@mbse.eu>
parents:
505
diff
changeset
|
281 | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
282 | /** |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
283 | * @brief Strcuture holding a co2 pressure log entry. |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
284 | */ |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
285 | typedef struct co2meter_log { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
286 | char *datetime; ///< Date/time stamp |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
287 | char *product_uuid; ///< Product uuid |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
288 | char *product_code; ///< Product code |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
289 | char *product_name; ///< Product name |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
290 | float temperature; ///< Temperature |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
291 | float pressure; ///< Pressure |
506
8ab0e87d579e
Added beer selection in the CO2 meter screen. Added CO2 pressure logging.
Michiel Broek <mbroek@mbse.eu>
parents:
505
diff
changeset
|
292 | char *uuid; ///< Unit uuid |
8ab0e87d579e
Added beer selection in the CO2 meter screen. Added CO2 pressure logging.
Michiel Broek <mbroek@mbse.eu>
parents:
505
diff
changeset
|
293 | char *node; ///< Unit node |
8ab0e87d579e
Added beer selection in the CO2 meter screen. Added CO2 pressure logging.
Michiel Broek <mbroek@mbse.eu>
parents:
505
diff
changeset
|
294 | char *alias; ///< Unit alias |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
295 | } co2pressure_log; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
296 | |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
297 | |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
298 | |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
334
diff
changeset
|
299 | /** |
0 | 300 | * @brief Brew computer controllers. Must have hardware + installation + recipe data. |
301 | */ | |
302 | typedef struct _brewer_list { | |
303 | struct _brewer_list *next; | |
304 | char *uuid; | |
305 | } brewer_list; | |
306 | ||
307 | ||
308 | // Make it universal and make it connectable with a fermenter. | |
309 | typedef struct _ispindel_list { | |
310 | struct _ispindel_list *next; | |
311 | char *uuid; ///< Fixed uuid string | |
312 | char *name; ///< Name or description (Red iSpindle). | |
313 | char *beercode; ///< Beer code if in use. | |
314 | float temperature; ///< Temperature of the beer. | |
315 | float gravity; ///< Measured gravity | |
316 | // What nore, battery? | |
317 | } ispindel_list; | |
318 | ||
319 | ||
320 | // Hergisting meters. | |
321 | ||
322 | /** | |
323 | * @brief Standalone temperature loggers. (Freezers, refrigerators, chambers). | |
324 | */ | |
325 | typedef struct _thb_list { | |
326 | struct _thb_list *next; | |
327 | char *uuit; ///< Fixed uuid string | |
328 | char *name; ///< Name or description | |
329 | char *beercode; ///< Beer code if needed. | |
330 | float temperature; ///< Temperature in Celcius | |
331 | float humidity; ///< Humidity in % | |
332 | float barometer; ///< Air pressure. | |
333 | float gps_latitude; ///< GPS latitude | |
334 | float gps_longitude; ///< GPS longitide | |
335 | float gps_altitude; ///< GPS altitude | |
336 | } thb_list; | |
337 | ||
338 | ||
339 | #endif |