From f445eeba40574358088a0a778888bea3e9dd2cd4 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 21 Nov 2023 19:57:22 +0000 Subject: [PATCH] add exclusion list --- .../TelematicBridgePlugin/manifest.json | 26 ++++------ .../src/TelematicBridgePlugin.cpp | 5 +- .../src/TelematicBridgePlugin.h | 1 + .../src/TelematicUnit.cpp | 47 +++++-------------- .../TelematicBridgePlugin/src/TelematicUnit.h | 28 ++++++----- 5 files changed, 38 insertions(+), 69 deletions(-) diff --git a/src/v2i-hub/TelematicBridgePlugin/manifest.json b/src/v2i-hub/TelematicBridgePlugin/manifest.json index 93f722efe..52991c070 100644 --- a/src/v2i-hub/TelematicBridgePlugin/manifest.json +++ b/src/v2i-hub/TelematicBridgePlugin/manifest.json @@ -12,29 +12,15 @@ "default": "INFO", "description": "The log level for this plugin" }, - { - "key": "NATSConnectionTimeOut", - "default": "1000", - "description": "The log level for this plugin" - - }, - { - "key": "NATSConnectionAttempts", - "default": "10", - "description": "The log level for this plugin" - - }, { "key": "NATSUrl", "default": "nats://127.0.0.1:4222", - "description": "The log level for this plugin" - + "description": "The log level for this plugin" }, { "key": "UnitId", "default": "v2xhub_id", - "description": "The log level for this plugin" - + "description": "The log level for this plugin" }, { "key": "UnitName", @@ -45,8 +31,12 @@ { "key": "UnitType", "default": "Infrastructure", - "description": "The log level for this plugin" - + "description": "The log level for this plugin" + }, + { + "key": "TopicExclusionList", + "default": "System_Alive_CommandPlugin,System_Alive_CARMAStreetsPlugin", + "description": "The list of topics are excluded from the available topic list." } ] } \ No newline at end of file diff --git a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.cpp b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.cpp index bbd6cb698..713a0e611 100644 --- a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.cpp +++ b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.cpp @@ -41,12 +41,11 @@ namespace TelematicBridge void TelematicBridgePlugin::UpdateConfigSettings() { lock_guard lock(_configMutex); - GetConfigValue("NATSConnectionTimeOut", _natsConnTimeOut); - GetConfigValue("NATSConnectionAttempts", _natsConnAttempts); GetConfigValue("NATSUrl", _natsURL); GetConfigValue("UnitId", _unitId); GetConfigValue("UnitName", _unitName); GetConfigValue("UnitType", _unitType); + GetConfigValue("TopicExclusionList", _excludedTopics); unit_st unit; unit.unitId = _unitId; unit.unitName = _unitName; @@ -54,6 +53,7 @@ namespace TelematicBridge if (_telematicUnitPtr) { _telematicUnitPtr->setUnit(unit); + _telematicUnitPtr->excludedTopics = _excludedTopics; } } @@ -72,7 +72,6 @@ namespace TelematicBridge void TelematicBridgePlugin::OnConfigChanged(const char *key, const char *value) { - PLOG(logDEBUG1) << "OnConfigChanged called"; PluginClient::OnConfigChanged(key, value); UpdateConfigSettings(); } diff --git a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.h b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.h index 33d381b9e..9f78c35be 100644 --- a/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.h +++ b/src/v2i-hub/TelematicBridgePlugin/src/TelematicBridgePlugin.h @@ -21,6 +21,7 @@ namespace TelematicBridge string _unitType; string _unitName; string _natsURL; + string _excludedTopics; mutex _configMutex; void OnMessageReceived(IvpMessage *msg); diff --git a/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.cpp b/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.cpp index 44a3151bc..e1d0c4ecd 100644 --- a/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.cpp +++ b/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.cpp @@ -2,41 +2,15 @@ namespace TelematicBridge { - TelematicUnit::TelematicUnit() - { - // natsStatus s; - // s = natsOptions_SetTimeout(_opts, natsConnTimeout); - // if (s == NATS_OK) - // { - // natsOptions_SetMaxReconnect(_opts, natsConnAttempts); - // } - - // if (s == NATS_OK) - // { - // s = natsOptions_SetRetryOnFailedConnect(_opts, true, nullptr, nullptr); - // } - - // if (s == NATS_OK) - // { - // s = natsOptions_SetURL(_opts, natsURL); - // } - - // if (s != NATS_OK) - // { - // nats_PrintLastErrorStack(stderr); - // throw TelematicBridgeException(natsStatus_GetText(s)); - // } - } - void TelematicUnit::connect(const string &natsURL, uint16_t natsConnAttempts, uint16_t natsConnTimeout) { - // Reset connection and registration status based on the latest config update + // Reset connection and registration status _isConnected = false; _isRegistered = false; PLOG(logINFO) << "Trying to connect to " << natsURL << " attempts: " << natsConnAttempts << ", nats connect timeout: " << natsConnTimeout; // auto s = natsConnection_Connect(&_conn, _opts); auto s = natsConnection_ConnectTo(&_conn, natsURL.c_str()); - PLOG(logINFO) << "natsConnection_Connect returned: " << natsStatus_GetText(s); + PLOG(logINFO) << "NATS connection returned: " << natsStatus_GetText(s); if (s == NATS_OK) { _isConnected = true; @@ -45,8 +19,6 @@ namespace TelematicBridge else { _isConnected = false; - nats_PrintLastErrorStack(stderr); - printf("NATS Connection Error: %u - %s\n", s, natsStatus_GetText(s)); throw TelematicBridgeException(natsStatus_GetText(s)); } } @@ -109,7 +81,6 @@ namespace TelematicBridge else { _isRegistered = false; - nats_PrintLastErrorStack(stderr); PLOG(logERROR) << "NATS regsiter Error: " << s << "-" << natsStatus_GetText(s); } natsMsg_Destroy(reply); @@ -172,7 +143,6 @@ namespace TelematicBridge } else { - nats_PrintLastErrorStack(stderr); throw TelematicBridgeException(natsStatus_GetText(s)); } } @@ -201,9 +171,12 @@ namespace TelematicBridge Json::Value topics; for (const auto &topic : obj->availableTopics) { - Json::Value topicJson; - topicJson["name"] = topic; - topics.append(topicJson); + if (!boost::icontains(obj->excludedTopics, topic)) + { + Json::Value topicJson; + topicJson["name"] = topic; + topics.append(topicJson); + } } payload["topics"] = topics; Json::FastWriter fasterWirter; @@ -231,6 +204,10 @@ namespace TelematicBridge } if (root["topics"].isArray()) { + // clear old selected topics + obj->selectedTopics.clear(); + + // update selected topics with selected topics from latest request for (auto itr = root["topics"].begin(); itr != root["topics"].end(); itr++) { obj->selectedTopics.push_back(itr->asString()); diff --git a/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.h b/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.h index 8902daa0d..6ff2be01f 100644 --- a/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.h +++ b/src/v2i-hub/TelematicBridgePlugin/src/TelematicUnit.h @@ -8,6 +8,7 @@ #include "ThreadTimer.h" #include "TelematicBridgeException.h" #include +#include using namespace std; using namespace tmx::utils; @@ -27,17 +28,18 @@ namespace TelematicBridge public: mutex _unitMutex; mutex _availableTopicsMutex; - atomic _isConnected {false}; + atomic _isConnected{false}; atomic _isRegistered{false}; - unit_st _unit; // Global variable to store the unit information - vector availableTopics; // Global variable to store available topics - vector selectedTopics; // Global variable to store selected topics confirmed by users - static CONSTEXPR const char *AVAILABLE_TOPICS = ".available_topics"; // NATS subject to pub/sub available topics - static CONSTEXPR const char *REGISTER_UNIT_TOPIC = "*.register_unit"; // NATS subject to pub/sub registering unit - static CONSTEXPR const char *PUBLISH_TOPICS = ".publish_topics"; // NATS subject to publish data stream - static CONSTEXPR const char *CHECK_STATUS = ".check_status"; // NATS subject to pub/sub checking unit status + unit_st _unit; // Global variable to store the unit information + vector availableTopics; // Global variable to store available topics + string excludedTopics; // Global variable to store topics that are excluded by the users + vector selectedTopics; // Global variable to store selected topics confirmed by users + static CONSTEXPR const char *AVAILABLE_TOPICS = ".available_topics"; // NATS subject to pub/sub available topics + static CONSTEXPR const char *REGISTER_UNIT_TOPIC = "*.register_unit"; // NATS subject to pub/sub registering unit + static CONSTEXPR const char *PUBLISH_TOPICS = ".publish_topics"; // NATS subject to publish data stream + static CONSTEXPR const char *CHECK_STATUS = ".check_status"; // NATS subject to pub/sub checking unit status unique_ptr _natsRegisterTh; - natsConnection *_conn = nullptr; // Global NATS connection object + natsConnection *_conn = nullptr; // Global NATS connection object natsOptions *_opts = nullptr; natsSubscription *subAvailableTopic = nullptr; natsSubscription *subSelectedTopic = nullptr; @@ -50,9 +52,9 @@ namespace TelematicBridge /** *@brief Construct telematic unit */ - explicit TelematicUnit(); + explicit TelematicUnit() = default; /** - * @brief A function for telematic unit to connect to NATS server. Throw exception is connection failed. * + * @brief A function for telematic unit to connect to NATS server. Throw exception is connection failed. * * @param const string NATS server URL * @param uint16_t The numbers of attempts to make connections to NATS server * @param uint16_t The timeout for between connection attempts @@ -79,8 +81,8 @@ namespace TelematicBridge * @brief Check if the given topic is inside the selectedTopics list * @param string A topic to check for existence * @return boolean indicator whether the input topic eixst. - */ - bool inSelectedTopics(const string& topic); + */ + bool inSelectedTopics(const string &topic); /** * @brief A NATS requestor for telematic unit to send register request to NATS server.