Clean up power state handling and fix voltage events

Seperate isPowerOn into two functions, one to set up the
match and one to poll the boolean. This way a dbus connection
object isn't needed in the sensor. Use this new function to
allow the ADCSensor to only signal threshold crosses if the
sensor is in the correct state.

Tested-by: Verified no SEL events for ADC sensors were created
during power cycling.

Change-Id: Ida800ab478b85ac2cb5976fa3471411c5d4bdc88
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/src/Utils.cpp b/src/Utils.cpp
index e4de9e0..c52f88f 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -28,6 +28,9 @@
 const static constexpr char* powerObjectName =
     "/xyz/openbmc_project/Chassis/Control/Power0";
 
+bool powerStatusOn = false;
+std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
+
 bool getSensorConfiguration(
     const std::string& type,
     const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
@@ -105,20 +108,20 @@
     return true;
 }
 
-// initially returns false, then sets up matches and returns status
-// should be called once first to initialize
-bool isPowerOn(const std::shared_ptr<sdbusplus::asio::connection>& conn)
+bool isPowerOn(void)
 {
-    static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
-    static bool powerStatusOn = false;
-
-    if (powerMatch != nullptr)
+    if (!powerMatch)
     {
-        return powerStatusOn;
+        throw std::runtime_error("Power Match Not Created");
     }
+    return powerStatusOn;
+}
+
+void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
+{
 
     // create a match for powergood changes, first time do a method call to
-    // return the correct value
+    // cache the correct value
     std::function<void(sdbusplus::message::message & message)> eventHandler =
         [](sdbusplus::message::message& message) {
             std::string objectName;
@@ -153,8 +156,6 @@
         },
         powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
         "Get", powerInterfaceName, "pgood");
-
-    return powerStatusOn;
 }
 
 // replaces limits if MinReading and MaxReading are found.