Move event init steps at startup to functions

Change-Id: I8a281935368cd705658d489c7c7af59b8dde7e4d
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/zone.cpp b/control/zone.cpp
index 17e0089..f5099c1 100644
--- a/control/zone.cpp
+++ b/control/zone.cpp
@@ -54,53 +54,7 @@
     // Do not enable set speed events when in init mode
     if (mode != Mode::init)
     {
-        // Setup signal trigger for set speed events
-        for (auto& event : std::get<setSpeedEventsPos>(def))
-        {
-            // Get the current value for each property
-            for (auto& entry : std::get<groupPos>(event))
-            {
-                try
-                {
-                    PropertyVariantType property;
-                    getProperty(_bus,
-                                entry.first,
-                                std::get<intfPos>(entry.second),
-                                std::get<propPos>(entry.second),
-                                property);
-                    setPropertyValue(entry.first.c_str(),
-                                     std::get<intfPos>(entry.second).c_str(),
-                                     std::get<propPos>(entry.second).c_str(),
-                                     property);
-                }
-                catch (const std::exception& e)
-                {
-                    log<level::ERR>(e.what());
-                }
-            }
-            // Setup signal matches for property change events
-            for (auto& prop : std::get<propChangeListPos>(event))
-            {
-                _signalEvents.emplace_back(
-                        std::make_unique<EventData>(
-                                EventData
-                                {
-                                    std::get<groupPos>(event),
-                                    std::get<handlerObjPos>(prop),
-                                    std::get<actionPos>(event)
-                                }));
-                _matches.emplace_back(
-                        bus,
-                        std::get<signaturePos>(prop).c_str(),
-                        std::bind(std::mem_fn(&Zone::handleEvent),
-                                  this,
-                                  std::placeholders::_1,
-                                  _signalEvents.back().get()));
-            }
-            // Run action function for initial event state
-            std::get<actionPos>(event)(*this,
-                                       std::get<groupPos>(event));
-        }
+        initEvents(def);
         // Start timer for fan speed decreases
         if (!_decTimer.running())
         {
@@ -197,6 +151,54 @@
     // Decrease timer is restarted since its repeating
 }
 
+void Zone::initEvents(const ZoneDefinition& def)
+{
+    // Setup signal trigger for set speed events
+    for (auto& event : std::get<setSpeedEventsPos>(def))
+    {
+        // Get the current value for each property
+        for (auto& entry : std::get<groupPos>(event))
+        {
+            refreshProperty(_bus,
+                            entry.first,
+                            std::get<intfPos>(entry.second),
+                            std::get<propPos>(entry.second));
+        }
+        // Setup signal matches for property change events
+        for (auto& prop : std::get<propChangeListPos>(event))
+        {
+            _signalEvents.emplace_back(
+                    std::make_unique<EventData>(
+                            EventData
+                            {
+                                std::get<groupPos>(event),
+                                std::get<handlerObjPos>(prop),
+                                std::get<actionPos>(event)
+                            }));
+            _matches.emplace_back(
+                    _bus,
+                    std::get<signaturePos>(prop).c_str(),
+                    std::bind(std::mem_fn(&Zone::handleEvent),
+                              this,
+                              std::placeholders::_1,
+                              _signalEvents.back().get()));
+        }
+        // Run action function for initial event state
+        std::get<actionPos>(event)(*this,
+                                   std::get<groupPos>(event));
+    }
+}
+
+void Zone::refreshProperty(sdbusplus::bus::bus& bus,
+                           const std::string& path,
+                           const std::string& iface,
+                           const std::string& prop)
+{
+    PropertyVariantType property;
+    getProperty(_bus, path, iface, prop, property);
+    setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property);
+}
+
 void Zone::getProperty(sdbusplus::bus::bus& bus,
                        const std::string& path,
                        const std::string& iface,
diff --git a/control/zone.hpp b/control/zone.hpp
index 30de150..bdde64a 100644
--- a/control/zone.hpp
+++ b/control/zone.hpp
@@ -309,6 +309,26 @@
         std::vector<sdbusplus::server::match::match> _matches;
 
         /**
+         * @brief Initialize all the set speed event properties and actions
+         *
+         * @param[in] def - zone definition containing set speed events
+         */
+        void initEvents(const ZoneDefinition& def);
+
+        /**
+         * @brief Refresh the given property's cached value
+         *
+         * @param[in] bus - the bus to use
+         * @param[in] path - the dbus path name
+         * @param[in] iface - the dbus interface name
+         * @param[in] prop - the property name
+         */
+        void refreshProperty(sdbusplus::bus::bus& bus,
+                             const std::string& path,
+                             const std::string& iface,
+                             const std::string& prop);
+
+        /**
          * @brief Get a property value from the path/interface given
          *
          * @param[in] bus - the bus to use