From d634d95c45f5db43b8ffdbb3c6d3c1c2d73dfef1 Mon Sep 17 00:00:00 2001 From: Justin Hammond Date: Sun, 10 Nov 2019 01:32:10 +0800 Subject: [PATCH] Porting to RapidJson --- qt-ozwdaemon/mqttcommands/IsNodeFailed.cpp | 19 +- qt-ozwdaemon/mqttcommands/IsNodeFailed.h | 2 +- qt-ozwdaemon/mqttcommands/addNode.cpp | 14 +- qt-ozwdaemon/mqttcommands/addNode.h | 2 +- .../mqttcommands/assignReturnRoute.cpp | 19 +- qt-ozwdaemon/mqttcommands/assignReturnRoute.h | 2 +- .../mqttcommands/cancelControllerCommand.cpp | 13 +- .../mqttcommands/cancelControllerCommand.h | 2 +- .../checkLatestConfigFileRevision.cpp | 19 +- .../checkLatestConfigFileRevision.h | 2 +- .../mqttcommands/checkLatestMFSRevision.cpp | 7 +- .../mqttcommands/checkLatestMFSRevision.h | 2 +- .../mqttcommands/deleteAllReturnRoute.cpp | 19 +- .../mqttcommands/deleteAllReturnRoute.h | 2 +- .../downloadLatestConfigFileRevision.cpp | 19 +- .../downloadLatestConfigFileRevision.h | 2 +- .../downloadLatestMFSRevision.cpp | 13 +- .../mqttcommands/downloadLatestMFSRevision.h | 2 +- .../mqttcommands/hardResetController.cpp | 7 +- .../mqttcommands/hardResetController.h | 2 +- qt-ozwdaemon/mqttcommands/hasNodeFailed.cpp | 19 +- qt-ozwdaemon/mqttcommands/hasNodeFailed.h | 2 +- qt-ozwdaemon/mqttcommands/healNetwork.cpp | 9 +- qt-ozwdaemon/mqttcommands/healNetwork.h | 2 +- qt-ozwdaemon/mqttcommands/healNetworkNode.cpp | 15 +- qt-ozwdaemon/mqttcommands/healNetworkNode.h | 2 +- qt-ozwdaemon/mqttcommands/mqttcommands.cpp | 104 +++++++---- qt-ozwdaemon/mqttcommands/mqttcommands.h | 10 +- qt-ozwdaemon/mqttcommands/open.cpp | 13 +- qt-ozwdaemon/mqttcommands/open.h | 2 +- qt-ozwdaemon/mqttcommands/ping.cpp | 6 +- qt-ozwdaemon/mqttcommands/ping.h | 2 +- qt-ozwdaemon/mqttcommands/refreshnodeinfo.cpp | 19 +- qt-ozwdaemon/mqttcommands/refreshnodeinfo.h | 2 +- .../mqttcommands/removeFailedNode.cpp | 19 +- qt-ozwdaemon/mqttcommands/removeFailedNode.h | 2 +- qt-ozwdaemon/mqttcommands/removeNode.cpp | 7 +- qt-ozwdaemon/mqttcommands/removeNode.h | 2 +- .../mqttcommands/replaceFailedNode.cpp | 19 +- qt-ozwdaemon/mqttcommands/replaceFailedNode.h | 2 +- .../mqttcommands/requestAllConfigParam.cpp | 15 +- .../mqttcommands/requestAllConfigParam.h | 2 +- .../mqttcommands/requestConfigParam.cpp | 15 +- .../mqttcommands/requestConfigParam.h | 2 +- .../mqttcommands/requestNetworkUpdate.cpp | 19 +- .../mqttcommands/requestNetworkUpdate.h | 2 +- .../mqttcommands/requestNodeDynamic.cpp | 19 +- .../mqttcommands/requestNodeDynamic.h | 2 +- .../requestNodeNeighborUpdate.cpp | 19 +- .../mqttcommands/requestNodeNeighborUpdate.h | 2 +- .../mqttcommands/requestNodeState.cpp | 19 +- qt-ozwdaemon/mqttcommands/requestNodeState.h | 2 +- .../mqttcommands/sendNodeInformation.cpp | 19 +- .../mqttcommands/sendNodeInformation.h | 2 +- qt-ozwdaemon/mqttcommands/setValue.cpp | 143 +++++---------- qt-ozwdaemon/mqttcommands/setValue.h | 2 +- .../mqttcommands/softResetController.cpp | 7 +- .../mqttcommands/softResetController.h | 2 +- qt-ozwdaemon/mqttcommands/testNetwork.cpp | 9 +- qt-ozwdaemon/mqttcommands/testNetwork.h | 2 +- qt-ozwdaemon/mqttcommands/testNetworkNode.cpp | 15 +- qt-ozwdaemon/mqttcommands/testNetworkNode.h | 2 +- qt-ozwdaemon/mqttpublisher.cpp | 167 ++++++++++-------- qt-ozwdaemon/mqttpublisher.h | 9 +- qt-ozwdaemon/qt-ozwdaemon.pro | 5 +- qt-ozwdaemon/qtrj.cpp | 132 ++++++++++++++ qt-ozwdaemon/qtrj.h | 21 +++ 67 files changed, 494 insertions(+), 588 deletions(-) create mode 100644 qt-ozwdaemon/qtrj.cpp create mode 100644 qt-ozwdaemon/qtrj.h diff --git a/qt-ozwdaemon/mqttcommands/IsNodeFailed.cpp b/qt-ozwdaemon/mqttcommands/IsNodeFailed.cpp index f640d95..c845169 100644 --- a/qt-ozwdaemon/mqttcommands/IsNodeFailed.cpp +++ b/qt-ozwdaemon/mqttcommands/IsNodeFailed.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_IsNodeFailed::Create(QObject *parent) { return new MqttCommand_IsNodeFailed(parent); } -bool MqttCommand_IsNodeFailed::processMessage(QJsonDocument msg) { +bool MqttCommand_IsNodeFailed::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->IsNodeFailed(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->IsNodeFailed(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/IsNodeFailed.h b/qt-ozwdaemon/mqttcommands/IsNodeFailed.h index 6f5dd6b..7edefbf 100644 --- a/qt-ozwdaemon/mqttcommands/IsNodeFailed.h +++ b/qt-ozwdaemon/mqttcommands/IsNodeFailed.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "IsNodeFailed";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_IsNodeFailed(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/addNode.cpp b/qt-ozwdaemon/mqttcommands/addNode.cpp index 04c519a..795a760 100644 --- a/qt-ozwdaemon/mqttcommands/addNode.cpp +++ b/qt-ozwdaemon/mqttcommands/addNode.cpp @@ -9,17 +9,7 @@ MqttCommand* MqttCommand_AddNode::Create(QObject *parent) { return new MqttCommand_AddNode(parent); } -bool MqttCommand_AddNode::processMessage(QJsonDocument msg) { - Q_UNUSED(msg); +bool MqttCommand_AddNode::processMessage(rapidjson::Document &msg) { QTOZWManager *mgr = getOZWManager(); - if (mgr->addNode(msg["secure"].toBool())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->addNode(msg["secure"].GetBool())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/addNode.h b/qt-ozwdaemon/mqttcommands/addNode.h index d1b551f..bfaf41d 100644 --- a/qt-ozwdaemon/mqttcommands/addNode.h +++ b/qt-ozwdaemon/mqttcommands/addNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "AddNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_AddNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/assignReturnRoute.cpp b/qt-ozwdaemon/mqttcommands/assignReturnRoute.cpp index 4ddaf6e..9f20103 100644 --- a/qt-ozwdaemon/mqttcommands/assignReturnRoute.cpp +++ b/qt-ozwdaemon/mqttcommands/assignReturnRoute.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_AssignReturnRoute::Create(QObject *parent) { return new MqttCommand_AssignReturnRoute(parent); } -bool MqttCommand_AssignReturnRoute::processMessage(QJsonDocument msg) { +bool MqttCommand_AssignReturnRoute::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->assignReturnRoute(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->assignReturnRoute(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/assignReturnRoute.h b/qt-ozwdaemon/mqttcommands/assignReturnRoute.h index 2cd8780..0ea9d64 100644 --- a/qt-ozwdaemon/mqttcommands/assignReturnRoute.h +++ b/qt-ozwdaemon/mqttcommands/assignReturnRoute.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "AssignReturnRoute";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_AssignReturnRoute(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/cancelControllerCommand.cpp b/qt-ozwdaemon/mqttcommands/cancelControllerCommand.cpp index 89be703..aa6c08d 100644 --- a/qt-ozwdaemon/mqttcommands/cancelControllerCommand.cpp +++ b/qt-ozwdaemon/mqttcommands/cancelControllerCommand.cpp @@ -8,17 +8,8 @@ MqttCommand* MqttCommand_CancelControllerCommand::Create(QObject *parent) { return new MqttCommand_CancelControllerCommand(parent); } -bool MqttCommand_CancelControllerCommand::processMessage(QJsonDocument msg) { +bool MqttCommand_CancelControllerCommand::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); - if (mgr->cancelControllerCommand()) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->cancelControllerCommand()); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/cancelControllerCommand.h b/qt-ozwdaemon/mqttcommands/cancelControllerCommand.h index 1ff25e3..a2ccded 100644 --- a/qt-ozwdaemon/mqttcommands/cancelControllerCommand.h +++ b/qt-ozwdaemon/mqttcommands/cancelControllerCommand.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "CancelControllerCommand";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_CancelControllerCommand(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.cpp b/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.cpp index 9740886..c98f41a 100644 --- a/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.cpp +++ b/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.cpp @@ -9,24 +9,11 @@ MqttCommand* MqttCommand_CheckLatestConfigFileRevision::Create(QObject *parent) return new MqttCommand_CheckLatestConfigFileRevision(parent); } -bool MqttCommand_CheckLatestConfigFileRevision::processMessage(QJsonDocument msg) { +bool MqttCommand_CheckLatestConfigFileRevision::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->checkLatestConfigFileRevision(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->checkLatestConfigFileRevision(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.h b/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.h index ecee18d..eb59188 100644 --- a/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.h +++ b/qt-ozwdaemon/mqttcommands/checkLatestConfigFileRevision.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "CheckLatestConfigFileRevision";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_CheckLatestConfigFileRevision(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.cpp b/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.cpp index bc6555a..2fd7d1e 100644 --- a/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.cpp +++ b/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.cpp @@ -8,12 +8,9 @@ MqttCommand* MqttCommand_CheckLatestMFSRevision::Create(QObject *parent) { return new MqttCommand_CheckLatestMFSRevision(parent); } -bool MqttCommand_CheckLatestMFSRevision::processMessage(QJsonDocument msg) { +bool MqttCommand_CheckLatestMFSRevision::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); mgr->checkLatestMFSRevision(); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.h b/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.h index 09b9595..a42f3d5 100644 --- a/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.h +++ b/qt-ozwdaemon/mqttcommands/checkLatestMFSRevision.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "CheckLatestMFSRevision";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_CheckLatestMFSRevision(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.cpp b/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.cpp index 5ae4334..6c6f09d 100644 --- a/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.cpp +++ b/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.cpp @@ -9,24 +9,11 @@ MqttCommand* MqttCommand_DeleteAllReturnRoute::Create(QObject *parent) { return new MqttCommand_DeleteAllReturnRoute(parent); } -bool MqttCommand_DeleteAllReturnRoute::processMessage(QJsonDocument msg) { +bool MqttCommand_DeleteAllReturnRoute::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->deleteAllReturnRoute(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->deleteAllReturnRoute(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.h b/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.h index e7ac75a..ae00a4a 100644 --- a/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.h +++ b/qt-ozwdaemon/mqttcommands/deleteAllReturnRoute.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "DeleteAllReturnRoute";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_DeleteAllReturnRoute(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.cpp b/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.cpp index c52b480..14978be 100644 --- a/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.cpp +++ b/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_DownloadLatestConfigFileRevision::Create(QObject *paren return new MqttCommand_DownloadLatestConfigFileRevision(parent); } -bool MqttCommand_DownloadLatestConfigFileRevision::processMessage(QJsonDocument msg) { +bool MqttCommand_DownloadLatestConfigFileRevision::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->downloadLatestConfigFileRevision(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->downloadLatestConfigFileRevision(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.h b/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.h index bbdfda0..1d461b7 100644 --- a/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.h +++ b/qt-ozwdaemon/mqttcommands/downloadLatestConfigFileRevision.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "DownloadLatestConfigFileRevision";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_DownloadLatestConfigFileRevision(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.cpp b/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.cpp index c06e146..9385694 100644 --- a/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.cpp +++ b/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.cpp @@ -8,17 +8,8 @@ MqttCommand* MqttCommand_DownloadLatestMFSRevision::Create(QObject *parent) { return new MqttCommand_DownloadLatestMFSRevision(parent); } -bool MqttCommand_DownloadLatestMFSRevision::processMessage(QJsonDocument msg) { +bool MqttCommand_DownloadLatestMFSRevision::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); - if (mgr->downloadLatestMFSRevision()) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->downloadLatestMFSRevision()); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.h b/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.h index f1ea690..624a646 100644 --- a/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.h +++ b/qt-ozwdaemon/mqttcommands/downloadLatestMFSRevision.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "DownloadLatestMFSRevision";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_DownloadLatestMFSRevision(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/hardResetController.cpp b/qt-ozwdaemon/mqttcommands/hardResetController.cpp index 4f7635b..917adb3 100644 --- a/qt-ozwdaemon/mqttcommands/hardResetController.cpp +++ b/qt-ozwdaemon/mqttcommands/hardResetController.cpp @@ -8,12 +8,9 @@ MqttCommand* MqttCommand_HardResetController::Create(QObject *parent) { return new MqttCommand_HardResetController(parent); } -bool MqttCommand_HardResetController::processMessage(QJsonDocument msg) { +bool MqttCommand_HardResetController::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); mgr->hardResetController(); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/hardResetController.h b/qt-ozwdaemon/mqttcommands/hardResetController.h index 680b335..e9a96f0 100644 --- a/qt-ozwdaemon/mqttcommands/hardResetController.h +++ b/qt-ozwdaemon/mqttcommands/hardResetController.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "HardResetController";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_HardResetController(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/hasNodeFailed.cpp b/qt-ozwdaemon/mqttcommands/hasNodeFailed.cpp index 6e65937..40e181f 100644 --- a/qt-ozwdaemon/mqttcommands/hasNodeFailed.cpp +++ b/qt-ozwdaemon/mqttcommands/hasNodeFailed.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_HasNodeFailed::Create(QObject *parent) { return new MqttCommand_HasNodeFailed(parent); } -bool MqttCommand_HasNodeFailed::processMessage(QJsonDocument msg) { +bool MqttCommand_HasNodeFailed::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->hasNodeFailed(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->hasNodeFailed(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/hasNodeFailed.h b/qt-ozwdaemon/mqttcommands/hasNodeFailed.h index 686b183..762ac44 100644 --- a/qt-ozwdaemon/mqttcommands/hasNodeFailed.h +++ b/qt-ozwdaemon/mqttcommands/hasNodeFailed.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "HasNodeFailed";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_HasNodeFailed(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/healNetwork.cpp b/qt-ozwdaemon/mqttcommands/healNetwork.cpp index 705269d..4829574 100644 --- a/qt-ozwdaemon/mqttcommands/healNetwork.cpp +++ b/qt-ozwdaemon/mqttcommands/healNetwork.cpp @@ -9,11 +9,8 @@ MqttCommand* MqttCommand_HealNetwork::Create(QObject *parent) { return new MqttCommand_HealNetwork(parent); } -bool MqttCommand_HealNetwork::processMessage(QJsonDocument msg) { +bool MqttCommand_HealNetwork::processMessage(rapidjson::Document &msg) { QTOZWManager *mgr = getOZWManager(); - mgr->healNetwork(msg["doreturnroute"].toBool()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->healNetwork(msg["doreturnroute"].GetBool()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/healNetwork.h b/qt-ozwdaemon/mqttcommands/healNetwork.h index 55b4da1..f166d78 100644 --- a/qt-ozwdaemon/mqttcommands/healNetwork.h +++ b/qt-ozwdaemon/mqttcommands/healNetwork.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "HealNetwork";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_HealNetwork(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/healNetworkNode.cpp b/qt-ozwdaemon/mqttcommands/healNetworkNode.cpp index 813069c..6aa0dfe 100644 --- a/qt-ozwdaemon/mqttcommands/healNetworkNode.cpp +++ b/qt-ozwdaemon/mqttcommands/healNetworkNode.cpp @@ -10,18 +10,11 @@ MqttCommand* MqttCommand_HealNetworkNode::Create(QObject *parent) { return new MqttCommand_HealNetworkNode(parent); } -bool MqttCommand_HealNetworkNode::processMessage(QJsonDocument msg) { +bool MqttCommand_HealNetworkNode::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - mgr->healNetworkNode(msg["node"].toInt(), msg["doreturnroute"].toBool()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->healNetworkNode(msg["node"].GetUint(), msg["doreturnroute"].GetBool()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/healNetworkNode.h b/qt-ozwdaemon/mqttcommands/healNetworkNode.h index b4d1264..250a72f 100644 --- a/qt-ozwdaemon/mqttcommands/healNetworkNode.h +++ b/qt-ozwdaemon/mqttcommands/healNetworkNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "HealNetworkNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_HealNetworkNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/mqttcommands.cpp b/qt-ozwdaemon/mqttcommands/mqttcommands.cpp index 3d6d2bd..14888f9 100644 --- a/qt-ozwdaemon/mqttcommands/mqttcommands.cpp +++ b/qt-ozwdaemon/mqttcommands/mqttcommands.cpp @@ -1,3 +1,6 @@ +#include + + #include "mqttcommands/mqttcommands.h" #include "mqttcommands/ping.h" #include "mqttcommands/open.h" @@ -61,61 +64,61 @@ mqttpublisher *MqttCommand::getMqttPublisher() { void MqttCommand::messageReceived(QMqttMessage msg) { qCDebug(ozwmc) << "Got "<< msg.topic().name()<< " Message: " << msg.payload(); - QJsonParseError jerrormsg; - QJsonDocument jmsg = QJsonDocument::fromJson(msg.payload(), &jerrormsg); - if (jmsg.isNull()) { - QJsonObject js; - js["Error"] = jerrormsg.errorString(); + rapidjson::Document jmsg; + jmsg.Parse(msg.payload()); + if (jmsg.HasParseError()) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", rapidjson::GetParseError_En(jmsg.GetParseError())); emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmc) << "Json Parse Error for " << GetCommand() << ": " << jerrormsg.errorString() << ": " << msg.payload(); + qCWarning(ozwmc) << "Json Parse Error for " << GetCommand() << ": " << rapidjson::GetParseError_En(jmsg.GetParseError()) << ": " << msg.payload(); return; } QString field; foreach (field, this->m_requiredIntFields) { - if (jmsg[field].isUndefined()) { - QJsonObject js; - js["Error"] = QString("Missing Field ").append(field); + if (!jmsg.HasMember(field.toStdString().c_str())) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload(); return; } - if (!jmsg[field].isDouble()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not Integer"); + if (!jmsg[field.toStdString().c_str()].IsNumber()) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not Integer").toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmc) << "Incorrect Field Type (Int) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload(); + qCWarning(ozwmc) << "Incorrect Field Type (Int) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload(); return; } } foreach (field, this->m_requiredStringFields) { - if (jmsg[field].isUndefined()) { - QJsonObject js; - js["Error"] = QString("Missing Field ").append(field); + if (!jmsg.HasMember(field.toStdString().c_str())) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload(); return; } - if (!jmsg[field].isString()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not String"); + if (!jmsg[field.toStdString().c_str()].IsString()) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not String").toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmc) << "Incorrect Field Type (String) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload(); + qCWarning(ozwmc) << "Incorrect Field Type (String) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload(); return; } } foreach (field, this->m_requiredBoolFields) { - if (jmsg[field].isUndefined()) { - QJsonObject js; - js["Error"] = QString("Missing Field ").append(field); + if (!jmsg.HasMember(field.toStdString().c_str())) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Missing Field ").append(field).toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); qCWarning(ozwmc) << "Missing Field for " << GetCommand() << ": " << field << ": " << msg.payload(); return; } - if (!jmsg[field].isBool()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type: ").append(field).append(": Not Bool"); + if (!jmsg[field.toStdString().c_str()].IsBool()) { + rapidjson::Document js; + QT2JS::SetString(js, "Error", QString("Incorrect Field Type: ").append(field).append(": Not Bool").toStdString().c_str()); emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmc) << "Incorrect Field Type (Bool) for " << GetCommand() << ": " << field << ": " << jmsg[field].type() << msg.payload(); + qCWarning(ozwmc) << "Incorrect Field Type (Bool) for " << GetCommand() << ": " << field << ": " << jmsg[field.toStdString().c_str()].GetType() << msg.payload(); return; } } @@ -128,29 +131,47 @@ void MqttCommand::messageReceived(QMqttMessage msg) { } } -bool MqttCommand::checkNode(QJsonDocument jmsg, QString field) { - quint8 node = jmsg[field].toInt(); +bool MqttCommand::checkNode(rapidjson::Document &jmsg, QString field) { + if (!jmsg.HasMember(field.toStdString().c_str())) { + qCWarning(ozwmc) << "Node " << field <<" Is Missing from Message"; + return false; + } + if (!jmsg[field.toStdString().c_str()].IsUint()) { + qCWarning(ozwmc) << "Node " << field << "is not a Uint32"; + return false; + } + quint8 node = jmsg[field.toStdString().c_str()].GetInt(); if (node == 0 || node == 255) { - qCWarning(ozwmc) << "Invalid Node in field " << field << " for message " << jmsg.toJson(); + qCWarning(ozwmc) << "Invalid Node in field " << field << " for message"; return false; } if (this->getMqttPublisher()->isValidNode(node)) { return true; } - qCWarning(ozwmc) << "Invalid Node in field " << field << " for message " << jmsg.toJson(); + qCWarning(ozwmc) << "Invalid Node in field " << field << " for message "; return false; } -bool MqttCommand::checkValue(QJsonDocument jmsg, QString field) { - quint64 vidKey = jmsg[field].toInt(); +bool MqttCommand::checkValue(rapidjson::Document &jmsg, QString field) { + if (!jmsg.HasMember(field.toStdString().c_str())) { + qCWarning(ozwmc) << "ValueIDKey " << field <<" Is Missing from Message"; + return false; + } + if (!jmsg[field.toStdString().c_str()].IsUint64()) { + qCWarning(ozwmc) << "ValueIDKey " << field << " Is not a uint64"; + return false; + } + + + quint64 vidKey = jmsg[field.toStdString().c_str()].GetUint64(); if (vidKey == 0) { - qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message " << jmsg.toJson(); + qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message "; return false; } if (this->getMqttPublisher()->isValidValueID(vidKey)) { return true; } - qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message " << jmsg.toJson(); + qCWarning(ozwmc) << "Invalid VidKey in field " << field << " for message "; return false; } @@ -162,6 +183,19 @@ bool MqttCommand::setValue(quint64 vidKey, QVariant data) { return this->getMqttPublisher()->setValue(vidKey, data); } +bool MqttCommand::sendSimpleStatus(bool status, QString error) { + rapidjson::Document js; + if (status == true) { + QT2JS::SetString(js, "status", "ok"); + } else { + QT2JS::SetString(js, "status", "failed"); + if (!error.isEmpty()) + QT2JS::SetString(js, "error", error); + } + emit sendCommandUpdate(GetCommand(), js); + return status; +} + MqttCommands::MqttCommands(QObject *parent) : QObject(parent) diff --git a/qt-ozwdaemon/mqttcommands/mqttcommands.h b/qt-ozwdaemon/mqttcommands/mqttcommands.h index 4f6b0db..0f2ba58 100644 --- a/qt-ozwdaemon/mqttcommands/mqttcommands.h +++ b/qt-ozwdaemon/mqttcommands/mqttcommands.h @@ -11,6 +11,7 @@ #include #include #include "mqttpublisher.h" +#include "qtrj.h" @@ -21,21 +22,22 @@ class MqttCommand : public QObject { public: void Setup(QMqttSubscription *); void messageReceived(QMqttMessage msg); - virtual bool processMessage(QJsonDocument) = 0; + virtual bool processMessage(rapidjson::Document &) = 0; virtual QString GetCommand() = 0; signals: - void sendCommandUpdate(QString, QJsonObject); + void sendCommandUpdate(QString, rapidjson::Document &); protected: MqttCommand(QObject *parent = nullptr); QTOZWManager *getOZWManager(); mqttpublisher *getMqttPublisher(); - bool checkNode(QJsonDocument, QString); - bool checkValue(QJsonDocument, QString); + bool checkNode(rapidjson::Document &, QString); + bool checkValue(rapidjson::Document &, QString); QVariant getValueData(quint64, QTOZW_ValueIds::ValueIdColumns); bool setValue(quint64, QVariant); QVector m_requiredStringFields; QVector m_requiredIntFields; QVector m_requiredBoolFields; + bool sendSimpleStatus(bool, QString error = QString()); private: QMqttSubscription *m_subscription; diff --git a/qt-ozwdaemon/mqttcommands/open.cpp b/qt-ozwdaemon/mqttcommands/open.cpp index f5f47d0..074f184 100644 --- a/qt-ozwdaemon/mqttcommands/open.cpp +++ b/qt-ozwdaemon/mqttcommands/open.cpp @@ -9,16 +9,7 @@ MqttCommand* MqttCommand_Open::Create(QObject *parent) { return new MqttCommand_Open(parent); } -bool MqttCommand_Open::processMessage(QJsonDocument msg) { +bool MqttCommand_Open::processMessage(rapidjson::Document &msg) { QTOZWManager *mgr = getOZWManager(); - if (mgr->open(msg["serialport"].toString())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->open(msg["serialport"].GetString())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/open.h b/qt-ozwdaemon/mqttcommands/open.h index 6885f48..42206e4 100644 --- a/qt-ozwdaemon/mqttcommands/open.h +++ b/qt-ozwdaemon/mqttcommands/open.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "Open";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_Open(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/ping.cpp b/qt-ozwdaemon/mqttcommands/ping.cpp index 6bd9b05..1fa1f4e 100644 --- a/qt-ozwdaemon/mqttcommands/ping.cpp +++ b/qt-ozwdaemon/mqttcommands/ping.cpp @@ -9,9 +9,9 @@ MqttCommand* MqttCommand_Ping::Create(QObject *parent) { return new MqttCommand_Ping(parent); } -bool MqttCommand_Ping::processMessage(QJsonDocument msg) { - QJsonObject js; - js["pong"] = msg["ping"]; +bool MqttCommand_Ping::processMessage(rapidjson::Document &msg) { + rapidjson::Document js; + QT2JS::SetString(js, "pong", msg["ping"].GetString()); emit sendCommandUpdate(MqttCommand_Ping::GetCommand(), js); return true; } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/ping.h b/qt-ozwdaemon/mqttcommands/ping.h index beab9eb..ac2e7af 100644 --- a/qt-ozwdaemon/mqttcommands/ping.h +++ b/qt-ozwdaemon/mqttcommands/ping.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "Ping";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_Ping(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/refreshnodeinfo.cpp b/qt-ozwdaemon/mqttcommands/refreshnodeinfo.cpp index dfd0f86..2b5572c 100644 --- a/qt-ozwdaemon/mqttcommands/refreshnodeinfo.cpp +++ b/qt-ozwdaemon/mqttcommands/refreshnodeinfo.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RefreshNodeInfo::Create(QObject *parent) { return new MqttCommand_RefreshNodeInfo(parent); } -bool MqttCommand_RefreshNodeInfo::processMessage(QJsonDocument msg) { +bool MqttCommand_RefreshNodeInfo::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->refreshNodeInfo(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->refreshNodeInfo(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/refreshnodeinfo.h b/qt-ozwdaemon/mqttcommands/refreshnodeinfo.h index ae0e3e3..ff23bd9 100644 --- a/qt-ozwdaemon/mqttcommands/refreshnodeinfo.h +++ b/qt-ozwdaemon/mqttcommands/refreshnodeinfo.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RefreshNodeInfo";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RefreshNodeInfo(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/removeFailedNode.cpp b/qt-ozwdaemon/mqttcommands/removeFailedNode.cpp index 2d4dd51..162a351 100644 --- a/qt-ozwdaemon/mqttcommands/removeFailedNode.cpp +++ b/qt-ozwdaemon/mqttcommands/removeFailedNode.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RemoveFailedNode::Create(QObject *parent) { return new MqttCommand_RemoveFailedNode(parent); } -bool MqttCommand_RemoveFailedNode::processMessage(QJsonDocument msg) { +bool MqttCommand_RemoveFailedNode::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->removeFailedNode(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->removeFailedNode(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/removeFailedNode.h b/qt-ozwdaemon/mqttcommands/removeFailedNode.h index 87fcb05..b512a05 100644 --- a/qt-ozwdaemon/mqttcommands/removeFailedNode.h +++ b/qt-ozwdaemon/mqttcommands/removeFailedNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RemoveFailedNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RemoveFailedNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/removeNode.cpp b/qt-ozwdaemon/mqttcommands/removeNode.cpp index 6a2b058..067026f 100644 --- a/qt-ozwdaemon/mqttcommands/removeNode.cpp +++ b/qt-ozwdaemon/mqttcommands/removeNode.cpp @@ -8,12 +8,9 @@ MqttCommand* MqttCommand_RemoveNode::Create(QObject *parent) { return new MqttCommand_RemoveNode(parent); } -bool MqttCommand_RemoveNode::processMessage(QJsonDocument msg) { +bool MqttCommand_RemoveNode::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); mgr->removeNode(); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/removeNode.h b/qt-ozwdaemon/mqttcommands/removeNode.h index bfc5141..8efd7d7 100644 --- a/qt-ozwdaemon/mqttcommands/removeNode.h +++ b/qt-ozwdaemon/mqttcommands/removeNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RemoveNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RemoveNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/replaceFailedNode.cpp b/qt-ozwdaemon/mqttcommands/replaceFailedNode.cpp index f0cb9dd..7abcfc8 100644 --- a/qt-ozwdaemon/mqttcommands/replaceFailedNode.cpp +++ b/qt-ozwdaemon/mqttcommands/replaceFailedNode.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_ReplaceFailedNode::Create(QObject *parent) { return new MqttCommand_ReplaceFailedNode(parent); } -bool MqttCommand_ReplaceFailedNode::processMessage(QJsonDocument msg) { +bool MqttCommand_ReplaceFailedNode::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->replaceFailedNode(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->replaceFailedNode(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/replaceFailedNode.h b/qt-ozwdaemon/mqttcommands/replaceFailedNode.h index ac7cbdc..a394cf3 100644 --- a/qt-ozwdaemon/mqttcommands/replaceFailedNode.h +++ b/qt-ozwdaemon/mqttcommands/replaceFailedNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "ReplaceFailedNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_ReplaceFailedNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestAllConfigParam.cpp b/qt-ozwdaemon/mqttcommands/requestAllConfigParam.cpp index 008bab3..09ac44f 100644 --- a/qt-ozwdaemon/mqttcommands/requestAllConfigParam.cpp +++ b/qt-ozwdaemon/mqttcommands/requestAllConfigParam.cpp @@ -9,18 +9,11 @@ MqttCommand* MqttCommand_RequestAllConfigParam::Create(QObject *parent) { return new MqttCommand_RequestAllConfigParam(parent); } -bool MqttCommand_RequestAllConfigParam::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestAllConfigParam::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - mgr->requestAllConfigParam(msg["node"].toInt()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->requestAllConfigParam(msg["node"].GetUint()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestAllConfigParam.h b/qt-ozwdaemon/mqttcommands/requestAllConfigParam.h index aefb2e1..2775e47 100644 --- a/qt-ozwdaemon/mqttcommands/requestAllConfigParam.h +++ b/qt-ozwdaemon/mqttcommands/requestAllConfigParam.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestAllConfigParam";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestAllConfigParam(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestConfigParam.cpp b/qt-ozwdaemon/mqttcommands/requestConfigParam.cpp index 3f71647..4ffe0a8 100644 --- a/qt-ozwdaemon/mqttcommands/requestConfigParam.cpp +++ b/qt-ozwdaemon/mqttcommands/requestConfigParam.cpp @@ -9,18 +9,11 @@ MqttCommand* MqttCommand_RequestConfigParam::Create(QObject *parent) { return new MqttCommand_RequestConfigParam(parent); } -bool MqttCommand_RequestConfigParam::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestConfigParam::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - mgr->requestConfigParam(msg["node"].toInt(), msg["param"].toInt()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->requestConfigParam(msg["node"].GetUint(), msg["param"].GetUint()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestConfigParam.h b/qt-ozwdaemon/mqttcommands/requestConfigParam.h index 23a67dc..b0f76f8 100644 --- a/qt-ozwdaemon/mqttcommands/requestConfigParam.h +++ b/qt-ozwdaemon/mqttcommands/requestConfigParam.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestConfigParam";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestConfigParam(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.cpp b/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.cpp index c973c98..3d0c73f 100644 --- a/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.cpp +++ b/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNetworkUpdate::Create(QObject *parent) { return new MqttCommand_RequestNetworkUpdate(parent); } -bool MqttCommand_RequestNetworkUpdate::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestNetworkUpdate::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->requestNetworkUpdate(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->requestNetworkUpdate(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.h b/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.h index b58c2d4..881676c 100644 --- a/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.h +++ b/qt-ozwdaemon/mqttcommands/requestNetworkUpdate.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestNetworkUpdate";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestNetworkUpdate(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestNodeDynamic.cpp b/qt-ozwdaemon/mqttcommands/requestNodeDynamic.cpp index d453823..9b5d67a 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeDynamic.cpp +++ b/qt-ozwdaemon/mqttcommands/requestNodeDynamic.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeDynamic::Create(QObject *parent) { return new MqttCommand_RequestNodeDynamic(parent); } -bool MqttCommand_RequestNodeDynamic::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestNodeDynamic::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->requestNodeDynamic(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->requestNodeDynamic(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestNodeDynamic.h b/qt-ozwdaemon/mqttcommands/requestNodeDynamic.h index ad973a4..40d17d4 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeDynamic.h +++ b/qt-ozwdaemon/mqttcommands/requestNodeDynamic.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestNodeDynamic";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestNodeDynamic(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.cpp b/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.cpp index 81530dd..0a11607 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.cpp +++ b/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeNeighborUpdate::Create(QObject *parent) { return new MqttCommand_RequestNodeNeighborUpdate(parent); } -bool MqttCommand_RequestNodeNeighborUpdate::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestNodeNeighborUpdate::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->requestNodeNeighborUpdate(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->requestNodeNeighborUpdate(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.h b/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.h index 8759c80..0a8f08e 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.h +++ b/qt-ozwdaemon/mqttcommands/requestNodeNeighborUpdate.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestNodeNeighborUpdate";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestNodeNeighborUpdate(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/requestNodeState.cpp b/qt-ozwdaemon/mqttcommands/requestNodeState.cpp index 8f36ef5..7d978d1 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeState.cpp +++ b/qt-ozwdaemon/mqttcommands/requestNodeState.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_RequestNodeState::Create(QObject *parent) { return new MqttCommand_RequestNodeState(parent); } -bool MqttCommand_RequestNodeState::processMessage(QJsonDocument msg) { +bool MqttCommand_RequestNodeState::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->requestNodeState(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->requestNodeState(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/requestNodeState.h b/qt-ozwdaemon/mqttcommands/requestNodeState.h index b3ecae0..f3faeac 100644 --- a/qt-ozwdaemon/mqttcommands/requestNodeState.h +++ b/qt-ozwdaemon/mqttcommands/requestNodeState.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "RequestNodeState";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_RequestNodeState(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/sendNodeInformation.cpp b/qt-ozwdaemon/mqttcommands/sendNodeInformation.cpp index 12f157e..c647846 100644 --- a/qt-ozwdaemon/mqttcommands/sendNodeInformation.cpp +++ b/qt-ozwdaemon/mqttcommands/sendNodeInformation.cpp @@ -9,23 +9,10 @@ MqttCommand* MqttCommand_SendNodeInformation::Create(QObject *parent) { return new MqttCommand_SendNodeInformation(parent); } -bool MqttCommand_SendNodeInformation::processMessage(QJsonDocument msg) { +bool MqttCommand_SendNodeInformation::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - if (mgr->sendNodeInformation(msg["node"].toInt())) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(mgr->sendNodeInformation(msg["node"].GetUint())); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/sendNodeInformation.h b/qt-ozwdaemon/mqttcommands/sendNodeInformation.h index 8c9fb2a..bc48e5a 100644 --- a/qt-ozwdaemon/mqttcommands/sendNodeInformation.h +++ b/qt-ozwdaemon/mqttcommands/sendNodeInformation.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "SendNodeInformation";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_SendNodeInformation(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/setValue.cpp b/qt-ozwdaemon/mqttcommands/setValue.cpp index 4a2336e..de53313 100644 --- a/qt-ozwdaemon/mqttcommands/setValue.cpp +++ b/qt-ozwdaemon/mqttcommands/setValue.cpp @@ -12,31 +12,19 @@ MqttCommand* MqttCommand_SetValue::Create(QObject *parent) { return new MqttCommand_SetValue(parent); } -bool MqttCommand_SetValue::processMessage(QJsonDocument msg) { +bool MqttCommand_SetValue::processMessage(rapidjson::Document &msg) { if (!this->checkValue(msg, "ValueIDKey")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid ValueIDKey Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid ValueIDKey Number"); } /* check that the Value Field exists */ - if (msg["Value"].isUndefined()) { - QJsonObject js; - js["Error"] = QString("Missing Field Value"); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Missing Field for " << GetCommand() << ": Value: " << msg.toJson(); - return false; + if (!msg.HasMember("Value")) { + return this->sendSimpleStatus(false, "Missing Field Value"); } - quint64 vidKey = msg["ValueIdKey"].toInt(); + quint64 vidKey = msg["ValueIdKey"].GetUint(); QBitArray flags = this->getValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::ValueFlags).value(); if (flags[QTOZW_ValueIds::ValueIDFlags::ReadOnly] == true) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "ValueID is Read Only"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "ValueID is Read Only"); } QTOZW_ValueIds::ValueIdTypes types = this->getValueData(vidKey, QTOZW_ValueIds::ValueIdColumns::Type).value(); QVariant data; @@ -47,72 +35,58 @@ bool MqttCommand_SetValue::processMessage(QJsonDocument msg) { } case QTOZW_ValueIds::ValueIdTypes::Bool: { - if (!msg["Value"].isBool()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsBool()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - data = msg["Value"].toBool(); + data = msg["Value"].GetBool(); break; } case QTOZW_ValueIds::ValueIdTypes::Button: { - if (!msg["Value"].isBool()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsBool()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Bool: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Bool) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - data = msg["Value"].toBool(); + data = msg["Value"].GetBool(); break; } case QTOZW_ValueIds::ValueIdTypes::Byte: { - if (!msg["Value"].isDouble()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Byte: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Byte) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsUint()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Byte: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Byte) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - if (msg["Value"].toInt() > UCHAR_MAX) { - QJsonObject js; - js["Error"] = QString("Value is Larger than Byte Field: ").append(msg["Value"].toInt()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Value is Larger than Byte Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson(); + if (msg["Value"].GetUint() > UCHAR_MAX) { + this->sendSimpleStatus(false, QString("Value is Larger than Byte Field: ").append(msg["Value"].GetUint())); + qCWarning(ozwmcsv) << "Value is Larger than Byte Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint(); return false; } - data = msg["Value"].toInt(); + data = msg["Value"].GetUint(); break; } case QTOZW_ValueIds::ValueIdTypes::Decimal: { - if (!msg["Value"].isDouble()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Decimal: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Decimal) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsDouble()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Decimal: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Decimal) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - data = msg["Value"].toDouble(); + data = msg["Value"].GetDouble(); break; } case QTOZW_ValueIds::ValueIdTypes::Int: { - if (!msg["Value"].isDouble()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Integer: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Integer) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsUint()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Integer: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Integer) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - if (static_cast(msg["Value"].toInt()) > UINT_MAX) { - QJsonObject js; - js["Error"] = QString("Value is Larger than Integer Field: ").append(msg["Value"].toInt()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Value is Larger than Integer Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson(); + if (static_cast(msg["Value"].GetUint()) > UINT_MAX) { + this->sendSimpleStatus(false, QString("Value is Larger than Integer Field: ").append(msg["Value"].GetUint())); + qCWarning(ozwmcsv) << "Value is Larger than Integer Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint(); return false; } - data = msg["Value"].toInt(); + data = msg["Value"].GetUint(); break; } case QTOZW_ValueIds::ValueIdTypes::List: @@ -123,60 +97,37 @@ bool MqttCommand_SetValue::processMessage(QJsonDocument msg) { } case QTOZW_ValueIds::ValueIdTypes::Short: { - if (!msg["Value"].isDouble()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not Short: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (Short) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsUint()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not Short: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (Short) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - if (msg["Value"].toInt() > USHRT_MAX) { - QJsonObject js; - js["Error"] = QString("Value is Larger than Short Field: ").append(msg["Value"].toInt()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Value is Larger than Short Field for " << GetCommand() << ": Value: " << msg["Value"].toInt() << msg.toJson(); + if (msg["Value"].GetUint() > USHRT_MAX) { + this->sendSimpleStatus(false, QString("Value is Larger than Short Field: ").append(msg["Value"].GetUint())); + qCWarning(ozwmcsv) << "Value is Larger than Short Field for " << GetCommand() << ": Value: " << msg["Value"].GetUint(); return false; } - data = msg["Value"].toInt(); + data = msg["Value"].GetUint(); break; } case QTOZW_ValueIds::ValueIdTypes::String: { - if (!msg["Value"].isString()) { - QJsonObject js; - js["Error"] = QString("Incorrect Field Type for Value: Not String: ").append(msg["Value"].type()); - emit sendCommandUpdate(GetCommand(), js); - qCWarning(ozwmcsv) << "Incorrect Field Type (String) for " << GetCommand() << ": Value: " << msg["Value"].type() << msg.toJson(); + if (!msg["Value"].IsString()) { + this->sendSimpleStatus(false, QString("Incorrect Field Type for Value: Not String: ").append(msg["Value"].GetType())); + qCWarning(ozwmcsv) << "Incorrect Field Type (String) for " << GetCommand() << ": Value: " << msg["Value"].GetType(); return false; } - data = msg["Value"].toString(); + data = msg["Value"].GetString(); break; } case QTOZW_ValueIds::ValueIdTypes::TypeCount: { - qCWarning(ozwmcsv) << "Invalid ValueID Type " << types << "for setValue" << msg.toJson(); - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Unknown ValueID Type"; - emit sendCommandUpdate(GetCommand(), js); - return false; + qCWarning(ozwmcsv) << "Invalid ValueID Type " << types << "for setValue"; + return this->sendSimpleStatus(false, "Unknown ValueID Type"); break; } } if (data.isNull()) { qCWarning(ozwmcsv) << "Data is undefined for setValue... Json Conversion Failed?"; - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "JSON Conversion Failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "JSON Conversion Failed"); } - if (this->setValue(vidKey, data)) { - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; - } - QJsonObject js; - js["status"] = "failed"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(this->setValue(vidKey, data)); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/setValue.h b/qt-ozwdaemon/mqttcommands/setValue.h index 7efe548..6bc2002 100644 --- a/qt-ozwdaemon/mqttcommands/setValue.h +++ b/qt-ozwdaemon/mqttcommands/setValue.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "SetValue";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_SetValue(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/softResetController.cpp b/qt-ozwdaemon/mqttcommands/softResetController.cpp index 2dfa81e..e304526 100644 --- a/qt-ozwdaemon/mqttcommands/softResetController.cpp +++ b/qt-ozwdaemon/mqttcommands/softResetController.cpp @@ -8,12 +8,9 @@ MqttCommand* MqttCommand_SoftResetController::Create(QObject *parent) { return new MqttCommand_SoftResetController(parent); } -bool MqttCommand_SoftResetController::processMessage(QJsonDocument msg) { +bool MqttCommand_SoftResetController::processMessage(rapidjson::Document &msg) { Q_UNUSED(msg); QTOZWManager *mgr = getOZWManager(); mgr->softResetController(); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/softResetController.h b/qt-ozwdaemon/mqttcommands/softResetController.h index e630d47..0f19864 100644 --- a/qt-ozwdaemon/mqttcommands/softResetController.h +++ b/qt-ozwdaemon/mqttcommands/softResetController.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "SoftResetController";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_SoftResetController(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/testNetwork.cpp b/qt-ozwdaemon/mqttcommands/testNetwork.cpp index e884748..3f257ad 100644 --- a/qt-ozwdaemon/mqttcommands/testNetwork.cpp +++ b/qt-ozwdaemon/mqttcommands/testNetwork.cpp @@ -9,11 +9,8 @@ MqttCommand* MqttCommand_TestNetwork::Create(QObject *parent) { return new MqttCommand_TestNetwork(parent); } -bool MqttCommand_TestNetwork::processMessage(QJsonDocument msg) { +bool MqttCommand_TestNetwork::processMessage(rapidjson::Document &msg) { QTOZWManager *mgr = getOZWManager(); - mgr->testNetwork(msg["count"].toInt()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->testNetwork(msg["count"].GetUint()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/testNetwork.h b/qt-ozwdaemon/mqttcommands/testNetwork.h index 86c33b1..4595898 100644 --- a/qt-ozwdaemon/mqttcommands/testNetwork.h +++ b/qt-ozwdaemon/mqttcommands/testNetwork.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "TestNetwork";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_TestNetwork(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttcommands/testNetworkNode.cpp b/qt-ozwdaemon/mqttcommands/testNetworkNode.cpp index ff942e3..9e0f5fc 100644 --- a/qt-ozwdaemon/mqttcommands/testNetworkNode.cpp +++ b/qt-ozwdaemon/mqttcommands/testNetworkNode.cpp @@ -9,18 +9,11 @@ MqttCommand* MqttCommand_TestNetworkNode::Create(QObject *parent) { return new MqttCommand_TestNetworkNode(parent); } -bool MqttCommand_TestNetworkNode::processMessage(QJsonDocument msg) { +bool MqttCommand_TestNetworkNode::processMessage(rapidjson::Document &msg) { if (!this->checkNode(msg, "node")) { - QJsonObject js; - js["status"] = "failed"; - js["Error"] = "Invalid Node Number"; - emit sendCommandUpdate(GetCommand(), js); - return false; + return this->sendSimpleStatus(false, "Invalid Node Number"); } QTOZWManager *mgr = getOZWManager(); - mgr->testNetworkNode(msg["node"].toInt(), msg["count"].toInt()); - QJsonObject js; - js["status"] = "ok"; - emit sendCommandUpdate(GetCommand(), js); - return true; + mgr->testNetworkNode(msg["node"].GetUint64(), msg["count"].GetUint()); + return this->sendSimpleStatus(true); } \ No newline at end of file diff --git a/qt-ozwdaemon/mqttcommands/testNetworkNode.h b/qt-ozwdaemon/mqttcommands/testNetworkNode.h index 1b262f6..87fff39 100644 --- a/qt-ozwdaemon/mqttcommands/testNetworkNode.h +++ b/qt-ozwdaemon/mqttcommands/testNetworkNode.h @@ -9,7 +9,7 @@ public: static MqttCommand *Create(QObject *parent = nullptr); static QString StaticGetCommand() { return "TestNetworkNode";}; QString GetCommand() override { return StaticGetCommand(); }; - bool processMessage(QJsonDocument) override; + bool processMessage(rapidjson::Document &) override; private: MqttCommand_TestNetworkNode(QObject *parent = nullptr); }; diff --git a/qt-ozwdaemon/mqttpublisher.cpp b/qt-ozwdaemon/mqttpublisher.cpp index fc3be40..ab49ea6 100644 --- a/qt-ozwdaemon/mqttpublisher.cpp +++ b/qt-ozwdaemon/mqttpublisher.cpp @@ -1,8 +1,12 @@ #include #include "mqttpublisher.h" +#include "qtrj.h" #include "mqttcommands/mqttcommands.h" +#include // for stringify JSON + + Q_LOGGING_CATEGORY(ozwmp, "ozw.mqtt.publisher"); Q_LOGGING_CATEGORY(ozwmpnode, "ozw.mqtt.publisher.node"); Q_LOGGING_CATEGORY(ozwmpvalue, "ozw.mqtt.publisher.value"); @@ -26,7 +30,10 @@ bool mqttNodeModel::isValidNode(quint8 node) { } -bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTOZWManager *mgr) { +bool mqttNodeModel::populateJsonObject(rapidjson::Document &jsonobject, quint8 node, QTOZWManager *mgr) { + if (jsonobject.IsNull()) + jsonobject.SetObject(); + for (int i = 0; i < this->columnCount(QModelIndex()); i++) { QVariant data = this->getNodeData(node, static_cast(i)); if (data.type() == QVariant::Invalid) { @@ -37,20 +44,20 @@ bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTO QBitArray flag = data.toBitArray(); QMetaEnum metaEnum = QMetaEnum::fromType(); for (int j = 0; j < nodeFlags::flagCount; j++) { - jsonobject->insert(metaEnum.valueToKey(j), flag.at(j)); + QT2JS::SetBool(jsonobject, metaEnum.valueToKey(j), flag.at(j)); } break; } default: { QMetaEnum metaEnum = QMetaEnum::fromType(); if (static_cast(data.type()) == QMetaType::QString) { - jsonobject->insert(metaEnum.valueToKey(i), data.toString()); + QT2JS::SetString(jsonobject, metaEnum.valueToKey(i), data.toString()); } else if (static_cast(data.type()) == QMetaType::Bool) { - jsonobject->insert(metaEnum.valueToKey(i), data.toBool()); + QT2JS::SetBool(jsonobject, metaEnum.valueToKey(i), data.toBool()); } else if (static_cast(data.type()) == QMetaType::Int) { - jsonobject->insert(metaEnum.valueToKey(i), data.toInt()); + QT2JS::SetInt(jsonobject, metaEnum.valueToKey(i), data.toInt()); } else if (static_cast(data.type()) == QMetaType::UInt) { - jsonobject->insert(metaEnum.valueToKey(i), data.toInt()); + QT2JS::SetUint(jsonobject, metaEnum.valueToKey(i), data.toUInt()); } else { qCWarning(ozwmpvalue) << "Can't Convert " << data.type() << "(" << metaEnum.valueToKey(i) << ") to store in JsonObject: " << node; } @@ -60,23 +67,33 @@ bool mqttNodeModel::populateJsonObject(QJsonObject *jsonobject, quint8 node, QTO } /* MetaData */ - QJsonObject metadata = jsonobject->value("MetaData").toObject(); - QMetaEnum metaEnum = QMetaEnum::fromType(); - if (metadata.empty()) { - for (int i = 0; i < QTOZWManagerSource::Identifier; i++) { - metadata.insert(metaEnum.valueToKey(i), mgr->GetMetaData(node, static_cast(i))); + rapidjson::Value metadata; + if (!jsonobject.HasMember("MetaData")) { + rapidjson::Value metadata; + metadata.SetObject(); + QMetaEnum metaEnum = QMetaEnum::fromType(); + if (metadata.IsNull()) { + metadata.SetObject(); } - metadata.insert("ProductPicBase64", QString(mgr->GetMetaDataProductPic(node).toBase64())); - jsonobject->insert("MetaData", metadata); + for (int i = 0; i < QTOZWManagerSource::Identifier; i++) { + metadata.AddMember(rapidjson::Value(metaEnum.valueToKey(i), jsonobject.GetAllocator()).Move(), + rapidjson::Value(mgr->GetMetaData(node, static_cast(i)).toStdString().c_str(), jsonobject.GetAllocator()).Move(), + jsonobject.GetAllocator()); + } + metadata.AddMember(rapidjson::Value("ProductPicBase64").Move(), + rapidjson::Value(QString(mgr->GetMetaDataProductPic(node).toBase64()).toStdString().c_str(), jsonobject.GetAllocator()).Move(), + jsonobject.GetAllocator()); + + jsonobject.AddMember(rapidjson::Value("MetaData"), metadata, jsonobject.GetAllocator()); } /* Neighbors */ QVector neighbors = mgr->GetNodeNeighbors(node); if (neighbors.size() > 0) { - QJsonArray N; + rapidjson::Value N(rapidjson::kArrayType); for (int i = 0; i < neighbors.count(); i++) { - N.append(neighbors[i]); + N.PushBack(neighbors[i], jsonobject.GetAllocator()); } - jsonobject->insert("Neighbors", N); + jsonobject.AddMember(rapidjson::Value("Neighbors").Move(), N, jsonobject.GetAllocator()); } return true; } @@ -247,6 +264,7 @@ mqttpublisher::mqttpublisher(QSettings *settings, QObject *parent) : QObject(par this->m_client->setHostname(settings->value("MQTTServer", "127.0.0.1").toString()); this->m_client->setPort(static_cast(settings->value("MQTTPort", 1883).toInt())); + this->m_ozwstatus.SetObject(); /* setup the Commands */ this->m_commands = new MqttCommands(this); @@ -501,16 +519,16 @@ void mqttpublisher::handleMessage(const QByteArray &message, const QMqttTopicNam // qCDebug(ozwmp) << "Received: " << topic.name() << ":" << message; } - bool mqttpublisher::sendStatusUpdate() { - this->m_ozwstatus["TimeStamp"] = QDateTime::currentSecsSinceEpoch(); - this->m_client->publish(QMqttTopicName(getTopic(MQTT_OZW_STATUS_TOPIC)), QJsonDocument(this->m_ozwstatus).toJson(), 0, true); + QT2JS::SetUInt64(this->m_ozwstatus, "TimeStamp", QDateTime::currentSecsSinceEpoch()); + this->m_client->publish(QMqttTopicName(getTopic(MQTT_OZW_STATUS_TOPIC)), QT2JS::getJSON(this->m_ozwstatus), 0, true); return true; } bool mqttpublisher::sendNodeUpdate(quint8 node) { - this->m_nodes[node]["TimeStamp"] = QDateTime::currentSecsSinceEpoch(); - this->m_client->publish(QMqttTopicName(getNodeTopic(MQTT_OZW_NODE_TOPIC, node)), QJsonDocument(this->m_nodes[node]).toJson(), 0, true); + QT2JS::SetUInt64(*this->m_nodes[node], "TimeStamp", QDateTime::currentSecsSinceEpoch()); + this->m_client->publish(QMqttTopicName(getNodeTopic(MQTT_OZW_NODE_TOPIC, node)), QT2JS::getJSON(*this->m_nodes[node]), 0, true); + qDebug() << QT2JS::getJSON(*this->m_nodes[node]); return true; } @@ -524,9 +542,9 @@ bool mqttpublisher::sendValueUpdate(quint64 vidKey) { this->m_client->publish(QMqttTopicName(getValueTopic(MQTT_OZW_VID_TOPIC, node, vidKey)), QJsonDocument(this->m_values[vidKey]).toJson(), 0, true); return true; } -void mqttpublisher::sendCommandUpdate(QString command, QJsonObject js) { - js["TimeStamp"] = QDateTime::currentSecsSinceEpoch(); - this->m_client->publish(QMqttTopicName(getCommandResponseTopic(command.toLower())), QJsonDocument(js).toJson(), 0, false); +void mqttpublisher::sendCommandUpdate(QString command, rapidjson::Document &js) { + QT2JS::SetUInt64(js, "TimeStamp", QDateTime::currentSecsSinceEpoch()); + this->m_client->publish(QMqttTopicName(getCommandResponseTopic(command.toLower())), QT2JS::getJSON(js), 0, false); return; } @@ -548,7 +566,7 @@ bool mqttpublisher::delValueTopic(quint64 vidKey) { void mqttpublisher::ready() { qCDebug(ozwmp) << "Publishing Event ready:"; - this->m_ozwstatus["Status"] = "Ready"; + QT2JS::SetString(this->m_ozwstatus, "Status", "Ready"); this->sendStatusUpdate(); } void mqttpublisher::valueAdded(quint64 vidKey) { @@ -576,27 +594,36 @@ void mqttpublisher::valueRefreshed(quint64 vidKey) { } void mqttpublisher::nodeNew(quint8 node) { qCDebug(ozwmp) << "Publishing Event NodeNew:" << node; - this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); - this->m_nodes[node]["Event"] = "nodeNew"; + this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeNew"); this->sendNodeUpdate(node); } void mqttpublisher::nodeAdded(quint8 node) { qCDebug(ozwmp) << "Publishing Event NodeAdded:" << node; - this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); - this->m_nodes[node]["Event"] = "nodeAdded"; + if (this->m_nodes.find(node) == this->m_nodes.end()) { + this->m_nodes.insert(node, new rapidjson::Document()); + } + this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeAdded"); this->sendNodeUpdate(node); } void mqttpublisher::nodeRemoved(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeRemoved:" << node; this->delNodeTopic(node); + if (this->m_nodes.find(node) == this->m_nodes.end()) { + this->m_nodes.remove(node); + } } void mqttpublisher::nodeReset(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeReset:" << node; this->delNodeTopic(node); + if (this->m_nodes.find(node) == this->m_nodes.end()) { + this->m_nodes.remove(node); + } } void mqttpublisher::nodeNaming(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeNaming:" << node; - this->m_nodes[node]["Event"] = "nodeNaming"; + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeNaming"); this->sendNodeUpdate(node); } void mqttpublisher::nodeEvent(quint8 node, quint8 event) { @@ -608,71 +635,71 @@ void mqttpublisher::nodeEvent(quint8 node, quint8 event) { } void mqttpublisher::nodeProtocolInfo(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeProtocolInfo:" << node; - this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); - this->m_nodes[node]["Event"] = "nodeProtocolInfo"; + this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeProtocolInfo"); this->sendNodeUpdate(node); } void mqttpublisher::nodeEssentialNodeQueriesComplete(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeEssentialNodeQueriesComplete:" << node; - this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); - this->m_nodes[node]["Event"] = "nodeEssentialNodeQueriesComplete"; + this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeEssentialNodeQueriesComplete"); this->sendNodeUpdate(node); } void mqttpublisher::nodeQueriesComplete(quint8 node) { qCDebug(ozwmp) << "Publishing Event nodeQueriesComplete:" << node; - this->m_nodeModel->populateJsonObject(&this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); - this->m_nodes[node]["Event"] = "nodeQueriesComplete"; + this->m_nodeModel->populateJsonObject(*this->m_nodes[node], node, this->m_qtozwdeamon->getManager()); + QT2JS::SetString(*this->m_nodes[node], "Event", "nodeQueriesComplete"); this->sendNodeUpdate(node); } void mqttpublisher::driverReady(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event driverReady:" << homeID; - this->m_ozwstatus["Status"] = "driverReady"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "driverReady"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); } void mqttpublisher::driverFailed(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event driverFailed:" << homeID; - this->m_ozwstatus["Status"] = "driverFailed"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "driverFailed"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); } void mqttpublisher::driverReset(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event driverReset:" << homeID; - this->m_ozwstatus["Status"] = "driverReset"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "driverReset"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); } void mqttpublisher::driverRemoved(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event driverRemoved:" << homeID; - this->m_ozwstatus["Status"] = "driverRemoved"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "driverRemoved"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); } void mqttpublisher::driverAllNodesQueriedSomeDead() { qCDebug(ozwmp) << "Publishing Event driverAllNodesQueriedSomeDead:" ; - this->m_ozwstatus["Status"] = "driverAllNodesQueriedSomeDead"; + QT2JS::SetString(this->m_ozwstatus, "Status", "driverAllNodesQueriedSomeDead"); this->sendStatusUpdate(); } void mqttpublisher::driverAllNodesQueried() { qCDebug(ozwmp) << "Publishing Event driverAllNodesQueried:" ; - this->m_ozwstatus["Status"] = "driverAllNodesQueried"; + QT2JS::SetString(this->m_ozwstatus, "Status", "driverAllNodesQueried"); this->sendStatusUpdate(); } void mqttpublisher::driverAwakeNodesQueried() { qCDebug(ozwmp) << "Publishing Event driverAwakeNodesQueried:" ; - this->m_ozwstatus["Status"] = "driverAwakeNodesQueried"; + QT2JS::SetString(this->m_ozwstatus, "Status", "driverAwakeNodesQueried"); this->sendStatusUpdate(); } void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Notification_Controller_Cmd command, NotificationTypes::QTOZW_Notification_Controller_State state, NotificationTypes::QTOZW_Notification_Controller_Error error) { qCDebug(ozwmp) << "Publishing Event controllerCommand" << node << command << state << error; - QJsonObject js; + rapidjson::Document js; if (node > 0) - js["Node"] = node; + QT2JS::SetUint(js, "Node", node); QMetaEnum metaEnum = QMetaEnum::fromType(); - js["State"] = metaEnum.valueToKey(state); + QT2JS::SetString(js, "State", metaEnum.valueToKey(state)); if (error != NotificationTypes::QTOZW_Notification_Controller_Error::Ctrl_Error_None) { metaEnum = QMetaEnum::fromType(); - js["Error"] = metaEnum.valueToKey(error); + QT2JS::SetString(js, "Error", metaEnum.valueToKey(error)); } switch(command) { @@ -689,12 +716,12 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti break; } case NotificationTypes::Ctrl_Cmd_CreateButton: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } case NotificationTypes::Ctrl_Cmd_CreateNewPrimary: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } @@ -703,7 +730,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti break; } case NotificationTypes::Ctrl_Cmd_DeleteButton: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } @@ -712,7 +739,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti break; } case NotificationTypes::Ctrl_Cmd_ReceiveConfiguration: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } @@ -729,7 +756,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti break; } case NotificationTypes::Ctrl_Cmd_ReplicationSend: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } @@ -746,7 +773,7 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti break; } case NotificationTypes::Ctrl_Cmd_TransferPrimaryRole: { - js["Command"] = QMetaEnum::fromType().valueToKey(command); + QT2JS::SetString(js, "Command", QMetaEnum::fromType().valueToKey(command)); this->sendCommandUpdate("ControllerCommand", js); break; } @@ -758,45 +785,45 @@ void mqttpublisher::controllerCommand(quint8 node, NotificationTypes::QTOZW_Noti } void mqttpublisher::ozwNotification(quint8 node, NotificationTypes::QTOZW_Notification_Code event) { qCDebug(ozwmp) << "Publishing Event ozwNotification"; - QJsonObject js; + rapidjson::Document js; QMetaEnum metaEnum = QMetaEnum::fromType(); - js["Node"] = node; - js["Event"] = metaEnum.valueToKey(event); + QT2JS::SetUint(js, "Node", node); + QT2JS::SetString(js, "Event", metaEnum.valueToKey(event)); this->sendCommandUpdate("Notification", js); } void mqttpublisher::ozwUserAlert(quint8 node, NotificationTypes::QTOZW_Notification_User event, quint8 retry) { qCDebug(ozwmp) << "Publishing Event ozwNotification"; - QJsonObject js; + rapidjson::Document js; QMetaEnum metaEnum = QMetaEnum::fromType(); - js["Node"] = node; - js["Event"] = metaEnum.valueToKey(event); + QT2JS::SetUint(js, "Node", node); + QT2JS::SetString(js, "Event", metaEnum.valueToKey(event)); if (event == NotificationTypes::QTOZW_Notification_User::Notification_User_ApplicationStatus_Retry) { - js["Retry"] = static_cast(retry); + QT2JS::SetUint(js, "Retry", retry); } this->sendCommandUpdate("UserAlert", js); } void mqttpublisher::manufacturerSpecificDBReady() { qCDebug(ozwmp) << "Publishing Event manufacturerSpecificDBReady"; - this->m_ozwstatus["ManufacturerSpecificDBReady"] = true; + QT2JS::SetBool(this->m_ozwstatus, "ManufacturerSpecificDBReady", true); this->sendStatusUpdate(); } void mqttpublisher::starting() { qCDebug(ozwmp) << "Publishing Event starting"; - this->m_ozwstatus["Status"] = "starting"; + QT2JS::SetString(this->m_ozwstatus, "Status", "starting"); this->sendStatusUpdate(); } void mqttpublisher::started(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event started"; - this->m_ozwstatus["Status"] = "started"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "started"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); this->m_statsTimer.start(10000); } void mqttpublisher::stopped(quint32 homeID) { qCDebug(ozwmp) << "Publishing Event stopped"; - this->m_ozwstatus["Status"] = "stopped"; - this->m_ozwstatus["homeID"] = QJsonValue(static_cast(homeID)); + QT2JS::SetString(this->m_ozwstatus, "Status", "stopped"); + QT2JS::SetUint(this->m_ozwstatus, "homeID", homeID); this->sendStatusUpdate(); this->m_statsTimer.stop(); } diff --git a/qt-ozwdaemon/mqttpublisher.h b/qt-ozwdaemon/mqttpublisher.h index 40caed8..3dc9e2b 100644 --- a/qt-ozwdaemon/mqttpublisher.h +++ b/qt-ozwdaemon/mqttpublisher.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "qtozwdaemon.h" #include "mqttcommands/mqttcommands.h" @@ -28,7 +29,7 @@ class mqttNodeModel : public QTOZW_Nodes { public: explicit mqttNodeModel(QObject *parent = nullptr); QVariant getNodeData(quint8, NodeColumns); - bool populateJsonObject(QJsonObject *, quint8, QTOZWManager *); + bool populateJsonObject(rapidjson::Document &, quint8, QTOZWManager *); bool isValidNode(quint8); }; @@ -50,7 +51,7 @@ public: explicit mqttpublisher(QSettings *setting, QObject *parent = nullptr); void setOZWDaemon(qtozwdaemon *ozwdaemon); QTOZWManager *getQTOZWManager(); - void sendCommandUpdate(QString, QJsonObject); + void sendCommandUpdate(QString, rapidjson::Document &); bool isValidNode(quint8 node); bool isValidValueID(quint64 vidKey); QVariant getValueData(quint64, mqttValueIDModel::ValueIdColumns); @@ -110,8 +111,8 @@ private: bool delNodeTopic(quint8); bool delValueTopic(quint64); - QJsonObject m_ozwstatus; - QMap m_nodes; + rapidjson::Document m_ozwstatus; + QMap m_nodes; mqttNodeModel *m_nodeModel; QMap m_values; mqttValueIDModel *m_valueModel; diff --git a/qt-ozwdaemon/qt-ozwdaemon.pro b/qt-ozwdaemon/qt-ozwdaemon.pro index acfaa64..9456f2b 100644 --- a/qt-ozwdaemon/qt-ozwdaemon.pro +++ b/qt-ozwdaemon/qt-ozwdaemon.pro @@ -4,7 +4,7 @@ QT += remoteobjects TARGET = ../ozwdaemon -CONFIG += c++11 console silent +CONFIG += c++11 console link_pkgconfig silent CONFIG -= app_bundle # The following define makes your compiler emit warnings if you use @@ -19,9 +19,11 @@ DEFINES += QT_DEPRECATED_WARNINGS QT_MESSAGELOGCONTEXT #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 qtHaveModule(mqtt) { + PKGCONFIG += RapidJSON QT += mqtt DEFINES += HAVE_MQTT SOURCES += mqttpublisher.cpp \ + qtrj.cpp \ mqttcommands/mqttcommands.cpp \ mqttcommands/ping.cpp \ mqttcommands/open.cpp \ @@ -55,6 +57,7 @@ qtHaveModule(mqtt) { mqttcommands/setValue.cpp HEADERS += mqttpublisher.h \ + qtrj.h \ mqttcommands/mqttcommands.h \ mqttcommands/ping.h \ mqttcommands/open.h \ diff --git a/qt-ozwdaemon/qtrj.cpp b/qt-ozwdaemon/qtrj.cpp new file mode 100644 index 0000000..f9acb34 --- /dev/null +++ b/qt-ozwdaemon/qtrj.cpp @@ -0,0 +1,132 @@ + +#include +#include +#include +#include +#include // 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 writer(sb); + doc.Accept(writer); // Accept() traverses the DOM and generates Handler events. + return sb.GetString(); +} diff --git a/qt-ozwdaemon/qtrj.h b/qt-ozwdaemon/qtrj.h new file mode 100644 index 0000000..580393f --- /dev/null +++ b/qt-ozwdaemon/qtrj.h @@ -0,0 +1,21 @@ +#ifndef QTRJ_H +#define QTRJ_H + +#include +#include +#include + +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 \ No newline at end of file