bmsd/co2meters.c

changeset 673
9924b1218d39
parent 558
a8e065a9f851
child 677
6e82fece1f8f
equal deleted inserted replaced
672:23f959713fcb 673:9924b1218d39
1 /** 1 /**
2 * @file co2meters.c 2 * @file co2meters.c
3 * @brief Handle co2meters status 3 * @brief Handle co2meters status
4 * @author Michiel Broek <mbroek at mbse dot eu> 4 * @author Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * Copyright (C) 2019 6 * Copyright (C) 2019-2020
7 * 7 *
8 * This file is part of the bms (Brewery Management System) 8 * This file is part of the bms (Brewery Management System)
9 * 9 *
10 * This is free software; you can redistribute it and/or modify it 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 11 * under the terms of the GNU General Public License as published by the
25 25
26 #include "bms.h" 26 #include "bms.h"
27 #include "xutil.h" 27 #include "xutil.h"
28 #include "co2meters.h" 28 #include "co2meters.h"
29 #include "mysql.h" 29 #include "mysql.h"
30 #include "websocket.h"
30 31
31 32
32 sys_co2meter_list *co2meters = NULL; 33 sys_co2meter_list *co2meters = NULL;
33 34
34 extern int debug; 35 extern int debug;
42 void co2meter_set(char *edge_node, char *alias, char *payload) 43 void co2meter_set(char *edge_node, char *alias, char *payload)
43 { 44 {
44 struct json_object *jobj, *val, *sensor; 45 struct json_object *jobj, *val, *sensor;
45 sys_co2meter_list *co2meter, *tmpp; 46 sys_co2meter_list *co2meter, *tmpp;
46 bool new_co2meter = true; 47 bool new_co2meter = true;
48 char *msg = NULL, buf[65];
47 49
48 // fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload); 50 // fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload);
49 51
50 /* 52 /*
51 * Search co2meter record in the memory array and use it if found. 53 * Search co2meter record in the memory array and use it if found.
134 if (json_object_object_get_ex(sensor, "bar", &val)) { 136 if (json_object_object_get_ex(sensor, "bar", &val)) {
135 co2meter->pressure_bar = json_object_get_double(val); 137 co2meter->pressure_bar = json_object_get_double(val);
136 } 138 }
137 } 139 }
138 json_object_put(jobj); 140 json_object_put(jobj);
141
142 msg = xstrcpy((char *)"{\"device\":\"co2meter\",\"node\":\"");
143 msg = xstrcat(msg, edge_node);
144 msg = xstrcat(msg, (char *)"\",\"unit\":\"");
145 msg = xstrcat(msg, alias);
146 msg = xstrcat(msg, (char *)"\",\"online\":");
147 msg = xstrcat(msg, co2meter->online ? (char *)"1":(char *)"0");
148 msg = xstrcat(msg, (char *)",\"mode\":\"");
149 msg = xstrcat(msg, co2meter->mode);
150 msg = xstrcat(msg, (char *)"\",\"temperature\":");
151 snprintf(buf, 64, "%.3f", co2meter->temperature);
152 msg = xstrcat(msg, buf);
153 msg = xstrcat(msg, (char *)",\"pressure_channel\":");
154 snprintf(buf, 64, "%d", co2meter->pressure_channel);
155 msg = xstrcat(msg, buf);
156 msg = xstrcat(msg, (char *)",\"pressure_voltage\":");
157 snprintf(buf, 64, "%.3f", co2meter->pressure_voltage);
158 msg = xstrcat(msg, buf);
159 msg = xstrcat(msg, (char *)",\"pressure_zero\":");
160 snprintf(buf, 64, "%.3f", co2meter->pressure_zero);
161 msg = xstrcat(msg, buf);
162 msg = xstrcat(msg, (char *)",\"pressure_bar\":");
163 snprintf(buf, 64, "%.3f", co2meter->pressure_bar);
164 msg = xstrcat(msg, buf);
165 msg = xstrcat(msg, (char *)",\"alarm\":");
166 snprintf(buf, 64, "%d", co2meter->alarm);
167 msg = xstrcat(msg, buf);
168 msg = xstrcat(msg, (char *)"}");
169 ws_broadcast(msg);
170 free(msg);
171 msg = NULL;
139 172
140 // co2meter_dump(co2meter); 173 // co2meter_dump(co2meter);
141 174
142 if (new_co2meter) { 175 if (new_co2meter) {
143 if (co2meters == NULL) { 176 if (co2meters == NULL) {

mercurial