From ea376a48cd764cd373cda0231ccc9efbddb090c5 Mon Sep 17 00:00:00 2001 From: Justin Hammond Date: Sun, 3 Nov 2019 16:50:28 +0800 Subject: [PATCH] add these missing files --- qt-ozwdaemon/mqttcommands/mqttcommands.cpp | 92 ++++++++++++++++++++++ qt-ozwdaemon/mqttcommands/mqttcommands.h | 52 ++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 qt-ozwdaemon/mqttcommands/mqttcommands.cpp create mode 100644 qt-ozwdaemon/mqttcommands/mqttcommands.h diff --git a/qt-ozwdaemon/mqttcommands/mqttcommands.cpp b/qt-ozwdaemon/mqttcommands/mqttcommands.cpp new file mode 100644 index 0000000..b228c8a --- /dev/null +++ b/qt-ozwdaemon/mqttcommands/mqttcommands.cpp @@ -0,0 +1,92 @@ +#include "mqttcommands/mqttcommands.h" +#include "mqttcommands/ping.h" + + +MqttCommand::MqttCommand(QObject *parent) : + QObject(parent) +{ +} + +void MqttCommand::Setup(QMqttSubscription *subscription) { + this->m_subscription = subscription; + qDebug() << "Subscription Setup for " << this->m_subscription->topic(); + connect(this->m_subscription, &QMqttSubscription::messageReceived, this, &MqttCommand::messageReceived); + connect(this, &MqttCommand::sendCommandUpdate, getMqttPublisher(), &mqttpublisher::sendCommandUpdate); +} + +QTOZWManager *MqttCommand::getOZWManager() { + mqttpublisher *mqttp = getMqttPublisher(); + if (mqttp) { + return mqttp->getQTOZWManager(); + } + return nullptr; +} +mqttpublisher *MqttCommand::getMqttPublisher() { + if (this->parent()) { + mqttpublisher *mqttp = qobject_cast(this->parent()); + return mqttp; + } + return nullptr; +} + +void MqttCommand::messageReceived(QMqttMessage msg) { + qDebug() << "Got "<< msg.topic().name()<< " Message: " << msg.payload(); + QJsonParseError jerrormsg; + QJsonDocument jmsg = QJsonDocument::fromJson(msg.payload(), &jerrormsg); + if (jmsg.isNull()) { + QJsonObject js; + js["Error"] = jerrormsg.errorString(); + emit sendCommandUpdate(GetCommand(), js); + qWarning() << "Json Parse Error for " << GetCommand() << ": " << jerrormsg.errorString() << ": " << msg.payload(); + return; + } + QString field; + foreach (field, this->m_requiredFields) { + if (jmsg[field].isUndefined()) { + QJsonObject js; + js["Error"] = QString("Missing Field ").append(field); + emit sendCommandUpdate(GetCommand(), js); + qWarning() << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload(); + return; + } + } + if (this->processMessage(jmsg)) { + qInfo() << "Processed Message for " << GetCommand() << ": " << msg.payload(); + return; + } else { + qWarning() << "Message Processing for " << GetCommand() << " failed: " << msg.payload(); + } +} + + + +MqttCommands::MqttCommands(QObject *parent) : + QObject(parent) +{ + +} + +void MqttCommands::Register(QString command, pfnCreateCommand_t _create) { + qDebug() << "Registering Command " << command; + this->m_commands.insert(command, _create); +} + +void MqttCommands::setupCommands() { + this->Register(MqttCommand_Ping::StaticGetCommand(), &MqttCommand_Ping::Create); +} + +void MqttCommands::setupSubscriptions(QMqttClient *mqttclient, QString topTopic) { + QMap::iterator it; + for (it = this->m_commands.begin(); it != this->m_commands.end(); it++) { + qDebug() << "Creating Subscription for " << it.key(); + pfnCreateCommand_t cmd = it.value(); + MqttCommand *command = cmd(this->parent()); + QMqttSubscription *subscription = mqttclient->subscribe(QMqttTopicFilter(topTopic.arg(it.key().toLower()))); + command->Setup(subscription); + } + + +} + + + diff --git a/qt-ozwdaemon/mqttcommands/mqttcommands.h b/qt-ozwdaemon/mqttcommands/mqttcommands.h new file mode 100644 index 0000000..cbbde8f --- /dev/null +++ b/qt-ozwdaemon/mqttcommands/mqttcommands.h @@ -0,0 +1,52 @@ +#ifndef MQTTCOMMANDS_H +#define MQTTCOMMANDS_H + +#include +#include +#include +#include +#include + +#include +#include +#include "mqttpublisher.h" + + +class mqttpublisher; + +class MqttCommand : public QObject { + Q_OBJECT +public: + MqttCommand(QObject *parent = nullptr); + void Setup(QMqttSubscription *); + void messageReceived(QMqttMessage msg); + virtual bool processMessage(QJsonDocument) = 0; + virtual QString GetCommand() = 0; +signals: + void sendCommandUpdate(QString, QJsonObject); +protected: + QTOZWManager *getOZWManager(); + mqttpublisher *getMqttPublisher(); + QVector m_requiredFields; + +private: + QMqttSubscription *m_subscription; +}; + + +typedef MqttCommand* (*pfnCreateCommand_t)(QObject *); + + +class MqttCommands : public QObject { + Q_OBJECT +public: + MqttCommands(QObject *parent = nullptr); + void Register(QString command, pfnCreateCommand_t _create); + void setupCommands(); + void setupSubscriptions(QMqttClient *, QString); +private: + QMap m_commands; +}; + + +#endif