Porting to RapidJson

This commit is contained in:
Justin Hammond 2019-11-10 01:32:10 +08:00
parent f805d596dc
commit d634d95c45
67 changed files with 494 additions and 588 deletions

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_IsNodeFailed::Create(QObject *parent) {
return new MqttCommand_IsNodeFailed(parent);
}
bool MqttCommand_IsNodeFailed::processMessage(QJsonDocument msg) {
bool MqttCommand_IsNodeFailed::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->IsNodeFailed(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->IsNodeFailed(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "IsNodeFailed";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_IsNodeFailed(QObject *parent = nullptr);
};

View file

@ -9,17 +9,7 @@ MqttCommand* MqttCommand_AddNode::Create(QObject *parent) {
return new MqttCommand_AddNode(parent);
}
bool MqttCommand_AddNode::processMessage(QJsonDocument msg) {
Q_UNUSED(msg);
bool MqttCommand_AddNode::processMessage(rapidjson::Document &msg) {
QTOZWManager *mgr = getOZWManager();
if (mgr->addNode(msg["secure"].toBool())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->addNode(msg["secure"].GetBool()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "AddNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_AddNode(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_AssignReturnRoute::Create(QObject *parent) {
return new MqttCommand_AssignReturnRoute(parent);
}
bool MqttCommand_AssignReturnRoute::processMessage(QJsonDocument msg) {
bool MqttCommand_AssignReturnRoute::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->assignReturnRoute(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->assignReturnRoute(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "AssignReturnRoute";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_AssignReturnRoute(QObject *parent = nullptr);
};

View file

@ -8,17 +8,8 @@ MqttCommand* MqttCommand_CancelControllerCommand::Create(QObject *parent) {
return new MqttCommand_CancelControllerCommand(parent);
}
bool MqttCommand_CancelControllerCommand::processMessage(QJsonDocument msg) {
bool MqttCommand_CancelControllerCommand::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
if (mgr->cancelControllerCommand()) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->cancelControllerCommand());
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "CancelControllerCommand";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_CancelControllerCommand(QObject *parent = nullptr);
};

View file

@ -9,24 +9,11 @@ MqttCommand* MqttCommand_CheckLatestConfigFileRevision::Create(QObject *parent)
return new MqttCommand_CheckLatestConfigFileRevision(parent);
}
bool MqttCommand_CheckLatestConfigFileRevision::processMessage(QJsonDocument msg) {
bool MqttCommand_CheckLatestConfigFileRevision::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->checkLatestConfigFileRevision(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->checkLatestConfigFileRevision(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "CheckLatestConfigFileRevision";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_CheckLatestConfigFileRevision(QObject *parent = nullptr);
};

View file

@ -8,12 +8,9 @@ MqttCommand* MqttCommand_CheckLatestMFSRevision::Create(QObject *parent) {
return new MqttCommand_CheckLatestMFSRevision(parent);
}
bool MqttCommand_CheckLatestMFSRevision::processMessage(QJsonDocument msg) {
bool MqttCommand_CheckLatestMFSRevision::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
mgr->checkLatestMFSRevision();
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "CheckLatestMFSRevision";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_CheckLatestMFSRevision(QObject *parent = nullptr);
};

View file

@ -9,24 +9,11 @@ MqttCommand* MqttCommand_DeleteAllReturnRoute::Create(QObject *parent) {
return new MqttCommand_DeleteAllReturnRoute(parent);
}
bool MqttCommand_DeleteAllReturnRoute::processMessage(QJsonDocument msg) {
bool MqttCommand_DeleteAllReturnRoute::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->deleteAllReturnRoute(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->deleteAllReturnRoute(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "DeleteAllReturnRoute";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_DeleteAllReturnRoute(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_DownloadLatestConfigFileRevision::Create(QObject *paren
return new MqttCommand_DownloadLatestConfigFileRevision(parent);
}
bool MqttCommand_DownloadLatestConfigFileRevision::processMessage(QJsonDocument msg) {
bool MqttCommand_DownloadLatestConfigFileRevision::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->downloadLatestConfigFileRevision(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->downloadLatestConfigFileRevision(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "DownloadLatestConfigFileRevision";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_DownloadLatestConfigFileRevision(QObject *parent = nullptr);
};

View file

@ -8,17 +8,8 @@ MqttCommand* MqttCommand_DownloadLatestMFSRevision::Create(QObject *parent) {
return new MqttCommand_DownloadLatestMFSRevision(parent);
}
bool MqttCommand_DownloadLatestMFSRevision::processMessage(QJsonDocument msg) {
bool MqttCommand_DownloadLatestMFSRevision::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
if (mgr->downloadLatestMFSRevision()) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->downloadLatestMFSRevision());
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "DownloadLatestMFSRevision";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_DownloadLatestMFSRevision(QObject *parent = nullptr);
};

View file

@ -8,12 +8,9 @@ MqttCommand* MqttCommand_HardResetController::Create(QObject *parent) {
return new MqttCommand_HardResetController(parent);
}
bool MqttCommand_HardResetController::processMessage(QJsonDocument msg) {
bool MqttCommand_HardResetController::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
mgr->hardResetController();
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "HardResetController";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_HardResetController(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_HasNodeFailed::Create(QObject *parent) {
return new MqttCommand_HasNodeFailed(parent);
}
bool MqttCommand_HasNodeFailed::processMessage(QJsonDocument msg) {
bool MqttCommand_HasNodeFailed::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->hasNodeFailed(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->hasNodeFailed(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "HasNodeFailed";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_HasNodeFailed(QObject *parent = nullptr);
};

View file

@ -9,11 +9,8 @@ MqttCommand* MqttCommand_HealNetwork::Create(QObject *parent) {
return new MqttCommand_HealNetwork(parent);
}
bool MqttCommand_HealNetwork::processMessage(QJsonDocument msg) {
bool MqttCommand_HealNetwork::processMessage(rapidjson::Document &msg) {
QTOZWManager *mgr = getOZWManager();
mgr->healNetwork(msg["doreturnroute"].toBool());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->healNetwork(msg["doreturnroute"].GetBool());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "HealNetwork";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_HealNetwork(QObject *parent = nullptr);
};

View file

@ -10,18 +10,11 @@ MqttCommand* MqttCommand_HealNetworkNode::Create(QObject *parent) {
return new MqttCommand_HealNetworkNode(parent);
}
bool MqttCommand_HealNetworkNode::processMessage(QJsonDocument msg) {
bool MqttCommand_HealNetworkNode::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
mgr->healNetworkNode(msg["node"].toInt(), msg["doreturnroute"].toBool());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->healNetworkNode(msg["node"].GetUint(), msg["doreturnroute"].GetBool());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "HealNetworkNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_HealNetworkNode(QObject *parent = nullptr);
};

View file

@ -1,3 +1,6 @@
#include <rapidjson/error/en.h>
#include "mqttcommands/mqttcommands.h"
#include "mqttcommands/ping.h"
#include "mqttcommands/open.h"
@ -61,61 +64,61 @@ mqttpublisher *MqttCommand::getMqttPublisher() {
void MqttCommand::messageReceived(QMqttMessage msg) {
qCDebug(ozwmc) << "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();
rapidjson::Document jmsg;
jmsg.Parse(msg.payload());
if (jmsg.HasParseError()) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", rapidjson::GetParseError_En(jmsg.GetParseError()));
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Json Parse Error for " << GetCommand() << ": " << jerrormsg.errorString() << ": " << msg.payload();
qCWarning(ozwmc) << "Json Parse Error for " << GetCommand() << ": " << rapidjson::GetParseError_En(jmsg.GetParseError()) << ": " << msg.payload();
return;
}
QString field;
foreach (field, this->m_requiredIntFields) {
if (jmsg[field].isUndefined()) {
QJsonObject js;
js["Error"] = QString("Missing Field ").append(field);
if (!jmsg.HasMember(field.toStdString().c_str())) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload();
return;
}
if (!jmsg[field].isDouble()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not Integer");
if (!jmsg[field.toStdString().c_str()].IsNumber()) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not Integer").toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Incorrect Field Type (Int) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload();
qCWarning(ozwmc) << "Incorrect Field Type (Int) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload();
return;
}
}
foreach (field, this->m_requiredStringFields) {
if (jmsg[field].isUndefined()) {
QJsonObject js;
js["Error"] = QString("Missing Field ").append(field);
if (!jmsg.HasMember(field.toStdString().c_str())) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload();
return;
}
if (!jmsg[field].isString()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not String");
if (!jmsg[field.toStdString().c_str()].IsString()) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not String").toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Incorrect Field Type (String) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload();
qCWarning(ozwmc) << "Incorrect Field Type (String) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload();
return;
}
}
foreach (field, this->m_requiredBoolFields) {
if (jmsg[field].isUndefined()) {
QJsonObject js;
js["Error"] = QString("Missing Field ").append(field);
if (!jmsg.HasMember(field.toStdString().c_str())) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload();
return;
}
if (!jmsg[field].isBool()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not Bool");
if (!jmsg[field.toStdString().c_str()].IsBool()) {
rapidjson::Document js;
QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not Bool").toStdString().c_str());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmc) << "Incorrect Field Type (Bool) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload();
qCWarning(ozwmc) << "Incorrect Field Type (Bool) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload();
return;
}
}
@ -128,29 +131,47 @@ void MqttCommand::messageReceived(QMqttMessage msg) {
}
}
bool MqttCommand::checkNode(QJsonDocument jmsg, QString field) {
quint8 node = jmsg[field].toInt();
bool MqttCommand::checkNode(rapidjson::Document &jmsg, QString field) {
if (!jmsg.HasMember(field.toStdString().c_str())) {
qCWarning(ozwmc) << "Node " << field <<" Is Missing from Message";
return false;
}
if (!jmsg[field.toStdString().c_str()].IsUint()) {
qCWarning(ozwmc) << "Node " << field << "is not a Uint32";
return false;
}
quint8 node = jmsg[field.toStdString().c_str()].GetInt();
if (node == 0 || node == 255) {
qCWarning(ozwmc) << "Invalid Node in field " << field << " for message " << jmsg.toJson();
qCWarning(ozwmc) << "Invalid Node in field " << field << " for message";
return false;
}
if (this->getMqttPublisher()->isValidNode(node)) {
return true;
}
qCWarning(ozwmc) << "Invalid Node in field " << field << " for message " << jmsg.toJson();
qCWarning(ozwmc) << "Invalid Node in field " << field << " for message ";
return false;
}
bool MqttCommand::checkValue(QJsonDocument jmsg, QString field) {
quint64 vidKey = jmsg[field].toInt();
bool MqttCommand::checkValue(rapidjson::Document &jmsg, QString field) {
if (!jmsg.HasMember(field.toStdString().c_str())) {
qCWarning(ozwmc) << "ValueIDKey " << field <<" Is Missing from Message";
return false;
}
if (!jmsg[field.toStdString().c_str()].IsUint64()) {
qCWarning(ozwmc) << "ValueIDKey " << field << " Is not a uint64";
return false;
}
quint64 vidKey = jmsg[field.toStdString().c_str()].GetUint64();
if (vidKey == 0) {
qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message " << jmsg.toJson();
qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message ";
return false;
}
if (this->getMqttPublisher()->isValidValueID(vidKey)) {
return true;
}
qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message " << jmsg.toJson();
qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message ";
return false;
}
@ -162,6 +183,19 @@ bool MqttCommand::setValue(quint64 vidKey, QVariant data) {
return this->getMqttPublisher()->setValue(vidKey, data);
}
bool MqttCommand::sendSimpleStatus(bool status, QString error) {
rapidjson::Document js;
if (status == true) {
QT2JS::SetString(js, "status", "ok");
} else {
QT2JS::SetString(js, "status", "failed");
if (!error.isEmpty())
QT2JS::SetString(js, "error", error);
}
emit sendCommandUpdate(GetCommand(), js);
return status;
}
MqttCommands::MqttCommands(QObject *parent) :
QObject(parent)

View file

@ -11,6 +11,7 @@
#include <qt-openzwave/qtozwmanager.h>
#include <qt-openzwave/qtozwvalueidmodel.h>
#include "mqttpublisher.h"
#include "qtrj.h"
@ -21,21 +22,22 @@ class MqttCommand : public QObject {
public:
void Setup(QMqttSubscription *);
void messageReceived(QMqttMessage msg);
virtual bool processMessage(QJsonDocument) = 0;
virtual bool processMessage(rapidjson::Document &) = 0;
virtual QString GetCommand() = 0;
signals:
void sendCommandUpdate(QString, QJsonObject);
void sendCommandUpdate(QString, rapidjson::Document &);
protected:
MqttCommand(QObject *parent = nullptr);
QTOZWManager *getOZWManager();
mqttpublisher *getMqttPublisher();
bool checkNode(QJsonDocument, QString);
bool checkValue(QJsonDocument, QString);
bool checkNode(rapidjson::Document &, QString);
bool checkValue(rapidjson::Document &, QString);
QVariant getValueData(quint64, QTOZW_ValueIds::ValueIdColumns);
bool setValue(quint64, QVariant);
QVector<QString> m_requiredStringFields;
QVector<QString> m_requiredIntFields;
QVector<QString> m_requiredBoolFields;
bool sendSimpleStatus(bool, QString error = QString());
private:
QMqttSubscription *m_subscription;

View file

@ -9,16 +9,7 @@ MqttCommand* MqttCommand_Open::Create(QObject *parent) {
return new MqttCommand_Open(parent);
}
bool MqttCommand_Open::processMessage(QJsonDocument msg) {
bool MqttCommand_Open::processMessage(rapidjson::Document &msg) {
QTOZWManager *mgr = getOZWManager();
if (mgr->open(msg["serialport"].toString())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->open(msg["serialport"].GetString()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "Open";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_Open(QObject *parent = nullptr);
};

View file

@ -9,9 +9,9 @@ MqttCommand* MqttCommand_Ping::Create(QObject *parent) {
return new MqttCommand_Ping(parent);
}
bool MqttCommand_Ping::processMessage(QJsonDocument msg) {
QJsonObject js;
js["pong"] = msg["ping"];
bool MqttCommand_Ping::processMessage(rapidjson::Document &msg) {
rapidjson::Document js;
QT2JS::SetString(js, "pong", msg["ping"].GetString());
emit sendCommandUpdate(MqttCommand_Ping::GetCommand(), js);
return true;
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "Ping";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_Ping(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RefreshNodeInfo::Create(QObject *parent) {
return new MqttCommand_RefreshNodeInfo(parent);
}
bool MqttCommand_RefreshNodeInfo::processMessage(QJsonDocument msg) {
bool MqttCommand_RefreshNodeInfo::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->refreshNodeInfo(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->refreshNodeInfo(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RefreshNodeInfo";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RefreshNodeInfo(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RemoveFailedNode::Create(QObject *parent) {
return new MqttCommand_RemoveFailedNode(parent);
}
bool MqttCommand_RemoveFailedNode::processMessage(QJsonDocument msg) {
bool MqttCommand_RemoveFailedNode::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->removeFailedNode(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->removeFailedNode(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RemoveFailedNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RemoveFailedNode(QObject *parent = nullptr);
};

View file

@ -8,12 +8,9 @@ MqttCommand* MqttCommand_RemoveNode::Create(QObject *parent) {
return new MqttCommand_RemoveNode(parent);
}
bool MqttCommand_RemoveNode::processMessage(QJsonDocument msg) {
bool MqttCommand_RemoveNode::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
mgr->removeNode();
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RemoveNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RemoveNode(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_ReplaceFailedNode::Create(QObject *parent) {
return new MqttCommand_ReplaceFailedNode(parent);
}
bool MqttCommand_ReplaceFailedNode::processMessage(QJsonDocument msg) {
bool MqttCommand_ReplaceFailedNode::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->replaceFailedNode(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->replaceFailedNode(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "ReplaceFailedNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_ReplaceFailedNode(QObject *parent = nullptr);
};

View file

@ -9,18 +9,11 @@ MqttCommand* MqttCommand_RequestAllConfigParam::Create(QObject *parent) {
return new MqttCommand_RequestAllConfigParam(parent);
}
bool MqttCommand_RequestAllConfigParam::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestAllConfigParam::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
mgr->requestAllConfigParam(msg["node"].toInt());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->requestAllConfigParam(msg["node"].GetUint());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestAllConfigParam";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestAllConfigParam(QObject *parent = nullptr);
};

View file

@ -9,18 +9,11 @@ MqttCommand* MqttCommand_RequestConfigParam::Create(QObject *parent) {
return new MqttCommand_RequestConfigParam(parent);
}
bool MqttCommand_RequestConfigParam::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestConfigParam::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
mgr->requestConfigParam(msg["node"].toInt(), msg["param"].toInt());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->requestConfigParam(msg["node"].GetUint(), msg["param"].GetUint());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestConfigParam";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestConfigParam(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNetworkUpdate::Create(QObject *parent) {
return new MqttCommand_RequestNetworkUpdate(parent);
}
bool MqttCommand_RequestNetworkUpdate::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestNetworkUpdate::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->requestNetworkUpdate(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->requestNetworkUpdate(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestNetworkUpdate";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestNetworkUpdate(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeDynamic::Create(QObject *parent) {
return new MqttCommand_RequestNodeDynamic(parent);
}
bool MqttCommand_RequestNodeDynamic::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestNodeDynamic::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->requestNodeDynamic(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->requestNodeDynamic(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestNodeDynamic";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestNodeDynamic(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeNeighborUpdate::Create(QObject *parent) {
return new MqttCommand_RequestNodeNeighborUpdate(parent);
}
bool MqttCommand_RequestNodeNeighborUpdate::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestNodeNeighborUpdate::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->requestNodeNeighborUpdate(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->requestNodeNeighborUpdate(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestNodeNeighborUpdate";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestNodeNeighborUpdate(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeState::Create(QObject *parent) {
return new MqttCommand_RequestNodeState(parent);
}
bool MqttCommand_RequestNodeState::processMessage(QJsonDocument msg) {
bool MqttCommand_RequestNodeState::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->requestNodeState(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->requestNodeState(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "RequestNodeState";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_RequestNodeState(QObject *parent = nullptr);
};

View file

@ -9,23 +9,10 @@ MqttCommand* MqttCommand_SendNodeInformation::Create(QObject *parent) {
return new MqttCommand_SendNodeInformation(parent);
}
bool MqttCommand_SendNodeInformation::processMessage(QJsonDocument msg) {
bool MqttCommand_SendNodeInformation::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
if (mgr->sendNodeInformation(msg["node"].toInt())) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(mgr->sendNodeInformation(msg["node"].GetUint()));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "SendNodeInformation";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_SendNodeInformation(QObject *parent = nullptr);
};

View file

@ -12,31 +12,19 @@ MqttCommand* MqttCommand_SetValue::Create(QObject *parent) {
return new MqttCommand_SetValue(parent);
}
bool MqttCommand_SetValue::processMessage(QJsonDocument msg) {
bool MqttCommand_SetValue::processMessage(rapidjson::Document &msg) {
if (!this->checkValue(msg, "ValueIDKey")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid ValueIDKey Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid ValueIDKey Number");
}
/* check that the Value Field exists */
if (msg["Value"].isUndefined()) {
QJsonObject js;
js["Error"] = QString("Missing Field Value");
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Missing Field for " << GetCommand() << ": Value: " << msg.toJson();
return false;
if (!msg.HasMember("Value")) {
return this->sendSimpleStatus(false, "Missing Field Value");
}
quint64 vidKey = msg["ValueIdKey"].toInt();
quint64 vidKey = msg["ValueIdKey"].GetUint();
QBitArray flags = this->getValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::ValueFlags).value<QBitArray>();
if (flags[QTOZW_ValueIds::ValueIDFlags::ReadOnly] == true) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "ValueID is Read Only";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "ValueID is Read Only");
}
QTOZW_ValueIds::ValueIdTypes types = this->getValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::Type).value<QTOZW_ValueIds::ValueIdTypes>();
QVariant data;
@ -47,72 +35,58 @@ bool MqttCommand_SetValue::processMessage(QJsonDocument msg) {
}
case QTOZW_ValueIds::ValueIdTypes::Bool: {
if (!msg["Value"].isBool()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsBool()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
data = msg["Value"].toBool();
data = msg["Value"].GetBool();
break;
}
case QTOZW_ValueIds::ValueIdTypes::Button: {
if (!msg["Value"].isBool()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsBool()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
data = msg["Value"].toBool();
data = msg["Value"].GetBool();
break;
}
case QTOZW_ValueIds::ValueIdTypes::Byte: {
if (!msg["Value"].isDouble()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Byte: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Byte) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsUint()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Byte: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Byte) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
if (msg["Value"].toInt() > UCHAR_MAX) {
QJsonObject js;
js["Error"] = QString("Value is Larger than Byte Field: ").append(msg["Value"].toInt());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Value is Larger than Byte Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson();
if (msg["Value"].GetUint() > UCHAR_MAX) {
this->sendSimpleStatus(false, QString("Value is Larger than Byte Field: ").append(msg["Value"].GetUint()));
qCWarning(ozwmcsv) << "Value is Larger than Byte Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint();
return false;
}
data = msg["Value"].toInt();
data = msg["Value"].GetUint();
break;
}
case QTOZW_ValueIds::ValueIdTypes::Decimal: {
if (!msg["Value"].isDouble()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Decimal: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Decimal) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsDouble()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Decimal: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Decimal) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
data = msg["Value"].toDouble();
data = msg["Value"].GetDouble();
break;
}
case QTOZW_ValueIds::ValueIdTypes::Int: {
if (!msg["Value"].isDouble()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Integer: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Integer) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsUint()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Integer: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Integer) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
if (static_cast<uint>(msg["Value"].toInt()) > UINT_MAX) {
QJsonObject js;
js["Error"] = QString("Value is Larger than Integer Field: ").append(msg["Value"].toInt());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Value is Larger than Integer Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson();
if (static_cast<uint>(msg["Value"].GetUint()) > UINT_MAX) {
this->sendSimpleStatus(false, QString("Value is Larger than Integer Field: ").append(msg["Value"].GetUint()));
qCWarning(ozwmcsv) << "Value is Larger than Integer Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint();
return false;
}
data = msg["Value"].toInt();
data = msg["Value"].GetUint();
break;
}
case QTOZW_ValueIds::ValueIdTypes::List:
@ -123,60 +97,37 @@ bool MqttCommand_SetValue::processMessage(QJsonDocument msg) {
}
case QTOZW_ValueIds::ValueIdTypes::Short: {
if (!msg["Value"].isDouble()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not Short: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (Short) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsUint()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Short: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (Short) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
if (msg["Value"].toInt() > USHRT_MAX) {
QJsonObject js;
js["Error"] = QString("Value is Larger than Short Field: ").append(msg["Value"].toInt());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Value is Larger than Short Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson();
if (msg["Value"].GetUint() > USHRT_MAX) {
this->sendSimpleStatus(false, QString("Value is Larger than Short Field: ").append(msg["Value"].GetUint()));
qCWarning(ozwmcsv) << "Value is Larger than Short Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint();
return false;
}
data = msg["Value"].toInt();
data = msg["Value"].GetUint();
break;
}
case QTOZW_ValueIds::ValueIdTypes::String: {
if (!msg["Value"].isString()) {
QJsonObject js;
js["Error"] = QString("Incorrect Field Type for Value: Not String: ").append(msg["Value"].type());
emit sendCommandUpdate(GetCommand(), js);
qCWarning(ozwmcsv) << "Incorrect Field Type (String) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson();
if (!msg["Value"].IsString()) {
this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not String: ").append(msg["Value"].GetType()));
qCWarning(ozwmcsv) << "Incorrect Field Type (String) for " << GetCommand() << ": Value: " << msg["Value"].GetType();
return false;
}
data = msg["Value"].toString();
data = msg["Value"].GetString();
break;
}
case QTOZW_ValueIds::ValueIdTypes::TypeCount: {
qCWarning(ozwmcsv) << "Invalid ValueID Type " << types << "for setValue" << msg.toJson();
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Unknown ValueID Type";
emit sendCommandUpdate(GetCommand(), js);
return false;
qCWarning(ozwmcsv) << "Invalid ValueID Type " << types << "for setValue";
return this->sendSimpleStatus(false, "Unknown ValueID Type");
break;
}
}
if (data.isNull()) {
qCWarning(ozwmcsv) << "Data is undefined for setValue... Json Conversion Failed?";
QJsonObject js;
js["status"] = "failed";
js["Error"] = "JSON Conversion Failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "JSON Conversion Failed");
}
if (this->setValue(vidKey, data)) {
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(this->setValue(vidKey, data));
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "SetValue";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_SetValue(QObject *parent = nullptr);
};

View file

@ -8,12 +8,9 @@ MqttCommand* MqttCommand_SoftResetController::Create(QObject *parent) {
return new MqttCommand_SoftResetController(parent);
}
bool MqttCommand_SoftResetController::processMessage(QJsonDocument msg) {
bool MqttCommand_SoftResetController::processMessage(rapidjson::Document &msg) {
Q_UNUSED(msg);
QTOZWManager *mgr = getOZWManager();
mgr->softResetController();
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "SoftResetController";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_SoftResetController(QObject *parent = nullptr);
};

View file

@ -9,11 +9,8 @@ MqttCommand* MqttCommand_TestNetwork::Create(QObject *parent) {
return new MqttCommand_TestNetwork(parent);
}
bool MqttCommand_TestNetwork::processMessage(QJsonDocument msg) {
bool MqttCommand_TestNetwork::processMessage(rapidjson::Document &msg) {
QTOZWManager *mgr = getOZWManager();
mgr->testNetwork(msg["count"].toInt());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->testNetwork(msg["count"].GetUint());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "TestNetwork";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_TestNetwork(QObject *parent = nullptr);
};

View file

@ -9,18 +9,11 @@ MqttCommand* MqttCommand_TestNetworkNode::Create(QObject *parent) {
return new MqttCommand_TestNetworkNode(parent);
}
bool MqttCommand_TestNetworkNode::processMessage(QJsonDocument msg) {
bool MqttCommand_TestNetworkNode::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) {
QJsonObject js;
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
return this->sendSimpleStatus(false, "Invalid Node Number");
}
QTOZWManager *mgr = getOZWManager();
mgr->testNetworkNode(msg["node"].toInt(), msg["count"].toInt());
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
mgr->testNetworkNode(msg["node"].GetUint64(), msg["count"].GetUint());
return this->sendSimpleStatus(true);
}

View file

@ -9,7 +9,7 @@ public:
static MqttCommand *Create(QObject *parent = nullptr);
static QString StaticGetCommand() { return "TestNetworkNode";};
QString GetCommand() override { return StaticGetCommand(); };
bool processMessage(QJsonDocument) override;
bool processMessage(rapidjson::Document &) override;
private:
MqttCommand_TestNetworkNode(QObject *parent = nullptr);
};

View file

@ -1,8 +1,12 @@
#include <QDateTime>
#include "mqttpublisher.h"
#include "qtrj.h"
#include "mqttcommands/mqttcommands.h"
#include <rapidjson/prettywriter.h> // for stringify JSON
Q_LOGGING_CATEGORY(ozwmp, "ozw.mqtt.publisher");
Q_LOGGING_CATEGORY(ozwmpnode, "ozw.mqtt.publisher.node");
Q_LOGGING_CATEGORY(ozwmpvalue, "ozw.mqtt.publisher.value");
@ -26,7 +30,10 @@ bool mqttNodeModel::isValidNode(quint8 node) {
}
bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTOZWManager *mgr) {
bool mqttNodeModel::populateJsonObject(rapidjson::Document &jsonobject, quint8 node, QTOZWManager *mgr) {
if (jsonobject.IsNull())
jsonobject.SetObject();
for (int i = 0; i < this->columnCount(QModelIndex()); i++) {
QVariant data = this->getNodeData(node, static_cast<NodeColumns>(i));
if (data.type() == QVariant::Invalid) {
@ -37,20 +44,20 @@ bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTO
QBitArray flag = data.toBitArray();
QMetaEnum metaEnum = QMetaEnum::fromType<nodeFlags>();
for (int j = 0; j < nodeFlags::flagCount; j++) {
jsonobject->insert(metaEnum.valueToKey(j), flag.at(j));
QT2JS::SetBool(jsonobject, metaEnum.valueToKey(j), flag.at(j));
}
break;
}
default: {
QMetaEnum metaEnum = QMetaEnum::fromType<NodeColumns>();
if (static_cast<QMetaType::Type>(data.type()) == QMetaType::QString) {
jsonobject->insert(metaEnum.valueToKey(i), data.toString());
QT2JS::SetString(jsonobject, metaEnum.valueToKey(i), data.toString());
} else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Bool) {
jsonobject->insert(metaEnum.valueToKey(i), data.toBool());
QT2JS::SetBool(jsonobject, metaEnum.valueToKey(i), data.toBool());
} else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Int) {
jsonobject->insert(metaEnum.valueToKey(i), data.toInt());
QT2JS::SetInt(jsonobject, metaEnum.valueToKey(i), data.toInt());
} else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::UInt) {
jsonobject->insert(metaEnum.valueToKey(i), data.toInt());
QT2JS::SetUint(jsonobject, metaEnum.valueToKey(i), data.toUInt());
} else {
qCWarning(ozwmpvalue) << "Can't Convert " << data.type() << "(" << metaEnum.valueToKey(i) << ") to store in JsonObject: " << node;
}
@ -60,23 +67,33 @@ bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTO
}
/* MetaData */
QJsonObject metadata = jsonobject->value("MetaData").toObject();
rapidjson::Value metadata;
if (!jsonobject.HasMember("MetaData")) {
rapidjson::Value metadata;
metadata.SetObject();
QMetaEnum metaEnum = QMetaEnum::fromType<QTOZWManagerSource::QTOZWMetaDataField>();
if (metadata.empty()) {
for (int i = 0; i < QTOZWManagerSource::Identifier; i++) {
metadata.insert(metaEnum.valueToKey(i), mgr->GetMetaData(node, static_cast<QTOZWManagerSource::QTOZWMetaDataField>(i)));
if (metadata.IsNull()) {
metadata.SetObject();
}
metadata.insert("ProductPicBase64", QString(mgr->GetMetaDataProductPic(node).toBase64()));
jsonobject->insert("MetaData", metadata);
for (int i = 0; i < QTOZWManagerSource::Identifier; i++) {
metadata.AddMember(rapidjson::Value(metaEnum.valueToKey(i), jsonobject.GetAllocator()).Move(),
rapidjson::Value(mgr->GetMetaData(node, static_cast<QTOZWManagerSource::QTOZWMetaDataField>(i)).toStdString().c_str(), jsonobject.GetAllocator()).Move(),
jsonobject.GetAllocator());
}
metadata.AddMember(rapidjson::Value("ProductPicBase64").Move(),
rapidjson::Value(QString(mgr->GetMetaDataProductPic(node).toBase64()).toStdString().c_str(), jsonobject.GetAllocator()).Move(),
jsonobject.GetAllocator());
jsonobject.AddMember(rapidjson::Value("MetaData"), metadata, jsonobject.GetAllocator());
}
/* Neighbors */
QVector<quint8> neighbors = mgr->GetNodeNeighbors(node);
if (neighbors.size() > 0) {
QJsonArray N;
rapidjson::Value N(rapidjson::kArrayType);
for (int i = 0; i < neighbors.count(); i++) {
N.append(neighbors[i]);
N.PushBack(neighbors[i], jsonobject.GetAllocator());
}
jsonobject->insert("Neighbors", N);
jsonobject.AddMember(rapidjson::Value("Neighbors").Move(), N, jsonobject.GetAllocator());
}
return true;
}
@ -247,6 +264,7 @@ mqttpublisher::mqttpublisher(QSettings *settings, QObject *parent) : QObject(par
this->m_client->setHostname(settings->value("MQTTServer", "127.0.0.1").toString());
this->m_client->setPort(static_cast<quint16>(settings->value("MQTTPort", 1883).toInt()));
this->m_ozwstatus.SetObject();
/* setup the Commands */
this->m_commands = new MqttCommands(this);
@ -501,16 +519,16 @@ void mqttpublisher::handleMessage(const QByteArray &message, const QMqttTopicNam
// qCDebug(ozwmp) << "Received: " << topic.name() << ":" << message;
}
bool mqttpublisher::sendStatusUpdate() {
this->m_ozwstatus["TimeStamp"] = QDateTime::currentSecsSinceEpoch();
this->m_client->publish(QMqttTopicName(getTopic(MQTT_OZW_STATUS_TOPIC)), QJsonDocument(this->m_ozwstatus).toJson(), 0, true);
QT2JS::SetUInt64(this->m_ozwstatus, "TimeStamp", QDateTime::currentSecsSinceEpoch());
this->m_client->publish(QMqttTopicName(getTopic(MQTT_OZW_STATUS_TOPIC)), QT2JS::getJSON(this->m_ozwstatus), 0, true);
return true;
}
bool mqttpublisher::sendNodeUpdate(quint8 node) {
this->m_nodes[node]["TimeStamp"] = QDateTime::currentSecsSinceEpoch();
this->m_client->publish(QMqttTopicName(getNodeTopic(MQTT_OZW_NODE_TOPIC, node)), QJsonDocument(this->m_nodes[node]).toJson(), 0, true);
QT2JS::SetUInt64(*this->m_nodes[node], "TimeStamp", QDateTime::currentSecsSinceEpoch());
this->m_client->publish(QMqttTopicName(getNodeTopic(MQTT_OZW_NODE_TOPIC, node)), QT2JS::getJSON(*this->m_nodes[node]), 0, true);
qDebug() << QT2JS::getJSON(*this->m_nodes[node]);
return true;
}
@ -524,9 +542,9 @@ 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) {
js["TimeStamp"] = QDateTime::currentSecsSinceEpoch();
this->m_client->publish(QMqttTopicName(getCommandResponseTopic(command.toLower())), QJsonDocument(js).toJson(), 0, false);
void mqttpublisher::sendCommandUpdate(QString command, rapidjson::Document &js) {
QT2JS::SetUInt64(js, "TimeStamp", QDateTime::currentSecsSinceEpoch());
this->m_client->publish(QMqttTopicName(getCommandResponseTopic(command.toLower())), QT2JS::getJSON(js), 0, false);
return;
}
@ -548,7 +566,7 @@ bool mqttpublisher::delValueTopic(quint64 vidKey) {
void mqttpublisher::ready() {
qCDebug(ozwmp) << "Publishing Event ready:";
this->m_ozwstatus["Status"] = "Ready";
QT2JS::SetString(this->m_ozwstatus, "Status", "Ready");
this->sendStatusUpdate();
}
void mqttpublisher::valueAdded(quint64 vidKey) {
@ -576,27 +594,36 @@ void mqttpublisher::valueRefreshed(quint64 vidKey) {
}
void mqttpublisher::nodeNew(quint8 node) {
qCDebug(ozwmp) << "Publishing Event NodeNew:" << node;
this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
this->m_nodes[node]["Event"] = "nodeNew";
this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeNew");
this->sendNodeUpdate(node);
}
void mqttpublisher::nodeAdded(quint8 node) {
qCDebug(ozwmp) << "Publishing Event NodeAdded:" << node;
this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
this->m_nodes[node]["Event"] = "nodeAdded";
if (this->m_nodes.find(node) == this->m_nodes.end()) {
this->m_nodes.insert(node, new rapidjson::Document());
}
this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeAdded");
this->sendNodeUpdate(node);
}
void mqttpublisher::nodeRemoved(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeRemoved:" << node;
this->delNodeTopic(node);
if (this->m_nodes.find(node) == this->m_nodes.end()) {
this->m_nodes.remove(node);
}
}
void mqttpublisher::nodeReset(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeReset:" << node;
this->delNodeTopic(node);
if (this->m_nodes.find(node) == this->m_nodes.end()) {
this->m_nodes.remove(node);
}
}
void mqttpublisher::nodeNaming(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeNaming:" << node;
this->m_nodes[node]["Event"] = "nodeNaming";
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeNaming");
this->sendNodeUpdate(node);
}
void mqttpublisher::nodeEvent(quint8 node, quint8 event) {
@ -608,71 +635,71 @@ void mqttpublisher::nodeEvent(quint8 node, quint8 event) {
}
void mqttpublisher::nodeProtocolInfo(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeProtocolInfo:" << node;
this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
this->m_nodes[node]["Event"] = "nodeProtocolInfo";
this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeProtocolInfo");
this->sendNodeUpdate(node);
}
void mqttpublisher::nodeEssentialNodeQueriesComplete(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeEssentialNodeQueriesComplete:" << node;
this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
this->m_nodes[node]["Event"] = "nodeEssentialNodeQueriesComplete";
this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeEssentialNodeQueriesComplete");
this->sendNodeUpdate(node);
}
void mqttpublisher::nodeQueriesComplete(quint8 node) {
qCDebug(ozwmp) << "Publishing Event nodeQueriesComplete:" << node;
this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
this->m_nodes[node]["Event"] = "nodeQueriesComplete";
this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager());
QT2JS::SetString(*this->m_nodes[node], "Event", "nodeQueriesComplete");
this->sendNodeUpdate(node);
}
void mqttpublisher::driverReady(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event driverReady:" << homeID;
this->m_ozwstatus["Status"] = "driverReady";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "driverReady");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
}
void mqttpublisher::driverFailed(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event driverFailed:" << homeID;
this->m_ozwstatus["Status"] = "driverFailed";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "driverFailed");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
}
void mqttpublisher::driverReset(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event driverReset:" << homeID;
this->m_ozwstatus["Status"] = "driverReset";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "driverReset");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
}
void mqttpublisher::driverRemoved(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event driverRemoved:" << homeID;
this->m_ozwstatus["Status"] = "driverRemoved";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "driverRemoved");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
}
void mqttpublisher::driverAllNodesQueriedSomeDead() {
qCDebug(ozwmp) << "Publishing Event driverAllNodesQueriedSomeDead:" ;
this->m_ozwstatus["Status"] = "driverAllNodesQueriedSomeDead";
QT2JS::SetString(this->m_ozwstatus, "Status", "driverAllNodesQueriedSomeDead");
this->sendStatusUpdate();
}
void mqttpublisher::driverAllNodesQueried() {
qCDebug(ozwmp) << "Publishing Event driverAllNodesQueried:" ;
this->m_ozwstatus["Status"] = "driverAllNodesQueried";
QT2JS::SetString(this->m_ozwstatus, "Status", "driverAllNodesQueried");
this->sendStatusUpdate();
}
void mqttpublisher::driverAwakeNodesQueried() {
qCDebug(ozwmp) << "Publishing Event driverAwakeNodesQueried:" ;
this->m_ozwstatus["Status"] = "driverAwakeNodesQueried";
QT2JS::SetString(this->m_ozwstatus, "Status", "driverAwakeNodesQueried");
this->sendStatusUpdate();
}
void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Notification_Controller_Cmd command, NotificationTypes::QTOZW_Notification_Controller_State state, NotificationTypes::QTOZW_Notification_Controller_Error error) {
qCDebug(ozwmp) << "Publishing Event controllerCommand" << node << command << state << error;
QJsonObject js;
rapidjson::Document js;
if (node > 0)
js["Node"] = node;
QT2JS::SetUint(js, "Node", node);
QMetaEnum metaEnum = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_State>();
js["State"] = metaEnum.valueToKey(state);
QT2JS::SetString(js, "State", metaEnum.valueToKey(state));
if (error != NotificationTypes::QTOZW_Notification_Controller_Error::Ctrl_Error_None) {
metaEnum = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Error>();
js["Error"] = metaEnum.valueToKey(error);
QT2JS::SetString(js, "Error", metaEnum.valueToKey(error));
}
switch(command) {
@ -689,12 +716,12 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
break;
}
case NotificationTypes::Ctrl_Cmd_CreateButton: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
case NotificationTypes::Ctrl_Cmd_CreateNewPrimary: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
@ -703,7 +730,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
break;
}
case NotificationTypes::Ctrl_Cmd_DeleteButton: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
@ -712,7 +739,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
break;
}
case NotificationTypes::Ctrl_Cmd_ReceiveConfiguration: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
@ -729,7 +756,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
break;
}
case NotificationTypes::Ctrl_Cmd_ReplicationSend: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
@ -746,7 +773,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
break;
}
case NotificationTypes::Ctrl_Cmd_TransferPrimaryRole: {
js["Command"] = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command);
QT2JS::SetString(js, "Command", QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Controller_Cmd>().valueToKey(command));
this->sendCommandUpdate("ControllerCommand", js);
break;
}
@ -758,45 +785,45 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti
}
void mqttpublisher::ozwNotification(quint8 node, NotificationTypes::QTOZW_Notification_Code event) {
qCDebug(ozwmp) << "Publishing Event ozwNotification";
QJsonObject js;
rapidjson::Document js;
QMetaEnum metaEnum = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_Code>();
js["Node"] = node;
js["Event"] = metaEnum.valueToKey(event);
QT2JS::SetUint(js, "Node", node);
QT2JS::SetString(js, "Event", metaEnum.valueToKey(event));
this->sendCommandUpdate("Notification", js);
}
void mqttpublisher::ozwUserAlert(quint8 node, NotificationTypes::QTOZW_Notification_User event, quint8 retry) {
qCDebug(ozwmp) << "Publishing Event ozwNotification";
QJsonObject js;
rapidjson::Document js;
QMetaEnum metaEnum = QMetaEnum::fromType<NotificationTypes::QTOZW_Notification_User>();
js["Node"] = node;
js["Event"] = metaEnum.valueToKey(event);
QT2JS::SetUint(js, "Node", node);
QT2JS::SetString(js, "Event", metaEnum.valueToKey(event));
if (event == NotificationTypes::QTOZW_Notification_User::Notification_User_ApplicationStatus_Retry) {
js["Retry"] = static_cast<int>(retry);
QT2JS::SetUint(js, "Retry", retry);
}
this->sendCommandUpdate("UserAlert", js);
}
void mqttpublisher::manufacturerSpecificDBReady() {
qCDebug(ozwmp) << "Publishing Event manufacturerSpecificDBReady";
this->m_ozwstatus["ManufacturerSpecificDBReady"] = true;
QT2JS::SetBool(this->m_ozwstatus, "ManufacturerSpecificDBReady", true);
this->sendStatusUpdate();
}
void mqttpublisher::starting() {
qCDebug(ozwmp) << "Publishing Event starting";
this->m_ozwstatus["Status"] = "starting";
QT2JS::SetString(this->m_ozwstatus, "Status", "starting");
this->sendStatusUpdate();
}
void mqttpublisher::started(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event started";
this->m_ozwstatus["Status"] = "started";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "started");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
this->m_statsTimer.start(10000);
}
void mqttpublisher::stopped(quint32 homeID) {
qCDebug(ozwmp) << "Publishing Event stopped";
this->m_ozwstatus["Status"] = "stopped";
this->m_ozwstatus["homeID"] = QJsonValue(static_cast<int>(homeID));
QT2JS::SetString(this->m_ozwstatus, "Status", "stopped");
QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID);
this->sendStatusUpdate();
this->m_statsTimer.stop();
}

View file

@ -7,6 +7,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QTimer>
#include <rapidjson/document.h>
#include "qtozwdaemon.h"
#include "mqttcommands/mqttcommands.h"
@ -28,7 +29,7 @@ class mqttNodeModel : public QTOZW_Nodes {
public:
explicit mqttNodeModel(QObject *parent = nullptr);
QVariant getNodeData(quint8, NodeColumns);
bool populateJsonObject(QJsonObject *, quint8, QTOZWManager *);
bool populateJsonObject(rapidjson::Document &, quint8, QTOZWManager *);
bool isValidNode(quint8);
};
@ -50,7 +51,7 @@ public:
explicit mqttpublisher(QSettings *setting, QObject *parent = nullptr);
void setOZWDaemon(qtozwdaemon *ozwdaemon);
QTOZWManager *getQTOZWManager();
void sendCommandUpdate(QString, QJsonObject);
void sendCommandUpdate(QString, rapidjson::Document &);
bool isValidNode(quint8 node);
bool isValidValueID(quint64 vidKey);
QVariant getValueData(quint64, mqttValueIDModel::ValueIdColumns);
@ -110,8 +111,8 @@ private:
bool delNodeTopic(quint8);
bool delValueTopic(quint64);
QJsonObject m_ozwstatus;
QMap<quint8, QJsonObject> m_nodes;
rapidjson::Document m_ozwstatus;
QMap<quint8, rapidjson::Document *> m_nodes;
mqttNodeModel *m_nodeModel;
QMap<quint64, QJsonObject> m_values;
mqttValueIDModel *m_valueModel;

View file

@ -4,7 +4,7 @@ QT += remoteobjects
TARGET = ../ozwdaemon
CONFIG += c++11 console silent
CONFIG += c++11 console link_pkgconfig silent
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
@ -19,9 +19,11 @@ DEFINES += QT_DEPRECATED_WARNINGS QT_MESSAGELOGCONTEXT
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
qtHaveModule(mqtt) {
PKGCONFIG += RapidJSON
QT += mqtt
DEFINES += HAVE_MQTT
SOURCES += mqttpublisher.cpp \
qtrj.cpp \
mqttcommands/mqttcommands.cpp \
mqttcommands/ping.cpp \
mqttcommands/open.cpp \
@ -55,6 +57,7 @@ qtHaveModule(mqtt) {
mqttcommands/setValue.cpp
HEADERS += mqttpublisher.h \
qtrj.h \
mqttcommands/mqttcommands.h \
mqttcommands/ping.h \
mqttcommands/open.h \

132
qt-ozwdaemon/qtrj.cpp Normal file
View file

@ -0,0 +1,132 @@
#include <QVariant>
#include <QDebug>
#include <QLoggingCategory>
#include <rapidjson/error/en.h>
#include <rapidjson/prettywriter.h> // for stringify JSON
#include "qtrj.h"
Q_LOGGING_CATEGORY(ozwqt2js, "ozw.mqtt.qt2js");
bool QT2JS::SetString(rapidjson::Document &doc, QString field, QString value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kStringType);
v.SetString(value.toStdString().c_str(), doc.GetAllocator());
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsString()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type String: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetString(value.toStdString().c_str(), doc.GetAllocator());
}
return true;
}
bool QT2JS::SetInt(rapidjson::Document &doc, QString field, qint32 value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kNumberType);
v.SetInt(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsInt()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type INT: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetInt(value);
}
return true;
}
bool QT2JS::SetUint(rapidjson::Document &doc, QString field, quint32 value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kNumberType);
v.SetUint(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsUint()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type UINT: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetUint(value);
}
return true;
}
bool QT2JS::SetInt64(rapidjson::Document &doc, QString field, qint64 value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kNumberType);
v.SetInt64(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsInt64()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type INT64: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetInt64(value);
}
return true;
}
bool QT2JS::SetUInt64(rapidjson::Document &doc, QString field, quint64 value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kNumberType);
v.SetUint64(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsUint64()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type UINT64: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetUint64(value);
}
return true;
}
bool QT2JS::SetBool(rapidjson::Document &doc, QString field, bool value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kFalseType);
v.SetBool(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsBool()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type Bool: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetBool(value);
}
return true;
}
bool QT2JS::SetDouble(rapidjson::Document &doc, QString field, double value) {
if (!doc.IsObject())
doc.SetObject();
if (!doc.HasMember(field.toStdString().c_str())) {
rapidjson::Value v(rapidjson::kNumberType);
v.SetDouble(value);
doc.AddMember(rapidjson::Value(field.toStdString().c_str(), doc.GetAllocator()).Move(), v.Move(), doc.GetAllocator());
} else {
if (!doc[field.toStdString().c_str()].IsDouble()) {
qCWarning(ozwqt2js) << "Field " << field << " Is Not of Type Double: " << doc[field.toStdString().c_str()].GetType();
return false;
}
doc[field.toStdString().c_str()].SetDouble(value);
}
return true;
}
QByteArray QT2JS::getJSON(rapidjson::Document &doc) {
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
doc.Accept(writer); // Accept() traverses the DOM and generates Handler events.
return sb.GetString();
}

21
qt-ozwdaemon/qtrj.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef QTRJ_H
#define QTRJ_H
#include <QObject>
#include <QByteArray>
#include <rapidjson/document.h>
class QT2JS {
public:
static bool SetString(rapidjson::Document &, QString, QString);
static bool SetInt(rapidjson::Document &, QString, qint32);
static bool SetUint(rapidjson::Document &, QString, quint32);
static bool SetInt64(rapidjson::Document &, QString, qint64);
static bool SetUInt64(rapidjson::Document &, QString, quint64);
static bool SetBool(rapidjson::Document &, QString, bool);
static bool SetDouble(rapidjson::Document &, QString, double);
static QByteArray getJSON(rapidjson::Document &);
};
#endif // QTRJ_H