-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.cpp
160 lines (135 loc) · 3.86 KB
/
mqtt.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
#include "freertos/FreeRTOS.h"
#include "mqtt_client.h"
#include "espp/mqtt.h"
#include "espp/utils/macros.h"
#include <utility>
#include <algorithm>
namespace espp {
namespace {
bool _SetMqttSingleton(bool init)
{
static bool is_inited = false;
if(init == is_inited) {
return false;
}
is_inited = init;
return true;
}
}
Mqtt::Mqtt(std::string url, std::string status_topic):
_url(std::move(url)),
_status_topic(std::move(status_topic))
{
ESPP_CHECK(_SetMqttSingleton(true));
}
Mqtt::~Mqtt()
{
ESPP_CHECK(_SetMqttSingleton(false));
if(_client != nullptr) {
ESP_ERROR_CHECK(esp_mqtt_client_destroy(_client));
}
}
void Mqtt::Init(const Buffer& status_msg)
{
INFO << "Init MQTT connection to" << _url;
esp_mqtt_client_config_t mqtt_cfg = {};
mqtt_cfg.uri = _url.data();
mqtt_cfg.event_handle = _EventHandler;
mqtt_cfg.user_context = this;
if(!_status_topic.empty()) {
DEBUG << "Init Mqtt LWT";
assert(!status_msg.empty());
mqtt_cfg.lwt_msg = status_msg.charData();
mqtt_cfg.lwt_msg_len = status_msg.length();
mqtt_cfg.lwt_topic = _status_topic.c_str();
mqtt_cfg.lwt_qos = 0;
mqtt_cfg.lwt_retain = 1;
mqtt_cfg.keepalive = _keep_alive_timeout;
}
_client = esp_mqtt_client_init(&mqtt_cfg);
ESPP_CHECK(_client != nullptr);
}
bool Mqtt::Connect()
{
INFO << "Connect Mqtt";
Mutex::LockGuard lock(_mutex);
ESPP_ASSERT(_client != nullptr);
const auto result = ESP_OK == esp_mqtt_client_start(_client);
DEBUG << "Connection finished" << result;
return result;
}
bool Mqtt::Disconnect()
{
INFO << "Disconnect Mqtt";
Mutex::LockGuard lock(_mutex);
const auto result = ESP_OK == esp_mqtt_client_stop(_client);
DEBUG << "Disconnection finished" << result;
return result;
}
void Mqtt::OnConnect(int)
{
}
void Mqtt::OnDisconnect()
{
}
void Mqtt::OnEvent(const esp_mqtt_event_handle_t& event)
{
Mutex::LockGuard lock(_mutex);
const Buffer topic(event->topic, event->topic_len);
DEBUG << "Got message in topic" << topic << event->topic;
const auto it = std::find_if(_subscriptions.begin(), _subscriptions.end(),
[&topic](const SubList::value_type& o) { return topic == o.first;});
if(it != _subscriptions.end()) {
DEBUG << "Found subscription";
it->second->OnEvent(event);
} else {
DEBUG << "Can't find subscription";
}
}
esp_err_t Mqtt::_EventHandler(esp_mqtt_event_handle_t event)
{
Mqtt* impl = reinterpret_cast<Mqtt*>(event->user_context);
impl->_ProcessEvent(event);
return ESP_OK;
}
void Mqtt::_ProcessEvent(esp_mqtt_event_handle_t event)
{
switch(event->event_id) {
case MQTT_EVENT_CONNECTED:
_ProcessConnect(event->session_present);
OnConnect(event->session_present);
break;
case MQTT_EVENT_DISCONNECTED:
_ProcessDisconnect();
OnDisconnect();
break;
case MQTT_EVENT_DATA:
OnEvent(event);
break;
default:;
}
}
void Mqtt::_ProcessConnect(int)
{
INFO << "Mqtt connected";
Mutex::LockGuard lock(_mutex);
for(auto& subscription: _subscriptions) {
DEBUG << "Subscribe" << subscription.first;
ESPP_CHECK(esp_mqtt_client_subscribe(_client, subscription.first.c_str(), 0) != -1);
}
_is_connected = true;
DEBUG << "Connection finished";
}
void Mqtt::_ProcessDisconnect()
{
INFO << "Mqtt disconnected";
Mutex::LockGuard lock(_mutex);
_is_connected = false;
}
void Mqtt::Subscribe(MqttSubscription& subscription, const std::string& topic)
{
assert(std::find_if(_subscriptions.begin(), _subscriptions.end(),
[&topic](const SubList::value_type& o) { return topic == o.first;}) == _subscriptions.end());
_subscriptions.push_back(std::make_pair(topic, &subscription));
}
}