From d23017274eca1744d995afb6dfdc60d07ada368c Mon Sep 17 00:00:00 2001 From: Fanda Vacek Date: Wed, 27 Dec 2023 17:04:01 +0100 Subject: [PATCH] Initial SHV v3 support, subscribe and unsubscribe works --- 3rdparty/libshv | 2 +- 3rdparty/necrolog | 2 +- shvspy/src/appversion.h | 2 +- .../src/servertreemodel/shvbrokernodeitem.cpp | 58 ++++++++++++++----- .../src/servertreemodel/shvbrokernodeitem.h | 7 ++- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/3rdparty/libshv b/3rdparty/libshv index a6a3e3a..55f7ada 160000 --- a/3rdparty/libshv +++ b/3rdparty/libshv @@ -1 +1 @@ -Subproject commit a6a3e3a19544d07e31e4a725d9e871375263ac5f +Subproject commit 55f7ada2d41d006be32a910ff67d51c7165a625c diff --git a/3rdparty/necrolog b/3rdparty/necrolog index 91f80ca..f7d3fcc 160000 --- a/3rdparty/necrolog +++ b/3rdparty/necrolog @@ -1 +1 @@ -Subproject commit 91f80caf70febcfde95e7f518d5975f841a1b171 +Subproject commit f7d3fcc7dc2c2511763755d170fd2c6326f14122 diff --git a/shvspy/src/appversion.h b/shvspy/src/appversion.h index 98a25e0..9ca13fa 100644 --- a/shvspy/src/appversion.h +++ b/shvspy/src/appversion.h @@ -1,4 +1,4 @@ #pragma once -#define APP_VERSION "1.5.3" +#define APP_VERSION "1.6.0" diff --git a/shvspy/src/servertreemodel/shvbrokernodeitem.cpp b/shvspy/src/servertreemodel/shvbrokernodeitem.cpp index 4954855..cb455cf 100644 --- a/shvspy/src/servertreemodel/shvbrokernodeitem.cpp +++ b/shvspy/src/servertreemodel/shvbrokernodeitem.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -119,7 +120,6 @@ void ShvBrokerNodeItem::setSubscriptionList(const QVariantList &subs) void ShvBrokerNodeItem::addSubscription(const std::string &shv_path, const std::string &method) { int rqid = callSubscribe(shv_path, method); - auto *cb = new shv::iotqt::rpc::RpcResponseCallBack(m_rpcConnection, rqid, this); cb->start(5000, this, [this, shv_path, method](const cp::RpcResponse &resp) { if(resp.isError() || (resp.result() == false)){ @@ -442,17 +442,29 @@ ShvNodeItem* ShvBrokerNodeItem::findNode(const std::string &path_) return ret; } -int ShvBrokerNodeItem::callSubscribe(const std::string &shv_path, std::string method) +int ShvBrokerNodeItem::callSubscribe(const std::string &shv_path, const std::string &method) { shv::iotqt::rpc::ClientConnection *cc = clientConnection(); - int rqid = cc->callMethodSubscribe(shv_path, method); + int rqid; + if(m_shvApiVersion == ShvApiVersion::V3) { + rqid = cc->callMethodSubscribe_v3(shv_path, method); + } + else { + rqid = cc->callMethodSubscribe(shv_path, method); + }; return rqid; } -int ShvBrokerNodeItem::callUnsubscribe(const std::string &shv_path, std::string method) +int ShvBrokerNodeItem::callUnsubscribe(const std::string &shv_path, const std::string &method) { shv::iotqt::rpc::ClientConnection *cc = clientConnection(); - int rqid = cc->callMethodUnsubscribe(shv_path, method); + int rqid; + if(m_shvApiVersion == ShvApiVersion::V3) { + rqid = cc->callMethodUnsubscribe_v3(shv_path, method); + } + else { + rqid = cc->callMethodUnsubscribe(shv_path, method); + }; return rqid; } @@ -551,18 +563,36 @@ void ShvBrokerNodeItem::onRpcMessageReceived(const shv::chainpack::RpcMessage &m void ShvBrokerNodeItem::createSubscriptions() { - QMetaEnum meta_sub = QMetaEnum::fromType(); - QVariant v = m_brokerPropeties.value(SUBSCRIPTIONS); - if(v.isValid()) { - QVariantList subs = v.toList(); + using namespace shv::iotqt::rpc; + using namespace shv::chainpack; + shv::iotqt::rpc::ClientConnection *cc = clientConnection(); + auto *rpc = RpcCall::create(cc); + rpc->setShvPath(Rpc::DIR_APP_BROKER_CURRENTCLIENT) + ->setMethod(Rpc::METH_DIR) + ->setParams(Rpc::METH_DIR); + connect(rpc, &RpcCall::maybeResult, this, [this](const auto &, const auto &err) { + if(err.isValid()) { + //shvError() << "Call check SHV API version error:" << err.toString(); + m_shvApiVersion = ShvApiVersion::V2; + } + else { + m_shvApiVersion = ShvApiVersion::V3; + } + + QMetaEnum meta_sub = QMetaEnum::fromType(); + QVariant v = m_brokerPropeties.value(SUBSCRIPTIONS); + if(v.isValid()) { + QVariantList subs = v.toList(); - for (const auto & sub : subs) { - QVariantMap s = sub.toMap(); + for (const auto & sub : subs) { + QVariantMap s = sub.toMap(); - if (s.value(meta_sub.valueToKey(SubscriptionItem::IsEnabled)).toBool()){ - callSubscribe(s.value(meta_sub.valueToKey(SubscriptionItem::Path)).toString().toStdString(), s.value(meta_sub.valueToKey(SubscriptionItem::Method)).toString().toStdString()); + if (s.value(meta_sub.valueToKey(SubscriptionItem::IsEnabled)).toBool()){ + callSubscribe(s.value(meta_sub.valueToKey(SubscriptionItem::Path)).toString().toStdString(), s.value(meta_sub.valueToKey(SubscriptionItem::Method)).toString().toStdString()); + } } } - } + }); + rpc->start(); } diff --git a/shvspy/src/servertreemodel/shvbrokernodeitem.h b/shvspy/src/servertreemodel/shvbrokernodeitem.h index 927dae4..3d341c2 100644 --- a/shvspy/src/servertreemodel/shvbrokernodeitem.h +++ b/shvspy/src/servertreemodel/shvbrokernodeitem.h @@ -58,9 +58,8 @@ class ShvBrokerNodeItem : public ShvNodeItem void onBrokerLoginError(const QString &err); void onRpcMessageReceived(const shv::chainpack::RpcMessage &msg); void createSubscriptions(); - int callSubscribe(const std::string &shv_path, std::string method); - int callUnsubscribe(const std::string &shv_path, std::string method); - + int callSubscribe(const std::string &shv_path, const std::string &method); + int callUnsubscribe(const std::string &shv_path, const std::string &method); private: int m_brokerId; QVariantMap m_brokerPropeties; @@ -70,5 +69,7 @@ class ShvBrokerNodeItem : public ShvNodeItem std::map m_runningRpcRequests; std::string m_shvRoot; int m_brokerLoginErrorCount = 0; + enum class ShvApiVersion {V2, V3}; + ShvApiVersion m_shvApiVersion = ShvApiVersion::V2; };