diff --git a/qt-ozwdaemon/mqttcommands/ping.cpp b/qt-ozwdaemon/mqttcommands/ping.cpp new file mode 100644 index 0000000..784a82c --- /dev/null +++ b/qt-ozwdaemon/mqttcommands/ping.cpp @@ -0,0 +1,17 @@ +#include "mqttcommands/ping.h" + +MqttCommand_Ping::MqttCommand_Ping(QObject *parent) : + MqttCommand(parent) +{ + this->m_requiredFields << "ping"; +} +MqttCommand* MqttCommand_Ping::Create(QObject *parent) { + return new MqttCommand_Ping(parent); +} + +bool MqttCommand_Ping::processMessage(QJsonDocument msg) { + QJsonObject js; + js["pong"] = msg["ping"]; + emit sendCommandUpdate(MqttCommand_Ping::GetCommand(), js); + return true; +} \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/ping.h b/qt-ozwdaemon/mqttcommands/ping.h new file mode 100644 index 0000000..e46d8a1 --- /dev/null +++ b/qt-ozwdaemon/mqttcommands/ping.h @@ -0,0 +1,16 @@ +#ifndef PING_H +#define PING_H + +#include "mqttcommands/mqttcommands.h" + +class MqttCommand_Ping : public MqttCommand { + Q_OBJECT +public: + MqttCommand_Ping(QObject *parent = nullptr); + static MqttCommand *Create(QObject *parent = nullptr); + static QString StaticGetCommand() { return "Ping";}; + QString GetCommand() override { return StaticGetCommand(); }; + bool processMessage(QJsonDocument) override; +}; + +#endif // PING_H diff --git a/qt-ozwdaemon/mqttpublisher.cpp b/qt-ozwdaemon/mqttpublisher.cpp index 20c6788..7c153a2 100644 --- a/qt-ozwdaemon/mqttpublisher.cpp +++ b/qt-ozwdaemon/mqttpublisher.cpp @@ -166,6 +166,11 @@ mqttpublisher::mqttpublisher(QObject *parent) : QObject(parent) this->m_client->setHostname(settings.value("MQTTServer", "127.0.0.1").toString()); this->m_client->setPort(static_cast(settings.value("MQTTPort", 1883).toInt())); + /* setup the Commands */ + this->m_commands = new MqttCommands(this); + this->m_commands->setupCommands(); + + connect(this->m_client, &QMqttClient::stateChanged, this, &mqttpublisher::updateLogStateChange); connect(this->m_client, &QMqttClient::disconnected, this, &mqttpublisher::brokerDisconnected); @@ -239,6 +244,19 @@ QString mqttpublisher::getValueTopic(QString topic, quint8 node, quint64 vid) { return t; } +QString mqttpublisher::getCommandTopic() { + QString t(MQTT_OZW_TOP_TOPIC); + t = t.arg(settings.value("Instance", 1).toInt()); + t.append(QString(MQTT_OZW_COMMAND_TOPIC)); + return t; +} + +QString mqttpublisher::getCommandResponseTopic(QString cmd) { + QString t(MQTT_OZW_TOP_TOPIC); + t = t.arg(settings.value("Instance", 1).toInt()); + t.append(QString(MQTT_OZW_RESPONSE_TOPIC).arg(cmd)); + return t; +} void mqttpublisher::setOZWDaemon(qtozwdaemon *ozwdaemon) { this->m_qtozwdeamon = ozwdaemon; @@ -288,6 +306,7 @@ void mqttpublisher::updateLogStateChange() qDebug() << content; if (this->m_client->state() == QMqttClient::ClientState::Connected) { this->m_client->subscribe(QMqttTopicFilter("/OpenZWave/commands")); + this->m_commands->setupSubscriptions(this->m_client, this->getCommandTopic()); } } @@ -319,6 +338,10 @@ bool mqttpublisher::sendValueUpdate(quint64 vidKey) { this->m_client->publish(QMqttTopicName(getValueTopic(MQTT_OZW_VID_TOPIC, node, vidKey)), QJsonDocument(this->m_values[vidKey]).toJson(), 0, true); return true; } +void mqttpublisher::sendCommandUpdate(QString command, QJsonObject js) { + this->m_client->publish(QMqttTopicName(getCommandResponseTopic(command.toLower())), QJsonDocument(js).toJson(), 0, false); + return; +} void mqttpublisher::ready() { @@ -466,3 +489,6 @@ void mqttpublisher::stopped(quint32 homeID) { } //void error(QTOZWErrorCodes errorcode); +QTOZWManager *mqttpublisher::getQTOZWManager() { + return this->m_qtozwdeamon->getManager(); +} diff --git a/qt-ozwdaemon/mqttpublisher.h b/qt-ozwdaemon/mqttpublisher.h index 0eb738a..c8d47f6 100644 --- a/qt-ozwdaemon/mqttpublisher.h +++ b/qt-ozwdaemon/mqttpublisher.h @@ -9,13 +9,17 @@ #include #include "qtozwdaemon.h" +#include "mqttcommands/mqttcommands.h" +class MqttCommands; #define MQTT_OZW_TOP_TOPIC "/OpenZWave/%1/" #define MQTT_OZW_STATS_TOPIC "statistics" #define MQTT_OZW_STATUS_TOPIC "status/" #define MQTT_OZW_NODE_TOPIC "node/%1/" #define MQTT_OZW_VID_TOPIC "node/%1/%2/" +#define MQTT_OZW_COMMAND_TOPIC "command/%1/" +#define MQTT_OZW_RESPONSE_TOPIC "event/%1/" class mqttNodeModel : public QTOZW_Nodes { Q_OBJECT @@ -41,6 +45,7 @@ class mqttpublisher : public QObject public: explicit mqttpublisher(QObject *parent = nullptr); void setOZWDaemon(qtozwdaemon *ozwdaemon); + signals: public slots: @@ -76,6 +81,8 @@ public slots: // void error(QTOZWErrorCodes errorcode); + QTOZWManager *getQTOZWManager(); + void sendCommandUpdate(QString, QJsonObject); private slots: @@ -89,10 +96,13 @@ private: QString getTopic(QString); QString getNodeTopic(QString, quint8); QString getValueTopic(QString, quint8, quint64); + QString getCommandTopic(); + QString getCommandResponseTopic(QString); bool sendStatusUpdate(); bool sendNodeUpdate(quint8); bool sendValueUpdate(quint64); + QJsonObject m_ozwstatus; QMap m_nodes; mqttNodeModel *m_nodeModel; @@ -103,6 +113,7 @@ private: qtozwdaemon *m_qtozwdeamon; QSettings settings; QTimer m_statsTimer; + MqttCommands *m_commands; }; #endif // MQTTPUBLISHER_H diff --git a/qt-ozwdaemon/qt-ozwdaemon.pro b/qt-ozwdaemon/qt-ozwdaemon.pro index de5b503..e72e18c 100644 --- a/qt-ozwdaemon/qt-ozwdaemon.pro +++ b/qt-ozwdaemon/qt-ozwdaemon.pro @@ -20,8 +20,10 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ + mqttcommands/ping.cpp \ mqttpublisher.cpp \ - qtozwdaemon.cpp + qtozwdaemon.cpp \ + mqttcommands/mqttcommands.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin @@ -29,8 +31,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ + mqttcommands/ping.h \ mqttpublisher.h \ - qtozwdaemon.h + qtozwdaemon.h \ + mqttcommands/mqttcommands.h include(../qt-openzwave.pri)