mirror of
https://github.com/Fishwaldo/qt-openzwave.git
synced 2025-03-16 03:51:25 +00:00
Introduce MQTT Command Processor
This commit is contained in:
parent
d3ac01e6d8
commit
8aaa26aa28
5 changed files with 76 additions and 2 deletions
17
qt-ozwdaemon/mqttcommands/ping.cpp
Normal file
17
qt-ozwdaemon/mqttcommands/ping.cpp
Normal file
|
@ -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;
|
||||
}
|
16
qt-ozwdaemon/mqttcommands/ping.h
Normal file
16
qt-ozwdaemon/mqttcommands/ping.h
Normal file
|
@ -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
|
|
@ -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<quint16>(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();
|
||||
}
|
||||
|
|
|
@ -9,13 +9,17 @@
|
|||
#include <QTimer>
|
||||
|
||||
#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<quint8, QJsonObject> m_nodes;
|
||||
mqttNodeModel *m_nodeModel;
|
||||
|
@ -103,6 +113,7 @@ private:
|
|||
qtozwdaemon *m_qtozwdeamon;
|
||||
QSettings settings;
|
||||
QTimer m_statsTimer;
|
||||
MqttCommands *m_commands;
|
||||
};
|
||||
|
||||
#endif // MQTTPUBLISHER_H
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue