labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

MQTTClient.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 File : MQTTClient.cpp
3 Project : LabPlot
4 Description : Represents a MQTT Client
5 --------------------------------------------------------------------
6 Copyright : (C) 2018 Kovacs Ferencz (kferike98@gmail.com)
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the Free Software *
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25 * Boston, MA 02110-1301 USA *
26 * *
27 ***************************************************************************/
33 
34 #include <QTimer>
35 
36 /*!
37  \class MQTTClient
38  \brief The MQTT Client connects to the broker set in ImportFileWidget.
39 It manages the MQTTSubscriptions, and the MQTTTopics.
40 
41  \ingroup datasources
42 */
43 MQTTClient::MQTTClient(const QString& name) : Folder(name, AspectType::MQTTClient),
44  m_updateTimer(new QTimer(this)),
45  m_client(new QMqttClient(this)),
46  m_willTimer(new QTimer(this)) {
47 
48  connect(m_updateTimer, &QTimer::timeout, this, &MQTTClient::read);
49  connect(m_client, &QMqttClient::connected, this, &MQTTClient::onMQTTConnect);
50  connect(m_willTimer, &QTimer::timeout, this, &MQTTClient::updateWillMessage);
51  connect(m_client, &QMqttClient::errorChanged, this, &MQTTClient::MQTTErrorChanged);
52 }
53 
55  emit clientAboutToBeDeleted(m_client->hostname(), m_client->port());
56  //stop reading before deleting the objects
57  pauseReading();
58  qDebug() << "Delete MQTTClient: " << m_client->hostname() << m_client->port();
59 
60  delete m_filter;
61  delete m_updateTimer;
62  delete m_willTimer;
63  m_client->disconnectFromHost();
64  delete m_client;
65 }
66 
67 /*!
68  * depending on the update type, periodically or on data changes, starts the timer.
69  */
73 }
74 
75 /*!
76  * \brief Updates the MQTTTopics of the client
77  */
79  m_updateTimer->stop();
80  read();
83 }
84 
85 /*!
86  * \brief Continue reading from messages after it was paused.
87  */
89  m_paused = false;
92 }
93 
94 /*!
95  * \brief Pause the reading from messages.
96  */
98  m_paused = true;
100  m_updateTimer->stop();
101 }
102 
103 /*!
104  * \brief Sets the filter of the MQTTClient.
105  * The ownership of the filter is passed to MQTTClient.
106  *
107  * \param f a pointer to the new filter
108  */
110  delete m_filter;
111  m_filter = f;
112 }
113 
114 /*!
115  * \brief Returns the filter of the MQTTClient.
116  */
118  return m_filter;
119 }
120 
121 /*!
122  * \brief Sets the MQTTclient's update interval to \c interval
123  * \param interval
124  */
125 void MQTTClient::setUpdateInterval(int interval) {
126  m_updateInterval = interval;
127  if (!m_paused)
129 }
130 
131 /*!
132  * \brief Returns the MQTTClient's update interval to \c interval
133  * \param interval
134  */
136  return m_updateInterval;
137 }
138 
139 /*!
140  * \brief Sets how many values we should store
141  * \param keepNValues
142  */
143 void MQTTClient::setKeepNValues(int keepNValues) {
145 }
146 
147 /*!
148  * \brief Returns how many values we should store
149  */
151  return m_keepNValues;
152 }
153 
154 /*!
155  * \brief Provides information about whether the reading is paused or not
156  *
157  * \return true if the reading is paused
158  * \return false otherwise
159  */
160 bool MQTTClient::isPaused() const {
161  return m_paused;
162 }
163 
164 /*!
165  * \brief Sets the size rate to sampleSize
166  * \param sampleSize
167  */
168 void MQTTClient::setSampleSize(int sampleSize) {
170 }
171 
172 /*!
173  * \brief Returns the size rate
174  */
176  return m_sampleSize;
177 }
178 
179 /*!
180  * \brief Sets the MQTTClient's reading type to readingType
181  * \param readingType
182  */
185 }
186 
187 /*!
188  * \brief Returns the MQTTClient's reading type
189  */
191  return m_readingType;
192 }
193 
194 /*!
195  * \brief Sets the MQTTClient's update type to updatetype and handles this change
196  * \param updatetype
197  */
200  m_updateTimer->stop();
201 
203 }
204 
205 /*!
206  * \brief Returns the MQTTClient's update type
207  */
209  return m_updateType;
210 }
211 
212 /*!
213  * \brief Returns the MQTTClient's icon
214  */
215 QIcon MQTTClient::icon() const {
216  return QIcon::fromTheme("network-server-database");
217 }
218 
219 /*!
220  * \brief Sets the host and port for the client.
221  *
222  * \param host the hostname of the broker we want to connect to
223  * \param port the port used by the broker
224  */
225 void MQTTClient::setMQTTClientHostPort(const QString& host, quint16 port) {
226  m_client->setHostname(host);
227  m_client->setPort(port);
228 }
229 
230 /*!
231  * \brief Returns hostname of the broker the client is connected to.
232  */
233 QString MQTTClient::clientHostName() const {
234  return m_client->hostname();
235 }
236 
237 /*!
238  * \brief Returns the port used by the broker.
239  */
240 quint16 MQTTClient::clientPort() const {
241  return m_client->port();
242 }
243 
244 /*!
245  * \brief Sets the flag on the given value.
246  * If set true it means that the broker requires authentication, otherwise it doesn't.
247  *
248  * \param use
249  */
252 }
253 
254 /*!
255  * \brief Returns whether the broker requires authentication or not.
256  */
259 }
260 
261 /*!
262  * \brief Sets the username and password for the client.
263  *
264  * \param username the username used for authentication
265  * \param password the password used for authentication
266  */
267 void MQTTClient::setMQTTClientAuthentication(const QString& username, const QString& password) {
268  m_client->setUsername(username);
269  m_client->setPassword(password);
270 }
271 
272 /*!
273  * \brief Returns the username used for authentication.
274  */
275 QString MQTTClient::clientUserName() const {
276  return m_client->username();
277 }
278 
279 /*!
280  * \brief Returns the password used for authentication.
281  */
282 QString MQTTClient::clientPassword() const {
283  return m_client->password();
284 }
285 
286 /*!
287  * \brief Sets the flag on the given value.
288  * If set true it means that user wants to set the client ID, otherwise it's not the case.
289  *
290  * \param use
291  */
292 void MQTTClient::setMQTTUseID(bool use) {
293  m_MQTTUseID = use;
294 }
295 
296 /*!
297  * \brief Returns whether the user wants to set the client ID or not.
298  */
299 bool MQTTClient::MQTTUseID() const {
300  return m_MQTTUseID;
301 }
302 
303 /*!
304  * \brief Sets the ID of the client
305  *
306  * \param id
307  */
308 void MQTTClient::setMQTTClientId(const QString& clientId) {
309  m_client->setClientId(clientId);
310 }
311 
312 /*!
313  * \brief Returns the ID of the client
314  */
315 QString MQTTClient::clientID () const {
316  return m_client->clientId();
317 }
318 
319 /*!
320  * \brief Sets the flag on the given value.
321  * If retain is true we interpret retain messages, otherwise we do not
322  *
323  * \param retain
324  */
325 void MQTTClient::setMQTTRetain(bool retain) {
326  m_MQTTRetain = retain;
327 }
328 
329 /*!
330  * \brief Returns the flag, which set to true means that interpret retain messages, otherwise we do not
331  */
333  return m_MQTTRetain;
334 }
335 
336 /*!
337  * \brief Returns the name of every MQTTTopics which already received a message, and is child of the MQTTClient
338  */
340  return m_topicNames;
341 }
342 
343 /*!
344  * \brief Adds the initial subscriptions that were set in ImportFileWidget
345  *
346  * \param filter the name of the subscribed topic
347  * \param qos the qos level of the subscription
348  */
349 void MQTTClient::addInitialMQTTSubscriptions(const QMqttTopicFilter& filter, quint8 qos) {
351 }
352 
353 /*!
354  * \brief Returns the name of every MQTTSubscription of the MQTTClient
355  */
357  return m_subscriptions;
358 }
359 
360 /*!
361  * \brief Adds a new MQTTSubscription to the MQTTClient
362  *
363  * \param topic, the name of the topic
364  * \param QoS
365  */
366 void MQTTClient::addMQTTSubscription(const QString& topicName, quint8 QoS) {
367  //Check whether the subscription already exists, if it doesn't, we can add it
368  if (!m_subscriptions.contains(topicName)) {
369  const QMqttTopicFilter filter {topicName};
370  QMqttSubscription* temp = m_client->subscribe(filter, QoS);
371 
372  if (temp) {
373 // qDebug()<<"Subscribe to: "<< temp->topic() << " " << temp->qos();
374  m_subscriptions.push_back(temp->topic().filter());
375  m_subscribedTopicNameQoS[temp->topic().filter()] = temp->qos();
376 
377  auto* newSubscription = new MQTTSubscription(temp->topic().filter());
378  newSubscription->setMQTTClient(this);
379 
380  addChildFast(newSubscription);
381  m_MQTTSubscriptions.push_back(newSubscription);
382 
383  //Search for inferior subscriptions, that the new subscription contains
384  bool found = false;
385  QVector<MQTTSubscription*> inferiorSubscriptions;
386  for (auto* subscription : m_MQTTSubscriptions) {
387  if (checkTopicContains(topicName, subscription->subscriptionName())
388  && topicName != subscription->subscriptionName()) {
389  found = true;
390  inferiorSubscriptions.push_back(subscription);
391  }
392  }
393 
394  //If there are some inferior subscriptions, we have to deal with them
395  if (found) {
396  for (auto* inferiorSubscription : inferiorSubscriptions) {
397 // qDebug()<<"Reparent topics of inferior subscription: "<< inferiorSubscription->subscriptionName();
398 
399  //We have to reparent every topic of the inferior subscription, so no data is lost
400  QVector<MQTTTopic*> topics = inferiorSubscription->topics();
401  for (auto* topic : topics) {
402  topic->reparent(newSubscription);
403  }
404 
405  //Then remove the subscription and every connected information
406  QMqttTopicFilter unsubscribeFilter {inferiorSubscription->subscriptionName()};
407  m_client->unsubscribe(unsubscribeFilter);
408 
409  for (int j = 0; j < m_MQTTSubscriptions.size(); ++j) {
410  if (m_MQTTSubscriptions[j]->subscriptionName() ==
411  inferiorSubscription->subscriptionName()) {
412  m_MQTTSubscriptions.remove(j);
413  }
414  }
415  m_subscriptions.removeAll(inferiorSubscription->subscriptionName());
416  m_subscribedTopicNameQoS.remove(inferiorSubscription->subscriptionName());
417 
418  removeChild(inferiorSubscription);
419  }
420  }
421 
422  connect(temp, &QMqttSubscription::messageReceived, this, &MQTTClient::MQTTSubscriptionMessageReceived);
423 
424  emit MQTTTopicsChanged();
425  }
426  }
427 }
428 
429 /*!
430  * \brief Removes a MQTTSubscription from the MQTTClient
431  *
432  * \param name, the name of the subscription to remove
433  */
434 void MQTTClient::removeMQTTSubscription(const QString& subscriptionName) {
435  //We can only remove the subscription if it exists
436  if (m_subscriptions.contains(subscriptionName)) {
437  //unsubscribe from the topic
438  const QMqttTopicFilter filter{subscriptionName};
439  m_client->unsubscribe(filter);
440 // qDebug()<<"Unsubscribe from: " << subscriptionName;
441 
442  //Remove every connected information
443  m_subscriptions.removeAll(subscriptionName);
444 
445  for (int i = 0; i < m_MQTTSubscriptions.size(); ++i) {
446  if (m_MQTTSubscriptions[i]->subscriptionName() == subscriptionName) {
447  MQTTSubscription* removeSubscription = m_MQTTSubscriptions[i];
448  m_MQTTSubscriptions.remove(i);
449  //Remove every topic of the subscription as well
450  QVector<MQTTTopic*> topics = removeSubscription->topics();
451  for (const auto& topic : topics)
452  m_topicNames.removeAll(topic->topicName());
453 
454  //Remove the MQTTSubscription
455  removeChild(removeSubscription);
456  break;
457  }
458  }
459 
460  QMapIterator<QMqttTopicFilter, quint8> j(m_subscribedTopicNameQoS);
461  while (j.hasNext()) {
462  j.next();
463  if (j.key().filter() == subscriptionName) {
464  m_subscribedTopicNameQoS.remove(j.key());
465  break;
466  }
467  }
468  emit MQTTTopicsChanged();
469  }
470 }
471 
472 /*!
473  * \brief Adds a MQTTSubscription to the MQTTClient
474  *Used when the user unsubscribes from a topic of a MQTTSubscription
475  *
476  * \param topic, the name of the topic
477  * \param QoS
478  */
479 void MQTTClient::addBeforeRemoveSubscription(const QString& topicName, quint8 QoS) {
480  //We can't add the subscription if it already exists
481  if (!m_subscriptions.contains(topicName)) {
482  //Subscribe to the topic
483  QMqttTopicFilter filter {topicName};
484  QMqttSubscription* temp = m_client->subscribe(filter, QoS);
485  if (temp) {
486  //Add the MQTTSubscription and other connected data
487 // qDebug()<<"Add subscription before remove: " << temp->topic() << " " << temp->qos();
488  m_subscriptions.push_back(temp->topic().filter());
489  m_subscribedTopicNameQoS[temp->topic().filter()] = temp->qos();
490 
491  auto* newSubscription = new MQTTSubscription(temp->topic().filter());
492  newSubscription->setMQTTClient(this);
493 
494  addChildFast(newSubscription);
495  m_MQTTSubscriptions.push_back(newSubscription);
496 
497  //Search for the subscription the topic belonged to
498  bool found = false;
499  MQTTSubscription* superiorSubscription = nullptr;
500  for (auto* subscription : m_MQTTSubscriptions) {
501  if (checkTopicContains(subscription->subscriptionName(), topicName)
502  && topicName != subscription->subscriptionName()) {
503  found = true;
504  superiorSubscription = subscription;
505  break;
506  }
507  }
508 
509  if (found) {
510  //Search for topics belonging to the superior(old) subscription
511  //which are also contained by the new subscription
512  QVector<MQTTTopic*> topics = superiorSubscription->topics();
513 // qDebug()<< topics.size();
514 
515  QVector<MQTTTopic*> inferiorTopics;
516  for (auto* topic : topics) {
517  if (checkTopicContains(topicName, topic->topicName())) {
518  inferiorTopics.push_back(topic);
519  }
520  }
521 
522  //Reparent these topics, in order to avoid data loss
523  for (auto* inferiorTopic : inferiorTopics) {
524  inferiorTopic->reparent(newSubscription);
525  }
526  }
527  connect(temp, &QMqttSubscription::messageReceived, this, &MQTTClient::MQTTSubscriptionMessageReceived);
528  }
529  }
530 }
531 
532 /*!
533  * \brief Reparents the given MQTTTopic to the given MQTTSubscription
534  *
535  * \param topic, the name of the MQTTTopic
536  * \param parent, the name of the MQTTSubscription
537  */
538 void MQTTClient::reparentTopic(const QString& topicName, const QString& parentTopicName) {
539  //We can only reparent if the parent containd the topic
540  if (m_subscriptions.contains(parentTopicName) && m_topicNames.contains(topicName)) {
541 // qDebug() << "Reparent " << topicName << " to " << parentTopicName;
542  //search for the parent MQTTSubscription
543  bool found = false;
544  MQTTSubscription* superiorSubscription = nullptr;
545  for (auto* subscription : m_MQTTSubscriptions) {
546  if (subscription->subscriptionName() == parentTopicName) {
547  found = true;
548  superiorSubscription = subscription;
549  break;
550  }
551  }
552 
553  if (found) {
554  //get every topic of the MQTTClient
556  //Search for the given topic among the MQTTTopics
557  for (auto* topic : topics) {
558  if (topicName == topic->topicName()) {
559  //if found, it is reparented to the parent MQTTSubscription
560  topic->reparent(superiorSubscription);
561  break;
562  }
563  }
564  }
565  }
566 }
567 
568 /*!
569  *\brief Checks if a topic contains another one
570  *
571  * \param superior the name of a topic
572  * \param inferior the name of a topic
573  * \return true if superior is equal to or contains(if superior contains wildcards) inferior,
574  * false otherwise
575  */
576 bool MQTTClient::checkTopicContains(const QString& superior, const QString& inferior) {
577  if (superior == inferior)
578  return true;
579  else {
580  if (superior.contains(QLatin1String("/"))) {
581  QStringList superiorList = superior.split('/', QString::SkipEmptyParts);
582  QStringList inferiorList = inferior.split('/', QString::SkipEmptyParts);
583 
584  //a longer topic can't contain a shorter one
585  if (superiorList.size() > inferiorList.size())
586  return false;
587 
588  bool ok = true;
589  for (int i = 0; i < superiorList.size(); ++i) {
590  if (superiorList.at(i) != inferiorList.at(i)) {
591  if ((superiorList.at(i) != '+') &&
592  !(superiorList.at(i) == '#' && i == superiorList.size() - 1)) {
593  //if the two topics differ, and the superior's current level isn't + or #(which can be only in the last position)
594  //then superior can't contain inferior
595  ok = false;
596  break;
597  } else if (i == superiorList.size() - 1 && (superiorList.at(i) == '+' && inferiorList.at(i) == '#') ) {
598  //if the two topics differ at the last level
599  //and the superior's current level is + while the inferior's is #(which can be only in the last position)
600  //then superior can't contain inferior
601  ok = false;
602  break;
603  }
604  }
605  }
606  return ok;
607  }
608  return false;
609  }
610 }
611 
612 /*!
613  *\brief Returns the '+' wildcard containing topic name, which includes the given topic names
614  *
615  * \param first the name of a topic
616  * \param second the name of a topic
617  * \return The name of the common topic, if it exists, otherwise an empty string
618  */
619 QString MQTTClient::checkCommonLevel(const QString& first, const QString& second) {
620  QStringList firstList = first.split('/', QString::SkipEmptyParts);
621  QStringList secondtList = second.split('/', QString::SkipEmptyParts);
622  QString commonTopic;
623 
624  if (!firstList.isEmpty()) {
625  //the two topics have to be the same size and can't be identic
626  if ((firstList.size() == secondtList.size()) && (first != second)) {
627 
628  //the index where they differ
629  int differIndex = -1;
630  for (int i = 0; i < firstList.size(); ++i) {
631  if (firstList.at(i) != secondtList.at(i)) {
632  differIndex = i;
633  break;
634  }
635  }
636 
637  //they can differ at only one level and that can't be the first
638  bool differ = false;
639  if (differIndex > 0) {
640  for (int j = differIndex +1; j < firstList.size(); ++j) {
641  if (firstList.at(j) != secondtList.at(j)) {
642  differ = true;
643  break;
644  }
645  }
646  }
647  else
648  differ = true;
649 
650  if (!differ) {
651  for (int i = 0; i < firstList.size(); ++i) {
652  if (i != differIndex) {
653  commonTopic.append(firstList.at(i));
654  } else {
655  //we put '+' wildcard at the level where they differ
656  commonTopic.append('+');
657  }
658 
659  if (i != firstList.size() - 1)
660  commonTopic.append("/");
661  }
662  }
663  }
664  }
665 // qDebug() << first << " " << second << " common topic: "<<commonTopic;
666  return commonTopic;
667 }
668 
669 void MQTTClient::setWillSettings(const MQTTWill& settings) {
670  m_MQTTWill = settings;
671 }
672 
674  return m_MQTTWill;
675 }
676 
677 /*!
678  * \brief Sets whether the user wants to use will message or not
679  *
680  * \param use
681  */
683  m_MQTTWill.enabled = use;
684  if (use == false)
685  m_willTimer->stop();
686 }
687 
688 /*!
689  * \brief Returns whether the user wants to use will message or not
690  */
692  return m_MQTTWill.enabled;
693 }
694 
695 /*!
696  * \brief Sets the will topic of the client
697  *
698  * \param topic
699  */
700 void MQTTClient::setWillTopic(const QString& topic) {
701 // qDebug() << "Set will topic:" << topic;
702  m_MQTTWill.willTopic = topic;
703 }
704 
705 /*!
706  * \brief Returns the will topic of the client
707  */
708 QString MQTTClient::willTopic() const {
709  return m_MQTTWill.willTopic;
710 }
711 
712 /*!
713  * \brief Sets the retain flag of the client's will message
714  *
715  * \param retain
716  */
717 void MQTTClient::setWillRetain(bool retain) {
718  m_MQTTWill.willRetain = retain;
719 }
720 
721 /*!
722  * \brief Returns the retain flag of the client's will message
723  */
725  return m_MQTTWill.willRetain;
726 }
727 
728 /*!
729  * \brief Sets the QoS level of the client's will message
730  *
731  * \param QoS
732  */
733 void MQTTClient::setWillQoS(quint8 QoS) {
734  m_MQTTWill.willQoS = QoS;
735 }
736 
737 /*!
738  * \brief Returns the QoS level of the client's will message
739  */
740 quint8 MQTTClient::willQoS() const {
741  return m_MQTTWill.willQoS;
742 }
743 
744 /*!
745  * \brief Sets the will message type of the client
746  *
747  * \param messageType
748  */
750  m_MQTTWill.willMessageType = messageType;
751 }
752 
753 /*!
754  * \brief Returns the will message type of the client
755  */
758 }
759 
760 /*!
761  * \brief Sets the own will message of the user
762  *
763  * \param ownMessage
764  */
765 void MQTTClient::setWillOwnMessage(const QString& ownMessage) {
766  m_MQTTWill.willOwnMessage = ownMessage;
767 }
768 
769 /*!
770  * \brief Returns the own will message of the user
771  */
772 QString MQTTClient::willOwnMessage() const {
773  return m_MQTTWill.willOwnMessage;
774 }
775 
776 /*!
777  * \brief Updates the will message of the client
778  */
780  QVector<const MQTTTopic*> topics = children<const MQTTTopic>(AbstractAspect::ChildIndexFlag::Recursive);
781  const MQTTTopic* willTopic = nullptr;
782 
783  //Search for the will topic
784  for (const auto* topic : topics) {
785  if (topic->topicName() == m_MQTTWill.willTopic) {
786  willTopic = topic;
787  break;
788  }
789  }
790 
791  //if the will topic is found we can update the will message
792  if (willTopic != nullptr) {
793  //To update the will message we have to disconnect first, then after setting everything connect again
794  if (m_MQTTWill.enabled && (m_client->state() == QMqttClient::ClientState::Connected) ) {
795  //Disconnect only once (disconnecting may take a while)
796  if (!m_disconnectForWill) {
797 // qDebug() << "Disconnecting from host in order to update will message";
798  m_client->disconnectFromHost();
799  m_disconnectForWill = true;
800  }
801  //Try to update again
803  }
804  //If client is disconnected we can update the settings
805  else if (m_MQTTWill.enabled && (m_client->state() == QMqttClient::ClientState::Disconnected) && m_disconnectForWill) {
806  m_client->setWillQoS(m_MQTTWill.willQoS);
807  m_client->setWillRetain(m_MQTTWill.willRetain);
808  m_client->setWillTopic(m_MQTTWill.willTopic);
809 
810  //Set the will message according to m_willMessageType
811  switch (m_MQTTWill.willMessageType) {
813  m_client->setWillMessage(m_MQTTWill.willOwnMessage.toUtf8());
814 // qDebug()<<"Will own message" << m_MQTTWill.willOwnMessage;
815  break;
817  //Statistics is only possible if the data stored in the MQTTTopic is of type integer or numeric
818  //check the column mode of the last column in the topic for this.
819  //TODO: check this logic again - why last column only?
820  const auto* col = willTopic->child<Column>(willTopic->childCount<Column>() - 1);
821  auto mode = col->columnMode();
823  m_client->setWillMessage(this->statistics(willTopic).toUtf8());
824  else
825  m_client->setWillMessage(QByteArray()); //empty message
826 // qDebug() << "Will statistics message: "<< QString(m_client->willMessage());
827  break;
828  }
830  m_client->setWillMessage(m_MQTTWill.willLastMessage.toUtf8());
831 // qDebug()<<"Will last message:\n" << m_MQTTWill.willLastMessage;
832  break;
833  default:
834  break;
835  }
836  m_disconnectForWill = false;
837  //Reconnect with the updated message
838  m_client->connectToHost();
839 // qDebug()<< "Reconnect to host after updating will message";
840  }
841  }
842 }
843 
844 /*!
845  * \brief Returns the statistical data that is needed by the topic for its MQTTClient's will message
846  * \param topic
847  */
848 QString MQTTClient::statistics(const MQTTTopic* topic) const {
849  const auto* col = topic->child<Column>(topic->childCount<Column>() - 1);
850  QString statistics;
851 
853  //Add every statistical data to the string, the flag of which is set true
854  for (int i = 0; i <= willStatistics.size(); i++) {
855  if (willStatistics[i]) {
856  switch (static_cast<MQTTClient::WillStatisticsType>(i) ) {
858  statistics += QLatin1String("Arithmetic mean: ") + QString::number(col->statistics().arithmeticMean) + "\n";
859  break;
861  statistics += QLatin1String("Contraharmonic mean: ") + QString::number(col->statistics().contraharmonicMean) + "\n";
862  break;
864  statistics += QLatin1String("Entropy: ") + QString::number(col->statistics().entropy) + "\n";
865  break;
867  statistics += QLatin1String("Geometric mean: ") + QString::number(col->statistics().geometricMean) + "\n";
868  break;
870  statistics += QLatin1String("Harmonic mean: ") + QString::number(col->statistics().harmonicMean) + "\n";
871  break;
873  statistics += QLatin1String("Kurtosis: ") + QString::number(col->statistics().kurtosis) + "\n";
874  break;
876  statistics += QLatin1String("Maximum: ") + QString::number(col->statistics().maximum) + "\n";
877  break;
879  statistics += QLatin1String("Mean deviation: ") + QString::number(col->statistics().meanDeviation) + "\n";
880  break;
882  statistics += QLatin1String("Mean deviation around median: ") + QString::number(col->statistics().meanDeviationAroundMedian) + "\n";
883  break;
885  statistics += QLatin1String("Median: ") + QString::number(col->statistics().median) + "\n";
886  break;
888  statistics += QLatin1String("Median deviation: ") + QString::number(col->statistics().medianDeviation) + "\n";
889  break;
891  statistics += QLatin1String("Minimum: ") + QString::number(col->statistics().minimum) + "\n";
892  break;
894  statistics += QLatin1String("Skewness: ") + QString::number(col->statistics().skewness) + "\n";
895  break;
897  statistics += QLatin1String("Standard deviation: ") + QString::number(col->statistics().standardDeviation) + "\n";
898  break;
900  statistics += QLatin1String("Variance: ") + QString::number(col->statistics().variance) + "\n";
901  break;
903  default:
904  break;
905  }
906  }
907  }
908  return statistics;
909 }
910 
911 /*!
912  * \brief Returns the MQTTClient's will update type
913  */
915  return m_MQTTWill.willUpdateType;
916 }
917 
918 /*!
919  * \brief Sets the MQTTClient's will update type
920  *
921  * \param willUpdateType
922  */
925 }
926 
927 /*!
928  * \brief Returns the time interval of updating the MQTTClient's will message
929  */
932 }
933 
934 /*!
935  * \brief Sets the time interval of updating the MQTTClient's will message, if update type is TimePeriod
936  *
937  * \param interval
938  */
940  m_MQTTWill.willTimeInterval = interval;
941 }
942 
943 /*!
944  * \brief Clear the lastly received message by the will topic
945  * Called when the will topic is changed
946  */
948  m_MQTTWill.willLastMessage.clear();
949 }
950 
951 /*!
952  * \brief Sets true the corresponding flag of the statistic type,
953  * what means that the given statistic type will be added to the will message
954  *
955  * \param statistics
956  */
958  m_MQTTWill.willStatistics[static_cast<int>(statistic)] = true;
959 }
960 
961 /*!
962  * \brief Sets false the corresponding flag of the statistic type,
963  * what means that the given statistic will no longer be added to the will message
964  *
965  * \param statistics
966  */
968  m_MQTTWill.willStatistics[static_cast<int>(statistic)] = false;
969 }
970 
971 /*!
972  * \brief Returns a bool vector, meaning which statistic types are included in the will message
973  * If the corresponding value is true, the statistic type is included, otherwise it isn't
974  */
976  return m_MQTTWill.willStatistics;
977 }
978 
979 /*!
980  * \brief Starts the will timer, which will update the will message
981  */
985 }
986 
987 /*!
988  * \brief Stops the will timer
989  */
991  m_willTimer->stop();
992 }
993 
994 //##############################################################################
995 //################################# SLOTS ####################################
996 //##############################################################################
997 
998 /*!
999  *\brief called periodically when update type is TimeInterval
1000  */
1002  if (!m_filter)
1003  return;
1004 
1005  if (!m_prepared) {
1006 // qDebug()<<"Connect";
1007  //connect to the broker
1008  m_client->connectToHost();
1009  m_prepared = true;
1010  }
1011 
1012  if ((m_client->state() == QMqttClient::ClientState::Connected) && m_MQTTFirstConnectEstablished) {
1013 // qDebug()<<"Read";
1014  //Signal for every MQTTTopic that they can read
1015  emit readFromTopics();
1016  }
1017 }
1018 
1019 /*!
1020  *\brief called when the client successfully connected to the broker
1021  */
1023  if (m_client->error() == QMqttClient::NoError) {
1024  //if this is the first connection (after setting the options in ImportFileWidget or loading saved project)
1026 // qDebug()<<"connection made in MQTTClient";
1027 
1028  //Subscribe to initial or loaded topics
1029  QMapIterator<QMqttTopicFilter, quint8> i(m_subscribedTopicNameQoS);
1030  while (i.hasNext()) {
1031  i.next();
1032 // qDebug()<<i.key();
1033  QMqttSubscription *temp = m_client->subscribe(i.key(), i.value());
1034  if (temp) {
1035  //If we didn't load the MQTTClient from xml we have to add the MQTTSubscriptions
1036  if (!m_loaded) {
1037  m_subscriptions.push_back(temp->topic().filter());
1038 
1039  auto* newSubscription = new MQTTSubscription(temp->topic().filter());
1040  newSubscription->setMQTTClient(this);
1041 
1042  addChildFast(newSubscription);
1043 
1044  m_MQTTSubscriptions.push_back(newSubscription);
1045  }
1046 
1047  connect(temp, &QMqttSubscription::messageReceived, this, &MQTTClient::MQTTSubscriptionMessageReceived);
1048  }
1049  }
1051  //Signal that the initial subscriptions were made
1052  emit MQTTSubscribed();
1053  }
1054  //if there was already a connection made(happens after updating will message)
1055  else {
1056 // qDebug() << "Start resubscribing after will message update";
1057  //Only the client has to make the subscriptions again, every other connected data is still available
1058  QMapIterator<QMqttTopicFilter, quint8> i(m_subscribedTopicNameQoS);
1059  while (i.hasNext()) {
1060  i.next();
1061  QMqttSubscription* temp = m_client->subscribe(i.key(), i.value());
1062  if (temp) {
1063 // qDebug()<<temp->topic()<<" "<<temp->qos();
1064  connect(temp, &QMqttSubscription::messageReceived, this, &MQTTClient::MQTTSubscriptionMessageReceived);
1065  }
1066 // else
1067 // qDebug()<<"Couldn't subscribe after will update";
1068  }
1069  }
1070  }
1071 }
1072 
1073 /*!
1074  *\brief called when a message is received by a topic belonging to one of subscriptions of the client.
1075  * It passes the message to the appropriate MQTTSubscription which will pass it to the appropriate MQTTTopic
1076  */
1077 void MQTTClient::MQTTSubscriptionMessageReceived(const QMqttMessage& msg) {
1078  //Decide to interpret retain message or not
1079  if (!msg.retain() || m_MQTTRetain) {
1080  //If this is the first message from the topic, save its name
1081  if (!m_topicNames.contains(msg.topic().name()))
1082  m_topicNames.push_back(msg.topic().name());
1083 
1084  //Pass the message and the topic name to the MQTTSubscription which contains the topic
1085  for (auto* subscription : m_MQTTSubscriptions) {
1086  if (checkTopicContains(subscription->subscriptionName(), msg.topic().name())) {
1087  subscription->messageArrived(msg.payload(), msg.topic().name());
1088  break;
1089  }
1090  }
1091 
1092  //if the message was received by the will topic, update the last message received by it
1093  if (msg.topic().name() == m_MQTTWill.willTopic) {
1094  m_MQTTWill.willLastMessage = QString(msg.payload());
1095 
1096  emit MQTTTopicsChanged();
1097  }
1098  }
1099 }
1100 
1101 /*!
1102  *\brief Handles some of the possible errors of the client, using MQTTErrorWidget
1103  */
1104 void MQTTClient::MQTTErrorChanged(QMqttClient::ClientError clientError) {
1105  if (clientError != QMqttClient::ClientError::NoError) {
1106  auto* errorWidget = new MQTTErrorWidget(clientError, this);
1107  errorWidget->show();
1108  }
1109 }
1110 
1111 /*!
1112  *\brief Called when a subscription is loaded.
1113  * Checks whether every saved subscription was loaded or not.
1114  * If everything is loaded, it makes the connection and starts the reading
1115  *
1116  * \param name, the name of the subscription
1117  */
1118 void MQTTClient::subscriptionLoaded(const QString &name) {
1119  if (!name.isEmpty()) {
1120 // qDebug() << "Finished loading: " << name;
1121  //Save information about the subscription
1123  m_subscriptions.push_back(name);
1124  QMqttTopicFilter filter {name};
1126 
1127  //Save the topics belonging to the subscription
1128  for (const auto* subscription : m_MQTTSubscriptions) {
1129  if (subscription->subscriptionName() == name) {
1130  const auto& topics = subscription->topics();
1131  for (auto* topic : topics) {
1132  m_topicNames.push_back(topic->topicName());
1133  }
1134  break;
1135  }
1136  }
1137 
1138  //Check whether every subscription was loaded or not
1140  //if everything was loaded we can start reading
1141  m_loaded = true;
1142  read();
1143  }
1144  }
1145 }
1146 
1147 //##############################################################################
1148 //################## Serialization/Deserialization ###########################
1149 //##############################################################################
1150 /*!
1151  Saves as XML.
1152  */
1153 void MQTTClient::save(QXmlStreamWriter* writer) const {
1154  writer->writeStartElement("MQTTClient");
1155  writeBasicAttributes(writer);
1156  writeCommentElement(writer);
1157 
1158  //general
1159  writer->writeStartElement("general");
1160  writer->writeAttribute("subscriptionCount", QString::number(m_MQTTSubscriptions.size()));
1161  writer->writeAttribute("updateType", QString::number(static_cast<int>(m_updateType)));
1162  writer->writeAttribute("readingType", QString::number(static_cast<int>(m_readingType)));
1163  writer->writeAttribute("keepValues", QString::number(m_keepNValues));
1164 
1166  writer->writeAttribute("updateInterval", QString::number(m_updateInterval));
1167 
1169  writer->writeAttribute("sampleSize", QString::number(m_sampleSize));
1170 
1171  writer->writeAttribute("host", m_client->hostname());
1172  writer->writeAttribute("port", QString::number(m_client->port()));
1173  writer->writeAttribute("username", m_client->username());
1174  writer->writeAttribute("password", m_client->password());
1175  writer->writeAttribute("clientId", m_client->clientId());
1176  writer->writeAttribute("useRetain", QString::number(m_MQTTRetain));
1177  writer->writeAttribute("useWill", QString::number(m_MQTTWill.enabled));
1178  writer->writeAttribute("willTopic", m_MQTTWill.willTopic);
1179  writer->writeAttribute("willOwnMessage", m_MQTTWill.willOwnMessage);
1180  writer->writeAttribute("willQoS", QString::number(m_MQTTWill.willQoS));
1181  writer->writeAttribute("willRetain", QString::number(m_MQTTWill.willRetain));
1182  writer->writeAttribute("willMessageType", QString::number(static_cast<int>(m_MQTTWill.willMessageType)));
1183  writer->writeAttribute("willUpdateType", QString::number(static_cast<int>(m_MQTTWill.willUpdateType)));
1184  writer->writeAttribute("willTimeInterval", QString::number(m_MQTTWill.willTimeInterval));
1185 
1186  for (int i = 0; i < m_MQTTWill.willStatistics.count(); ++i)
1187  writer->writeAttribute("willStatistics"+QString::number(i), QString::number(m_MQTTWill.willStatistics[i]));
1188  writer->writeAttribute("useID", QString::number(m_MQTTUseID));
1189  writer->writeAttribute("useAuthentication", QString::number(m_MQTTUseAuthentication));
1190 
1191  writer->writeEndElement();
1192 
1193  //filter
1194  m_filter->save(writer);
1195 
1196  //MQTTSubscription
1197  for (auto* sub : children<MQTTSubscription>(AbstractAspect::ChildIndexFlag::IncludeHidden))
1198  sub->save(writer);
1199 
1200  writer->writeEndElement(); // "MQTTClient"
1201 }
1202 
1203 /*!
1204  Loads from XML.
1205 */
1206 bool MQTTClient::load(XmlStreamReader* reader, bool preview) {
1207  if (!readBasicAttributes(reader))
1208  return false;
1209 
1210  QString attributeWarning = i18n("Attribute '%1' missing or empty, default value is used");
1211  QXmlStreamAttributes attribs;
1212  QString str;
1213 
1214  while (!reader->atEnd()) {
1215  reader->readNext();
1216  if (reader->isEndElement() && reader->name() == "MQTTClient")
1217  break;
1218 
1219  if (!reader->isStartElement())
1220  continue;
1221 
1222  if (reader->name() == "comment") {
1223  if (!readCommentElement(reader))
1224  return false;
1225  } else if (reader->name() == "general") {
1226  attribs = reader->attributes();
1227 
1228  str = attribs.value("subscriptionCount").toString();
1229  if (str.isEmpty())
1230  reader->raiseWarning(attributeWarning.arg("'subscriptionCount'"));
1231  else
1232  m_subscriptionCountToLoad = str.toInt();
1233 
1234  str = attribs.value("keepValues").toString();
1235  if (str.isEmpty())
1236  reader->raiseWarning(attributeWarning.arg("'keepValues'"));
1237  else
1238  m_keepNValues = str.toInt();
1239 
1240  str = attribs.value("updateType").toString();
1241  if (str.isEmpty())
1242  reader->raiseWarning(attributeWarning.arg("'updateType'"));
1243  else
1244  m_updateType = static_cast<UpdateType>(str.toInt());
1245 
1246  str = attribs.value("readingType").toString();
1247  if (str.isEmpty())
1248  reader->raiseWarning(attributeWarning.arg("'readingType'"));
1249  else
1250  m_readingType = static_cast<ReadingType>(str.toInt());
1251 
1253  str = attribs.value("updateInterval").toString();
1254  if (str.isEmpty())
1255  reader->raiseWarning(attributeWarning.arg("'updateInterval'"));
1256  else
1257  m_updateInterval = str.toInt();
1258  }
1259 
1261  str = attribs.value("sampleSize").toString();
1262  if (str.isEmpty())
1263  reader->raiseWarning(attributeWarning.arg("'sampleSize'"));
1264  else
1265  m_sampleSize = str.toInt();
1266  }
1267 
1268  str = attribs.value("host").toString();
1269  if (str.isEmpty())
1270  reader->raiseWarning(attributeWarning.arg("'host'"));
1271  else
1272  m_client->setHostname(str);
1273 
1274  str = attribs.value("port").toString();
1275  if (str.isEmpty())
1276  reader->raiseWarning(attributeWarning.arg("'port'"));
1277  else
1278  m_client->setPort(str.toUInt());
1279 
1280  str = attribs.value("useAuthentication").toString();
1281  if (str.isEmpty())
1282  reader->raiseWarning(attributeWarning.arg("'useAuthentication'"));
1283  else
1284  m_MQTTUseAuthentication = str.toInt();
1285 
1287  str = attribs.value("username").toString();
1288  if (!str.isEmpty())
1289  m_client->setUsername(str);
1290 
1291  str = attribs.value("password").toString();
1292  if (!str.isEmpty())
1293  m_client->setPassword(str);
1294  }
1295 
1296  str = attribs.value("useID").toString();
1297  if (str.isEmpty())
1298  reader->raiseWarning(attributeWarning.arg("'useID'"));
1299  else
1300  m_MQTTUseID = str.toInt();
1301 
1302  if (m_MQTTUseID) {
1303  str = attribs.value("clientId").toString();
1304  if (!str.isEmpty())
1305  m_client->setClientId(str);
1306  }
1307 
1308  str = attribs.value("useRetain").toString();
1309  if (str.isEmpty())
1310  reader->raiseWarning(attributeWarning.arg("'useRetain'"));
1311  else
1312  m_MQTTRetain = str.toInt();
1313 
1314  str = attribs.value("useWill").toString();
1315  if (str.isEmpty())
1316  reader->raiseWarning(attributeWarning.arg("'useWill'"));
1317  else
1318  m_MQTTWill.enabled = str.toInt();
1319 
1320  if (m_MQTTWill.enabled) {
1321  str = attribs.value("willTopic").toString();
1322  if (str.isEmpty())
1323  reader->raiseWarning(attributeWarning.arg("'willTopic'"));
1324  else
1325  m_MQTTWill.willTopic = str;
1326 
1327  str = attribs.value("willOwnMessage").toString();
1328  if (str.isEmpty())
1329  reader->raiseWarning(attributeWarning.arg("'willOwnMessage'"));
1330  else
1331  m_MQTTWill.willOwnMessage = str;
1332 
1333  str = attribs.value("willQoS").toString();
1334  if (str.isEmpty())
1335  reader->raiseWarning(attributeWarning.arg("'willQoS'"));
1336  else
1337  m_MQTTWill.willQoS = str.toUInt();
1338 
1339  str = attribs.value("willRetain").toString();
1340  if (str.isEmpty())
1341  reader->raiseWarning(attributeWarning.arg("'willRetain'"));
1342  else
1343  m_MQTTWill.willRetain = str.toInt();
1344 
1345  str = attribs.value("willMessageType").toString();
1346  if (str.isEmpty())
1347  reader->raiseWarning(attributeWarning.arg("'willMessageType'"));
1348  else
1349  m_MQTTWill.willMessageType = static_cast<MQTTClient::WillMessageType>(str.toInt());
1350 
1351  str = attribs.value("willUpdateType").toString();
1352  if (str.isEmpty())
1353  reader->raiseWarning(attributeWarning.arg("'willUpdateType'"));
1354  else
1355  m_MQTTWill.willUpdateType = static_cast<MQTTClient::WillUpdateType>(str.toInt());
1356 
1357  str = attribs.value("willTimeInterval").toString();
1358  if (str.isEmpty())
1359  reader->raiseWarning(attributeWarning.arg("'willTimeInterval'"));
1360  else
1361  m_MQTTWill.willTimeInterval = str.toInt();
1362 
1363  for (int i = 0; i < m_MQTTWill.willStatistics.count(); ++i) {
1364  str = attribs.value("willStatistics"+QString::number(i)).toString();
1365  if (str.isEmpty())
1366  reader->raiseWarning(attributeWarning.arg("'willTimeInterval'"));
1367  else
1368  m_MQTTWill.willStatistics[i] = str.toInt();
1369  }
1370  }
1371  } else if (reader->name() == "asciiFilter") {
1372  setFilter(new AsciiFilter);
1373  if (!m_filter->load(reader))
1374  return false;
1375  } else if (reader->name() == "MQTTSubscription") {
1376  auto* subscription = new MQTTSubscription(QString());
1377  subscription->setMQTTClient(this);
1378  m_MQTTSubscriptions.push_back(subscription);
1379  connect(subscription, &MQTTSubscription::loaded, this, &MQTTClient::subscriptionLoaded);
1380  if (!subscription->load(reader, preview)) {
1381  delete subscription;
1382  return false;
1383  }
1384  addChildFast(subscription);
1385  } else {// unknown element
1386  reader->raiseWarning(i18n("unknown element '%1'", reader->name().toString()));
1387  if (!reader->skipToEndElement()) return false;
1388  }
1389  }
1390 
1391  return !reader->hasError();
1392 }
AspectType
bool readCommentElement(XmlStreamReader *)
Load comment from an XML element.
@ Recursive
Recursively handle all descendents, not just immediate children.
@ IncludeHidden
Include aspects marked as "hidden" in numbering or listing children.
void addChildFast(AbstractAspect *)
Add the given Aspect to my list of children without any checks and without putting this step onto the...
QString name() const
int childCount(ChildIndexFlags flags={}) const
Return the number of child Aspects inheriting from given class.
void writeBasicAttributes(QXmlStreamWriter *) const
Save name and creation time to XML.
T * child(int index, ChildIndexFlags flags={}) const
bool readBasicAttributes(XmlStreamReader *)
Load name and creation time from XML.
void writeCommentElement(QXmlStreamWriter *) const
Save the comment to XML.
void removeChild(AbstractAspect *)
Remove the given Aspect from my list of children.
Manages the import/export of data organized as columns (vectors) from/to an ASCII-file.
Definition: AsciiFilter.h:42
void save(QXmlStreamWriter *) const override
bool load(XmlStreamReader *) override
Aspect that manages a column.
Definition: Column.h:42
Folder in a project.
Definition: Folder.h:35
The MQTT Client connects to the broker set in ImportFileWidget. It manages the MQTTSubscriptions,...
Definition: MQTTClient.h:48
bool m_paused
Definition: MQTTClient.h:218
void read()
called periodically when update type is TimeInterval
QString clientID() const
Returns the ID of the client.
Definition: MQTTClient.cpp:315
bool MQTTUseAuthentication() const
Returns whether the broker requires authentication or not.
Definition: MQTTClient.cpp:257
QString willTopic() const
Returns the will topic of the client.
Definition: MQTTClient.cpp:708
void MQTTTopicsChanged()
void onMQTTConnect()
called when the client successfully connected to the broker
QVector< QString > m_subscriptions
Definition: MQTTClient.h:227
void MQTTErrorChanged(QMqttClient::ClientError)
Handles some of the possible errors of the client, using MQTTErrorWidget.
void stopWillTimer() const
Stops the will timer.
Definition: MQTTClient.cpp:990
quint16 clientPort() const
Returns the port used by the broker.
Definition: MQTTClient.cpp:240
QString checkCommonLevel(const QString &first, const QString &second)
Returns the '+' wildcard containing topic name, which includes the given topic names.
Definition: MQTTClient.cpp:619
void setMQTTClientHostPort(const QString &, quint16)
Sets the host and port for the client.
Definition: MQTTClient.cpp:225
bool isPaused() const
Provides information about whether the reading is paused or not.
Definition: MQTTClient.cpp:160
bool willRetain() const
Returns the retain flag of the client's will message.
Definition: MQTTClient.cpp:724
bool m_MQTTFirstConnectEstablished
Definition: MQTTClient.h:231
QTimer * m_updateTimer
Definition: MQTTClient.h:224
void setWillUpdateType(WillUpdateType)
Sets the MQTTClient's will update type.
Definition: MQTTClient.cpp:923
void MQTTSubscriptionMessageReceived(const QMqttMessage &)
called when a message is received by a topic belonging to one of subscriptions of the client....
QString clientHostName() const
Returns hostname of the broker the client is connected to.
Definition: MQTTClient.cpp:233
QString clientUserName() const
Returns the username used for authentication.
Definition: MQTTClient.cpp:275
WillStatisticsType
Definition: MQTTClient.h:74
void MQTTSubscribed()
void setReadingType(ReadingType)
Sets the MQTTClient's reading type to readingType.
Definition: MQTTClient.cpp:183
MQTTWill m_MQTTWill
Definition: MQTTClient.h:240
MQTTWill willSettings() const
Definition: MQTTClient.cpp:673
void setSampleSize(int)
Sets the size rate to sampleSize.
Definition: MQTTClient.cpp:168
void addInitialMQTTSubscriptions(const QMqttTopicFilter &, quint8)
Adds the initial subscriptions that were set in ImportFileWidget.
Definition: MQTTClient.cpp:349
void addMQTTSubscription(const QString &, quint8)
Adds a new MQTTSubscription to the MQTTClient.
Definition: MQTTClient.cpp:366
int m_updateInterval
Definition: MQTTClient.h:222
void setFilter(AsciiFilter *)
Sets the filter of the MQTTClient. The ownership of the filter is passed to MQTTClient.
Definition: MQTTClient.cpp:109
void setKeepNValues(int)
Sets how many values we should store.
Definition: MQTTClient.cpp:143
void removeMQTTSubscription(const QString &)
Removes a MQTTSubscription from the MQTTClient.
Definition: MQTTClient.cpp:434
void setMQTTClientId(const QString &)
Sets the ID of the client.
Definition: MQTTClient.cpp:308
UpdateType updateType() const
Returns the MQTTClient's update type.
Definition: MQTTClient.cpp:208
void setWillTimeInterval(int)
Sets the time interval of updating the MQTTClient's will message, if update type is TimePeriod.
Definition: MQTTClient.cpp:939
int sampleSize() const
Returns the size rate.
Definition: MQTTClient.cpp:175
void addBeforeRemoveSubscription(const QString &, quint8)
Adds a MQTTSubscription to the MQTTClient Used when the user unsubscribes from a topic of a MQTTSubsc...
Definition: MQTTClient.cpp:479
void reparentTopic(const QString &topic, const QString &parent)
Reparents the given MQTTTopic to the given MQTTSubscription.
Definition: MQTTClient.cpp:538
void setMQTTWillUse(bool)
Sets whether the user wants to use will message or not.
Definition: MQTTClient.cpp:682
int willTimeInterval() const
Returns the time interval of updating the MQTTClient's will message.
Definition: MQTTClient.cpp:930
bool load(XmlStreamReader *, bool preview) override
QVector< QString > m_topicNames
Definition: MQTTClient.h:228
void readFromTopics()
void setMQTTRetain(bool)
Sets the flag on the given value. If retain is true we interpret retain messages, otherwise we do not...
Definition: MQTTClient.cpp:325
quint8 willQoS() const
Returns the QoS level of the client's will message.
Definition: MQTTClient.cpp:740
AsciiFilter * m_filter
Definition: MQTTClient.h:223
QString clientPassword() const
Returns the password used for authentication.
Definition: MQTTClient.cpp:282
void setUpdateInterval(int)
Sets the MQTTclient's update interval to interval.
Definition: MQTTClient.cpp:125
ReadingType readingType() const
Returns the MQTTClient's reading type.
Definition: MQTTClient.cpp:190
AsciiFilter * filter() const
Returns the filter of the MQTTClient.
Definition: MQTTClient.cpp:117
QVector< MQTTSubscription * > m_MQTTSubscriptions
Definition: MQTTClient.h:235
bool m_disconnectForWill
Definition: MQTTClient.h:236
QVector< bool > willStatistics() const
Returns a bool vector, meaning which statistic types are included in the will message If the correspo...
Definition: MQTTClient.cpp:975
QString willOwnMessage() const
Returns the own will message of the user.
Definition: MQTTClient.cpp:772
void startWillTimer() const
Starts the will timer, which will update the will message.
Definition: MQTTClient.cpp:982
bool m_prepared
Definition: MQTTClient.h:219
void setWillRetain(bool)
Sets the retain flag of the client's will message.
Definition: MQTTClient.cpp:717
int m_subscriptionCountToLoad
Definition: MQTTClient.h:239
void removeWillStatistics(WillStatisticsType)
Sets false the corresponding flag of the statistic type, what means that the given statistic will no ...
Definition: MQTTClient.cpp:967
void setMQTTUseAuthentication(bool)
Sets the flag on the given value. If set true it means that the broker requires authentication,...
Definition: MQTTClient.cpp:250
virtual ~MQTTClient() override
Definition: MQTTClient.cpp:54
ReadingType m_readingType
Definition: MQTTClient.h:217
void clientAboutToBeDeleted(const QString &, quint16)
int updateInterval() const
Returns the MQTTClient's update interval to interval.
Definition: MQTTClient.cpp:135
void addWillStatistics(WillStatisticsType)
Sets true the corresponding flag of the statistic type, what means that the given statistic type will...
Definition: MQTTClient.cpp:957
void setUpdateType(UpdateType)
Sets the MQTTClient's update type to updatetype and handles this change.
Definition: MQTTClient.cpp:198
QMqttClient * m_client
Definition: MQTTClient.h:225
WillMessageType willMessageType() const
Returns the will message type of the client.
Definition: MQTTClient.cpp:756
void setWillTopic(const QString &)
Sets the will topic of the client.
Definition: MQTTClient.cpp:700
bool m_MQTTUseAuthentication
Definition: MQTTClient.h:234
void setWillMessageType(WillMessageType)
Sets the will message type of the client.
Definition: MQTTClient.cpp:749
int m_sampleSize
Definition: MQTTClient.h:220
bool m_loaded
Definition: MQTTClient.h:237
QString statistics(const MQTTTopic *) const
Returns the statistical data that is needed by the topic for its MQTTClient's will message.
Definition: MQTTClient.cpp:848
void ready()
Definition: MQTTClient.cpp:70
void setWillSettings(const MQTTWill &)
Definition: MQTTClient.cpp:669
bool MQTTWillUse() const
Returns whether the user wants to use will message or not.
Definition: MQTTClient.cpp:691
QMap< QMqttTopicFilter, quint8 > m_subscribedTopicNameQoS
Definition: MQTTClient.h:226
void setMQTTClientAuthentication(const QString &, const QString &)
Sets the username and password for the client.
Definition: MQTTClient.cpp:267
QIcon icon() const override
Returns the MQTTClient's icon.
Definition: MQTTClient.cpp:215
QTimer * m_willTimer
Definition: MQTTClient.h:230
void setWillOwnMessage(const QString &)
Sets the own will message of the user.
Definition: MQTTClient.cpp:765
bool m_MQTTUseID
Definition: MQTTClient.h:233
void setMQTTUseID(bool)
Sets the flag on the given value. If set true it means that user wants to set the client ID,...
Definition: MQTTClient.cpp:292
void subscriptionLoaded(const QString &)
Called when a subscription is loaded. Checks whether every saved subscription was loaded or not....
QVector< QString > MQTTSubscriptions() const
Returns the name of every MQTTSubscription of the MQTTClient.
Definition: MQTTClient.cpp:356
void updateWillMessage()
Updates the will message of the client.
Definition: MQTTClient.cpp:779
void save(QXmlStreamWriter *) const override
bool m_MQTTRetain
Definition: MQTTClient.h:232
int keepNValues() const
Returns how many values we should store.
Definition: MQTTClient.cpp:150
int m_keepNValues
Definition: MQTTClient.h:221
QVector< QString > topicNames() const
Returns the name of every MQTTTopics which already received a message, and is child of the MQTTClient...
Definition: MQTTClient.cpp:339
UpdateType m_updateType
Definition: MQTTClient.h:216
bool MQTTRetain() const
Returns the flag, which set to true means that interpret retain messages, otherwise we do not.
Definition: MQTTClient.cpp:332
void continueReading()
Continue reading from messages after it was paused.
Definition: MQTTClient.cpp:88
bool MQTTUseID() const
Returns whether the user wants to set the client ID or not.
Definition: MQTTClient.cpp:299
void clearLastMessage()
Clear the lastly received message by the will topic Called when the will topic is changed.
Definition: MQTTClient.cpp:947
WillUpdateType willUpdateType() const
Returns the MQTTClient's will update type.
Definition: MQTTClient.cpp:914
int m_subscriptionsLoaded
Definition: MQTTClient.h:238
MQTTClient(const QString &name)
Definition: MQTTClient.cpp:43
bool checkTopicContains(const QString &superior, const QString &inferior)
Checks if a topic contains another one.
Definition: MQTTClient.cpp:576
void setWillQoS(quint8)
Sets the QoS level of the client's will message.
Definition: MQTTClient.cpp:733
void pauseReading()
Pause the reading from messages.
Definition: MQTTClient.cpp:97
void updateNow()
Updates the MQTTTopics of the client.
Definition: MQTTClient.cpp:78
Represents a subscription made in a MQTTClient object. It plays a role in managing MQTTTopic objects ...
const QVector< MQTTTopic * > topics() const
Returns the object's MQTTTopic children.
void loaded(const QString &)
Represents a topic of a subscription made in MQTTClient.
Definition: MQTTTopic.h:39
MQTTClient * mqttClient() const
Returns the MQTTClient the topic belongs to.
Definition: MQTTTopic.cpp:167
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...
void raiseWarning(const QString &)
@ NoError
Definition: qxtnamespace.h:64
#define i18n(m)
Definition: nsl_common.h:38
QVector< bool > willStatistics
Definition: MQTTClient.h:104
WillUpdateType willUpdateType
Definition: MQTTClient.h:103
WillMessageType willMessageType
Definition: MQTTClient.h:99