|
1 /** |
|
2 * @file co2meters.c |
|
3 * @brief Handle co2meters status |
|
4 * @author Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * Copyright (C) 2019 |
|
7 * |
|
8 * This file is part of the bms (Brewery Management System) |
|
9 * |
|
10 * This is free software; you can redistribute it and/or modify it |
|
11 * under the terms of the GNU General Public License as published by the |
|
12 * Free Software Foundation; either version 2, or (at your option) any |
|
13 * later version. |
|
14 * |
|
15 * bms is distributed in the hope that it will be useful, but |
|
16 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 * General Public License for more details. |
|
19 * |
|
20 * You should have received a copy of the GNU General Public License |
|
21 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
22 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
23 */ |
|
24 |
|
25 |
|
26 #include "bms.h" |
|
27 #include "xutil.h" |
|
28 #include "co2meters.h" |
|
29 #include "mysql.h" |
|
30 |
|
31 |
|
32 sys_co2meter_list *co2meters = NULL; |
|
33 |
|
34 extern int debug; |
|
35 extern sys_config Config; |
|
36 extern MYSQL *con; |
|
37 extern MYSQL_RES *res_set; |
|
38 extern MYSQL_ROW row; |
|
39 |
|
40 |
|
41 |
|
42 void co2meter_set(char *edge_node, char *alias, char *payload) |
|
43 { |
|
44 struct json_object *jobj, *val, *sensor; |
|
45 sys_co2meter_list *co2meter, *tmpp; |
|
46 bool new_co2meter = true; |
|
47 |
|
48 // fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload); |
|
49 |
|
50 /* |
|
51 * Search co2meter record in the memory array and use it if found. |
|
52 */ |
|
53 if (co2meters) { |
|
54 for (tmpp = co2meters; tmpp; tmpp = tmpp->next) { |
|
55 if ((strcmp(tmpp->alias, alias) == 0) && (strcmp(tmpp->node, edge_node) == 0)) { |
|
56 new_co2meter = false; |
|
57 co2meter = tmpp; |
|
58 break; |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 // printf("new_co2meter %s\n", new_co2meter ? "true":"false"); |
|
64 |
|
65 /* |
|
66 * Allocate new co2meter if not yet known. |
|
67 */ |
|
68 if (new_co2meter) { |
|
69 co2meter = (sys_co2meter_list *)malloc(sizeof(sys_co2meter_list)); |
|
70 memset(co2meter, 0, sizeof(sys_co2meter_list)); |
|
71 co2meter->alias = xstrcpy(alias); |
|
72 co2meter->node = xstrcpy(edge_node); |
|
73 co2meter->mode = xstrcpy((char *)"OFF"); |
|
74 } |
|
75 |
|
76 if (! co2meter->online) { |
|
77 co2meter->online = true; |
|
78 syslog(LOG_NOTICE, "Online co2meter %s/%s mode %s", edge_node, alias, co2meter->mode); |
|
79 } |
|
80 |
|
81 /* |
|
82 * Process the JSON formatted payload. |
|
83 * Update only the fields that are found in the payload. |
|
84 */ |
|
85 jobj = json_tokener_parse(payload); |
|
86 |
|
87 if (json_object_object_get_ex(jobj, "uuid", &val)) { |
|
88 if (co2meter->uuid) |
|
89 free(co2meter->uuid); |
|
90 co2meter->uuid = xstrcpy((char *)json_object_get_string(val)); |
|
91 } |
|
92 if (json_object_object_get_ex(jobj, "mode", &val)) { |
|
93 if (co2meter->mode) { |
|
94 if (strcmp(co2meter->mode, (char *)json_object_get_string(val))) { |
|
95 syslog(LOG_NOTICE, "Change mode co2meter %s/%s: %s to %s", edge_node, alias, co2meter->mode, (char *)json_object_get_string(val)); |
|
96 } |
|
97 free(co2meter->mode); |
|
98 } |
|
99 co2meter->mode = xstrcpy((char *)json_object_get_string(val)); |
|
100 } |
|
101 if (json_object_object_get_ex(jobj, "alarm", &val)) { |
|
102 co2meter->alarm = json_object_get_int(val); |
|
103 } |
|
104 if (json_object_object_get_ex(jobj, "temperature", &sensor)) { |
|
105 if (json_object_object_get_ex(sensor, "address", &val)) { |
|
106 if (co2meter->temperature_address) |
|
107 free(co2meter->temperature_address); |
|
108 co2meter->temperature_address = xstrcpy((char *)json_object_get_string(val)); |
|
109 } |
|
110 if (json_object_object_get_ex(sensor, "state", &val)) { |
|
111 if (co2meter->temperature_state) |
|
112 free(co2meter->temperature_state); |
|
113 co2meter->temperature_state = xstrcpy((char *)json_object_get_string(val)); |
|
114 } |
|
115 if (json_object_object_get_ex(sensor, "temperature", &val)) { |
|
116 co2meter->temperature = json_object_get_double(val); |
|
117 } |
|
118 } |
|
119 if (json_object_object_get_ex(jobj, "pressure", &sensor)) { |
|
120 if (json_object_object_get_ex(sensor, "state", &val)) { |
|
121 if (co2meter->pressure_state) |
|
122 free(co2meter->pressure_state); |
|
123 co2meter->pressure_state = xstrcpy((char *)json_object_get_string(val)); |
|
124 } |
|
125 if (json_object_object_get_ex(sensor, "channel", &val)) { |
|
126 co2meter->pressure_channel = json_object_get_int(val); |
|
127 } |
|
128 if (json_object_object_get_ex(sensor, "voltage", &val)) { |
|
129 co2meter->pressure_voltage = json_object_get_double(val); |
|
130 } |
|
131 if (json_object_object_get_ex(sensor, "zero", &val)) { |
|
132 co2meter->pressure_zero = json_object_get_double(val); |
|
133 } |
|
134 if (json_object_object_get_ex(sensor, "bar", &val)) { |
|
135 co2meter->pressure_bar = json_object_get_double(val); |
|
136 } |
|
137 } |
|
138 json_object_put(jobj); |
|
139 |
|
140 // co2meter_dump(co2meter); |
|
141 |
|
142 if (new_co2meter) { |
|
143 if (co2meters == NULL) { |
|
144 co2meters = co2meter; |
|
145 } else { |
|
146 for (tmpp = co2meters; tmpp; tmpp = tmpp->next) { |
|
147 if (tmpp->next == NULL) { |
|
148 tmpp->next = co2meter; |
|
149 break; |
|
150 } |
|
151 } |
|
152 } |
|
153 co2meter_mysql_insert(co2meter); |
|
154 } else { |
|
155 co2meter_mysql_update(co2meter); |
|
156 } |
|
157 |
|
158 } |
|
159 |
|
160 |
|
161 |
|
162 /* |
|
163 * With DBIRTH all active co2meters are publishd in an array. |
|
164 */ |
|
165 void co2meter_birth_data(char *topic, char *payload) |
|
166 { |
|
167 char *message_type, *edge_node, *alias; |
|
168 struct json_object *jobj, *val, *metric, *units, *unit; |
|
169 int arraylen; |
|
170 |
|
171 strtok(topic, "/"); // ignore namespace |
|
172 strtok(NULL, "/"); |
|
173 message_type = strtok(NULL, "/"); |
|
174 edge_node = strtok(NULL, "/\0"); |
|
175 alias = strtok(NULL, "/\0"); |
|
176 |
|
177 if ((alias == NULL) && (strcmp("DBIRTH", message_type) == 0)) { |
|
178 /* |
|
179 * Global initial DBIRTH message with array of co2meters. |
|
180 */ |
|
181 jobj = json_tokener_parse(payload); |
|
182 |
|
183 if (json_object_object_get_ex(jobj, "metric", &metric)) { |
|
184 if (json_object_object_get_ex(metric, "units", &units)) { |
|
185 arraylen = json_object_array_length(units); |
|
186 for (int i = 0; i < arraylen; i++) { |
|
187 /* |
|
188 * Parse the array of units |
|
189 */ |
|
190 unit = json_object_array_get_idx(units, i); |
|
191 |
|
192 if (json_object_object_get_ex(unit, "alias", &val)) { |
|
193 if (alias) |
|
194 free(alias); |
|
195 alias = xstrcpy((char *)json_object_get_string(val)); |
|
196 co2meter_set(edge_node, alias, (char *)json_object_to_json_string_ext(unit, 0)); |
|
197 free(alias); |
|
198 alias = NULL; |
|
199 } |
|
200 } |
|
201 } |
|
202 } |
|
203 json_object_put(jobj); |
|
204 return; |
|
205 } |
|
206 |
|
207 /* |
|
208 * The rest are errors. |
|
209 */ |
|
210 printf("ERROR co2meter_birth_data: %s %s %s\n", message_type, edge_node, alias); |
|
211 } |
|
212 |
|
213 |
|
214 |
|
215 void co2meter_log(char *topic, char *payload) |
|
216 { |
|
217 char *edge_node, *alias, *line, buf[128], *logfile; |
|
218 struct json_object *jobj, *val, *metric; |
|
219 co2pressure_log *log; |
|
220 struct tm *mytime; |
|
221 time_t timestamp; |
|
222 FILE *fp; |
|
223 |
|
224 strtok(topic, "/"); // ignore namespace |
|
225 strtok(NULL, "/"); // group_id |
|
226 strtok(NULL, "/"); // message_type |
|
227 edge_node = strtok(NULL, "/\0"); |
|
228 alias = strtok(NULL, "/\0"); |
|
229 |
|
230 log = (co2pressure_log *)malloc(sizeof(co2pressure_log)); |
|
231 memset(log, 0, sizeof(co2pressure_log)); |
|
232 |
|
233 log->node = xstrcpy(edge_node); |
|
234 log->alias = xstrcpy(alias); |
|
235 jobj = json_tokener_parse(payload); |
|
236 |
|
237 timestamp = time(NULL); |
|
238 log->datetime = malloc(21); |
|
239 mytime = localtime(×tamp); |
|
240 snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d", |
|
241 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
|
242 |
|
243 if (json_object_object_get_ex(jobj, "metric", &metric)) { |
|
244 if (json_object_object_get_ex(metric, "uuid", &val)) { |
|
245 if (strcmp((char *)"(null)", json_object_get_string(val))) |
|
246 log->uuid = xstrcpy((char *)json_object_get_string(val)); |
|
247 } |
|
248 if (json_object_object_get_ex(metric, "temperature", &val)) { |
|
249 log->temperature = json_object_get_double(val); |
|
250 } |
|
251 if (json_object_object_get_ex(metric, "pressure", &val)) { |
|
252 log->pressure = json_object_get_double(val); |
|
253 } |
|
254 } |
|
255 json_object_put(jobj); |
|
256 |
|
257 /* |
|
258 * Because co2meters are not so smart and don't hold product information |
|
259 * search the missing pieces in the database. |
|
260 */ |
|
261 snprintf(buf, 127, "SELECT beercode,beername,beeruuid FROM mon_co2meters WHERE uuid='%s'", log->uuid); |
|
262 if (mysql_query(con, buf)) { |
|
263 syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", buf, mysql_errno(con), mysql_error(con)); |
|
264 } else { |
|
265 res_set = mysql_store_result(con); |
|
266 if (res_set == NULL) { |
|
267 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con)); |
|
268 } else { |
|
269 if ((row = mysql_fetch_row(res_set)) != NULL) { |
|
270 log->product_code = xstrcpy(row[0]); |
|
271 log->product_name = xstrcpy(row[1]); |
|
272 log->product_uuid = xstrcpy(row[2]); |
|
273 } |
|
274 } |
|
275 } |
|
276 |
|
277 /* |
|
278 * Build csv log line |
|
279 */ |
|
280 line = xstrcpy(log->datetime); |
|
281 line = xstrcat(line, (char *)","); |
|
282 snprintf(buf, 64, "%.3f", log->temperature); |
|
283 line = xstrcat(line, buf); |
|
284 line = xstrcat(line, (char *)","); |
|
285 snprintf(buf, 64, "%.3f", log->pressure); |
|
286 line = xstrcat(line, buf); |
|
287 line = xstrcat(line, (char *)","); |
|
288 line = xstrcat(line, log->uuid); |
|
289 |
|
290 /* |
|
291 * Build logfile name |
|
292 */ |
|
293 logfile = xstrcpy(Config.web_root); |
|
294 logfile = xstrcat(logfile, (char *)"/log/co2pressure/"); |
|
295 logfile = xstrcat(logfile, log->product_code); |
|
296 logfile = xstrcat(logfile, (char *)" "); |
|
297 logfile = xstrcat(logfile, log->product_name); |
|
298 logfile = xstrcat(logfile, (char *)".log"); |
|
299 |
|
300 if (debug) |
|
301 fprintf(stdout, "%s %s\n", logfile, line); |
|
302 |
|
303 fp = fopen(logfile, "a"); |
|
304 if (fp) { |
|
305 fprintf(fp, "%s\n", line); |
|
306 fclose(fp); |
|
307 } else { |
|
308 syslog(LOG_NOTICE, "cannot append to `%s'", logfile); |
|
309 } |
|
310 |
|
311 free(logfile); |
|
312 logfile = NULL; |
|
313 free(line); |
|
314 line = NULL; |
|
315 |
|
316 if (log->datetime) |
|
317 free(log->datetime); |
|
318 if (log->product_uuid ) |
|
319 free(log->product_uuid ); |
|
320 if (log->product_code ) |
|
321 free(log->product_code ); |
|
322 if (log->product_name ) |
|
323 free(log->product_name ); |
|
324 if (log->uuid) |
|
325 free(log->uuid); |
|
326 if (log->node) |
|
327 free(log->node); |
|
328 if (log->alias) |
|
329 free(log->alias); |
|
330 free(log); |
|
331 } |
|
332 |
|
333 |
|
334 |
|
335 void co2meter_dump(sys_co2meter_list *co2meter) |
|
336 { |
|
337 if (debug) { |
|
338 printf("uuid %s\n", co2meter->uuid); |
|
339 printf("alias %s\n", co2meter->alias); |
|
340 printf("node %s\n", co2meter->node); |
|
341 printf("online %s\n", co2meter->online ? "yes":"no"); |
|
342 printf("product %s / %s\n", co2meter->beercode, co2meter->beername); |
|
343 printf("Temperature %-16s %10s %8.3f\n", co2meter->temperature_address, co2meter->temperature_state, co2meter->temperature); |
|
344 printf("Pressure %10s %d %.3f %.3f %.3f\n", co2meter->pressure_state, co2meter->pressure_channel, |
|
345 co2meter->pressure_voltage, co2meter->pressure_zero, co2meter->pressure_bar); |
|
346 printf("mode %s\n", co2meter->mode); |
|
347 } |
|
348 } |
|
349 |
|
350 |