forked from mannkind/rf24node_msgproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTTWrapper.cpp
55 lines (43 loc) · 1.8 KB
/
MQTTWrapper.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
#include "MQTTWrapper.h"
MQTTWrapper::MQTTWrapper(std::string id, std::string host, int port, std::string tls_ca_file, std::string tls_cert_file, std::string tls_key_file, bool tls_insecure_mode) :
mosqpp::mosquittopp(id.c_str(), true), host(host), port(port), tls_ca_file(tls_ca_file), tls_cert_file(tls_cert_file), tls_key_file(tls_key_file), tls_insecure_mode(tls_insecure_mode) {}
void MQTTWrapper::begin(void) {
mosqpp::lib_init();
if (!this->tls_ca_file.empty() && !this->tls_cert_file.empty() && !this->tls_key_file.empty()) {
printf("Enabling TLS mode.\n");
this->tls_set(this->tls_ca_file.c_str(), NULL, this->tls_cert_file.c_str(), this->tls_key_file.c_str());
this->tls_insecure_set(this->tls_insecure_mode);
}
printf("Connecting to MQTT\n");
this->connect(this->host.c_str(), this->port, 60 /* keepalive */);
}
void MQTTWrapper::end(void) {
mosqpp::lib_cleanup();
}
void MQTTWrapper::loop(void) {
mosqpp::mosquittopp::loop();
}
void MQTTWrapper::send_message(std::string subject, std::string body) {
this->publish(nullptr, subject.c_str(), body.length(), body.c_str(), 0);
}
void MQTTWrapper::set_on_message_callback(on_msg_cb cb) {
this->cb = cb;
}
void MQTTWrapper::on_connect(int rc) {
this->subscribe(nullptr, "/sensornet/in/#");
}
void MQTTWrapper::on_log(int level, const char *str) {
//printf("LOG [%d] %s\n", level, str);
}
void MQTTWrapper::on_disconnect(int mid) {
printf("Disconnecting from MQTT\n");
this->end();
sleep(15);
this->begin();
}
void MQTTWrapper::on_message(const struct mosquitto_message *message) {
auto subject = std::string(message->topic);
auto body = message->payloadlen > 0 ?
std::string(reinterpret_cast<char *>(message->payload)) : std::string("");
this->cb(subject, body);
}