mainloop: add helper methods for sensor removal

Add a helper method that will remove from dbus sensors that are marked
for removal and another helper method that will attempt to add sensors
marked for removal back.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Iecb96045cc63d93f2896af9f6a8c2672ca8fcbb2
diff --git a/mainloop.cpp b/mainloop.cpp
index 09c148b..469a8d7 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -477,18 +477,31 @@
         }
     }
 
+    removeSensors();
+
+#ifndef REMOVE_ON_FAIL
+    addDroppedSensors();
+#endif
+}
+
+void MainLoop::removeSensors()
+{
     // Remove any sensors marked for removal
     for (const auto& i : _rmSensors)
     {
         // Remove sensor object from dbus using emit_object_removed()
         auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
         auto& objPath = std::get<std::string>(objInfo);
+
         _bus.emit_object_removed(objPath.c_str());
+
         // Erase sensor object info
         _state.erase(i.first);
     }
+}
 
-#ifndef REMOVE_ON_FAIL
+void MainLoop::addDroppedSensors()
+{
     // Attempt to add any sensors that were removed
     auto it = _rmSensors.begin();
     while (it != _rmSensors.end())
@@ -497,6 +510,7 @@
         {
             SensorSet::container_t::value_type ssValueType =
                 std::make_pair(it->first, it->second);
+
             auto object = getObject(ssValueType);
             if (object)
             {
@@ -514,8 +528,10 @@
                 auto file = sysfs::make_sysfs_path(
                     _ioAccess.path(), it->first.first, it->first.second,
                     hwmon::entry::cinput);
+
                 log<level::INFO>("Added sensor to dbus after successful read",
                                  entry("FILE=%s", file.c_str()));
+
                 it = _rmSensors.erase(it);
             }
             else
@@ -529,7 +545,6 @@
             it = _rmSensors.erase(it);
         }
     }
-#endif
 }
 
 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/mainloop.hpp b/mainloop.hpp
index 4f7293d..a50020d 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -66,6 +66,14 @@
      */
     void shutdown() noexcept;
 
+    /** @brief Remove sensors slated for removal.
+     */
+    void removeSensors();
+
+    /** @brief Attempt to add sensors back that had been removed.
+     */
+    void addDroppedSensors();
+
   private:
     using mapped_type =
         std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;