mqtt_dialog.cpp
Go to the documentation of this file.
1 #include "mqtt_dialog.h"
2 #include <QFileDialog>
3 #include "PlotJuggler/svg_util.h"
4 
6 {
7  _client->disconnect();
9  ui->listWidget->clear();
10  _topic_list.clear();
11  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
12  _topic_list_timer->stop();
13 }
14 
16  : QDialog(nullptr), ui(new Ui::DataStreamMQTT), _client(mosq_client)
17 {
18  ui->setupUi(this);
19  ui->lineEditPort->setValidator(new QIntValidator);
20 
21  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
22 
23  static QString uuid = QString("Plotjuggler-") + QString::number(rand());
24 
25  QSettings settings;
26  restoreGeometry(settings.value("MosquittoMQTT::geometry").toByteArray());
27 
28  QString host = settings.value("MosquittoMQTT::host").toString();
29  ui->lineEditHost->setText(host);
30 
31  int port = settings.value("MosquittoMQTT::port", 1883).toInt();
32  ui->lineEditPort->setText(QString::number(port));
33 
34  int qos = settings.value("MosquittoMQTT::qos", 0).toInt();
35  ui->comboBoxQoS->setCurrentIndex(qos);
36 
37  int protocol_mqtt = settings.value("MosquittoMQTT::protocol_version").toInt();
38  ui->comboBoxVersion->setCurrentIndex(protocol_mqtt);
39 
40  QString topic_filter = settings.value("MosquittoMQTT::filter").toString();
41  ui->lineEditTopicFilter->setText(topic_filter);
42 
43  QString username = settings.value("MosquittoMQTT::username", "").toString();
44  ui->lineEditUsername->setText(username);
45 
46  QString password = settings.value("MosquittoMQTT::password", "").toString();
47  ui->lineEditPassword->setText(password);
48 
50  settings.value("MosquittoMQTT::server_certificate", "").toString();
51  if (!_server_certificate_file.isEmpty())
52  {
53  ui->labelServerCertificate->setText(QFileInfo(_server_certificate_file).fileName());
54  ui->buttonEraseServerCertificate->setEnabled(true);
55  }
56 
58  settings.value("MosquittoMQTT::client_certificate", "").toString();
59  if (!_client_certificate_file.isEmpty())
60  {
61  ui->labelClientCertificate->setText(QFileInfo(_client_certificate_file).fileName());
62  ui->buttonEraseClientCertificate->setEnabled(true);
63  }
64 
65  _private_key_file = settings.value("MosquittoMQTT::private_key", "").toString();
66  if (!_private_key_file.isEmpty())
67  {
68  ui->labelPrivateKey->setText(QFileInfo(_private_key_file).fileName());
69  ui->buttonErasePrivateKey->setEnabled(true);
70  }
71 
72  bool enable_tls = settings.value("MosquittoMQTT::enable_tls", false).toBool();
73  ui->checkBoxSecurity->setChecked(enable_tls);
74 
75  _topic_list_timer = new QTimer(this);
76 
77  QString theme = settings.value("StyleSheet::theme", "light").toString();
78 
79  const QPixmap& icon_pixmap = LoadSvg(":/resources/svg/import.svg", theme);
80  ui->buttonLoadServerCertificate->setIcon(icon_pixmap);
81  ui->buttonLoadClientCertificate->setIcon(icon_pixmap);
82  ui->buttonLoadPrivateKey->setIcon(icon_pixmap);
83 
84  connect(ui->buttonConnect, &QPushButton::clicked, this, &MQTT_Dialog::onButtonConnect);
85 
86  connect(_topic_list_timer, &QTimer::timeout, this, &MQTT_Dialog::onUpdateTopicList);
87 
88  connect(ui->listWidget, &QListWidget::itemSelectionChanged, this,
90 
91  connect(ui->buttonLoadServerCertificate, &QPushButton::clicked, this,
93 
94  connect(ui->buttonLoadClientCertificate, &QPushButton::clicked, this,
96 
97  connect(ui->buttonLoadPrivateKey, &QPushButton::clicked, this,
99 
100  connect(ui->buttonEraseServerCertificate, &QPushButton::clicked, this, [this]() {
101  ui->buttonEraseServerCertificate->setEnabled(false);
102  ui->labelServerCertificate->setText("");
103  _server_certificate_file.clear();
104  });
105 
106  connect(ui->buttonEraseClientCertificate, &QPushButton::clicked, this, [this]() {
107  ui->buttonEraseClientCertificate->setEnabled(false);
108  ui->labelClientCertificate->setText("");
109  _client_certificate_file.clear();
110  });
111 
112  connect(ui->buttonErasePrivateKey, &QPushButton::clicked, this, [this]() {
113  ui->buttonErasePrivateKey->setEnabled(false);
114  ui->labelPrivateKey->setText("");
115  _private_key_file.clear();
116  });
117 
118  connect(_client.get(), &MQTTClient::disconnected, this, [this]() {
119  onConnectionClosed();
120  QMessageBox::warning(this, "Connection Lost",
121  "Client disconnected. Maybe a problem with autentication?");
122  });
123 }
124 
126 {
127  QSettings settings;
128  settings.setValue("MosquittoMQTT::geometry", this->saveGeometry());
129 
130  // save back to service
131  settings.setValue("MosquittoMQTT::host", ui->lineEditHost->text());
132  settings.setValue("MosquittoMQTT::port", ui->lineEditPort->text().toInt());
133  settings.setValue("MosquittoMQTT::filter", ui->lineEditTopicFilter->text());
134  settings.setValue("MosquittoMQTT::username", ui->lineEditPassword->text());
135  settings.setValue("MosquittoMQTT::password", ui->lineEditUsername->text());
136  settings.setValue("MosquittoMQTT::protocol_version",
137  ui->comboBoxVersion->currentIndex());
138  settings.setValue("MosquittoMQTT::qos", ui->comboBoxQoS->currentIndex());
139  settings.setValue("MosquittoMQTT::serialization_protocol",
140  ui->comboBoxProtocol->currentText());
141  settings.setValue("MosquittoMQTT::server_certificate", _server_certificate_file);
142  settings.setValue("MosquittoMQTT::client_certificate", _client_certificate_file);
143  settings.setValue("MosquittoMQTT::private_key", _private_key_file);
144  settings.setValue("MosquittoMQTT::enable_tls", ui->checkBoxSecurity->isChecked());
145 }
146 
148 {
149  while (ui->layoutOptions->count() > 0)
150  {
151  auto item = ui->layoutOptions->takeAt(0);
152  item->widget()->setParent(nullptr);
153  }
154 
155  saveSettings();
156 
157  delete ui;
158 }
159 
161 {
162  if (_client->isConnected())
163  {
165  return;
166  }
167 
168  MosquittoConfig config;
169 
170  config.host = ui->lineEditHost->text().toStdString();
171  config.port = ui->lineEditPort->text().toInt();
172  config.topics.clear();
173  config.topics.push_back(ui->lineEditTopicFilter->text().toStdString());
174  config.username = ui->lineEditUsername->text().toStdString();
175  config.password = ui->lineEditPassword->text().toStdString();
176  config.qos = ui->comboBoxQoS->currentIndex();
177  config.protocol_version = MQTT_PROTOCOL_V31 + ui->comboBoxVersion->currentIndex();
178 
179  config.keepalive = 60; // TODO
180  config.bind_address = ""; // TODO
181  config.max_inflight = 20; // TODO
182  config.clean_session = true; // TODO
183  config.eol = true; // TODO
184 
185  if (ui->checkBoxSecurity->isChecked())
186  {
187  config.cafile = _server_certificate_file.toStdString();
188  config.certfile = _client_certificate_file.toStdString();
189  config.keyfile = _private_key_file.toStdString();
190  }
191 
192  changeConnectionState(true);
193  _client->connect(config);
194  _topic_list_timer->start(2000);
195 
196  saveSettings();
197 }
198 
200 {
201  auto topic_list = _client->getTopicList();
202 
203  bool changed = ui->listWidget->count() != topic_list.size();
204  if (!changed)
205  {
206  for (const auto& topic : topic_list)
207  {
208  if (_topic_list.count(topic) == 0)
209  {
210  changed = true;
211  break;
212  }
213  }
214  }
215 
216  if (changed)
217  {
218  ui->listWidget->clear();
219  for (const auto& topic : topic_list)
220  {
221  ui->listWidget->addItem(QString::fromStdString(topic));
222  _topic_list.insert(topic);
223  }
224  ui->listWidget->sortItems();
225  }
226 
227  if (_client->isConnected() == false)
228  {
230  }
231 }
232 
234 {
235  bool selected = ui->listWidget->selectedItems().count() > 0;
236  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(selected);
237 }
238 
240 {
241  ui->connectionFrame->setEnabled(!connected);
242  ui->buttonConnect->setText(connected ? "Disconnect" : "Connect");
243  ui->lineEditTopicFilter->setEnabled(!connected);
244  ui->listWidget->setEnabled(connected);
245 }
246 
248 {
249  QSettings settings;
250  QString directory =
251  settings.value("MQTT_Dialog.loadDirectory", QDir::currentPath()).toString();
252 
253  QString filename = QFileDialog::getOpenFileName(this, "Select Server Certificate",
254  directory, tr("CRT (*.crt)"));
255 
256  if (!filename.isEmpty())
257  {
258  _server_certificate_file = filename;
259  directory = QFileInfo(filename).absolutePath();
260  settings.setValue("MQTT_Dialog.loadDirectory", directory);
261 
262  ui->labelServerCertificate->setText(QFileInfo(filename).fileName());
263  ui->buttonEraseServerCertificate->setEnabled(true);
264  }
265 }
266 
268 {
269  QSettings settings;
270  QString directory =
271  settings.value("MQTT_Dialog.loadDirectory", QDir::currentPath()).toString();
272 
273  QString filename = QFileDialog::getOpenFileName(this, "Select Client Certificate",
274  directory, tr("CRT (*.crt)"));
275 
276  if (!filename.isEmpty())
277  {
278  _client_certificate_file = filename;
279  directory = QFileInfo(filename).absolutePath();
280  settings.setValue("MQTT_Dialog.loadDirectory", directory);
281 
282  ui->labelClientCertificate->setText(QFileInfo(filename).fileName());
283  ui->buttonEraseClientCertificate->setEnabled(true);
284  }
285 }
286 
288 {
289  QSettings settings;
290  QString directory =
291  settings.value("MQTT_Dialog.loadDirectory", QDir::currentPath()).toString();
292 
293  QString filename = QFileDialog::getOpenFileName(this, "Select PrivateKey", directory,
294  tr("Key (*.key)"));
295 
296  if (!filename.isEmpty())
297  {
298  _private_key_file = filename;
299  directory = QFileInfo(filename).absolutePath();
300  settings.setValue("MQTT_Dialog.loadDirectory", directory);
301 
302  ui->labelPrivateKey->setText(QFileInfo(filename).fileName());
303  ui->buttonErasePrivateKey->setEnabled(true);
304  }
305 }
MQTT_Dialog::saveSettings
void saveSettings()
Definition: mqtt_dialog.cpp:125
MosquittoConfig::host
std::string host
Definition: mosquitto_config.h:12
MQTT_Dialog::_topic_list
std::unordered_set< std::string > _topic_list
Definition: mqtt_dialog.h:52
LoadSvg
const QPixmap & LoadSvg(QString filename, QString style_name="light")
Definition: svg_util.h:26
MQTT_Dialog::_topic_list_timer
QTimer * _topic_list_timer
Definition: mqtt_dialog.h:34
MosquittoConfig::password
std::string password
Definition: mosquitto_config.h:22
MQTT_Dialog::MQTT_Dialog
MQTT_Dialog(MQTTClient::Ptr mosq_client)
Definition: mqtt_dialog.cpp:15
MosquittoConfig::port
int port
Definition: mosquitto_config.h:13
MQTT_Dialog::ui
Ui::DataStreamMQTT * ui
Definition: mqtt_dialog.h:19
MQTT_Dialog::onConnectionClosed
void onConnectionClosed()
Definition: mqtt_dialog.cpp:5
MosquittoConfig::username
std::string username
Definition: mosquitto_config.h:21
mqtt_dialog.h
MQTT_Dialog::_client
MQTTClient::Ptr _client
Definition: mqtt_dialog.h:32
MQTT_Dialog::onLoadServerCertificate
void onLoadServerCertificate()
Definition: mqtt_dialog.cpp:247
DataStreamMQTT
Definition: datastream_mqtt.h:15
MQTT_Dialog::changeConnectionState
void changeConnectionState(bool connected)
Definition: mqtt_dialog.cpp:239
MQTT_Dialog::onSelectionChanged
void onSelectionChanged()
Definition: mqtt_dialog.cpp:233
MosquittoConfig::keyfile
std::string keyfile
Definition: mosquitto_config.h:27
MQTT_Dialog::onUpdateTopicList
void onUpdateTopicList()
Definition: mqtt_dialog.cpp:199
start_test_publisher.topic
topic
Definition: start_test_publisher.py:22
MQTT_Dialog::_private_key_file
QString _private_key_file
Definition: mqtt_dialog.h:50
Ui
Definition: cheatsheet_dialog.h:6
MosquittoConfig::max_inflight
unsigned int max_inflight
Definition: mosquitto_config.h:20
MQTT_Dialog::~MQTT_Dialog
~MQTT_Dialog()
Definition: mqtt_dialog.cpp:147
MQTT_Dialog::_client_certificate_file
QString _client_certificate_file
Definition: mqtt_dialog.h:49
MosquittoConfig::eol
bool eol
Definition: mosquitto_config.h:41
MosquittoConfig
Definition: mosquitto_config.h:8
MQTTClient::disconnected
void disconnected()
MosquittoConfig::cafile
std::string cafile
Definition: mosquitto_config.h:24
MQTTClient::Ptr
std::shared_ptr< MQTTClient > Ptr
Definition: mqtt_client.h:19
MosquittoConfig::clean_session
bool clean_session
Definition: mosquitto_config.h:36
MosquittoConfig::keepalive
int keepalive
Definition: mosquitto_config.h:11
MQTT_Dialog::onButtonConnect
void onButtonConnect()
Definition: mqtt_dialog.cpp:160
MQTT_Dialog::_server_certificate_file
QString _server_certificate_file
Definition: mqtt_dialog.h:48
MosquittoConfig::topics
std::vector< std::string > topics
Definition: mosquitto_config.h:37
MQTT_Dialog::onLoadPrivateKey
void onLoadPrivateKey()
Definition: mqtt_dialog.cpp:287
MosquittoConfig::certfile
std::string certfile
Definition: mosquitto_config.h:26
MQTT_Dialog::onLoadClientCertificate
void onLoadClientCertificate()
Definition: mqtt_dialog.cpp:267
MosquittoConfig::qos
int qos
Definition: mosquitto_config.h:14
MosquittoConfig::bind_address
std::string bind_address
Definition: mosquitto_config.h:16
nullptr
#define nullptr
Definition: backward.hpp:386
svg_util.h
MosquittoConfig::protocol_version
int protocol_version
Definition: mosquitto_config.h:10


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45