CPUSensor: Fix reading failure after DC Cycle

This makes it so the timer is called all of the time
regardless if the async handler has fired. Making it
so that when the handler doesn't respond because of
error, we don't have issues.

Tested: DC cycled, continued to get readings

Change-Id: Ib12e172ae93eb6c7bb83876457ce4320822ba56e
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/CPUSensor.hpp b/include/CPUSensor.hpp
index 4cad880..30359ae 100644
--- a/include/CPUSensor.hpp
+++ b/include/CPUSensor.hpp
@@ -44,6 +44,7 @@
     double privTcontrol;
     double dtsOffset;
     bool show;
+    size_t pollTime;
     void setupRead(void);
     void handleResponse(const boost::system::error_code& err);
     void checkThresholds(void) override;
diff --git a/src/CPUSensor.cpp b/src/CPUSensor.cpp
index bf2b6aa..49e65ac 100644
--- a/src/CPUSensor.cpp
+++ b/src/CPUSensor.cpp
@@ -44,10 +44,9 @@
     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
            std::move(_thresholds), sensorConfiguration, objectType, maxReading,
            minReading, PowerState::on),
-    objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
-    waitTimer(io), path(path),
+    objServer(objectServer), inputDev(io), waitTimer(io), path(path),
     privTcontrol(std::numeric_limits<double>::quiet_NaN()),
-    dtsOffset(dtsOffset), show(show)
+    dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs)
 {
     nameTcontrol = labelTcontrol;
     nameTcontrol += " CPU" + std::to_string(cpuId);
@@ -109,10 +108,38 @@
 
 void CPUSensor::setupRead(void)
 {
-    boost::asio::async_read_until(
-        inputDev, readBuf, '\n',
-        [&](const boost::system::error_code& ec,
-            std::size_t /*bytes_transfered*/) { handleResponse(ec); });
+    if (readingStateGood())
+    {
+        inputDev.close();
+        int fd = open(path.c_str(), O_RDONLY);
+        if (fd >= 0)
+        {
+            inputDev.assign(fd);
+
+            boost::asio::async_read_until(
+                inputDev, readBuf, '\n',
+                [&](const boost::system::error_code& ec,
+                    std::size_t /*bytes_transfered*/) { handleResponse(ec); });
+        }
+        else
+        {
+            std::cerr << name << " unable to open fd!\n";
+            pollTime = sensorFailedPollTimeMs;
+        }
+    }
+    else
+    {
+        pollTime = sensorFailedPollTimeMs;
+        markAvailable(false);
+    }
+    waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
+    waitTimer.async_wait([&](const boost::system::error_code& ec) {
+        if (ec == boost::asio::error::operation_aborted)
+        {
+            return; // we're being canceled
+        }
+        setupRead();
+    });
 }
 
 void CPUSensor::updateMinMaxValues(void)
@@ -171,7 +198,7 @@
     {
         return; // we're being destroyed
     }
-    size_t pollTime = CPUSensor::sensorPollMs;
+    pollTime = CPUSensor::sensorPollMs;
     std::istream responseStream(&readBuf);
     if (!err)
     {
@@ -238,21 +265,6 @@
     }
 
     responseStream.clear();
-    inputDev.close();
-    int fd = open(path.c_str(), O_RDONLY);
-    if (fd < 0)
-    {
-        return; // we're no longer valid
-    }
-    inputDev.assign(fd);
-    waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
-    waitTimer.async_wait([&](const boost::system::error_code& ec) {
-        if (ec == boost::asio::error::operation_aborted)
-        {
-            return; // we're being canceled
-        }
-        setupRead();
-    });
 }
 
 void CPUSensor::checkThresholds(void)