-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_rf.cpp
executable file
·353 lines (322 loc) · 11.1 KB
/
mqtt_rf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
The MIT License (MIT)
Copyright (c) 2017 Wassim Filali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
___________________________________________________________________________________
dependencies :
- sudo apt-get install libmosquitto-dev
- sudo apt-get install libmosquittopp-dev
___________________________________________________________________________________
start date : 04.03.2017
renamed mqtt_rf on 14.12.2017
mqtt application wrapper
*/
#include "mqtt_rf.hpp"
//for gethostname
#include <unistd.h>
#include "mesh.hpp"
//for printf
#include <stdio.h>
//for stdout
#include <iostream>
#include <string>
#include <cstring>
//for config
#include "utils.hpp"
//for Log::cout
#include "log.hpp"
#include "json.hpp"
using json = nlohmann::json;
//one per app
mqtt_rf_c::mqtt_rf_c(json &v_conf,Serial &l_rfcom) : mosquittopp("rf_gateway"),rfcom(l_rfcom)
{
conf = v_conf;
isConnected = false;
shouldBeConnected = false;
shouldPublish = false;
rgb.sendCount = 0;
//logfile : log into a file------------------------------------------------------
if(( conf.find("enable_connect") != conf.end() ) && conf["enable_connect"] )
{
if( conf.find("host") != conf.end() )
{
if( conf.find("port") != conf.end() )
{
mosqpp::lib_init();
if(conf.find("client_id") != conf.end())
{
char hostname[15];
gethostname(hostname,15);
std::string this_host(hostname);
std::string id = conf["client_id"];
id = id + "_" +this_host;
reinitialise(id.c_str(), true);
Log::cout << "mqtt"<<"\t"<<"client id: " << id << Log::Info();
}
int keepalive = 60;
int port = conf["port"];
std::string host = conf["host"];
shouldBeConnected = true;//independent of failure or success
int res = connect(host.c_str(), port, keepalive);
if(res == MOSQ_ERR_SUCCESS)
{
isConnected = true;
}
else
{
Log::cout << "mqtt"<<"\t"<<"X Failed to connect" << Log::Error();
}
}
}
if(isConnected)
{
Log::cout << "mqtt"<<"\t"<<"connected to " << conf["host"] << " : " << conf["port"] << Log::Info();
}
else
{
Log::cout << "mqtt"<<"\t"<<"X Connection failed, will not be used" << Log::Error();
}
if(( conf.find("enable_publish") != conf.end() ) && conf["enable_publish"] )
{
shouldPublish = true;
Log::cout << "mqtt"<<"\t"<<"publish enabled - as long as connected"<< Log::Info();
}
else
{
Log::cout << "mqtt"<<"\t"<<"publish not enabled"<< Log::Info();
}
}
else
{
Log::cout << "mqtt"<<"\t"<<"X Connection not enabled" << Log::Info();
}
};
void mqtt_rf_c::handle_dimmer(int TargetNodeId,json &jMsg)
{
bool found = false;
if(jMsg.find("All") != jMsg.end())
{
uint16_t light = jMsg["All"];
mesh::msg::dimmer::all(rfcom,TargetNodeId,light);
}
else if( (jMsg.find("Channel") != jMsg.end()) &&
(jMsg.find("Value") != jMsg.end()) )
{
Log::cout << "mqtt> dimmer Channel not yet implemented"<< Log::Warning();
}
else if(jMsg.find("Array") != jMsg.end())
{
Log::cout << "mqtt> dimmer Array not yet implemented"<< Log::Warning();
}
else if(jMsg.find("List") != jMsg.end())
{
Log::cout << "mqtt> dimmer List not yet implemented"<< Log::Warning();
}
else
{
Log::cout << "mqtt>\tUnknown dimmer format"<< Log::Error();
}
}
void mqtt_rf_c::handle_hexRGB(int TargetNodeId,std::string &message)
{
if(message.find("#")==0)
{
//the topic is "NodesActions/6/RGB"
rgb.NodeId = TargetNodeId;
utl::TakeParseTo(message,'#');
unsigned int hxVal = std::stoul(message, nullptr, 16);
rgb.R = ((hxVal >> 16) & 0xFF);
rgb.G = ((hxVal >> 8) & 0xFF);
rgb.B = ((hxVal) & 0xFF);
Log::cout << "mqtt"<<"\t"<<"=> NodeId:"<< rgb.NodeId << " RGB: ("<< rgb.R <<","<< rgb.G <<","<< rgb.B <<")"<< Log::Debug();
mesh::msg::color_txt(rfcom,rgb.NodeId,rgb.R,rgb.G,rgb.B);
}
}
void mqtt_rf_c::handle_RGB(int NodeId,json &jMsg)
{
bool found = false;
if(jMsg.find("Red") != jMsg.end())
if(jMsg.find("Green") != jMsg.end())
if(jMsg.find("Blue") != jMsg.end())
{
found = true;
}
if(found)
{
int R = std::stoi(jMsg["Red"].dump());
int G = std::stoi(jMsg["Green"].dump());
int B = std::stoi(jMsg["Blue"].dump());
Log::cout << "mqtt"<<"\t"<<"=> NodeId:"<< NodeId << " RGB: ("<< R <<","<< G <<","<< B <<")"<< Log::Debug();
mesh::msg::color_txt(rfcom,NodeId,R,G,B);
}
else
{
Log::cout << "mqtt>\twrong RGB format"<< Log::Error();
}
}
void mqtt_rf_c::handle_heat(int TargetNodeId,int heat_val)
{
char text[31];
int nbWrite = sprintf(text,"theat 0x%02x 0x%02x\r",TargetNodeId,heat_val);
rfcom.send(text,nbWrite);
std::string s(text);
Log::cout << "ser\t" << s << Log::Debug();
Log::cout << "mqtt"<<"\t"<<"=> NodeId:"<< TargetNodeId << " Heat: ("<< heat_val <<")"<< Log::Debug();
}
void mqtt_rf_c::handle_MeshRF(int NodeId,json &jMsg)
{
}
void mqtt_rf_c::handle_RawRF(std::string &message)
{
Log::cout << "mqtt"<<"\t"<<"sending:" << message << Log::Info();
mesh::raw::send_txt(rfcom,message);
}
void mqtt_rf_c::run()
{
static int cycle = 0;
if(shouldBeConnected)
{
int status = loop(0);//immediate return,
if((cycle % 500) == 0)
{
if( (status == MOSQ_ERR_CONN_LOST) || (status != MOSQ_ERR_SUCCESS) )
{
Log::cout << "mqtt"<<"\t"<<"error status("<<status << "), reconnecting" << Log::Error();
int res = reconnect();
if(res == MOSQ_ERR_SUCCESS)
{
isConnected = true;
}
else
{
Log::cout << "mqtt"<<"\t"<<"X Failed to reconnect" << Log::Error();
}
}
}
cycle++;
}
}
void mqtt_rf_c::on_connect(int rc)
{
Log::cout << "mqtt"<<"\t"<<"connected id(" << rc << ")" << Log::Info();
valueActions = "Nodes/";
jsonActions = "jNodes/";
std::string Subscribe1 = valueActions+"#";
std::string Subscribe2 = jsonActions +"#";
if(( conf.find("enable_subscribe") != conf.end() ) && conf["enable_subscribe"] )
{
//TODO rather subscribe to the "actions" list
//for .. subscribe(NULL,"Actions/+/{action1}");,...
subscribe(NULL,Subscribe1.c_str());
Log::cout << "mqtt\tsubscribing to: " << Subscribe1 << Log::Info();
subscribe(NULL,Subscribe2.c_str());
Log::cout << "mqtt\tsubscribing to: " << Subscribe2 << Log::Info();
}
else
{
Log::cout << "mqtt\tconnected but subscribe is not enabled: " << Log::Info();
}
}
void mqtt_rf_c::on_message(const struct mosquitto_message *message)
{
std::string msg(static_cast<const char*>(message->payload) );
std::string topic(message->topic);
//Log::cout << "mqtt"<<"\t"<<"Topic : "<< topic <<" ; Msg : "<< msg << Log::Debug();
std::string Action = topic;
utl::TakeParseTo(Action,'/');//remove first section "NodesActions/"
std::string Id = utl::TakeParseTo(Action,'/');//take the second element
int NodeId = (uint8_t)std::stoi(Id);
if(topic.find(valueActions) == 0)
{
if(Action.find("dimmer")==0)
{
uint16_t light = std::stoi(msg);
mesh::msg::dimmer::all(rfcom,NodeId,light);
}
else if(Action.find("heat")==0)
{
uint16_t heat = std::stoi(msg);
handle_heat(NodeId,heat);
}
}
else if(topic.find(jsonActions) == 0)
{
if(Action.find("RawRF")==0)
{
handle_RawRF(msg);//exception not json
}
else if(Action.find("hexRGB")==0)
{
handle_hexRGB(NodeId,msg);//exception not json
}
else
{
json jMsg = json::parse(msg);
if(Action.find("dimmer")==0)
{
handle_dimmer(NodeId,jMsg);
}
else if(Action.find("RGB")==0)
{
handle_RGB(NodeId,jMsg);
}
else if(Action.find("MeshRF")==0)
{
handle_MeshRF(NodeId,jMsg);
}
}
}
else
{
Log::cout << "mqtt"<<"\t"<<"unexpected Topic : "<< topic << Log::Debug();
Log::cout << "mqtt"<<"\t"<<"=> "<< msg<< Log::Debug();
}
}
void mqtt_rf_c::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
Log::cout << "mqtt"<<"\t"<<"subscribed to message id(" << mid << ")" << Log::Info();
}
void mqtt_rf_c::publish_measures(NodeMap_t &NodesSensorsVals)
{
if(isConnected && shouldPublish)
{
for(auto const& sensorsTables : NodesSensorsVals)
{
int NodeId = sensorsTables.first;
std::string Node = std::to_string(NodeId);
for(auto const& Table : sensorsTables.second)
{
std::string SensorName = Table.first;
for(auto const& Measure : Table.second)
{
//TODO should make the publish adress configurable
std::string topic = "Nodes/" + Node + "/" + SensorName;
std::string Value = std::to_string(Measure.value);
int status = publish(NULL,topic.c_str(),Value.size(),Value.c_str());
if(status == MOSQ_ERR_SUCCESS)
{
Log::cout << "mqtt" << "\t" << "published @ "<<topic << Log::Debug();
}
else
{
Log::cout << "mqtt" << "\t" << "publish Fail" << Log::Error();
}
}
}
}
}
}