@@ -56,6 +56,11 @@ MainWindow::MainWindow(QWidget *parent) :
56
56
57
57
ui->toolBar ->insertWidget (ui->actionConnect_Disconnect , mqttHost);
58
58
59
+ mqttTopic = new QLineEdit ();
60
+ mqttTopic->setMaximumWidth (200 );
61
+
62
+ ui->toolBar ->insertWidget (ui->actionConnect_Disconnect , mqttTopic);
63
+
59
64
led1 = new QLedIndicator (this );
60
65
led1->setOn (true );
61
66
@@ -128,6 +133,7 @@ MainWindow::MainWindow(QWidget *parent) :
128
133
connect (subscriber, SIGNAL (disconnected ()), this , SLOT (onDisconnected ()));
129
134
130
135
connect (ui->actionConnect_Disconnect , SIGNAL (toggled (bool )), this , SLOT (connectDisconnectMQTT (bool )));
136
+ connect (mqttTopic, SIGNAL (textChanged (QString)), this , SLOT (updateMQTTSubscription ()));
131
137
132
138
loadSettings ();
133
139
@@ -170,7 +176,6 @@ void MainWindow::connectDisconnectMQTT(bool connect) {
170
176
subscriber->connectToHost ();
171
177
subscriber->setAutoReconnect (true );
172
178
ui->statusbar ->showMessage (tr (" Connecting to %1" ).arg (mqttHost->text ()));
173
-
174
179
}
175
180
} else {
176
181
subscriber->setAutoReconnect (false );
@@ -180,12 +185,24 @@ void MainWindow::connectDisconnectMQTT(bool connect) {
180
185
}
181
186
}
182
187
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
+
183
198
void MainWindow::onConnected () {
184
199
ui->statusbar ->showMessage (tr (" Connected to %1" ).arg (subscriber->hostName ()), 1000 );
185
200
log (QString (" connected to %1" ).arg (subscriber->hostName ()));
186
201
led1->setDisconnected (false );
187
202
ui->actionConnect_Disconnect ->setChecked (true );
188
203
ui->actionConnect_Disconnect ->setToolTip (tr (" Disconnect" ));
204
+
205
+ updateMQTTSubscription ();
189
206
}
190
207
191
208
void MainWindow::onDisconnected () {
@@ -201,32 +218,28 @@ void MainWindow::onReceived(QString topic, QString msg) {
201
218
202
219
log (QString (" %1: %2" ).arg (topic, msg));
203
220
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);
209
224
225
+ if (ok1 && ok2) {
226
+ led1->setOn (tmp);
210
227
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);
213
231
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 ,' ' ));
217
234
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));
227
241
228
- log (tr (" %1: error while converting %1" ).arg (topic, info));
229
- }
242
+ log (tr (" %1: error while converting %1" ).arg (topic, info));
230
243
}
231
244
}
232
245
@@ -253,6 +266,7 @@ void MainWindow::loadSettings() {
253
266
254
267
settings->beginGroup (" mqtt" );
255
268
mqttHost->setText (settings->value (" host" , " 127.0.0.1" ).toString ());
269
+ mqttTopic->setText (settings->value (" topic" , " led/0/status" ).toString ());
256
270
settings->endGroup ();
257
271
}
258
272
@@ -275,6 +289,7 @@ void MainWindow::saveSettings() {
275
289
276
290
settings->beginGroup (" mqtt" );
277
291
settings->setValue (" host" , mqttHost->text ());
292
+ settings->setValue (" topic" , mqttTopic->text ());
278
293
settings->endGroup ();
279
294
}
280
295
0 commit comments