Stop logging bad SEL on power transitions

Improve the power transition handling logic to avoid
extra sel.

Tested: DC cycled multiple times and watched SEL not happen

Change-Id: I173f8179ff983e08a12af0c618c03f4145c0ac04
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/Thresholds.hpp b/include/Thresholds.hpp
index f374ab6..37ce108 100644
--- a/include/Thresholds.hpp
+++ b/include/Thresholds.hpp
@@ -39,61 +39,58 @@
 void assertThresholds(Sensor* sensor, thresholds::Level level,
                       thresholds::Direction direction, bool assert);
 
+using TimerPair = std::pair<bool, boost::asio::deadline_timer>;
+
 struct ThresholdTimer
 {
 
-    ThresholdTimer(boost::asio::io_service& io, Sensor* sensor) :
-        criticalTimer(io), warningTimer(io), sensor(sensor)
+    ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
+        io(ioService), sensor(sensor)
     {
     }
 
     void startTimer(const Threshold& threshold)
     {
-        constexpr const size_t waitTime = 2;
+        constexpr const size_t waitTime = 5;
+        TimerPair* pair = nullptr;
 
-        if (threshold.level == WARNING && !warningRunning)
+        for (TimerPair& timer : timers)
         {
-            warningRunning = true;
-            warningTimer.expires_from_now(boost::posix_time::seconds(waitTime));
-            warningTimer.async_wait(
-                [this, threshold](boost::system::error_code ec) {
-                    if (ec == boost::asio::error::operation_aborted)
-                    {
-                        return; // we're being canceled
-                    }
-                    if (isPowerOn())
-                    {
-                        assertThresholds(sensor, threshold.level,
-                                         threshold.direction, true);
-                    }
-                    warningRunning = false;
-                });
+            if (!timer.first)
+            {
+                pair = &timer;
+                break;
+            }
         }
-        else if (threshold.level == CRITICAL && !criticalRunning)
+        if (pair == nullptr)
         {
-            criticalRunning = true;
-            criticalTimer.expires_from_now(
-                boost::posix_time::seconds(waitTime));
-            criticalTimer.async_wait(
-                [this, threshold](boost::system::error_code ec) {
-                    if (ec == boost::asio::error::operation_aborted)
-                    {
-                        return; // we're being canceled
-                    }
-                    if (isPowerOn())
-                    {
-                        assertThresholds(sensor, threshold.level,
-                                         threshold.direction, true);
-                    }
-                    criticalRunning = false;
-                });
+            pair = &timers.emplace_back(false, boost::asio::deadline_timer(io));
         }
+        pair->first = true;
+        pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
+        pair->second.async_wait(
+            [this, pair, threshold](boost::system::error_code ec) {
+                pair->first = false;
+
+                if (ec == boost::asio::error::operation_aborted)
+                {
+                    return; // we're being canceled
+                }
+                else if (ec)
+                {
+                    std::cerr << "timer error: " << ec.message() << "\n";
+                    return;
+                }
+                if (isPowerOn())
+                {
+                    assertThresholds(sensor, threshold.level,
+                                     threshold.direction, true);
+                }
+            });
     }
 
-    boost::asio::deadline_timer criticalTimer;
-    boost::asio::deadline_timer warningTimer;
-    bool criticalRunning = false;
-    bool warningRunning = false;
+    boost::asio::io_service& io;
+    std::list<TimerPair> timers;
     Sensor* sensor;
 };
 
diff --git a/src/Utils.cpp b/src/Utils.cpp
index fade0e6..f1a70b5 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -128,6 +128,7 @@
 
 void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
 {
+    static boost::asio::steady_timer timer(conn->get_io_context());
     // create a match for powergood changes, first time do a method call to
     // cache the correct value
     if (powerMatch)
@@ -148,8 +149,28 @@
             auto findState = values.find(power::property);
             if (findState != values.end())
             {
-                powerStatusOn = boost::ends_with(
+                bool on = boost::ends_with(
                     std::get<std::string>(findState->second), "Running");
+                if (!on)
+                {
+                    timer.cancel();
+                    powerStatusOn = false;
+                    return;
+                }
+                // on comes too quickly
+                timer.expires_after(std::chrono::seconds(10));
+                timer.async_wait([](boost::system::error_code ec) {
+                    if (ec == boost::asio::error::operation_aborted)
+                    {
+                        return;
+                    }
+                    else if (ec)
+                    {
+                        std::cerr << "Timer error " << ec.message() << "\n";
+                        return;
+                    }
+                    powerStatusOn = true;
+                });
             }
         });
 
@@ -236,4 +257,4 @@
         association->register_property("associations", associations);
         association->initialize();
     }
-}
\ No newline at end of file
+}