Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 9c73bdb

Browse files
committed
Add text field for watched MQTT topics (WIP)
1 parent c6abd8f commit 9c73bdb

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

mainwindow.cpp

+37-22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ MainWindow::MainWindow(QWidget *parent) :
5656

5757
ui->toolBar->insertWidget(ui->actionConnect_Disconnect, mqttHost);
5858

59+
mqttTopic = new QLineEdit();
60+
mqttTopic->setMaximumWidth(200);
61+
62+
ui->toolBar->insertWidget(ui->actionConnect_Disconnect, mqttTopic);
63+
5964
led1 = new QLedIndicator(this);
6065
led1->setOn(true);
6166

@@ -128,6 +133,7 @@ MainWindow::MainWindow(QWidget *parent) :
128133
connect(subscriber, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
129134

130135
connect(ui->actionConnect_Disconnect, SIGNAL(toggled(bool)), this, SLOT(connectDisconnectMQTT(bool)));
136+
connect(mqttTopic, SIGNAL(textChanged(QString)), this, SLOT(updateMQTTSubscription()));
131137

132138
loadSettings();
133139

@@ -170,7 +176,6 @@ void MainWindow::connectDisconnectMQTT(bool connect) {
170176
subscriber->connectToHost();
171177
subscriber->setAutoReconnect(true);
172178
ui->statusbar->showMessage(tr("Connecting to %1").arg(mqttHost->text()));
173-
174179
}
175180
} else {
176181
subscriber->setAutoReconnect(false);
@@ -180,12 +185,24 @@ void MainWindow::connectDisconnectMQTT(bool connect) {
180185
}
181186
}
182187

188+
void MainWindow::updateMQTTSubscription() {
189+
if (!mqttTopicOld.isEmpty()) {
190+
subscriber->unsubscribe(mqttTopicOld);
191+
}
192+
193+
subscriber->subscribe(mqttTopic->text(), 0);
194+
195+
mqttTopicOld = mqttTopic->text();
196+
}
197+
183198
void MainWindow::onConnected() {
184199
ui->statusbar->showMessage(tr("Connected to %1").arg(subscriber->hostName()), 1000);
185200
log(QString("connected to %1").arg(subscriber->hostName()));
186201
led1->setDisconnected(false);
187202
ui->actionConnect_Disconnect->setChecked(true);
188203
ui->actionConnect_Disconnect->setToolTip(tr("Disconnect"));
204+
205+
updateMQTTSubscription();
189206
}
190207

191208
void MainWindow::onDisconnected() {
@@ -201,32 +218,28 @@ void MainWindow::onReceived(QString topic, QString msg) {
201218

202219
log(QString("%1: %2").arg(topic, msg));
203220

204-
if (QString(EXAMPLE_TOPIC) == topic) {
205-
206-
bool ok1, ok2;
207-
double ts = msg.split(" ")[0].toDouble(&ok1);
208-
double tmp = msg.split(" ")[1].toDouble(&ok2);
221+
bool ok1, ok2;
222+
double ts = msg.split(" ")[0].toDouble(&ok1);
223+
double tmp = msg.split(" ")[1].toDouble(&ok2);
209224

225+
if (ok1 && ok2) {
226+
led1->setOn(tmp);
210227

211-
if (ok1 && ok2) {
212-
led1->setOn(tmp);
228+
ui->plot->graph(0)->addData(ts, tmp);
229+
ui->plot->graph(0)->removeDataBefore(ts - qMin((int)(zoomTime*10), 600)); // max 10 minutes buffer size
230+
// ui->plot->graph(0)->setName(topic);
213231

214-
ui->plot->graph(0)->addData(ts, tmp);
215-
ui->plot->graph(0)->removeDataBefore(ts - qMin((int)(zoomTime*10), 600)); // max 10 minutes buffer size
216-
ui->plot->graph(0)->setName(topic);
232+
double lagt = (tsl - ts) * 1000;
233+
lag->setText(tr("%1%2 msec lag").arg(lagt < 0 ? '-' : ' ').arg(qFabs(lagt),5,'f',2,' '));
217234

218-
double lagt = (tsl - ts) * 1000;
219-
lag->setText(tr("%1%2 msec lag").arg(lagt < 0 ? '-' : ' ').arg(qFabs(lagt),5,'f',2,' '));
220-
221-
} else {
222-
QString info;
223-
info = QString("%1 %2 %3 %4").arg(QString::number(ts, 'f'),
224-
QString::number(tmp),
225-
QString::number(ok1),
226-
QString::number(ok2));
235+
} else {
236+
QString info;
237+
info = QString("%1 %2 %3 %4").arg(QString::number(ts, 'f'),
238+
QString::number(tmp),
239+
QString::number(ok1),
240+
QString::number(ok2));
227241

228-
log(tr("%1: error while converting %1").arg(topic, info));
229-
}
242+
log(tr("%1: error while converting %1").arg(topic, info));
230243
}
231244
}
232245

@@ -253,6 +266,7 @@ void MainWindow::loadSettings() {
253266

254267
settings->beginGroup("mqtt");
255268
mqttHost->setText(settings->value("host", "127.0.0.1").toString());
269+
mqttTopic->setText(settings->value("topic", "led/0/status").toString());
256270
settings->endGroup();
257271
}
258272

@@ -275,6 +289,7 @@ void MainWindow::saveSettings() {
275289

276290
settings->beginGroup("mqtt");
277291
settings->setValue("host", mqttHost->text());
292+
settings->setValue("topic", mqttTopic->text());
278293
settings->endGroup();
279294
}
280295

mainwindow.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
const QHostAddress EXAMPLE_HOST = QHostAddress("192.168.0.114");
4646
const quint16 EXAMPLE_PORT = 1883;
47-
const QString EXAMPLE_TOPIC = "siggen.0.sine";
47+
const QString EXAMPLE_TOPIC = "rm501/0/pos";
4848

4949
class Publisher : public QMQTT::Client
5050
{
@@ -118,13 +118,12 @@ class Subscriber : public QMQTT::Client
118118
void signalReceived(QString topic, QString msg);
119119
void signalConnected();
120120
void signalDisconnected();
121-
void signalSubscribed();
121+
void signalSubscribed(QString topic);
122122

123123
public slots:
124124
void onConnected()
125125
{
126126
// _qout << "connected" << endl;
127-
subscribe(EXAMPLE_TOPIC, 0);
128127
emit signalConnected();
129128
}
130129

@@ -138,7 +137,7 @@ public slots:
138137
{
139138
Q_UNUSED(topic)
140139
// _qout << "subscribed " << topic << endl;
141-
emit signalSubscribed();
140+
emit signalSubscribed(topic);
142141
}
143142

144143
void onReceived(const QMQTT::Message& message)
@@ -183,6 +182,8 @@ public slots:
183182

184183
void printPreview(QPrinter *printer);
185184

185+
void updateMQTTSubscription();
186+
186187
private slots:
187188
void closeEvent(QCloseEvent *);
188189
void updatePlot();
@@ -194,6 +195,7 @@ private slots:
194195

195196
QSettings *settings;
196197
QLineEdit *mqttHost;
198+
QLineEdit *mqttTopic;
197199

198200
QLedIndicator *led1;
199201

@@ -202,6 +204,8 @@ private slots:
202204
QLabel *lag;
203205

204206
double zoomTime;
207+
208+
QString mqttTopicOld;
205209
};
206210

207211
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)