From f095d0aff59539ffa54a02a3c44b9d43d38d3fac Mon Sep 17 00:00:00 2001
From: Fabian Peller <fabian.peller-konrad@kit.edu>
Date: Wed, 10 May 2023 12:58:35 +0200
Subject: [PATCH] add heartbeat component plugin user

---
 .../HeartbeatComponentPlugin.cpp              | 57 +++++++++++++------
 .../HeartbeatComponentPlugin.h                | 16 ++++++
 2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.cpp b/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.cpp
index 41b48bdff..ceafcbf56 100644
--- a/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.cpp
+++ b/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.cpp
@@ -7,50 +7,58 @@
 
 namespace armarx::plugins
 {
-    void HeartbeatComponentPlugin::configureHeartbeatChannel(const std::string& channel,
-                                                             const RobotHealthHeartbeatArgs& args)
+    void
+    HeartbeatComponentPlugin::configureHeartbeatChannel(const std::string& channel,
+                                                        const RobotHealthHeartbeatArgs& args)
     {
         channelHeartbeatConfig.emplace(channel, args);
     }
 
-    void HeartbeatComponentPlugin::configureHeartbeatChannel(const std::string& channel, const std::string& message)
+    void
+    HeartbeatComponentPlugin::configureHeartbeatChannel(const std::string& channel,
+                                                        const std::string& message)
     {
         auto args = heartbeatArgs;
         args.message = message;
         configureHeartbeatChannel(channel, args);
     }
 
-    void HeartbeatComponentPlugin::heartbeat()
+    void
+    HeartbeatComponentPlugin::heartbeat()
     {
 
         if (robotHealthTopic)
         {
             robotHealthTopic->heartbeat(componentName, heartbeatArgs);
-        } else
+        }
+        else
         {
             ARMARX_WARNING << "No robot health topic available!";
         }
     }
 
-    void HeartbeatComponentPlugin::heartbeat(const std::string& channel)
+    void
+    HeartbeatComponentPlugin::heartbeat(const std::string& channel)
     {
         const auto argsIt = channelHeartbeatConfig.find(channel);
-        ARMARX_CHECK(argsIt != channelHeartbeatConfig.end()) << "heartbeat() called for unknown channel '" << channel
-                                                             << "'."
-                                                             << "You must register the config using configureHeartbeatChannel(channel) first!";
+        ARMARX_CHECK(argsIt != channelHeartbeatConfig.end())
+            << "heartbeat() called for unknown channel '" << channel << "'."
+            << "You must register the config using configureHeartbeatChannel(channel) first!";
 
         const auto& args = argsIt->second;
 
         if (robotHealthTopic)
         {
             robotHealthTopic->heartbeat(componentName + "_" + channel, args);
-        } else
+        }
+        else
         {
             ARMARX_WARNING << "No robot health topic available!";
         }
     }
 
-    void HeartbeatComponentPlugin::preOnInitComponent()
+    void
+    HeartbeatComponentPlugin::preOnInitComponent()
     {
         //        if (topicName.empty())
         //        {
@@ -59,35 +67,50 @@ namespace armarx::plugins
         //        parent<Component>().offeringTopic(topicName);
     }
 
-    void HeartbeatComponentPlugin::postOnInitComponent()
+    void
+    HeartbeatComponentPlugin::postOnInitComponent()
     {
     }
 
-    void HeartbeatComponentPlugin::preOnConnectComponent()
+    void
+    HeartbeatComponentPlugin::preOnConnectComponent()
     {
         //        robotHealthTopic = parent<Component>().getTopic<RobotHealthInterfacePrx>(topicName);
         componentName = parent<Component>().getName();
     }
 
-    void HeartbeatComponentPlugin::postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties)
+    void
+    HeartbeatComponentPlugin::postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties)
     {
         if (!properties->hasDefinition(makePropertyName(topicPropertyName)))
         {
-            properties->topic(robotHealthTopic, topicName, topicPropertyName,
+            properties->topic(robotHealthTopic,
+                              topicName,
+                              topicPropertyName,
                               "Name of the topic the DebugObserver listens on");
         }
 
         if (not properties->hasDefinition(makePropertyName(maximumCycleTimeWarningMSPropertyName)))
         {
-            properties->required(heartbeatArgs.maximumCycleTimeWarningMS, maximumCycleTimeWarningMSPropertyName,
+            properties->required(heartbeatArgs.maximumCycleTimeWarningMS,
+                                 maximumCycleTimeWarningMSPropertyName,
                                  "maximum cycle time before warning is emitted");
         }
 
         if (not properties->hasDefinition(makePropertyName(maximumCycleTimeErrorMSPropertyName)))
         {
-            properties->required(heartbeatArgs.maximumCycleTimeErrorMS, maximumCycleTimeErrorMSPropertyName,
+            properties->required(heartbeatArgs.maximumCycleTimeErrorMS,
+                                 maximumCycleTimeErrorMSPropertyName,
                                  "maximum cycle time before error is emitted");
         }
     }
 
 } // namespace armarx::plugins
+
+namespace armarx
+{
+    HeartbeatComponentPluginUser::HeartbeatComponentPluginUser()
+    {
+        addPlugin(heartbeat);
+    }
+} // namespace armarx
diff --git a/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.h b/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.h
index bbefa079c..26d4d9537 100644
--- a/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.h
+++ b/source/RobotAPI/libraries/RobotAPIComponentPlugins/HeartbeatComponentPlugin.h
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <ArmarXCore/core/ComponentPlugin.h>
+#include <ArmarXCore/core/ManagedIceObject.h>
 
 #include <RobotAPI/interface/components/RobotHealthInterface.h>
 
@@ -95,3 +96,18 @@ namespace armarx::plugins
         std::unordered_map<std::string, RobotHealthHeartbeatArgs> channelHeartbeatConfig;
     };
 } // namespace armarx::plugins
+
+namespace armarx
+{
+    /**
+     * @brief Provides a ready-to-use ArViz client `arviz` as member variable.
+     */
+    class HeartbeatComponentPluginUser : virtual public ManagedIceObject
+    {
+    public:
+        HeartbeatComponentPluginUser();
+
+    protected:
+        armarx::plugins::HeartbeatComponentPlugin* heartbeat = nullptr;
+    };
+} // namespace armarx
-- 
GitLab