Sat, 23 Feb 2019 22:56:35 +0100
Fixed save empty dates
0 | 1 | /** |
2 | * @file nodes.c | |
3 | * @brief Handle nodes status | |
4 | * @author Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * Copyright (C) 2018 | |
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 "nodes.h" | |
29 | #include "mysql.h" | |
30 | ||
31 | ||
32 | sys_node_list *nodes = NULL; | |
33 | ||
34 | extern int debug; | |
35 | ||
36 | ||
37 | ||
38 | void node_birth_data(char *topic, char *payload) | |
39 | { | |
40 | struct json_object *jobj, *val, *metric, *metric2; | |
41 | sys_node_list *node, *tmpp; | |
42 | char *group_id, *message_type, *edge_node; | |
43 | bool new_node = true; | |
44 | ||
45 | // fprintf(stdout, "node_birth: %s %s\n", topic, payload); | |
46 | ||
47 | strtok(topic, "/"); // ignore namespace | |
48 | group_id = strtok(NULL, "/"); | |
49 | message_type = strtok(NULL, "/"); | |
50 | edge_node = strtok(NULL, "/\0"); | |
51 | ||
52 | /* | |
53 | * Search node record in the memory array and use it if found. | |
54 | */ | |
55 | if (nodes) { | |
56 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
57 | if ((strcmp(tmpp->group_id, group_id) == 0) && (strcmp(tmpp->node, edge_node) == 0)) { | |
58 | new_node = false; | |
59 | node = tmpp; | |
60 | node->lastseen = time(NULL); | |
61 | node->online = true; | |
62 | break; | |
63 | } | |
64 | } | |
65 | } | |
66 | ||
67 | /* | |
68 | * Allocate new node if not yet known. | |
69 | */ | |
70 | if (new_node) { | |
71 | node = (sys_node_list *)malloc(sizeof(sys_node_list)); | |
72 | node->uuid = NULL; | |
73 | node->next = NULL; | |
74 | node->group_id = xstrcpy(group_id); | |
75 | node->node = xstrcpy(edge_node); | |
76 | node->online = true; | |
77 | node->hardwaremake = node->hardwaremodel = node->os = node->os_version = node->firmware = NULL; | |
78 | node->firstseen = node->lastseen = time(NULL); | |
79 | node->temperature = node->humidity = node->barometer = 0.0; | |
80 | node->gps_latitude = node->gps_longitude = node->gps_altitude = 0.0; | |
81 | node->net_address = node->net_ifname = NULL; | |
82 | node->net_rssi = 0; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Process the JSON formatted payload. | |
87 | * Update only the fields that are found in the payload. | |
88 | */ | |
89 | jobj = json_tokener_parse(payload); | |
90 | ||
91 | if (json_object_object_get_ex(jobj, "timestamp", &val)) { | |
92 | if (strcmp((char *)"NDATA", message_type)) { | |
93 | node->firstseen = json_object_get_int(val); | |
94 | syslog(LOG_NOTICE, "Online node `%s/%s'", node->group_id, node->node); | |
95 | } else { | |
96 | node->lastseen = json_object_get_int(val); | |
97 | } | |
98 | } | |
99 | ||
100 | /* | |
101 | if (json_object_object_get_ex(jobj, "seq", &val)) { | |
102 | printf("seq: %s\n", json_object_to_json_string_ext(val, 0)); // Do we need it? | |
103 | } | |
104 | */ | |
105 | ||
106 | if (json_object_object_get_ex(jobj, "metric", &metric)) { | |
107 | if (json_object_object_get_ex(metric, "uuid", &val)) { | |
108 | if (node->uuid) | |
109 | free(node->uuid); | |
110 | node->uuid = xstrcpy((char *)json_object_get_string(val)); | |
111 | } | |
112 | if (json_object_object_get_ex(metric, "properties", &metric2)) { | |
113 | if (json_object_object_get_ex(metric2, "hardwaremake", &val)) { | |
114 | if (node->hardwaremake) | |
115 | free(node->hardwaremake); | |
116 | node->hardwaremake = xstrcpy((char *)json_object_get_string(val)); | |
117 | } | |
118 | if (json_object_object_get_ex(metric2, "hardwaremodel", &val)) { | |
119 | if (node->hardwaremodel) | |
120 | free(node->hardwaremodel); | |
121 | node->hardwaremodel = xstrcpy((char *)json_object_get_string(val)); | |
122 | } | |
123 | if (json_object_object_get_ex(metric2, "os", &val)) { | |
124 | if (node->os) | |
125 | free(node->os); | |
126 | node->os = xstrcpy((char *)json_object_get_string(val)); | |
127 | } | |
128 | if (json_object_object_get_ex(metric2, "os_version", &val)) { | |
129 | if (node->os_version) | |
130 | free(node->os_version); | |
131 | node->os_version = xstrcpy((char *)json_object_get_string(val)); | |
132 | } | |
133 | if (json_object_object_get_ex(metric2, "FW", &val)) { | |
134 | if (node->firmware) | |
135 | free(node->firmware); | |
136 | node->firmware = xstrcpy((char *)json_object_get_string(val)); | |
137 | } | |
138 | } | |
139 | if (json_object_object_get_ex(metric, "THB", &metric2)) { | |
140 | if (json_object_object_get_ex(metric2, "temperature", &val)) { | |
141 | node->temperature = json_object_get_double(val); | |
142 | } | |
143 | if (json_object_object_get_ex(metric2, "humidity", &val)) { | |
144 | node->humidity= json_object_get_double(val); | |
145 | } | |
146 | if (json_object_object_get_ex(metric2, "barometer", &val)) { | |
147 | node->barometer = json_object_get_double(val); | |
148 | } | |
149 | } | |
150 | if (json_object_object_get_ex(metric, "GPS", &metric2)) { | |
151 | if (json_object_object_get_ex(metric2, "latitude", &val)) { | |
152 | node->gps_latitude = json_object_get_double(val); | |
153 | } | |
154 | if (json_object_object_get_ex(metric2, "longitude", &val)) { | |
155 | node->gps_longitude = json_object_get_double(val); | |
156 | } | |
157 | if (json_object_object_get_ex(metric2, "altitude", &val)) { | |
158 | node->gps_altitude = json_object_get_double(val); | |
159 | } | |
160 | } | |
161 | if (json_object_object_get_ex(metric, "net", &metric2)) { | |
162 | if (json_object_object_get_ex(metric2, "address", &val)) { | |
163 | if (node->net_address) | |
164 | free(node->net_address); | |
165 | node->net_address = xstrcpy((char *)json_object_get_string(val)); | |
166 | } | |
167 | if (json_object_object_get_ex(metric2, "ifname", &val)) { | |
168 | if (node->net_ifname) | |
169 | free(node->net_ifname); | |
170 | node->net_ifname = xstrcpy((char *)json_object_get_string(val)); | |
171 | } | |
172 | if (json_object_object_get_ex(metric2, "rssi", &val)) { | |
173 | node->net_rssi = json_object_get_int(val); | |
174 | } | |
175 | } | |
176 | } | |
177 | json_object_put(jobj); | |
178 | ||
179 | // node_dump(node); | |
180 | ||
181 | if (new_node) { | |
182 | if (nodes == NULL) { | |
183 | nodes = node; | |
184 | } else { | |
185 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
186 | if (tmpp->next == NULL) { | |
187 | tmpp->next = node; | |
188 | break; | |
189 | } | |
190 | } | |
191 | } | |
192 | node_mysql_insert(node); | |
193 | } else { | |
194 | node_mysql_update(node); | |
195 | } | |
196 | } | |
197 | ||
198 | ||
199 | ||
200 | void node_dump(sys_node_list *node) | |
201 | { | |
202 | if (debug) { | |
203 | printf("online %s\n", node->online ? "yes":"no"); | |
204 | printf("uuid %s\n", node->uuid); | |
205 | printf("node %s\n", node->node); | |
206 | printf("group %s\n", node->group_id); | |
207 | printf("hw make %s model %s\n", node->hardwaremake, node->hardwaremodel); | |
208 | printf("os %s version %s\n", node->os, node->os_version); | |
209 | printf("firmware %s\n", node->firmware); | |
210 | printf("first %ld last %ld\n", node->firstseen, node->lastseen); | |
211 | printf("THB %.2f %.2f %.2f\n", node->temperature, node->humidity, node->barometer); | |
212 | printf("GPS %.5f %.5f %.5f\n", node->gps_latitude, node->gps_longitude, node->gps_altitude); | |
213 | printf("net %s:%s\n", node->net_ifname, node->net_address); | |
214 | printf("rssi %d\n", node->net_rssi); | |
215 | } | |
216 | } | |
217 | ||
218 | ||
219 | ||
220 | void node_death(char *topic) | |
221 | { | |
222 | char *group_id, *edge_node; | |
223 | sys_node_list *tmpp; | |
224 | ||
225 | strtok(topic, "/"); // ignore namespace | |
226 | group_id = strtok(NULL, "/"); | |
227 | strtok(NULL, "/"); // ignore message_type | |
228 | edge_node = strtok(NULL, "/\0"); | |
229 | ||
230 | syslog(LOG_NOTICE, "Offline node `%s/%s'", group_id, edge_node); | |
231 | node_mysql_death(edge_node); | |
232 | ||
233 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
234 | if (strcmp(tmpp->node, edge_node) == 0) { | |
235 | tmpp->online = false; | |
236 | break; | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 |