bmsd/co2meters.c

changeset 502
a8a6901b5a99
child 505
c09b67fd8323
equal deleted inserted replaced
501:9c41e865144a 502:a8a6901b5a99
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
37
38
39 void co2meter_set(char *edge_node, char *alias, char *payload)
40 {
41 struct json_object *jobj, *val, *sensor;
42 sys_co2meter_list *co2meter, *tmpp;
43 bool new_co2meter = true;
44
45 // fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload);
46
47 /*
48 * Search co2meter record in the memory array and use it if found.
49 */
50 if (co2meters) {
51 for (tmpp = co2meters; tmpp; tmpp = tmpp->next) {
52 if ((strcmp(tmpp->alias, alias) == 0) && (strcmp(tmpp->node, edge_node) == 0)) {
53 new_co2meter = false;
54 co2meter = tmpp;
55 break;
56 }
57 }
58 }
59
60 // printf("new_co2meter %s\n", new_co2meter ? "true":"false");
61
62 /*
63 * Allocate new co2meter if not yet known.
64 */
65 if (new_co2meter) {
66 co2meter = (sys_co2meter_list *)malloc(sizeof(sys_co2meter_list));
67 memset(co2meter, 0, sizeof(sys_co2meter_list));
68 co2meter->alias = xstrcpy(alias);
69 co2meter->node = xstrcpy(edge_node);
70 co2meter->mode = xstrcpy((char *)"OFF");
71 }
72
73 if (! co2meter->online) {
74 co2meter->online = true;
75 syslog(LOG_NOTICE, "Online co2meter %s/%s mode %s", edge_node, alias, co2meter->mode);
76 }
77
78 /*
79 * Process the JSON formatted payload.
80 * Update only the fields that are found in the payload.
81 */
82 jobj = json_tokener_parse(payload);
83
84 if (json_object_object_get_ex(jobj, "uuid", &val)) {
85 if (co2meter->uuid)
86 free(co2meter->uuid);
87 co2meter->uuid = xstrcpy((char *)json_object_get_string(val));
88 }
89 if (json_object_object_get_ex(jobj, "mode", &val)) {
90 if (co2meter->mode) {
91 if (strcmp(co2meter->mode, (char *)json_object_get_string(val))) {
92 syslog(LOG_NOTICE, "Change mode co2meter %s/%s: %s to %s", edge_node, alias, co2meter->mode, (char *)json_object_get_string(val));
93 }
94 free(co2meter->mode);
95 }
96 co2meter->mode = xstrcpy((char *)json_object_get_string(val));
97 }
98 if (json_object_object_get_ex(jobj, "temperature", &sensor)) {
99 if (json_object_object_get_ex(sensor, "address", &val)) {
100 if (co2meter->temperature_address)
101 free(co2meter->temperature_address);
102 co2meter->temperature_address = xstrcpy((char *)json_object_get_string(val));
103 }
104 if (json_object_object_get_ex(sensor, "state", &val)) {
105 if (co2meter->temperature_state)
106 free(co2meter->temperature_state);
107 co2meter->temperature_state = xstrcpy((char *)json_object_get_string(val));
108 }
109 if (json_object_object_get_ex(sensor, "temperature", &val)) {
110 co2meter->temperature = json_object_get_double(val);
111 }
112 }
113 if (json_object_object_get_ex(jobj, "pressure", &sensor)) {
114 if (json_object_object_get_ex(sensor, "state", &val)) {
115 if (co2meter->pressure_state)
116 free(co2meter->pressure_state);
117 co2meter->pressure_state = xstrcpy((char *)json_object_get_string(val));
118 }
119 if (json_object_object_get_ex(sensor, "channel", &val)) {
120 co2meter->pressure_channel = json_object_get_int(val);
121 }
122 if (json_object_object_get_ex(sensor, "voltage", &val)) {
123 co2meter->pressure_voltage = json_object_get_double(val);
124 }
125 if (json_object_object_get_ex(sensor, "zero", &val)) {
126 co2meter->pressure_zero = json_object_get_double(val);
127 }
128 if (json_object_object_get_ex(sensor, "bar", &val)) {
129 co2meter->pressure_bar = json_object_get_double(val);
130 }
131 }
132 json_object_put(jobj);
133
134 // co2meter_dump(co2meter);
135
136 if (new_co2meter) {
137 if (co2meters == NULL) {
138 co2meters = co2meter;
139 } else {
140 for (tmpp = co2meters; tmpp; tmpp = tmpp->next) {
141 if (tmpp->next == NULL) {
142 tmpp->next = co2meter;
143 break;
144 }
145 }
146 }
147 co2meter_mysql_insert(co2meter);
148 } else {
149 co2meter_mysql_update(co2meter);
150 }
151
152 }
153
154
155
156 /*
157 * With DBIRTH all active co2meters are publishd in an array.
158 */
159 void co2meter_birth_data(char *topic, char *payload)
160 {
161 char *message_type, *edge_node, *alias;
162 struct json_object *jobj, *val, *metric, *units, *unit;
163 int arraylen;
164
165 strtok(topic, "/"); // ignore namespace
166 strtok(NULL, "/");
167 message_type = strtok(NULL, "/");
168 edge_node = strtok(NULL, "/\0");
169 alias = strtok(NULL, "/\0");
170
171 if ((alias == NULL) && (strcmp("DBIRTH", message_type) == 0)) {
172 /*
173 * Global initial DBIRTH message with array of co2meters.
174 */
175 jobj = json_tokener_parse(payload);
176
177 if (json_object_object_get_ex(jobj, "metric", &metric)) {
178 if (json_object_object_get_ex(metric, "units", &units)) {
179 arraylen = json_object_array_length(units);
180 for (int i = 0; i < arraylen; i++) {
181 /*
182 * Parse the array of units
183 */
184 unit = json_object_array_get_idx(units, i);
185
186 if (json_object_object_get_ex(unit, "alias", &val)) {
187 if (alias)
188 free(alias);
189 alias = xstrcpy((char *)json_object_get_string(val));
190 co2meter_set(edge_node, alias, (char *)json_object_to_json_string_ext(unit, 0));
191 free(alias);
192 alias = NULL;
193 }
194 }
195 }
196 }
197 json_object_put(jobj);
198 return;
199 }
200
201 /*
202 * The rest are errors.
203 */
204 printf("ERROR co2meter_birth_data: %s %s %s\n", message_type, edge_node, alias);
205 }
206
207
208
209 void co2meter_log(char *topic, char *payload)
210 {
211 char *edge_node, *alias, *line, buf[65], *logfile;
212 struct json_object *jobj, *val, *metric;
213 co2pressure_log *log;
214 struct tm *mytime;
215 time_t timestamp;
216 FILE *fp;
217
218 strtok(topic, "/"); // ignore namespace
219 strtok(NULL, "/"); // group_id
220 strtok(NULL, "/"); // message_type
221 edge_node = strtok(NULL, "/\0");
222 alias = strtok(NULL, "/\0");
223
224 log = (co2pressure_log *)malloc(sizeof(co2pressure_log));
225 memset(log, 0, sizeof(co2pressure_log));
226
227 log->co2meter_node = xstrcpy(edge_node);
228 log->co2meter_alias = xstrcpy(alias);
229 jobj = json_tokener_parse(payload);
230
231 timestamp = time(NULL);
232 log->datetime = malloc(21);
233 mytime = localtime(&timestamp);
234 snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d",
235 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
236
237 if (json_object_object_get_ex(jobj, "metric", &metric)) {
238
239 /* if (json_object_object_get_ex(metric2, "uuid", &val)) {
240 if (strcmp((char *)"(null)", json_object_get_string(val)))
241 log->product_uuid = xstrcpy((char *)json_object_get_string(val));
242 }
243 if (json_object_object_get_ex(metric2, "code", &val)) {
244 if (strcmp((char *)"(null)", json_object_get_string(val)))
245 log->product_code = xstrcpy((char *)json_object_get_string(val));
246 }
247 if (json_object_object_get_ex(metric2, "name", &val)) {
248 if (strcmp((char *)"(null)", json_object_get_string(val)))
249 log->product_name = xstrcpy((char *)json_object_get_string(val));
250 }
251 */
252 if (json_object_object_get_ex(metric, "co2meter_uuid", &val)) {
253 if (strcmp((char *)"(null)", json_object_get_string(val)))
254 log->co2meter_uuid = xstrcpy((char *)json_object_get_string(val));
255 }
256 if (json_object_object_get_ex(metric, "temperature", &val)) {
257 log->temperature = json_object_get_double(val);
258 }
259 if (json_object_object_get_ex(metric, "pressure", &val)) {
260 log->pressure = json_object_get_double(val);
261 }
262 }
263 json_object_put(jobj);
264
265 /*
266 * Because co2meters are not so smart and don't hold product information
267 * search the missing pieces in the database.
268 */
269 // log->co2meter_uuid is the search, fill:
270 // log->product_uuid log->product_name log->product_code
271 // log->co2meter_node log->co2meter_alias
272
273 /*
274 * Build csv log line
275 */
276 line = xstrcpy(log->datetime);
277 line = xstrcat(line, (char *)",");
278 snprintf(buf, 64, "%.3f", log->temperature);
279 line = xstrcat(line, buf);
280 line = xstrcat(line, (char *)",");
281 snprintf(buf, 64, "%.3f", log->pressure);
282 line = xstrcat(line, buf);
283 line = xstrcat(line, (char *)",");
284 line = xstrcat(line, log->co2meter_uuid);
285
286 /*
287 * Build logfile name
288 */
289 logfile = xstrcpy(Config.web_root);
290 logfile = xstrcat(logfile, (char *)"/log/co2pressure/");
291 logfile = xstrcat(logfile, log->product_code);
292 logfile = xstrcat(logfile, (char *)" ");
293 logfile = xstrcat(logfile, log->product_name);
294 logfile = xstrcat(logfile, (char *)".log");
295
296 if (debug)
297 fprintf(stdout, "%s %s\n", logfile, line);
298
299 fp = fopen(logfile, "a");
300 if (fp) {
301 fprintf(fp, "%s\n", line);
302 fclose(fp);
303 } else {
304 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
305 }
306
307 free(logfile);
308 logfile = NULL;
309 free(line);
310 line = NULL;
311
312 if (log->datetime)
313 free(log->datetime);
314 if (log->product_uuid )
315 free(log->product_uuid );
316 if (log->product_code )
317 free(log->product_code );
318 if (log->product_name )
319 free(log->product_name );
320 if (log->co2meter_uuid)
321 free(log->co2meter_uuid);
322 if (log->co2meter_node)
323 free(log->co2meter_node);
324 if (log->co2meter_alias)
325 free(log->co2meter_alias);
326 free(log);
327 }
328
329
330
331 void co2meter_dump(sys_co2meter_list *co2meter)
332 {
333 if (debug) {
334 printf("uuid %s\n", co2meter->uuid);
335 printf("alias %s\n", co2meter->alias);
336 printf("node %s\n", co2meter->node);
337 printf("online %s\n", co2meter->online ? "yes":"no");
338 printf("product %s / %s\n", co2meter->beercode, co2meter->beername);
339 printf("Temperature %-16s %10s %8.3f\n", co2meter->temperature_address, co2meter->temperature_state, co2meter->temperature);
340 printf("Pressure %10s %d %.3f %.3f %.3f\n", co2meter->pressure_state, co2meter->pressure_channel,
341 co2meter->pressure_voltage, co2meter->pressure_zero, co2meter->pressure_bar);
342 printf("mode %s\n", co2meter->mode);
343 }
344 }
345
346

mercurial