bmsd/co2meters.c

Tue, 12 May 2020 13:16:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 May 2020 13:16:25 +0200
changeset 673
9924b1218d39
parent 558
a8e065a9f851
child 677
6e82fece1f8f
permissions
-rw-r--r--

Added co2meters to the websockets. Added node and units offline messages to the websockets.

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

mercurial