control: Create config key check function

Move the configuration/profile check to include groups into events to a
function that can be used by other configurations that also need to do
this check. Update the check to include a fan within a zone to use this
function.

Change-Id: I71259ce0fcd61917a5d7a5bb9ac2d1906130389e
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/event.cpp b/control/json/event.cpp
index ca94d9a..b5e986d 100644
--- a/control/json/event.cpp
+++ b/control/json/event.cpp
@@ -109,43 +109,11 @@
             value = getJsonValue(group["property"]["value"]);
         }
 
-        configKey key =
+        configKey eventProfile =
             std::make_pair(group["name"].get<std::string>(), _profiles);
-        auto grpEntry =
-            std::find_if(groups.begin(), groups.end(), [&key](const auto& grp) {
-                if (grp.first.first != key.first)
-                {
-                    return false;
-                }
-                // Groups with no profiles specified can be used in any event
-                if (grp.first.second.empty())
-                {
-                    return true;
-                }
-                else
-                {
-                    // Groups with profiles must have one match in the event's
-                    // profiles(and they must be an active profile) to be used
-                    // in the event
-                    return std::any_of(
-                        grp.first.second.begin(), grp.first.second.end(),
-                        [&key](const auto& grpProfile) {
-                            return std::any_of(
-                                key.second.begin(), key.second.end(),
-                                [&grpProfile](const auto& eventProfile) {
-                                    if (grpProfile != eventProfile)
-                                    {
-                                        return false;
-                                    }
-                                    auto activeProfs =
-                                        Manager::getActiveProfiles();
-                                    return std::find(activeProfs.begin(),
-                                                     activeProfs.end(),
-                                                     grpProfile) !=
-                                           activeProfs.end();
-                                });
-                        });
-                }
+        auto grpEntry = std::find_if(
+            groups.begin(), groups.end(), [&eventProfile](const auto& grp) {
+                return Manager::inConfig(grp.first, eventProfile);
             });
         if (grpEntry != groups.end())
         {
diff --git a/control/json/manager.cpp b/control/json/manager.cpp
index b1b5f91..64921fc 100644
--- a/control/json/manager.cpp
+++ b/control/json/manager.cpp
@@ -74,11 +74,12 @@
     auto fans = getConfig<Fan>(false, bus, 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();
-                         });
+        configKey fanProfile =
+            std::make_pair(fan.second->getZone(), fan.first.second);
+        auto itZone = std::find_if(
+            _zones.begin(), _zones.end(), [&fanProfile](const auto& zone) {
+                return Manager::inConfig(fanProfile, zone.first);
+            });
         if (itZone != _zones.end())
         {
             if (itZone->second->getTarget() != fan.second->getTarget() &&
@@ -105,6 +106,40 @@
     return _activeProfiles;
 }
 
+bool Manager::inConfig(const configKey& input, const configKey& comp)
+{
+    // Config names dont match, do not include in config
+    if (input.first != comp.first)
+    {
+        return false;
+    }
+    // No profiles specified by input config, can be used in any config
+    if (input.second.empty())
+    {
+        return true;
+    }
+    else
+    {
+        // Profiles must have one match in the other's profiles(and they must be
+        // an active profile) to be used in the config
+        return std::any_of(
+            input.second.begin(), input.second.end(),
+            [&comp](const auto& lProfile) {
+                return std::any_of(
+                    comp.second.begin(), comp.second.end(),
+                    [&lProfile](const auto& rProfile) {
+                        if (lProfile != rProfile)
+                        {
+                            return false;
+                        }
+                        auto activeProfs = getActiveProfiles();
+                        return std::find(activeProfs.begin(), activeProfs.end(),
+                                         lProfile) != activeProfs.end();
+                    });
+            });
+    }
+}
+
 bool Manager::hasOwner(const std::string& path, const std::string& intf)
 {
     auto itServ = _servTree.find(path);
diff --git a/control/json/manager.hpp b/control/json/manager.hpp
index 8ac48d1..d6a4499 100644
--- a/control/json/manager.hpp
+++ b/control/json/manager.hpp
@@ -168,6 +168,17 @@
     }
 
     /**
+     * @brief Check if the given input configuration key matches with another
+     * configuration key that it's to be included in
+     *
+     * @param[in] input - Config key to be included in another config object
+     * @param[in] comp - Config key of the config object to compare with
+     *
+     * @return Whether the configuration object should be included
+     */
+    static bool inConfig(const configKey& input, const configKey& comp);
+
+    /**
      * @brief Check if the given path and inteface is owned by a dbus service
      *
      * @param[in] path - Dbus object path