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); return new MqttCommand_IsNodeFailed(parent);
} }
bool MqttCommand_IsNodeFailed::processMessage(QJsonDocument msg) { bool MqttCommand_IsNodeFailed::processMessage(rapidjson::Document &msg) {
if (!this->checkNode(msg, "node")) { if (!this->checkNode(msg, "node")) {
QJsonObject js; return this->sendSimpleStatus(false, "Invalid Node Number");
js["status"] = "failed";
js["Error"] = "Invalid Node Number";
emit sendCommandUpdate(GetCommand(), js);
return false;
} }
QTOZWManager *mgr = getOZWManager(); QTOZWManager *mgr = getOZWManager();
if (mgr->IsNodeFailed(msg["node"].toInt())) { return this->sendSimpleStatus(mgr->IsNodeFailed(msg["node"].GetUint()));
QJsonObject js;
js["status"] = "ok";
emit sendCommandUpdate(GetCommand(), js);
return true;
}
QJsonObject js;
js["status"] = "failed";
emit sendCommandUpdate(GetCommand(), js);
return false;
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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