use async context more in health monitor

Push the async context into the HealthMonitor class implementation.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I465c9c839016416e683415dd235e69817b7e1ba6
diff --git a/health_monitor.cpp b/health_monitor.cpp
index b839ae7..0c732b0 100644
--- a/health_monitor.cpp
+++ b/health_monitor.cpp
@@ -10,12 +10,12 @@
 
 using namespace phosphor::health::utils;
 
-void HealthMonitor::create()
+auto HealthMonitor::startup() -> sdbusplus::async::task<>
 {
     info("Creating Health Monitor with config size {SIZE}", "SIZE",
          configs.size());
     constexpr auto BMCInventoryItem = "xyz.openbmc_project.Inventory.Item.Bmc";
-    auto bmcPaths = findPaths(bus, BMCInventoryItem);
+    auto bmcPaths = findPaths(ctx.get_bus(), BMCInventoryItem);
 
     for (auto& [type, collectionConfig] : configs)
     {
@@ -23,18 +23,24 @@
              std::to_underlying(type));
         collections[type] =
             std::make_unique<CollectionIntf::HealthMetricCollection>(
-                bus, type, collectionConfig, bmcPaths);
+                ctx.get_bus(), type, collectionConfig, bmcPaths);
     }
+
+    co_await run();
 }
 
-void HealthMonitor::run()
+auto HealthMonitor::run() -> sdbusplus::async::task<>
 {
     info("Running Health Monitor");
-    for (auto& [type, collection] : collections)
+    while (!ctx.stop_requested())
     {
-        debug("Reading Health Metric Collection for {TYPE}", "TYPE",
-              std::to_underlying(type));
-        collection->read();
+        for (auto& [type, collection] : collections)
+        {
+            debug("Reading Health Metric Collection for {TYPE}", "TYPE",
+                  std::to_underlying(type));
+            collection->read();
+        }
+        co_await sdbusplus::async::sleep_for(ctx, std::chrono::seconds(5));
     }
 }
 
@@ -50,17 +56,9 @@
     constexpr auto healthMonitorServiceName = "xyz.openbmc_project.HealthMon";
 
     info("Creating health monitor");
-    HealthMonitor healthMonitor{ctx.get_bus()};
+    HealthMonitor healthMonitor{ctx};
     ctx.request_name(healthMonitorServiceName);
 
-    ctx.spawn([&]() -> sdbusplus::async::task<> {
-        while (!ctx.stop_requested())
-        {
-            healthMonitor.run();
-            co_await sdbusplus::async::sleep_for(ctx, std::chrono::seconds(5));
-        }
-    }());
-
     ctx.run();
     return 0;
 }
diff --git a/health_monitor.hpp b/health_monitor.hpp
index 31d16ba..29c6247 100644
--- a/health_monitor.hpp
+++ b/health_monitor.hpp
@@ -2,6 +2,8 @@
 
 #include "health_metric_collection.hpp"
 
+#include <sdbusplus/async.hpp>
+
 #include <unordered_map>
 
 namespace phosphor::health::monitor
@@ -14,23 +16,24 @@
   public:
     HealthMonitor() = delete;
 
-    HealthMonitor(sdbusplus::bus_t& bus) :
-        bus(bus), configs(ConfigIntf::getHealthMetricConfigs())
+    explicit HealthMonitor(sdbusplus::async::context& ctx) :
+        ctx(ctx), configs(ConfigIntf::getHealthMetricConfigs())
     {
-        create();
+        ctx.spawn(startup());
     }
 
-    /** @brief Run the health monitor */
-    void run();
-
   private:
+    /** @brief Setup and run a new health monitor object */
+    auto startup() -> sdbusplus::async::task<>;
+    /** @brief Run the health monitor */
+    auto run() -> sdbusplus::async::task<>;
+
     using map_t = std::unordered_map<
         MetricIntf::Type,
         std::unique_ptr<CollectionIntf::HealthMetricCollection>>;
-    /** @brief Create a new health monitor object */
-    void create();
-    /** @brief D-Bus bus connection */
-    sdbusplus::bus_t& bus;
+
+    /** @brief D-Bus context */
+    sdbusplus::async::context& ctx;
     /** @brief Health metric configs */
     ConfigIntf::HealthMetric::map_t configs;
     map_t collections;