bmsd/nodes.c

changeset 795
9472106a3143
parent 792
36d51473aa81
equal deleted inserted replaced
794:37e1704fd11f 795:9472106a3143
24 24
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 "mqtt.h"
29 #include "mysql.h" 30 #include "mysql.h"
30 #include "websocket.h" 31 #include "websocket.h"
31 32
32 33
33 sys_node_list *nodes = NULL; 34 sys_node_list *nodes = NULL;
410 } 411 }
411 } 412 }
412 } 413 }
413 414
414 415
415 416 void node_ws_receive(char *payload)
417 {
418 struct json_object *jobj, *val;
419 char *node = NULL, *group = NULL, *control = NULL, *topic = NULL, *pay = NULL, buf[33];
420
421 syslog(LOG_NOTICE, "node_ws %s", payload);
422 /*
423 * Process the JSON formatted payload.
424 */
425 jobj = json_tokener_parse(payload);
426 if (json_object_object_get_ex(jobj, "node", &val))
427 node = xstrcpy((char *)json_object_get_string(val));
428 if (json_object_object_get_ex(jobj, "group_id", &val))
429 group = xstrcpy((char *)json_object_get_string(val));
430 if (json_object_object_get_ex(jobj, "control", &val))
431 control = xstrcpy((char *)json_object_get_string(val));
432
433 if (node && group && control) {
434
435 /*
436 * Prepare MQTT topic
437 */
438 topic = xstrcpy((char *)"mbv1.0/");
439 topic = xstrcat(topic, group);
440 topic = xstrcat(topic, (char *)"/NCMD/");
441 topic = xstrcat(topic, node);
442
443 pay = xstrcpy((char *)"{\"timestamp\":");
444 snprintf(buf, 32, "%ld", time(NULL));
445 pay = xstrcat(pay, buf);
446 pay = xstrcat(pay, (char *)",\"metric\":");
447
448 if (strcmp(control, (char *)"reboot") == 0) {
449 pay = xstrcat(pay, (char *)"{\"Node Control/Reboot\":true}}");
450 } else if (strcmp(control, (char *)"rebirth") == 0) {
451 pay = xstrcat(pay, (char *)"{\"Node Control/Rebirth\":true}}");
452 }
453 syslog(LOG_NOTICE, "%s %s", topic, pay);
454 mqtt_publish(topic, pay);
455 }
456
457 if (node)
458 free(node);
459 if (group)
460 free(group);
461 if (control)
462 free(control);
463 if (topic)
464 free(topic);
465 if (pay)
466 free(pay);
467 }

mercurial