Fix the PSU Service crash issue.

When entity-manager changes, PSU service will crash when recreating
PSU Sensors. Change delay time to 3s for PSU service has enough time
to finish last creating, change sensor monitoring time to 1s to avoid
sysfs file reading failure.

Tested:
When starting system or reset entity-manager, PSU service will not
crash.

Signed-off-by: Cheng C Yang <cheng.c.yang@linux.intel.com>
Change-Id: I43e7f5207c137cd3601877af5313a3c340ee05f3
diff --git a/include/PSUEvent.hpp b/include/PSUEvent.hpp
index 71fd5d0..623806b 100644
--- a/include/PSUEvent.hpp
+++ b/include/PSUEvent.hpp
@@ -41,6 +41,7 @@
 
   private:
     int value = 0;
+    int fd;
     size_t errCount;
     std::string path;
     std::string eventName;
diff --git a/include/PSUSensor.hpp b/include/PSUSensor.hpp
index 4ab553f..14b6d96 100644
--- a/include/PSUSensor.hpp
+++ b/include/PSUSensor.hpp
@@ -33,7 +33,8 @@
     void handleResponse(const boost::system::error_code& err);
     void checkThresholds(void) override;
 
-    static constexpr unsigned int sensorPollMs = 500;
+    int fd;
+    static constexpr unsigned int sensorPollMs = 1000;
     static constexpr size_t warnAfterErrorCount = 10;
 };
 
diff --git a/src/PSUEvent.cpp b/src/PSUEvent.cpp
index bfe0776..4e5c5a7 100644
--- a/src/PSUEvent.cpp
+++ b/src/PSUEvent.cpp
@@ -135,10 +135,17 @@
     std::shared_ptr<bool> state, const std::string& psuName) :
     eventInterface(eventInterface),
     asserts(asserts), combineEvent(combineEvent), assertState(state),
-    errCount(0), path(path), eventName(eventName), waitTimer(io),
-    inputDev(io, open(path.c_str(), O_RDONLY)), psuName(psuName),
-    groupEventName(groupEventName)
+    errCount(0), path(path), eventName(eventName), waitTimer(io), inputDev(io),
+    psuName(psuName), groupEventName(groupEventName)
 {
+    fd = open(path.c_str(), O_RDONLY);
+    if (fd < 0)
+    {
+        std::cerr << "PSU sub event failed to open file\n";
+        return;
+    }
+    inputDev.assign(fd);
+
     auto found = logID.find(eventName);
     if (found == logID.end())
     {
@@ -174,8 +181,8 @@
 
 PSUSubEvent::~PSUSubEvent()
 {
-    inputDev.close();
     waitTimer.cancel();
+    inputDev.close();
 }
 
 void PSUSubEvent::handleResponse(const boost::system::error_code& err)
@@ -217,14 +224,7 @@
         updateValue(0);
         errCount++;
     }
-    responseStream.clear();
-    inputDev.close();
-    int fd = open(path.c_str(), O_RDONLY);
-    if (fd < 0)
-    {
-        return;
-    }
-    inputDev.assign(fd);
+    lseek(fd, 0, SEEK_SET);
     waitTimer.expires_from_now(boost::posix_time::milliseconds(eventPollMs));
     waitTimer.async_wait([&](const boost::system::error_code& ec) {
         if (ec == boost::asio::error::operation_aborted)
diff --git a/src/PSUSensor.cpp b/src/PSUSensor.cpp
index fc24704..d5f1b23 100644
--- a/src/PSUSensor.cpp
+++ b/src/PSUSensor.cpp
@@ -44,8 +44,8 @@
                      double max, double min) :
     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
            std::move(_thresholds), sensorConfiguration, objectType, max, min),
-    objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
-    waitTimer(io), path(path), errCount(0),
+    objServer(objectServer), inputDev(io), waitTimer(io), path(path),
+    errCount(0),
 
     sensorFactor(factor)
 {
@@ -58,6 +58,14 @@
                   << sensorName << "\"\n";
     }
 
+    fd = open(path.c_str(), O_RDONLY);
+    if (fd < 0)
+    {
+        std::cerr << "PSU sensor failed to open file\n";
+        return;
+    }
+    inputDev.assign(fd);
+
     std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
 
     sensorInterface = objectServer.add_interface(
@@ -83,8 +91,8 @@
 
 PSUSensor::~PSUSensor()
 {
-    inputDev.close();
     waitTimer.cancel();
+    inputDev.close();
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(thresholdInterfaceWarning);
     objServer.remove_interface(thresholdInterfaceCritical);
@@ -156,15 +164,7 @@
         errCount++;
     }
 
-    responseStream.clear();
-    inputDev.close();
-    int fd = open(path.c_str(), O_RDONLY);
-    if (fd < 0)
-    {
-        std::cerr << "Failed to open path " << path << "\n";
-        return;
-    }
-    inputDev.assign(fd);
+    lseek(fd, 0, SEEK_SET);
     waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
     waitTimer.async_wait([&](const boost::system::error_code& ec) {
         if (ec == boost::asio::error::operation_aborted)
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index c5a7251..ce96c72 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -836,7 +836,7 @@
                 std::cerr << "callback method error\n";
                 return;
             }
-            filterTimer.expires_from_now(boost::posix_time::seconds(1));
+            filterTimer.expires_from_now(boost::posix_time::seconds(3));
             filterTimer.async_wait([&](const boost::system::error_code& ec) {
                 if (ec == boost::asio::error::operation_aborted)
                 {