Sat, 19 Aug 2023 15:11:35 +0200
De iSpindel plato berekening is in het webscript gezet omdat er dan met meer cijfers achter de komman gerekend wordt. De uitkomst verschilt 0.25 plato! De calibratie is nu dus extern.
0 | 1 | /** |
2 | * @file nodes.c | |
3 | * @brief Handle nodes status | |
4 | * @author Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
792
36d51473aa81
Fix a wrong object name in websocket node json.
Michiel Broek <mbroek@mbse.eu>
parents:
703
diff
changeset
|
6 | * Copyright (C) 2018-2022 |
0 | 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" | |
795
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
29 | #include "mqtt.h" |
0 | 30 | #include "mysql.h" |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
31 | #include "websocket.h" |
0 | 32 | |
33 | ||
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
34 | sys_node_list *nodes = NULL; |
0 | 35 | |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
36 | extern int debug; |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
37 | extern sys_fermenter_list *fermenters; |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
38 | extern sys_co2meter_list *co2meters; |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
39 | extern sys_ispindel_list *ispindels; |
0 | 40 | |
41 | ||
42 | void node_birth_data(char *topic, char *payload) | |
43 | { | |
44 | struct json_object *jobj, *val, *metric, *metric2; | |
45 | sys_node_list *node, *tmpp; | |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
46 | struct tm *mytime; |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
47 | char *group_id, *message_type, *edge_node, *msg = NULL, buf[74]; |
0 | 48 | bool new_node = true; |
49 | ||
50 | // fprintf(stdout, "node_birth: %s %s\n", topic, payload); | |
51 | ||
52 | strtok(topic, "/"); // ignore namespace | |
53 | group_id = strtok(NULL, "/"); | |
54 | message_type = strtok(NULL, "/"); | |
55 | edge_node = strtok(NULL, "/\0"); | |
56 | ||
57 | /* | |
58 | * Search node record in the memory array and use it if found. | |
59 | */ | |
60 | if (nodes) { | |
61 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
62 | if ((strcmp(tmpp->group_id, group_id) == 0) && (strcmp(tmpp->node, edge_node) == 0)) { | |
63 | new_node = false; | |
64 | node = tmpp; | |
65 | node->lastseen = time(NULL); | |
66 | node->online = true; | |
67 | break; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | /* | |
73 | * Allocate new node if not yet known. | |
74 | */ | |
75 | if (new_node) { | |
76 | node = (sys_node_list *)malloc(sizeof(sys_node_list)); | |
77 | node->uuid = NULL; | |
78 | node->next = NULL; | |
79 | node->group_id = xstrcpy(group_id); | |
80 | node->node = xstrcpy(edge_node); | |
81 | node->online = true; | |
82 | node->hardwaremake = node->hardwaremodel = node->os = node->os_version = node->firmware = NULL; | |
83 | node->firstseen = node->lastseen = time(NULL); | |
84 | node->temperature = node->humidity = node->barometer = 0.0; | |
85 | node->gps_latitude = node->gps_longitude = node->gps_altitude = 0.0; | |
703
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
86 | node->net_address = node->net_ifname = node->net_ssid = NULL; |
0 | 87 | node->net_rssi = 0; |
578
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
88 | node->interval = 300; |
0 | 89 | } |
90 | ||
91 | /* | |
92 | * Process the JSON formatted payload. | |
93 | * Update only the fields that are found in the payload. | |
94 | */ | |
95 | jobj = json_tokener_parse(payload); | |
96 | ||
97 | if (json_object_object_get_ex(jobj, "timestamp", &val)) { | |
98 | if (strcmp((char *)"NDATA", message_type)) { | |
99 | node->firstseen = json_object_get_int(val); | |
100 | syslog(LOG_NOTICE, "Online node `%s/%s'", node->group_id, node->node); | |
101 | } else { | |
102 | node->lastseen = json_object_get_int(val); | |
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 | } | |
578
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
112 | if (json_object_object_get_ex(metric, "interval", &val)) { |
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
113 | node->interval = json_object_get_int(val); |
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
114 | } |
0 | 115 | if (json_object_object_get_ex(metric, "properties", &metric2)) { |
116 | if (json_object_object_get_ex(metric2, "hardwaremake", &val)) { | |
117 | if (node->hardwaremake) | |
118 | free(node->hardwaremake); | |
119 | node->hardwaremake = xstrcpy((char *)json_object_get_string(val)); | |
120 | } | |
121 | if (json_object_object_get_ex(metric2, "hardwaremodel", &val)) { | |
122 | if (node->hardwaremodel) | |
123 | free(node->hardwaremodel); | |
124 | node->hardwaremodel = xstrcpy((char *)json_object_get_string(val)); | |
125 | } | |
126 | if (json_object_object_get_ex(metric2, "os", &val)) { | |
127 | if (node->os) | |
128 | free(node->os); | |
129 | node->os = xstrcpy((char *)json_object_get_string(val)); | |
130 | } | |
131 | if (json_object_object_get_ex(metric2, "os_version", &val)) { | |
132 | if (node->os_version) | |
133 | free(node->os_version); | |
134 | node->os_version = xstrcpy((char *)json_object_get_string(val)); | |
135 | } | |
136 | if (json_object_object_get_ex(metric2, "FW", &val)) { | |
137 | if (node->firmware) | |
138 | free(node->firmware); | |
139 | node->firmware = xstrcpy((char *)json_object_get_string(val)); | |
140 | } | |
141 | } | |
142 | if (json_object_object_get_ex(metric, "THB", &metric2)) { | |
143 | if (json_object_object_get_ex(metric2, "temperature", &val)) { | |
144 | node->temperature = json_object_get_double(val); | |
145 | } | |
146 | if (json_object_object_get_ex(metric2, "humidity", &val)) { | |
147 | node->humidity= json_object_get_double(val); | |
148 | } | |
149 | if (json_object_object_get_ex(metric2, "barometer", &val)) { | |
150 | node->barometer = json_object_get_double(val); | |
151 | } | |
152 | } | |
153 | if (json_object_object_get_ex(metric, "GPS", &metric2)) { | |
154 | if (json_object_object_get_ex(metric2, "latitude", &val)) { | |
155 | node->gps_latitude = json_object_get_double(val); | |
156 | } | |
157 | if (json_object_object_get_ex(metric2, "longitude", &val)) { | |
158 | node->gps_longitude = json_object_get_double(val); | |
159 | } | |
160 | if (json_object_object_get_ex(metric2, "altitude", &val)) { | |
161 | node->gps_altitude = json_object_get_double(val); | |
162 | } | |
163 | } | |
164 | if (json_object_object_get_ex(metric, "net", &metric2)) { | |
165 | if (json_object_object_get_ex(metric2, "address", &val)) { | |
166 | if (node->net_address) | |
167 | free(node->net_address); | |
168 | node->net_address = xstrcpy((char *)json_object_get_string(val)); | |
169 | } | |
170 | if (json_object_object_get_ex(metric2, "ifname", &val)) { | |
171 | if (node->net_ifname) | |
172 | free(node->net_ifname); | |
173 | node->net_ifname = xstrcpy((char *)json_object_get_string(val)); | |
174 | } | |
703
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
175 | if (json_object_object_get_ex(metric2, "ssid", &val)) { |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
176 | if (node->net_ssid) |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
177 | free(node->net_ssid); |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
178 | node->net_ssid = xstrcpy((char *)json_object_get_string(val)); |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
179 | } |
0 | 180 | if (json_object_object_get_ex(metric2, "rssi", &val)) { |
181 | node->net_rssi = json_object_get_int(val); | |
182 | } | |
183 | } | |
184 | } | |
185 | json_object_put(jobj); | |
186 | ||
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
187 | msg = xstrcpy((char *)"{\"node\":\""); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
188 | msg = xstrcat(msg, edge_node); |
695
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
189 | msg = xstrcat(msg, (char *)"\",\"group_id\":\""); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
190 | msg = xstrcat(msg, group_id); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
191 | msg = xstrcat(msg, (char *)"\",\"online\":"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
192 | msg = xstrcat(msg, node->online ? (char *)"1":(char *)"0"); |
695
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
193 | msg = xstrcat(msg, (char *)",\"interval\":"); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
194 | snprintf(buf, 73, "%d", node->interval); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
195 | msg = xstrcat(msg, buf); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
196 | if (node->hardwaremake) { |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
197 | msg = xstrcat(msg, (char *)",\"hardwaremake\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
198 | msg = xstrcat(msg, node->hardwaremake); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
199 | msg = xstrcat(msg, (char *)"\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
200 | } |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
201 | if (node->hardwaremodel) { |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
202 | msg = xstrcat(msg, (char *)",\"hardwaremodel\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
203 | msg = xstrcat(msg, node->hardwaremodel); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
204 | msg = xstrcat(msg, (char *)"\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
205 | } |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
206 | if (node->os) { |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
207 | msg = xstrcat(msg, (char *)",\"os\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
208 | msg = xstrcat(msg, node->os); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
209 | msg = xstrcat(msg, (char *)"\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
210 | } |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
211 | if (node->os_version) { |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
212 | msg = xstrcat(msg, (char *)",\"os_version\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
213 | msg = xstrcat(msg, node->os_version); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
214 | msg = xstrcat(msg, (char *)"\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
215 | } |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
216 | if (node->firmware) { |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
217 | msg = xstrcat(msg, (char *)",\"firmware\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
218 | msg = xstrcat(msg, node->firmware); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
219 | msg = xstrcat(msg, (char *)"\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
220 | } |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
221 | msg = xstrcat(msg, (char *)",\"lastseen\":\""); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
222 | mytime = localtime(&node->lastseen); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
223 | snprintf(buf, 73, "%04d-%02d-%02d %02d:%02d:%02d", |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
224 | mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
225 | msg = xstrcat(msg, buf); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
226 | msg = xstrcat(msg, (char *)"\""); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
227 | if (node->temperature) { |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
228 | msg = xstrcat(msg, (char *)",\"temperature\":"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
229 | snprintf(buf, 64, "%.1f", node->temperature); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
230 | msg = xstrcat(msg, buf); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
231 | } |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
232 | if (node->humidity) { |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
233 | msg = xstrcat(msg, (char *)",\"humidity\":"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
234 | snprintf(buf, 64, "%.1f", node->humidity); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
235 | msg = xstrcat(msg, buf); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
236 | } |
695
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
237 | msg = xstrcat(msg, (char *)",\"net_ifname\":\""); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
238 | msg = xstrcat(msg, node->net_ifname); |
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
239 | msg = xstrcat(msg, (char *)"\",\"net_address\":\""); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
240 | msg = xstrcat(msg, node->net_address); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
241 | msg = xstrcat(msg, (char *)"\""); |
703
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
242 | if (node->net_ssid) { |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
243 | msg = xstrcat(msg, (char *)",\"net_ssid\":\""); |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
244 | msg = xstrcat(msg, node->net_ssid); |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
245 | msg = xstrcat(msg, (char *)"\""); |
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
246 | } |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
247 | if (node->net_rssi != 0) { |
695
cfe232710027
Monitor nodes now uses websockets instead of polling.
Michiel Broek <mbroek@mbse.eu>
parents:
686
diff
changeset
|
248 | msg = xstrcat(msg, (char *)",\"net_rssi\":"); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
249 | snprintf(buf, 64, "%d", node->net_rssi); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
250 | msg = xstrcat(msg, buf); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
251 | } |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
252 | msg = xstrcat(msg, (char *)"}"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
253 | ws_broadcast(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
254 | free(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
255 | msg = NULL; |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
256 | |
0 | 257 | // node_dump(node); |
258 | ||
259 | if (new_node) { | |
260 | if (nodes == NULL) { | |
261 | nodes = node; | |
262 | } else { | |
263 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
264 | if (tmpp->next == NULL) { | |
265 | tmpp->next = node; | |
266 | break; | |
267 | } | |
268 | } | |
269 | } | |
270 | node_mysql_insert(node); | |
271 | } else { | |
272 | node_mysql_update(node); | |
273 | } | |
274 | } | |
275 | ||
276 | ||
277 | ||
278 | void node_dump(sys_node_list *node) | |
279 | { | |
280 | if (debug) { | |
281 | printf("online %s\n", node->online ? "yes":"no"); | |
282 | printf("uuid %s\n", node->uuid); | |
283 | printf("node %s\n", node->node); | |
284 | printf("group %s\n", node->group_id); | |
285 | printf("hw make %s model %s\n", node->hardwaremake, node->hardwaremodel); | |
286 | printf("os %s version %s\n", node->os, node->os_version); | |
287 | printf("firmware %s\n", node->firmware); | |
288 | printf("first %ld last %ld\n", node->firstseen, node->lastseen); | |
289 | printf("THB %.2f %.2f %.2f\n", node->temperature, node->humidity, node->barometer); | |
290 | printf("GPS %.5f %.5f %.5f\n", node->gps_latitude, node->gps_longitude, node->gps_altitude); | |
291 | printf("net %s:%s\n", node->net_ifname, node->net_address); | |
703
faeede125639
Version 0.3.35 Added nodes database net_ssid field.
Michiel Broek <mbroek@mbse.eu>
parents:
695
diff
changeset
|
292 | printf("ssid rssi %s %d\n", node->net_ssid, node->net_rssi); |
578
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
293 | printf("interval %d\n", node->interval); |
0 | 294 | } |
295 | } | |
296 | ||
297 | ||
298 | ||
299 | void node_death(char *topic) | |
300 | { | |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
301 | char *group_id, *edge_node, *msg; |
0 | 302 | sys_node_list *tmpp; |
303 | ||
304 | strtok(topic, "/"); // ignore namespace | |
305 | group_id = strtok(NULL, "/"); | |
306 | strtok(NULL, "/"); // ignore message_type | |
307 | edge_node = strtok(NULL, "/\0"); | |
308 | ||
309 | syslog(LOG_NOTICE, "Offline node `%s/%s'", group_id, edge_node); | |
310 | node_mysql_death(edge_node); | |
311 | ||
312 | for (tmpp = nodes; tmpp; tmpp = tmpp->next) { | |
313 | if (strcmp(tmpp->node, edge_node) == 0) { | |
314 | tmpp->online = false; | |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
315 | msg = xstrcpy((char *)"{\"node\":\""); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
316 | msg = xstrcat(msg, edge_node); |
792
36d51473aa81
Fix a wrong object name in websocket node json.
Michiel Broek <mbroek@mbse.eu>
parents:
703
diff
changeset
|
317 | msg = xstrcat(msg, (char *)"\",\"group_id\":\""); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
318 | msg = xstrcat(msg, group_id); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
319 | msg = xstrcat(msg, (char *)"\",\"online\":0}"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
320 | ws_broadcast(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
321 | free(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
322 | msg = NULL; |
0 | 323 | break; |
324 | } | |
325 | } | |
326 | } | |
327 | ||
328 | ||
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
329 | |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
330 | void nodes_check_online() |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
331 | { |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
332 | sys_node_list *tmpn; |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
333 | sys_fermenter_list *tmpf; |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
334 | sys_co2meter_list *tmpc; |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
335 | sys_ispindel_list *tmpi; |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
336 | time_t now = time(NULL); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
337 | char *msg = NULL; |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
338 | |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
339 | for (tmpn = nodes; tmpn; tmpn = tmpn->next) { |
578
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
340 | // if (debug) |
e75ce5bbda73
Changed the interface from the iSpindels to be the same as other devices. A webpage converts each call to two standard MQTT messages. The nodes MQTT message extended with an interval parameter. iSpindels now have a generated uuid made up from the chipid.
Michiel Broek <mbroek@mbse.eu>
parents:
572
diff
changeset
|
341 | // printf("%-20s online %s %ld %d\n", tmpn->node, tmpn->online ? "yes":"no ", tmpn->lastseen, tmpn->interval); |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
342 | if (tmpn->online && ((now - tmpn->lastseen) > (tmpn->interval * 2 + 5))) { // 2 times interval + 5 seconds |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
343 | syslog(LOG_NOTICE, "Timeout node `%s/%s' after %ld seconds", tmpn->group_id, tmpn->node, (now - tmpn->lastseen)); |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
344 | tmpn->online = false; |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
345 | node_mysql_death(tmpn->node); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
346 | msg = xstrcpy((char *)"{\"node\":\""); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
347 | msg = xstrcat(msg, tmpn->node); |
792
36d51473aa81
Fix a wrong object name in websocket node json.
Michiel Broek <mbroek@mbse.eu>
parents:
703
diff
changeset
|
348 | msg = xstrcat(msg, (char *)"\",\"group_id\":\""); |
672
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
349 | msg = xstrcat(msg, tmpn->group_id); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
350 | msg = xstrcat(msg, (char *)"\",\"online\":0}"); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
351 | ws_broadcast(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
352 | free(msg); |
23f959713fcb
Added websockets for node status changes.
Michiel Broek <mbroek@mbse.eu>
parents:
578
diff
changeset
|
353 | msg = NULL; |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
354 | |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
355 | for (tmpf = fermenters; tmpf; tmpf = tmpf->next) { |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
356 | if (strcmp(tmpf->node, tmpn->node) == 0) { |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
357 | if (tmpf->online) { |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
358 | syslog(LOG_NOTICE, "Timeout fermenter %s/%s", tmpf->node, tmpf->alias); |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
359 | tmpf->online = false; |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
360 | fermenter_mysql_death(tmpf->node, tmpf->alias); |
677
6e82fece1f8f
Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents:
675
diff
changeset
|
361 | msg = xstrcpy((char *)"{\"device\":\"fermenters\",\"node\":\""); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
362 | msg = xstrcat(msg, tmpf->node); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
363 | 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:
672
diff
changeset
|
364 | msg = xstrcat(msg, tmpf->alias); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
365 | msg = xstrcat(msg, (char *)"\",\"online\":0}"); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
366 | ws_broadcast(msg); |
675
a27af02ab16a
Added iSpindel to the websockets. Better offline detection for websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
673
diff
changeset
|
367 | syslog(LOG_NOTICE, msg); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
368 | free(msg); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
369 | msg = NULL; |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
370 | } |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
371 | } |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
372 | } |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
373 | |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
374 | for (tmpc = co2meters; tmpc; tmpc = tmpc->next) { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
375 | if (strcmp(tmpc->node, tmpn->node) == 0) { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
376 | if (tmpc->online) { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
377 | syslog(LOG_NOTICE, "Timeout co2meter %s/%s", tmpc->node, tmpc->alias); |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
378 | tmpc->online = false; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
379 | co2meter_mysql_death(tmpc->node, tmpc->alias); |
677
6e82fece1f8f
Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents:
675
diff
changeset
|
380 | msg = xstrcpy((char *)"{\"device\":\"co2meters\",\"node\":\""); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
381 | msg = xstrcat(msg, tmpc->node); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
382 | 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:
672
diff
changeset
|
383 | msg = xstrcat(msg, tmpc->alias); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
384 | msg = xstrcat(msg, (char *)"\",\"online\":0}"); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
385 | ws_broadcast(msg); |
675
a27af02ab16a
Added iSpindel to the websockets. Better offline detection for websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
673
diff
changeset
|
386 | syslog(LOG_NOTICE, msg); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
387 | free(msg); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
388 | msg = NULL; |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
389 | } |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
390 | } |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
384
diff
changeset
|
391 | } |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
392 | |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
393 | for (tmpi = ispindels; tmpi; tmpi = tmpi->next) { |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
394 | if (strcmp(tmpi->node, tmpn->node) == 0) { |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
395 | if (tmpi->online) { |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
396 | syslog(LOG_NOTICE, "Timeout ispindel %s", tmpi->node); |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
397 | tmpi->online = false; |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
398 | ispindel_mysql_death(tmpi->node); |
677
6e82fece1f8f
Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents:
675
diff
changeset
|
399 | msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"node\":\""); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
400 | msg = xstrcat(msg, tmpi->node); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
401 | 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:
672
diff
changeset
|
402 | msg = xstrcat(msg, tmpi->alias); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
403 | msg = xstrcat(msg, (char *)"\",\"online\":0}"); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
404 | ws_broadcast(msg); |
675
a27af02ab16a
Added iSpindel to the websockets. Better offline detection for websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
673
diff
changeset
|
405 | syslog(LOG_NOTICE, msg); |
673
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
406 | free(msg); |
9924b1218d39
Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents:
672
diff
changeset
|
407 | msg = NULL; |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
408 | } |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
409 | } |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
410 | } |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
411 | } |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
412 | } |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
413 | } |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
414 | |
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
415 | |
795
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
416 | void node_ws_receive(char *payload) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
417 | { |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
418 | struct json_object *jobj, *val; |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
419 | char *node = NULL, *group = NULL, *control = NULL, *topic = NULL, *pay = NULL, buf[33]; |
684
ccb9f24d0fe9
Report any online status change via websocket broadcast. When starting, load all in memory tables sorted. The websocket status on the web pages has it's own panel. Prepared the menu system for dynamic updates.
Michiel Broek <mbroek@mbse.eu>
parents:
677
diff
changeset
|
420 | |
795
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
421 | syslog(LOG_NOTICE, "node_ws %s", payload); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
422 | /* |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
423 | * Process the JSON formatted payload. |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
424 | */ |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
425 | jobj = json_tokener_parse(payload); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
426 | if (json_object_object_get_ex(jobj, "node", &val)) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
427 | node = xstrcpy((char *)json_object_get_string(val)); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
428 | if (json_object_object_get_ex(jobj, "group_id", &val)) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
429 | group = xstrcpy((char *)json_object_get_string(val)); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
430 | if (json_object_object_get_ex(jobj, "control", &val)) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
431 | control = xstrcpy((char *)json_object_get_string(val)); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
432 | |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
433 | if (node && group && control) { |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
434 | |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
435 | /* |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
436 | * Prepare MQTT topic |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
437 | */ |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
438 | topic = xstrcpy((char *)"mbv1.0/"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
439 | topic = xstrcat(topic, group); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
440 | topic = xstrcat(topic, (char *)"/NCMD/"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
441 | topic = xstrcat(topic, node); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
442 | |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
443 | pay = xstrcpy((char *)"{\"timestamp\":"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
444 | snprintf(buf, 32, "%ld", time(NULL)); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
445 | pay = xstrcat(pay, buf); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
446 | pay = xstrcat(pay, (char *)",\"metric\":"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
447 | |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
448 | if (strcmp(control, (char *)"reboot") == 0) { |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
449 | pay = xstrcat(pay, (char *)"{\"Node Control/Reboot\":true}}"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
450 | } else if (strcmp(control, (char *)"rebirth") == 0) { |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
451 | pay = xstrcat(pay, (char *)"{\"Node Control/Rebirth\":true}}"); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
452 | } |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
453 | syslog(LOG_NOTICE, "%s %s", topic, pay); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
454 | mqtt_publish(topic, pay); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
455 | } |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
456 | |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
457 | if (node) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
458 | free(node); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
459 | if (group) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
460 | free(group); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
461 | if (control) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
462 | free(control); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
463 | if (topic) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
464 | free(topic); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
465 | if (pay) |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
466 | free(pay); |
9472106a3143
Version 0.3.40. Added passing thru websocket control messages via mqtt to nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
792
diff
changeset
|
467 | } |