diff --git a/src/backend/datasources/MQTTSubscriptions.cpp b/src/backend/datasources/MQTTSubscriptions.cpp index 62f2dfbb0..d6a1e1da8 100644 --- a/src/backend/datasources/MQTTSubscriptions.cpp +++ b/src/backend/datasources/MQTTSubscriptions.cpp @@ -1,168 +1,239 @@ +/*************************************************************************** +File : MQTTSubscriptions.cpp +Project : LabPlot +Description : Represents a subscription made in MQTTClient +-------------------------------------------------------------------- +Copyright : (C) 2018 Kovacs Ferencz (kferike98@gmail.com) + +***************************************************************************/ + +/*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the Free Software * +* Foundation, Inc., 51 Franklin Street, Fifth Floor, * +* Boston, MA 02110-1301 USA * +* * +***************************************************************************/ + #ifdef HAVE_MQTT #include "backend/datasources/MQTTSubscriptions.h" - #include "backend/datasources/MQTTTopic.h" #include "backend/datasources/MQTTClient.h" #include #include +/*! + \class MQTTSubscriptions + \brief Represents a subscription made in a MQTTClient object. It plays a role in managing MQTTTopic objects + and makes possible representing the subscriptions and topics in a tree like structure + \ingroup datasources +*/ MQTTSubscriptions::MQTTSubscriptions(const QString& name) : Folder(name), m_subscriptionName(name) { qDebug()<<"MQTTSubscriptions constructor"; } MQTTSubscriptions::~MQTTSubscriptions() { qDebug()<<"MQTTSubscriptions destructor"; } +/*! + *\brief Adds an MQTTTopic as a child + * + * \param topicName the name of the topic, which will be added to the tree widget + */ void MQTTSubscriptions::addTopic(const QString& topicName) { MQTTTopic * newTopic = new MQTTTopic(topicName, this, false); m_topics.push_back(newTopic); qDebug()<<"Adding child topic: "+topicName; - //this->parentAspect()->addChild(newTopic); addChild(newTopic); } +/*! + *\brief Returns the object's MQTTTopic children + * + * \return a vector of pointers to the children of the MQTTSubscription + */ const QVector MQTTSubscriptions::topics() { qDebug()<<"returning topics"; return children(); } +/*! + *\brief Returns the object's parent + * + * \return a pointer to the parent MQTTTopic of the object + */ MQTTClient* MQTTSubscriptions::mqttClient() const{ return m_MQTTClient; } - -void MQTTSubscriptions::messageArrived(const QString& message, const QString& topicName){ +/*! + *\brief Called when a message arrived to a topic contained by the MQTTSubscription + * If the topic can't be found among the children, a new MQTTTopic is instantiated + * Passes the messages to the appropriate MQTTTopic + * + * \param message the message to pass + * \param topicName the name of the topic the message was sent to + */ +void MQTTSubscriptions::messageArrived(const QString& message, const QString& topicName){ bool found = false; QVector topics = children(); + //search for the topic among the MQTTTopic children for(int i = 0; i < topics.size(); ++i) { if(topicName == topics[i]->topicName()) { + //pass the message to the topic topics[i]->newMessage(message); + + //read the message if needed if((m_MQTTClient->updateType() == MQTTClient::UpdateType::NewData) && !m_MQTTClient->isPaused()) topics[i]->read(); + //add topic to m_topics if it isn't part of it bool addKnown = true; - for(int j = 0; j < m_topics.size(); ++j) { if(m_topics[j]->topicName() == topics[i]->topicName()) { addKnown = false; break; } } if(addKnown) m_topics.push_back(topics[i]); found = true; break; } } + //if the topic can't be found, we add it as a new MQTTTopic, and read from it if needed if(!found) { addTopic(topicName); m_topics.last()->newMessage(message); if((m_MQTTClient->updateType() == MQTTClient::UpdateType::NewData) && !m_MQTTClient->isPaused()) m_topics.last()->read(); } } -void MQTTSubscriptions::topicRead(const QString& topicName) { - for(int i = 0; i < m_topics.count(); ++i) { - if(topicName == m_topics[i]->topicName()) { - m_topics[i]->read(); - break; - } - } -} - +/*! + *\brief Returns the subscription's name + * + * \return m_subscriptionName + */ QString MQTTSubscriptions::subscriptionName() const { return m_subscriptionName; } +/*! + *\brief Sets the MQTTClient the subscription belongs to + * + * \param client + */ +void MQTTSubscriptions::setMQTTClient(MQTTClient* client) { + m_MQTTClient = client; +} + +/*! + *\brief Returns the icon of MQTTSubscriptions + */ +QIcon MQTTSubscriptions::icon() const { + QIcon icon; + icon = QIcon::fromTheme("labplot-MQTT"); + return icon; +} + +//############################################################################## +//################## Serialization/Deserialization ########################### +//############################################################################## +/*! + Saves as XML. + */ void MQTTSubscriptions::save(QXmlStreamWriter* writer) const { writer->writeStartElement("MQTTSubscriptions"); writeBasicAttributes(writer); writeCommentElement(writer); //general writer->writeStartElement("general"); writer->writeAttribute("subscriptionName", m_subscriptionName); writer->writeEndElement(); //MQTTTopics for(auto* topic : children(IncludeHidden)) topic->save(writer); writer->writeEndElement(); // "MQTTSubscriptions" } +/*! + Loads from XML. +*/ bool MQTTSubscriptions::load(XmlStreamReader* reader, bool preview) { qDebug()<<"Start loading MQTTSubscripiton"; if (!readBasicAttributes(reader)) return false; QString attributeWarning = i18n("Attribute '%1' missing or empty, default value is used"); QXmlStreamAttributes attribs; QString str; while (!reader->atEnd()) { reader->readNext(); if (reader->isEndElement() && reader->name() == "MQTTSubscriptions") break; if (!reader->isStartElement()) continue; if (reader->name() == "comment") { if (!readCommentElement(reader)) return false; } else if (reader->name() == "general") { qDebug()<<"MQTTSub general"; attribs = reader->attributes(); str = attribs.value("subscriptionName").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.arg("'subscriptionName'")); else { qDebug()<name() == QLatin1String("MQTTTopic")) { qDebug()<<"Load MQTTTopic"; MQTTTopic* topic = new MQTTTopic("", this, false); if (!topic->load(reader, preview)) { delete topic; return false; } m_topics.push_back(topic); addChildFast(topic); } else {// unknown element reader->raiseWarning(i18n("unknown element '%1'", reader->name().toString())); if (!reader->skipToEndElement()) return false; } } qDebug()<<"End loading MQTTSubscripiton"; qDebug()<name()); return !reader->hasError(); } - -void MQTTSubscriptions::setMQTTClient(MQTTClient* client) { - m_MQTTClient = client; -} - -QIcon MQTTSubscriptions::icon() const { - QIcon icon; - icon = QIcon::fromTheme("labplot-MQTT"); - return icon; -} - #endif diff --git a/src/backend/datasources/MQTTSubscriptions.h b/src/backend/datasources/MQTTSubscriptions.h index ce99f1cce..104a0b0d0 100644 --- a/src/backend/datasources/MQTTSubscriptions.h +++ b/src/backend/datasources/MQTTSubscriptions.h @@ -1,42 +1,41 @@ #ifndef MQTTSUBSCRIPTIONS_H #define MQTTSUBSCRIPTIONS_H #include #include #include "backend/core/Folder.h" #include "backend/datasources/MQTTTopic.h" class MQTTSubscriptions : public Folder{ Q_OBJECT public: MQTTSubscriptions(const QString& name); ~MQTTSubscriptions() override; void setMQTTClient(MQTTClient *client); QString subscriptionName() const; void addTopic(const QString&); const QVector topics(); MQTTClient* mqttClient() const; void messageArrived(const QString&, const QString&); - void topicRead(const QString&); QIcon icon() const override; void save(QXmlStreamWriter*) const override; bool load(XmlStreamReader*, bool preview) override; private: QString m_subscriptionName; MQTTClient* m_MQTTClient; QVector m_topics; public slots: private slots: signals: void loaded(const QString &); }; #endif // MQTTSUBSCRIPTIONS_H