EntityManager: Extract deviceRequiresPowerOn()

Replace a couple of open-coded instances of the behaviour.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ie20416506f6a41fd418eeb7d494b4e6c08b2da47
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index 655f7f7..0472612 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -835,6 +835,24 @@
     return true;
 }
 
+static bool deviceRequiresPowerOn(const nlohmann::json& entity)
+{
+    auto powerState = entity.find("PowerState");
+    if (powerState != entity.end())
+    {
+        auto ptr = powerState->get_ptr<const std::string*>();
+        if (ptr)
+        {
+            if (*ptr == "On" || *ptr == "BiosPost")
+            {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}
+
 void startRemovedTimer(boost::asio::steady_timer& timer,
                        nlohmann::json& systemConfiguration)
 {
@@ -856,53 +874,41 @@
     }
 
     timer.expires_after(std::chrono::seconds(10));
-    timer.async_wait(
-        [&systemConfiguration](const boost::system::error_code& ec) {
-            if (ec == boost::asio::error::operation_aborted)
-            {
-                // we were cancelled
-                return;
-            }
+    timer.async_wait([&systemConfiguration](
+                         const boost::system::error_code& ec) {
+        if (ec == boost::asio::error::operation_aborted)
+        {
+            // we were cancelled
+            return;
+        }
 
-            bool powerOff = !isPowerOn();
-            for (const auto& item : lastJson.items())
+        bool powerOff = !isPowerOn();
+        for (const auto& item : lastJson.items())
+        {
+            if (systemConfiguration.find(item.key()) ==
+                systemConfiguration.end())
             {
-                if (systemConfiguration.find(item.key()) ==
-                    systemConfiguration.end())
+                bool isDetectedPowerOn = deviceRequiresPowerOn(item.value());
+                if (powerOff && isDetectedPowerOn)
                 {
-                    bool isDetectedPowerOn = false;
-                    auto powerState = item.value().find("PowerState");
-                    if (powerState != item.value().end())
-                    {
-                        auto ptr = powerState->get_ptr<const std::string*>();
-                        if (ptr)
-                        {
-                            if (*ptr == "On" || *ptr == "BiosPost")
-                            {
-                                isDetectedPowerOn = true;
-                            }
-                        }
-                    }
-                    if (powerOff && isDetectedPowerOn)
-                    {
-                        // power not on yet, don't know if it's there or not
-                        continue;
-                    }
-                    if (!powerOff && scannedPowerOff && isDetectedPowerOn)
-                    {
-                        // already logged it when power was off
-                        continue;
-                    }
-
-                    logDeviceRemoved(item.value());
+                    // power not on yet, don't know if it's there or not
+                    continue;
                 }
+                if (!powerOff && scannedPowerOff && isDetectedPowerOn)
+                {
+                    // already logged it when power was off
+                    continue;
+                }
+
+                logDeviceRemoved(item.value());
             }
-            scannedPowerOff = true;
-            if (!powerOff)
-            {
-                scannedPowerOn = true;
-            }
-        });
+        }
+        scannedPowerOff = true;
+        if (!powerOff)
+        {
+            scannedPowerOn = true;
+        }
+    });
 }
 
 // main properties changed entry
@@ -960,20 +966,7 @@
                 bool powerOff = !isPowerOn();
                 for (const auto& item : missingConfigurations->items())
                 {
-                    bool isDetectedPowerOn = false;
-                    auto powerState = item.value().find("PowerState");
-                    if (powerState != item.value().end())
-                    {
-                        auto ptr = powerState->get_ptr<const std::string*>();
-                        if (ptr)
-                        {
-                            if (*ptr == "On" || *ptr == "BiosPost")
-                            {
-                                isDetectedPowerOn = true;
-                            }
-                        }
-                    }
-                    if (powerOff && isDetectedPowerOn)
+                    if (powerOff && deviceRequiresPowerOn(item.value()))
                     {
                         // power not on yet, don't know if it's there or not
                         continue;