control: Add fan objects to their zones

Load the fan configurations and add each fan to its configured zone.

Change-Id: Ib798db9f272484c60b6eadd58e6545bd704b9573
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/manager.cpp b/control/json/manager.cpp
index 804b15e..78aac29 100644
--- a/control/json/manager.cpp
+++ b/control/json/manager.cpp
@@ -15,6 +15,7 @@
  */
 #include "manager.hpp"
 
+#include "fan.hpp"
 #include "json_config.hpp"
 #include "profile.hpp"
 #include "zone.hpp"
@@ -23,6 +24,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdeventplus/event.hpp>
 
+#include <algorithm>
 #include <filesystem>
 #include <vector>
 
@@ -49,6 +51,21 @@
 
     // Load the zone configurations
     _zones = getConfig<Zone>(bus);
+
+    // Load the fan configurations and move each fan into its zone
+    auto fans = getConfig<Fan>(bus);
+    for (auto& fan : fans)
+    {
+        auto itZone =
+            std::find_if(_zones.begin(), _zones.end(),
+                         [&fanZone = fan.second->getZone()](const auto& zone) {
+                             return fanZone == zone.second->getName();
+                         });
+        if (itZone != _zones.end())
+        {
+            itZone->second->addFan(std::move(fan.second));
+        }
+    }
 }
 
 const std::vector<std::string>& Manager::getActiveProfiles()
diff --git a/control/json/zone.cpp b/control/json/zone.cpp
index 33ab048..11b7814 100644
--- a/control/json/zone.cpp
+++ b/control/json/zone.cpp
@@ -16,6 +16,7 @@
 #include "zone.hpp"
 
 #include "../zone.hpp"
+#include "fan.hpp"
 #include "functor.hpp"
 #include "handlers.hpp"
 #include "types.hpp"
@@ -66,6 +67,11 @@
     }
 }
 
+void Zone::addFan(std::unique_ptr<Fan> fan)
+{
+    _fans.emplace_back(std::move(fan));
+}
+
 void Zone::setFullSpeed(const json& jsonObj)
 {
     if (!jsonObj.contains("full_speed"))
diff --git a/control/json/zone.hpp b/control/json/zone.hpp
index 4f7742c..2ed3166 100644
--- a/control/json/zone.hpp
+++ b/control/json/zone.hpp
@@ -16,6 +16,7 @@
 #pragma once
 
 #include "config_base.hpp"
+#include "fan.hpp"
 #include "types.hpp"
 
 #include <nlohmann/json.hpp>
@@ -148,6 +149,17 @@
         return _zoneHandlers;
     }
 
+    /**
+     * @brief Add a fan object to the zone
+     *
+     * @param[in] fan - Unique pointer to a fan object that will be moved into
+     * the zone
+     *
+     * Adds a fan object to the list of fans that make up the zone by moving the
+     * fan object into the list.
+     */
+    void addFan(std::unique_ptr<Fan> fan);
+
   private:
     /* The zone's full speed value for fans */
     uint64_t _fullSpeed;
@@ -171,6 +183,9 @@
     static const std::map<std::string, std::map<std::string, propHandler>>
         _intfPropHandlers;
 
+    /* List of fans included in this zone */
+    std::vector<std::unique_ptr<Fan>> _fans;
+
     /**
      * @brief Parse and set the zone's full speed value
      *