-
Notifications
You must be signed in to change notification settings - Fork 2
/
MqttClientDbgCommand.cpp
237 lines (210 loc) · 8.27 KB
/
MqttClientDbgCommand.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
/*
* MqttClientDbgCommand.cpp
*
* Created on: 18.10.2016
* Author: nid
*/
#include <DbgCliNode.h>
#include <DbgCliTopic.h>
#include <DbgTraceContext.h>
#include <DbgTracePort.h>
#include <DbgTraceLevel.h>
#include <DbgPrintConsole.h>
#include <DbgTraceOut.h>
#include <IMqttClientWrapper.h>
#include <MqttClientController.h>
#include <MqttClientDbgCommand.h>
#include <ConnectionMonitor.h>
#include <MqttTopic.h>
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientCon::DbgCli_Cmd_MqttClientCon(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "con", "Connect MQTT client to broker.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientCon::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
if (argc - idxToFirstArgToHandle > 0)
{
printUsage();
}
else
{
if (0 != m_mqttClient)
{
if (m_mqttClient->mqttClientWrapper()->connected())
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client is already connected to broker.");
}
else
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client connecting to broker now.");
m_mqttClient->setShallConnect(true);
}
}
}
}
void DbgCli_Cmd_MqttClientCon::printUsage()
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "%s", getHelpText());
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Usage: %s %s %s", RootNode()->getNodeName(), getParentNode()->getNodeName(), getNodeName());
}
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientDis::DbgCli_Cmd_MqttClientDis(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "dis", "Disconnect MQTT client from broker.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientDis::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
if (argc - idxToFirstArgToHandle > 0)
{
printUsage();
}
else
{
if (0 != m_mqttClient)
{
m_mqttClient->setShallConnect(false);
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client disconnecting from broker now.");
}
}
}
void DbgCli_Cmd_MqttClientDis::printUsage()
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "%s", getHelpText());
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Usage: %s %s %s", RootNode()->getNodeName(), getParentNode()->getNodeName(), getNodeName());
}
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientPub::DbgCli_Cmd_MqttClientPub(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "pub", "Publish a value to a topic using MQTT client.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientPub::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
if (argc - idxToFirstArgToHandle != 2)
{
printUsage();
}
else
{
if (0 != m_mqttClient)
{
MqttTopicPublisher* publisher = m_mqttClient->findPublisherByTopic(args[idxToFirstArgToHandle]);
if (0 == publisher)
{
publisher = new MqttTopicPublisher(args[idxToFirstArgToHandle], args[idxToFirstArgToHandle+1], MqttTopicPublisher::DO_AUTO_PUBLISH);
}
else
{
publisher->setData(args[idxToFirstArgToHandle+1]);
publisher->publish();
}
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client, published to %s", publisher->getTopicString(), publisher->getData());
}
}
}
void DbgCli_Cmd_MqttClientPub::printUsage()
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "%s", getHelpText());
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Usage: %s %s %s <topic> <value>", RootNode()->getNodeName(), getParentNode()->getNodeName(), getNodeName());
}
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientSub::DbgCli_Cmd_MqttClientSub(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "sub", "Subscribe to a topic using MQTT client.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientSub::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
if (argc - idxToFirstArgToHandle != 1)
{
printUsage();
}
else
{
if (0 != m_mqttClient)
{
MqttTopicSubscriber* subscriber = m_mqttClient->findSubscriberByTopic(args[idxToFirstArgToHandle]);
if (0 == subscriber)
{
subscriber = new DefaultMqttSubscriber(args[idxToFirstArgToHandle]);
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client, subscribed to %s", subscriber->getTopicString());
}
else
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client, already subscribed to %s", subscriber->getTopicString());
}
}
}
}
void DbgCli_Cmd_MqttClientSub::printUsage()
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "%s", getHelpText());
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Usage: %s %s %s <topic>", RootNode()->getNodeName(), getParentNode()->getNodeName(), getNodeName());
}
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientUnsub::DbgCli_Cmd_MqttClientUnsub(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "unsub", "Unsubscribe a topic using MQTT client.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientUnsub::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
if (argc - idxToFirstArgToHandle != 1)
{
printUsage();
}
else
{
if (0 != m_mqttClient)
{
MqttTopicSubscriber* subscriber = m_mqttClient->findSubscriberByTopic(args[idxToFirstArgToHandle]);
if (0 != subscriber)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client, unsubscribing from %s", subscriber->getTopicString());
delete subscriber;
subscriber = 0;
}
else
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT client, unsubscribe not possible, topic %s was not subscribed.", args[idxToFirstArgToHandle]);
}
}
}
}
void DbgCli_Cmd_MqttClientUnsub::printUsage()
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "%s", getHelpText());
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Usage: %s %s %s <topic>", RootNode()->getNodeName(), getParentNode()->getNodeName(), getNodeName());
}
//-----------------------------------------------------------------------------
DbgCli_Cmd_MqttClientShow::DbgCli_Cmd_MqttClientShow(DbgCli_Topic* mqttClientTopic, MqttClientController* mqttClient)
: DbgCli_Command(mqttClientTopic, "show", "Show info from the MQTT client.")
, m_mqttClient(mqttClient)
{ }
void DbgCli_Cmd_MqttClientShow::execute(unsigned int argc, const char** args, unsigned int idxToFirstArgToHandle)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Subscriber Topics:");
MqttTopicSubscriber* subscriber = m_mqttClient->mqttSubscriberChain();
if (0 == subscriber)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "=x= no subscribers in the list.");
}
while (0 != subscriber)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "- %s", subscriber->getTopicString());
subscriber = subscriber->next();
}
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Publisher Topics:");
MqttTopicPublisher* publisher = m_mqttClient->mqttPublisherChain();
if (0 == publisher)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "=x= no publishers in the list.");
}
while (0 != publisher)
{
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "- %s", publisher->getTopicString());
publisher = publisher->next();
}
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "MQTT Client Status: Shall %s connect", m_mqttClient->getShallConnect() ? "" : "not ");
TR_PRINTF(m_mqttClient->trPort(), DbgTrace_Level::alert, "Connection Monitor State: %s", m_mqttClient->connMon()->state()->toString());
m_mqttClient->loop();
}
//-----------------------------------------------------------------------------