File indexing completed on 2024-05-12 15:26:57

0001 /***************************************************************************
0002 File        : MQTTClient.h
0003 Project     : LabPlot
0004 Description : Represents a MQTT Client
0005 --------------------------------------------------------------------
0006 Copyright   : (C) 2018 Kovacs Ferencz (kferike98@gmail.com)
0007 
0008 ***************************************************************************/
0009 
0010 /***************************************************************************
0011 *                                                                         *
0012 *  This program is free software; you can redistribute it and/or modify   *
0013 *  it under the terms of the GNU General Public License as published by   *
0014 *  the Free Software Foundation; either version 2 of the License, or      *
0015 *  (at your option) any later version.                                    *
0016 *                                                                         *
0017 *  This program is distributed in the hope that it will be useful,        *
0018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020 *  GNU General Public License for more details.                           *
0021 *                                                                         *
0022 *   You should have received a copy of the GNU General Public License     *
0023 *   along with this program; if not, write to the Free Software           *
0024 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025 *   Boston, MA  02110-1301  USA                                           *
0026 *                                                                         *
0027 ***************************************************************************/
0028 #ifndef MQTTCLIENT_H
0029 #define MQTTCLIENT_H
0030 
0031 #include "backend/core/Folder.h"
0032 
0033 #include <QVector>
0034 #include <QtMqtt/QMqttClient>
0035 #include <QtMqtt/QMqttMessage>
0036 #include <QtMqtt/QMqttSubscription>
0037 #include <QtMqtt/QMqttTopicFilter>
0038 #include <QtMqtt/QMqttTopicName>
0039 #include <QMap>
0040 
0041 class AsciiFilter;
0042 class MQTTSubscription;
0043 class MQTTTopic;
0044 class QAction;
0045 class QTimer;
0046 class QString;
0047 
0048 class MQTTClient : public Folder {
0049     Q_OBJECT
0050 
0051 public:
0052     enum class UpdateType {
0053         TimeInterval = 0,
0054         NewData
0055     };
0056 
0057     enum class ReadingType {
0058         ContinuousFixed = 0,
0059         FromEnd,
0060         TillEnd
0061     };
0062 
0063     enum class WillMessageType {
0064         OwnMessage = 0,
0065         Statistics,
0066         LastMessage
0067     };
0068 
0069     enum class WillUpdateType {
0070         TimePeriod = 0,
0071         OnClick
0072     };
0073 
0074     enum class WillStatisticsType {
0075         NoStatistics = -1,
0076         Minimum,
0077         Maximum,
0078         ArithmeticMean,
0079         GeometricMean,
0080         HarmonicMean,
0081         ContraharmonicMean,
0082         Median,
0083         Variance,
0084         StandardDeviation,
0085         MeanDeviation,
0086         MeanDeviationAroundMedian,
0087         MedianDeviation,
0088         Skewness,
0089         Kurtosis,
0090         Entropy
0091     };
0092 
0093     struct MQTTWill {
0094         bool enabled{false};
0095         QString willMessage;
0096         QString willTopic;
0097         bool willRetain{false};
0098         quint8 willQoS{0};
0099         WillMessageType willMessageType{MQTTClient::WillMessageType::OwnMessage};
0100         QString willOwnMessage;
0101         QString willLastMessage;
0102         int willTimeInterval{1000};
0103         WillUpdateType willUpdateType{MQTTClient::WillUpdateType::TimePeriod};
0104         QVector<bool> willStatistics{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
0105     };
0106 
0107     explicit MQTTClient(const QString& name);
0108     virtual ~MQTTClient() override;
0109 
0110     void ready();
0111 
0112     UpdateType updateType() const;
0113     void setUpdateType(UpdateType);
0114 
0115     ReadingType readingType() const;
0116     void setReadingType(ReadingType);
0117 
0118     int sampleSize() const;
0119     void setSampleSize(int);
0120 
0121     bool isPaused() const;
0122 
0123     void setUpdateInterval(int);
0124     int updateInterval() const;
0125 
0126     void setKeepNValues(int);
0127     int keepNValues() const;
0128 
0129     void setKeepLastValues(bool);
0130     bool keepLastValues() const;
0131 
0132     void setMQTTClientHostPort(const QString&, quint16);
0133     void setMQTTClientAuthentication(const QString&, const QString&);
0134     void setMQTTClientId(const QString&);
0135 
0136     void addInitialMQTTSubscriptions(const QMqttTopicFilter&, quint8);
0137     QVector<QString> MQTTSubscriptions() const;
0138 
0139     bool checkTopicContains(const QString& superior, const QString& inferior);
0140     QString checkCommonLevel(const QString& first, const QString& second);
0141 
0142     QString clientHostName() const;
0143     quint16 clientPort() const;
0144     QString clientPassword() const;
0145     QString clientUserName() const;
0146     QString clientID () const;
0147 
0148     void updateNow();
0149     void pauseReading();
0150     void continueReading();
0151 
0152     void setFilter(AsciiFilter*);
0153     AsciiFilter* filter() const;
0154 
0155     QIcon icon() const override;
0156 
0157     void save(QXmlStreamWriter*) const override;
0158     bool load(XmlStreamReader*, bool preview) override;
0159 
0160     QVector<QString> topicNames() const;
0161     bool checkAllArrived();
0162 
0163     void setWillSettings(const MQTTWill&);
0164     MQTTWill willSettings() const;
0165 
0166     void setMQTTWillUse(bool);
0167     bool MQTTWillUse() const;
0168 
0169     void setWillTopic(const QString&);
0170     QString willTopic() const;
0171     QString statistics(const MQTTTopic*) const;
0172 
0173     void setWillRetain(bool);
0174     bool willRetain() const;
0175 
0176     void setWillQoS(quint8);
0177     quint8 willQoS() const;
0178 
0179     void setWillMessageType(WillMessageType);
0180     WillMessageType willMessageType() const;
0181 
0182     void setWillOwnMessage(const QString&);
0183     QString willOwnMessage() const;
0184 
0185     WillUpdateType willUpdateType() const;
0186     void setWillUpdateType(WillUpdateType);
0187 
0188     int willTimeInterval() const;
0189     void setWillTimeInterval(int);
0190 
0191     void startWillTimer() const;
0192     void stopWillTimer() const;
0193 
0194     void updateWillMessage() ;
0195 
0196     void setMQTTRetain(bool);
0197     bool MQTTRetain() const;
0198 
0199     void setMQTTUseID(bool);
0200     bool MQTTUseID() const;
0201 
0202     void setMQTTUseAuthentication(bool);
0203     bool MQTTUseAuthentication() const;
0204 
0205     void clearLastMessage();
0206     void addWillStatistics(WillStatisticsType);
0207     void removeWillStatistics(WillStatisticsType);
0208     QVector<bool> willStatistics() const;
0209 
0210     void addMQTTSubscription(const QString&, quint8);
0211     void removeMQTTSubscription(const QString&);
0212     void addBeforeRemoveSubscription(const QString&, quint8);
0213     void reparentTopic(const QString& topic, const QString& parent);
0214 
0215 private:
0216     UpdateType m_updateType{UpdateType::TimeInterval};
0217     ReadingType m_readingType{ReadingType::ContinuousFixed};
0218     bool m_paused{false};
0219     bool m_prepared{false};
0220     int m_sampleSize{1};
0221     int m_keepNValues{0};
0222     int m_updateInterval{1000};
0223     AsciiFilter* m_filter{nullptr};
0224     QTimer* m_updateTimer;
0225     QMqttClient* m_client;
0226     QMap<QMqttTopicFilter, quint8> m_subscribedTopicNameQoS;
0227     QVector<QString> m_subscriptions;
0228     QVector<QString> m_topicNames;
0229     bool m_MQTTTest{false};
0230     QTimer* m_willTimer;
0231     bool m_MQTTFirstConnectEstablished{false};
0232     bool m_MQTTRetain{false};
0233     bool m_MQTTUseID{false};
0234     bool m_MQTTUseAuthentication{false};
0235     QVector<MQTTSubscription*> m_MQTTSubscriptions;
0236     bool m_disconnectForWill{false};
0237     bool m_loaded{false};
0238     int m_subscriptionsLoaded{0};
0239     int m_subscriptionCountToLoad{0};
0240     MQTTWill m_MQTTWill;
0241 
0242 public slots:
0243     void read();
0244 
0245 private slots:
0246     void onMQTTConnect();
0247     void MQTTSubscriptionMessageReceived(const QMqttMessage&);
0248     void MQTTErrorChanged(QMqttClient::ClientError);
0249     void subscriptionLoaded(const QString&);
0250 
0251 signals:
0252     void MQTTSubscribed();
0253     void MQTTTopicsChanged();
0254     void readFromTopics();
0255     void clientAboutToBeDeleted(const QString&, quint16);
0256 };
0257 #endif // MQTTCLIENT_H