-
Notifications
You must be signed in to change notification settings - Fork 2
/
MqttClientWrapper.cpp
172 lines (147 loc) · 3.59 KB
/
MqttClientWrapper.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
/*
* MqttClientWrapper.cpp
*
* Created on: 22.06.2018
* Author: nid
*/
#include <DbgTracePort.h>
#include <DbgTraceLevel.h>
#include <MQTT.h>
#include <MqttTopic.h>
#include <MqttClientController.h>
#include <MqttClientWrapper.h>
IMqttClientWrapper* MqttClientWrapper::s_mqttClientWrapper = 0;
MqttClientWrapper::MqttClientWrapper()
: m_mqttClient(new MQTTClient(512))
, m_callbackAdapter(0)
{
s_mqttClientWrapper = this;
}
MqttClientWrapper::~MqttClientWrapper()
{ }
void mqttClientCallback(String& topic, String& payload)
{
IMqttClientWrapper* mqttWrapper = MqttClientWrapper::s_mqttClientWrapper;
if (0 != mqttWrapper)
{
IMqttClientCallbackAdapter* callbackAdapter = mqttWrapper->callbackAdapter();
if (0 != callbackAdapter)
{
callbackAdapter->messageReceived(topic.c_str(), payload.c_str(), payload.length());
}
}
}
void MqttClientWrapper::setCallbackAdapter(IMqttClientCallbackAdapter* callbackAdapter)
{
m_callbackAdapter = callbackAdapter;
if (0 != m_mqttClient)
{
m_mqttClient->onMessage(mqttClientCallback);
}
}
IMqttClientCallbackAdapter* MqttClientWrapper::callbackAdapter()
{
return m_callbackAdapter;
}
void MqttClientWrapper::begin(const char* domain, uint16_t port, Client& client)
{
if (0 != m_mqttClient)
{
m_mqttClient->begin(domain, port, client);
}
}
bool MqttClientWrapper::connect(const char* id)
{
bool isConnected = false;
if (0 != m_mqttClient)
{
isConnected = m_mqttClient->connect(id);
}
return isConnected;
}
void MqttClientWrapper::disconnect()
{
if (0 != m_mqttClient)
{
m_mqttClient->disconnect();
}
}
bool MqttClientWrapper::connected()
{
bool isConnected = false;
if (0 != m_mqttClient)
{
isConnected = m_mqttClient->connected();
}
return isConnected;
}
bool MqttClientWrapper::loop()
{
bool isConnected = false;
if (0 != m_mqttClient)
{
isConnected = m_mqttClient->loop();
}
return isConnected;
}
unsigned char MqttClientWrapper::publish(const char* topic, const char* data)
{
unsigned char ret = 0;
if (0 != m_mqttClient)
{
ret = m_mqttClient->publish(topic, data);
}
return ret;
}
unsigned char MqttClientWrapper::subscribe(const char* topic)
{
unsigned char ret = 0;
if (0 != m_mqttClient)
{
ret = m_mqttClient->subscribe(topic);
}
return ret;
}
unsigned char MqttClientWrapper::unsubscribe(const char* topic)
{
unsigned char ret = 0;
if (0 != m_mqttClient)
{
ret = m_mqttClient->unsubscribe(topic);
}
return ret;
}
IMqttClientWrapper::eIMqttClientState MqttClientWrapper::state()
{
// TODO
eIMqttClientState iMqttClientState = eIMqttCS_ConnectUnavailable;
return iMqttClientState;
};
//-----------------------------------------------------------------------------
MqttClientCallbackAdapter::MqttClientCallbackAdapter()
: m_trPortMqttRx(new DbgTrace_Port("mqttrx", DbgTrace_Level::info))
, m_rxMsg(new MqttRxMsg())
{ }
MqttClientCallbackAdapter::~MqttClientCallbackAdapter()
{
delete m_rxMsg;
m_rxMsg = 0;
delete m_trPortMqttRx;
m_trPortMqttRx = 0;
}
void MqttClientCallbackAdapter::messageReceived(const char* topic, const char* payload, unsigned int length)
{
char msg[length+1];
memcpy(msg, payload, length);
msg[length] = 0;
TR_PRINTF(m_trPortMqttRx, DbgTrace_Level::info, "Message arrived, topic: %s - msg: %s (len: %d)", topic, msg, length);
if (0 != m_rxMsg)
{
m_rxMsg->prepare(topic, payload, length);
}
MqttTopicSubscriber* mqttSubscriberChain = MqttClientController::Instance()->mqttSubscriberChain();
if (0 != mqttSubscriberChain)
{
mqttSubscriberChain->handleMessage(m_rxMsg, m_trPortMqttRx);
}
}