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)  

Go to the documentation of this file.
1 /***************************************************************************
2 File : MQTTClient.h
3 Project : LabPlot
4 Description : Represents a MQTT Client
5 --------------------------------------------------------------------
6 Copyright : (C) 2018 Kovacs Ferencz (
8 ***************************************************************************/
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 *
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 ***************************************************************************/
28 #ifndef MQTTCLIENT_H
29 #define MQTTCLIENT_H
31 #include "backend/core/Folder.h"
33 #include <QVector>
34 #include <QtMqtt/QMqttClient>
35 #include <QtMqtt/QMqttMessage>
36 #include <QtMqtt/QMqttSubscription>
37 #include <QtMqtt/QMqttTopicFilter>
38 #include <QtMqtt/QMqttTopicName>
39 #include <QMap>
41 class AsciiFilter;
42 class MQTTSubscription;
43 class MQTTTopic;
44 class QAction;
45 class QTimer;
46 class QString;
48 class MQTTClient : public Folder {
51 public:
52  enum class UpdateType {
53  TimeInterval = 0,
54  NewData
55  };
57  enum class ReadingType {
58  ContinuousFixed = 0,
59  FromEnd,
60  TillEnd
61  };
63  enum class WillMessageType {
64  OwnMessage = 0,
65  Statistics,
67  };
69  enum class WillUpdateType {
70  TimePeriod = 0,
71  OnClick
72  };
74  enum class WillStatisticsType {
75  NoStatistics = -1,
76  Minimum,
77  Maximum,
82  Median,
83  Variance,
88  Skewness,
89  Kurtosis,
90  Entropy
91  };
93  struct MQTTWill {
94  bool enabled{false};
95  QString willMessage;
96  QString willTopic;
97  bool willRetain{false};
98  quint8 willQoS{0};
100  QString willOwnMessage;
102  int willTimeInterval{1000};
104  QVector<bool> willStatistics{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
105  };
107  explicit MQTTClient(const QString& name);
108  virtual ~MQTTClient() override;
110  void ready();
112  UpdateType updateType() const;
115  ReadingType readingType() const;
118  int sampleSize() const;
119  void setSampleSize(int);
121  bool isPaused() const;
123  void setUpdateInterval(int);
124  int updateInterval() const;
126  void setKeepNValues(int);
127  int keepNValues() const;
129  void setKeepLastValues(bool);
130  bool keepLastValues() const;
132  void setMQTTClientHostPort(const QString&, quint16);
133  void setMQTTClientAuthentication(const QString&, const QString&);
134  void setMQTTClientId(const QString&);
136  void addInitialMQTTSubscriptions(const QMqttTopicFilter&, quint8);
139  bool checkTopicContains(const QString& superior, const QString& inferior);
140  QString checkCommonLevel(const QString& first, const QString& second);
142  QString clientHostName() const;
143  quint16 clientPort() const;
144  QString clientPassword() const;
145  QString clientUserName() const;
146  QString clientID () const;
148  void updateNow();
149  void pauseReading();
150  void continueReading();
152  void setFilter(AsciiFilter*);
153  AsciiFilter* filter() const;
155  QIcon icon() const override;
157  void save(QXmlStreamWriter*) const override;
158  bool load(XmlStreamReader*, bool preview) override;
163  void setWillSettings(const MQTTWill&);
164  MQTTWill willSettings() const;
166  void setMQTTWillUse(bool);
167  bool MQTTWillUse() const;
169  void setWillTopic(const QString&);
170  QString willTopic() const;
171  QString statistics(const MQTTTopic*) const;
173  void setWillRetain(bool);
174  bool willRetain() const;
176  void setWillQoS(quint8);
177  quint8 willQoS() const;
182  void setWillOwnMessage(const QString&);
183  QString willOwnMessage() const;
188  int willTimeInterval() const;
189  void setWillTimeInterval(int);
191  void startWillTimer() const;
192  void stopWillTimer() const;
194  void updateWillMessage() ;
196  void setMQTTRetain(bool);
197  bool MQTTRetain() const;
199  void setMQTTUseID(bool);
200  bool MQTTUseID() const;
202  void setMQTTUseAuthentication(bool);
203  bool MQTTUseAuthentication() const;
205  void clearLastMessage();
210  void addMQTTSubscription(const QString&, quint8);
211  void removeMQTTSubscription(const QString&);
212  void addBeforeRemoveSubscription(const QString&, quint8);
213  void reparentTopic(const QString& topic, const QString& parent);
215 private:
218  bool m_paused{false};
219  bool m_prepared{false};
220  int m_sampleSize{1};
222  int m_updateInterval{1000};
224  QTimer* m_updateTimer;
225  QMqttClient* m_client;
226  QMap<QMqttTopicFilter, quint8> m_subscribedTopicNameQoS;
229  bool m_MQTTTest{false};
230  QTimer* m_willTimer;
232  bool m_MQTTRetain{false};
233  bool m_MQTTUseID{false};
236  bool m_disconnectForWill{false};
237  bool m_loaded{false};
242 public slots:
243  void read();
245 private slots:
246  void onMQTTConnect();
247  void MQTTSubscriptionMessageReceived(const QMqttMessage&);
248  void MQTTErrorChanged(QMqttClient::ClientError);
249  void subscriptionLoaded(const QString&);
251 signals:
255  void clientAboutToBeDeleted(const QString&, quint16);
256 };
257 #endif // MQTTCLIENT_H
AbstractAspect * parent(AspectType type) const
In the parent-child hierarchy, return the first parent of type.
QString name() const
Manages the import/export of data organized as columns (vectors) from/to an ASCII-file.
Definition: AsciiFilter.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
bool m_MQTTTest
Definition: MQTTClient.h:229
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
Definition: MQTTClient.h:74
void MQTTSubscribed()
void setReadingType(ReadingType)
Sets the MQTTClient's reading type to readingType.
Definition: MQTTClient.cpp:183
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
bool checkAllArrived()
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
bool keepLastValues() const
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
void setKeepLastValues(bool)
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 ...
Represents a topic of a subscription made in MQTTClient.
Definition: MQTTTopic.h:39
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...
QVector< bool > willStatistics
Definition: MQTTClient.h:104
WillUpdateType willUpdateType
Definition: MQTTClient.h:103
WillMessageType willMessageType
Definition: MQTTClient.h:99