bmsd/nodes.c

changeset 672
23f959713fcb
parent 578
e75ce5bbda73
child 673
9924b1218d39
equal deleted inserted replaced
671:4b54d6f79d25 672:23f959713fcb
25 25
26 #include "bms.h" 26 #include "bms.h"
27 #include "xutil.h" 27 #include "xutil.h"
28 #include "nodes.h" 28 #include "nodes.h"
29 #include "mysql.h" 29 #include "mysql.h"
30 #include "websocket.h"
30 31
31 32
32 sys_node_list *nodes = NULL; 33 sys_node_list *nodes = NULL;
33 34
34 extern int debug; 35 extern int debug;
39 40
40 void node_birth_data(char *topic, char *payload) 41 void node_birth_data(char *topic, char *payload)
41 { 42 {
42 struct json_object *jobj, *val, *metric, *metric2; 43 struct json_object *jobj, *val, *metric, *metric2;
43 sys_node_list *node, *tmpp; 44 sys_node_list *node, *tmpp;
44 char *group_id, *message_type, *edge_node; 45 struct tm *mytime;
46 char *group_id, *message_type, *edge_node, *msg = NULL, buf[74];
45 bool new_node = true; 47 bool new_node = true;
46 48
47 // fprintf(stdout, "node_birth: %s %s\n", topic, payload); 49 // fprintf(stdout, "node_birth: %s %s\n", topic, payload);
48 50
49 strtok(topic, "/"); // ignore namespace 51 strtok(topic, "/"); // ignore namespace
98 } else { 100 } else {
99 node->lastseen = json_object_get_int(val); 101 node->lastseen = json_object_get_int(val);
100 } 102 }
101 } 103 }
102 104
103 /*
104 if (json_object_object_get_ex(jobj, "seq", &val)) {
105 printf("seq: %s\n", json_object_to_json_string_ext(val, 0)); // Do we need it?
106 }
107 */
108
109 if (json_object_object_get_ex(jobj, "metric", &metric)) { 105 if (json_object_object_get_ex(jobj, "metric", &metric)) {
110 if (json_object_object_get_ex(metric, "uuid", &val)) { 106 if (json_object_object_get_ex(metric, "uuid", &val)) {
111 if (node->uuid) 107 if (node->uuid)
112 free(node->uuid); 108 free(node->uuid);
113 node->uuid = xstrcpy((char *)json_object_get_string(val)); 109 node->uuid = xstrcpy((char *)json_object_get_string(val));
179 node->net_rssi = json_object_get_int(val); 175 node->net_rssi = json_object_get_int(val);
180 } 176 }
181 } 177 }
182 } 178 }
183 json_object_put(jobj); 179 json_object_put(jobj);
180
181 msg = xstrcpy((char *)"{\"node\":\"");
182 msg = xstrcat(msg, edge_node);
183 msg = xstrcat(msg, (char *)"\",\"group\":\"");
184 msg = xstrcat(msg, group_id);
185 msg = xstrcat(msg, (char *)"\",\"online\":");
186 msg = xstrcat(msg, node->online ? (char *)"1":(char *)"0");
187 msg = xstrcat(msg, (char *)",\"lastseen\":\"");
188 mytime = localtime(&node->lastseen);
189 snprintf(buf, 73, "%04d-%02d-%02d %02d:%02d:%02d",
190 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
191 msg = xstrcat(msg, buf);
192 msg = xstrcat(msg, (char *)"\"");
193 if (node->temperature) {
194 msg = xstrcat(msg, (char *)",\"temperature\":");
195 snprintf(buf, 64, "%.1f", node->temperature);
196 msg = xstrcat(msg, buf);
197 }
198 if (node->humidity) {
199 msg = xstrcat(msg, (char *)",\"humidity\":");
200 snprintf(buf, 64, "%.1f", node->humidity);
201 msg = xstrcat(msg, buf);
202 }
203 msg = xstrcat(msg, (char *)",\"ip\":\"");
204 msg = xstrcat(msg, node->net_address);
205 msg = xstrcat(msg, (char *)"\"");
206 if (node->net_rssi != 0) {
207 msg = xstrcat(msg, (char *)",\"rssi\":");
208 snprintf(buf, 64, "%d", node->net_rssi);
209 msg = xstrcat(msg, buf);
210 }
211 msg = xstrcat(msg, (char *)"}");
212 ws_broadcast(msg);
213 free(msg);
214 msg = NULL;
184 215
185 // node_dump(node); 216 // node_dump(node);
186 217
187 if (new_node) { 218 if (new_node) {
188 if (nodes == NULL) { 219 if (nodes == NULL) {
224 255
225 256
226 257
227 void node_death(char *topic) 258 void node_death(char *topic)
228 { 259 {
229 char *group_id, *edge_node; 260 char *group_id, *edge_node, *msg;
230 sys_node_list *tmpp; 261 sys_node_list *tmpp;
231 262
232 strtok(topic, "/"); // ignore namespace 263 strtok(topic, "/"); // ignore namespace
233 group_id = strtok(NULL, "/"); 264 group_id = strtok(NULL, "/");
234 strtok(NULL, "/"); // ignore message_type 265 strtok(NULL, "/"); // ignore message_type
238 node_mysql_death(edge_node); 269 node_mysql_death(edge_node);
239 270
240 for (tmpp = nodes; tmpp; tmpp = tmpp->next) { 271 for (tmpp = nodes; tmpp; tmpp = tmpp->next) {
241 if (strcmp(tmpp->node, edge_node) == 0) { 272 if (strcmp(tmpp->node, edge_node) == 0) {
242 tmpp->online = false; 273 tmpp->online = false;
274 msg = xstrcpy((char *)"{\"node\":\"");
275 msg = xstrcat(msg, edge_node);
276 msg = xstrcat(msg, (char *)"\",\"group\":\"");
277 msg = xstrcat(msg, group_id);
278 msg = xstrcat(msg, (char *)"\",\"online\":0}");
279 ws_broadcast(msg);
280 free(msg);
281 msg = NULL;
243 break; 282 break;
244 } 283 }
245 } 284 }
246 } 285 }
247 286
252 sys_node_list *tmpn; 291 sys_node_list *tmpn;
253 sys_fermenter_list *tmpf; 292 sys_fermenter_list *tmpf;
254 sys_co2meter_list *tmpc; 293 sys_co2meter_list *tmpc;
255 sys_ispindel_list *tmpi; 294 sys_ispindel_list *tmpi;
256 time_t now = time(NULL); 295 time_t now = time(NULL);
296 char *msg = NULL;
257 297
258 for (tmpn = nodes; tmpn; tmpn = tmpn->next) { 298 for (tmpn = nodes; tmpn; tmpn = tmpn->next) {
259 // if (debug) 299 // if (debug)
260 // printf("%-20s online %s %ld %d\n", tmpn->node, tmpn->online ? "yes":"no ", tmpn->lastseen, tmpn->interval); 300 // printf("%-20s online %s %ld %d\n", tmpn->node, tmpn->online ? "yes":"no ", tmpn->lastseen, tmpn->interval);
261 if (tmpn->online && ((now - tmpn->lastseen) > (tmpn->interval * 2 + 5))) { // 2 times interval + 5 seconds 301 if (tmpn->online && ((now - tmpn->lastseen) > (tmpn->interval * 2 + 5))) { // 2 times interval + 5 seconds
262 syslog(LOG_NOTICE, "Timeout node `%s/%s' after %ld seconds", tmpn->group_id, tmpn->node, (now - tmpn->lastseen)); 302 syslog(LOG_NOTICE, "Timeout node `%s/%s' after %ld seconds", tmpn->group_id, tmpn->node, (now - tmpn->lastseen));
263 tmpn->online = false; 303 tmpn->online = false;
264 node_mysql_death(tmpn->node); 304 node_mysql_death(tmpn->node);
305 msg = xstrcpy((char *)"{\"node\":\"");
306 msg = xstrcat(msg, tmpn->node);
307 msg = xstrcat(msg, (char *)"\",\"group\":\"");
308 msg = xstrcat(msg, tmpn->group_id);
309 msg = xstrcat(msg, (char *)"\",\"online\":0}");
310 ws_broadcast(msg);
311 free(msg);
312 msg = NULL;
265 313
266 for (tmpf = fermenters; tmpf; tmpf = tmpf->next) { 314 for (tmpf = fermenters; tmpf; tmpf = tmpf->next) {
267 if (strcmp(tmpf->node, tmpn->node) == 0) { 315 if (strcmp(tmpf->node, tmpn->node) == 0) {
268 if (tmpf->online) { 316 if (tmpf->online) {
269 syslog(LOG_NOTICE, "Timeout fermenter %s/%s", tmpf->node, tmpf->alias); 317 syslog(LOG_NOTICE, "Timeout fermenter %s/%s", tmpf->node, tmpf->alias);

mercurial