-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleCode.cpp
More file actions
184 lines (168 loc) · 5.53 KB
/
SampleCode.cpp
File metadata and controls
184 lines (168 loc) · 5.53 KB
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
#include "../include/BeMqttClient.h"
#include <iostream>
#include <unistd.h>
uint64_t timestamp()
{
auto now = std::chrono::system_clock::now();
auto tse = now.time_since_epoch();
auto msTm = std::chrono::duration_cast<std::chrono::milliseconds>(tse);
return uint64_t(msTm.count());
}
//Callback : connected
void v_Connected(BeMqttClient *client, const std::string &cause)
{
//Optional: implement callback code
return;
if (client != NULL)
{
cout << "<" << (client) << "> " << client->classname << " [" << client->getClientID() << "] :" << endl;
}
std::cout << "\tConnected: [" << cause << "]" << std::endl;
}
//Callback : connection lost
void v_ConnectionLost(BeMqttClient *client, const std::string &cause)
{
//Optional: implement callback code
return;
if (client != NULL)
{
cout << "<" << (client) << "> " << client->classname << " [" << client->getClientID() << "] ConnectionLost:" << endl;
}
std::cout << "\tConnectionLost: [" << cause << "]" << std::endl;
if (!cause.empty())
{
std::cout << "\tcause: " << cause << std::endl;
}
}
//Callback : message arrival
void v_MsgArrived(BeMqttClient *client, mqtt::const_message_ptr msg)
{
//Optional: implement callback code
if (client != NULL)
{
cout << "[" << client->getClientID() << "] " << client->classname << " :" << endl;
std::cout << "\tMessage arrived at " << timestamp() << ":" <<endl;
}
std::cout << "\ttopic: '" << msg->get_topic() << "' "
<< " message: '" << msg->to_string() << "'" << std::endl;
}
void v_DeliveryComplete(BeMqttClient *client, mqtt::delivery_token_ptr token)
{
//Optional: implement callback code
return;
if (client != NULL)
{
cout << "[" << client->getClientID() << "] " << client->classname << " :" << endl;
}
std::cout << "\tDeliveryComplete:" << (token ? token->get_message()->get_payload_str() : "NULL") << std::endl;
}
void v_onSuccess(BeMqttClient *client, const mqtt::token &tok)
{
//Optional: implement callback code
if (client != NULL)
{
cout << "<" << (client) << "> " << client->classname << " [" << client->getClientID() << "] On Success:" << endl;
}
if (tok.get_message_id() <= 0)
{
std::cout << "\tOn Success for FIRST token: [" << tok.get_message_id() << "]" << std::endl;
}
else
{
std::cout << "\tOn Success token: [" << tok.get_message_id() << "]" << std::endl;
}
auto top = tok.get_topics();
if (top && !top->empty())
{
std::cout << "\tSuccess Token topic id: [" << tok.get_message_id() << "] topic: "
<< (*top)[0] << "', ..." << std::endl;
}
}
void v_OnFailure(BeMqttClient *client, const mqtt::token &tok)
{
//Optional: implement callback code
return;
if (client != NULL)
{
cout << "<" << (client) << "> " << client->classname << " [" << client->getClientID() << "] On Failure:" << endl;
}
cout << "\n\tListener failure for token: [" << tok.get_message_id() << "]"
<< tok.get_type() << endl;
}
void printCommand(bool listtitle)
{
if (listtitle)
{
std::cout << "Command List :" << std::endl;
}
std::cout << " - Press [P] [Enter] to Publish" << std::endl;
std::cout << " - Press [T] [Enter] to Print Datetime." << std::endl;
std::cout << " - Press [Q] [Enter] to Quit." << std::endl;
std::cout << "Input command : " << std::flush;
}
int main()
{
std::string clientid = "ClientID-001";
BeMqttClient client(clientid,BeMqttOption::BrokerAddr,BeMqttOption::BrokerUsername,BeMqttOption::BrokerPassword);
client.setVerbose(Verbose::VerboseError);
//client.setConnected(v_Connected);
//client.setConnectionLost(v_ConnectionLost);
client.setMessageArrived(v_MsgArrived);
client.setDeliveryComplete(v_DeliveryComplete);
//client.setOnSuccess(v_onSuccess);
//client.setOnFailure(v_OnFailure);
client.connect();
client.subscribe("#", QosLevel::QoSLv1);
client.publish("T1","Test");
//BeMqttClient cli2("AB");
//cli2.setMessageArrived(v_MsgArr2);
//cli2.connect();
//cli2.subscribe("T2");
printCommand(true);
//while (std::tolower(std::cin.get()) != 'q')
//;
//鍵盤輸入 Q 離開
std::string inputString;
bool doLoop = true;
while (doLoop)
{
//char q = std::tolower(std::cin.get());
getline(std::cin, inputString);
for (auto &c : inputString)
{
c = tolower(c);
}
if (inputString == "q" || inputString == "exit" || inputString == "quit")
{
doLoop = false;
}
else if (inputString == "p")
{
int i = 1;
for (i = 1; i <= 100; i++)
{
char msg[150];
sprintf(msg, "No. %i : timestamp %ld", i, timestamp());
client.publish("T1", msg, QosLevel::QoSLv1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
printCommand(true);
}
else if (inputString == "t"){
int i = 1;
for (i = 1; i <= 40; i++)
{
std::cout << "No." << i << " : Timestamp " << timestamp() << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
printCommand(true);
}
else
{
std::cout << "Error!! Invalid input : " << inputString << std::endl;
printCommand(false);
}
};
usleep(1);
client.disConnect();
}