-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmainwindow.cpp
255 lines (215 loc) · 7.92 KB
/
mainwindow.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Copyright (C) 2013-2017 Feng Lee <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QMQTT::Client *client, QWidget *parent) :
QMainWindow(parent),
_client(client),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->conForm->init(client);
ui->pubForm->init(client);
ui->subForm->init(client);
//add pushBtuoon slots
connect(ui->pushButtonConnect,SIGNAL(clicked(bool)),this,SLOT(connectServer()));
connect(ui->pushButtonPusblish,SIGNAL(clicked(bool)),this,SLOT(publishTopic()));
connect(ui->pushButtonSubscribe,SIGNAL(clicked(bool)),this,SLOT(subscribeTopic()));
connect(ui->pushButtonQuit,SIGNAL(clicked(bool)),this,SLOT(quitApp()));
connect(ui->pushButtonClear,SIGNAL(clicked(bool)),this,SLOT(clearMsg()));
connect(ui->pushButtonAbout,SIGNAL(clicked(bool)),this,SLOT(aboutApp()));
//connform slots
connect(_client, SIGNAL(disconnected()), ui->conForm, SLOT(updateUiStatus()));
connect(_client, SIGNAL(connected()), ui->conForm, SLOT(updateUiStatus()));
//mainwindow slots
connect(_client, SIGNAL(connected()), this, SLOT(onMQTT_Connected()));
//todo: should emit on server suback
//connect(_client, SIGNAL(connacked(quint8)), this, SLOT(onMQTT_Connacked(quint8)));
connect(_client, SIGNAL(error(QMQTT::ClientError)), this, SLOT(onMQTT_error(QMQTT::ClientError)));
//slots changes
//API: void published(const QMQTT::Message& message);
connect(_client,SIGNAL(published(const QMQTT::Message &)),this,SLOT(onMQTT_Published(const QMQTT::Message &)));
//todo: should emit on server suback
//connect(_client, SIGNAL(pubacked(quint8, quint16)), this, SLOT(onMQTT_Pubacked(quint8, quint16)));
connect(_client, SIGNAL(received(const QMQTT::Message &)), this, SLOT(onMQTT_Received(const QMQTT::Message &)));
connect(_client, SIGNAL(subscribed(const QString &)), this, SLOT(onMQTT_subscribed(const QString &)));
//todo: should emit on server suback
//connect(_client, SIGNAL(subacked(quint16, quint8)), this, SLOT(onMQTT_subacked(quint16, quint8)));
connect(_client, SIGNAL(unsubscribed(const QString &)), this, SLOT(onMQTT_unsubscribed(const QString &)));
//todo: should emit on server suback
//connect(_client, SIGNAL(unsubacked(quint16)), this, SLOT(onMQTT_unsubacked(quint16)));
//connect(_client, SIGNAL(pong()), this, SLOT(onMQTT_Pong()));
connect(_client, SIGNAL(disconnected()), this, SLOT(onMQTT_disconnected()));
}
MainWindow::~MainWindow()
{
delete ui;
}
/* -----------------------------------------------------------
MQTT Client Slots
-----------------------------------------------------------*/
void MainWindow::onMQTT_Connected()
{
log(tr("connected to %1:%2").arg(_client->host().toString()).arg(_client->port()));
//todo: should emit on server suback
ui->pushButtonPusblish->setEnabled(true);
ui->pushButtonSubscribe->setEnabled(true);
}
void MainWindow::onMQTT_Connacked(quint8 ack)
{
//todo: should emit on server suback
/*
QString ackStatus;
switch(ack) {
case QMQTT::CONNACK_ACCEPT:
ui->pushButtonPusblish->setEnabled(true);
ui->pushButtonSubscribe->setEnabled(true);
ackStatus = "Connection Accepted";
break;
case QMQTT::CONNACK_PROTO_VER:
ackStatus = "Connection Refused: unacceptable protocol version";
break;
case QMQTT::CONNACK_INVALID_ID:
ackStatus = "Connection Refused: identifier rejected";
break;
case QMQTT::CONNACK_SERVER:
ackStatus = "Connection Refused: server unavailable";
break;
case QMQTT::CONNACK_CREDENTIALS:
ackStatus = "Connection Refused: bad user name or password";
break;
case QMQTT::CONNACK_AUTH:
ackStatus = "Connection Refused: not authorized";
break;
}
log(tr("connacked: %1, %2").arg(ack).arg(ackStatus));
*/
}
void MainWindow::onMQTT_error(QMQTT::ClientError err)
{
//todo: should emit on server suback
/*
QString errInfo;
switch(err) {
// 0 The connection was refused by the peer (or timed out).
case QAbstractSocket::ConnectionRefusedError:
errInfo = tr("Connection Refused");
// 1 The remote host closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent.
case QAbstractSocket::RemoteHostClosedError:
errInfo = tr("Remote Host Closed");
// 2 The host address was not found.
case QAbstractSocket::HostNotFoundError:
errInfo = tr("Host Not Found Error");
// 3 The socket operation failed because the application lacked the required privileges.
case QAbstractSocket::SocketAccessError:
errInfo = tr("Socket Access Error");
// 4 The local system ran out of resources (e.g., too many sockets).
case QAbstractSocket::SocketResourceError:
errInfo = tr("Socket Resource Error");
// 5 The socket operation timed out.
case QAbstractSocket::SocketTimeoutError:
errInfo = tr("Socket Timeout Error");
default:
errInfo = tr("Socket Error");
}
log(errInfo);
*/
}
void MainWindow::onMQTT_Published(const QMQTT::Message &message)
{
log(tr("message published to %1").arg(message.topic()));
log(message.payload());
}
void MainWindow::onMQTT_Pubacked(quint8 type, quint16 msgid)
{
log(tr("pubacked: type=%1, msgid=%2").arg(type, msgid));
}
void MainWindow::onMQTT_Received(const QMQTT::Message &message)
{
log(tr("message recevied from %1: qos=%2").arg(message.topic()).arg(message.qos()));
log(message.payload());
}
void MainWindow::onMQTT_subscribed(const QString &topic)
{
log(tr("subscribed %1").arg(topic));
}
void MainWindow::onMQTT_subacked(quint16 msgid, quint8 qos)
{
log(tr("subacked: msgid=%1, qos=%2").arg(msgid).arg(qos));
}
void MainWindow::onMQTT_unsubscribed(const QString &topic)
{
log(tr("unsubscribed %1").arg(topic));
}
void MainWindow::onMQTT_unsubacked(quint16 msgid)
{
log(tr("unsubacked: msgid=%1").arg(msgid));
}
void MainWindow::onMQTT_Pong()
{
log("pong received.");
}
void MainWindow::onMQTT_disconnected()
{
ui->pushButtonPusblish->setEnabled(false);
ui->pushButtonSubscribe->setEnabled(false);
log("disconnected");
}
/* -----------------------------------------------------------
UI Action Slots
-----------------------------------------------------------*/
void MainWindow::log(const QString & msg)
{
ui->logConsole->append(msg);
}
void MainWindow::quitApp()
{
qApp->quit();
}
void MainWindow::connectServer()
{
ui->stackedWidget->setCurrentWidget(ui->conForm);
}
void MainWindow::aboutApp()
{
QMessageBox::information(NULL, tr("About QMQTT"),
tr("Version: 0.1.1\n\n"
"MQTT Client written with Qt\n\n"
"Site: https://www.github.com/emqtt/qmqtt-client\n"));
}
void MainWindow::clearMsg()
{
ui->logConsole->clear();
}
void MainWindow::subscribeTopic()
{
ui->stackedWidget->setCurrentWidget(ui->subForm);
ui->subForm->clearLineEdit(); // also sets focus to lineEdit
}
void MainWindow::publishTopic()
{
ui->stackedWidget->setCurrentWidget(ui->pubForm);
ui->pubForm->lineEditSetFocus();
}
void MainWindow::on_actionAbout_triggered()
{
aboutApp();
}
void MainWindow::on_actionQuit_triggered()
{
quitApp();
}