HwmonTempSensor: Add PollRate attribute

Tested:
1. Add the following configuration for test case:
   {
      "Address": "0x5c",
      "Bus": "47",
      "Name": "inlet",
      "PollRate": 100.0,
      "Thresholds": [
         ...
      ],
      "Type": "MAX31725"
   },
   - update sensor once every 100 seconds and verify pass.
2. Add "PollRate" in configuration for positive and negative value and
   verify pass.

Signed-off-by: Jeff Lin <JeffLin2@quantatw.com>
Change-Id: Ieeee2dd7ab6b3b2d237496b356e29b856078eb06
diff --git a/include/HwmonTempSensor.hpp b/include/HwmonTempSensor.hpp
index 3064c53..5cf0da5 100644
--- a/include/HwmonTempSensor.hpp
+++ b/include/HwmonTempSensor.hpp
@@ -19,6 +19,7 @@
                     std::shared_ptr<sdbusplus::asio::connection>& conn,
                     boost::asio::io_service& io, const std::string& fanName,
                     std::vector<thresholds::Threshold>&& thresholds,
+                    const float pollRate,
                     const std::string& sensorConfiguration,
                     const PowerState powerState);
     ~HwmonTempSensor();
@@ -31,6 +32,7 @@
     boost::asio::streambuf readBuf;
     std::string path;
     size_t errCount;
+    unsigned int sensorPollMs;
 
     void handleResponse(const boost::system::error_code& err);
     void checkThresholds(void) override;
diff --git a/src/HwmonTempMain.cpp b/src/HwmonTempMain.cpp
index b5cbf23..8a583b9 100644
--- a/src/HwmonTempMain.cpp
+++ b/src/HwmonTempMain.cpp
@@ -38,6 +38,7 @@
 #include <vector>
 
 static constexpr bool DEBUG = false;
+static constexpr float pollRateDefault = 0.5;
 
 namespace fs = std::filesystem;
 static constexpr std::array<const char*, 13> sensorTypes = {
@@ -207,6 +208,19 @@
                     std::cerr << "error populating thresholds for "
                               << sensorName << "\n";
                 }
+
+                auto findPollRate = baseConfiguration->second.find("PollRate");
+                float pollRate = pollRateDefault;
+                if (findPollRate != baseConfiguration->second.end())
+                {
+                    pollRate = std::visit(VariantToFloatVisitor(),
+                                          findPollRate->second);
+                    if (pollRate <= 0.0f)
+                    {
+                        pollRate = pollRateDefault; // polling time too short
+                    }
+                }
+
                 auto findPowerOn = baseConfiguration->second.find("PowerState");
                 PowerState readState = PowerState::always;
                 if (findPowerOn != baseConfiguration->second.end())
@@ -225,7 +239,7 @@
                 {
                     sensor = std::make_shared<HwmonTempSensor>(
                         *hwmonFile, sensorType, objectServer, dbusConnection,
-                        io, sensorName, std::move(sensorThresholds),
+                        io, sensorName, std::move(sensorThresholds), pollRate,
                         *interfacePath, readState);
                     sensor->setupRead();
                 }
@@ -253,7 +267,7 @@
                         sensor = std::make_shared<HwmonTempSensor>(
                             *hwmonFile, sensorType, objectServer,
                             dbusConnection, io, sensorName,
-                            std::vector<thresholds::Threshold>(),
+                            std::vector<thresholds::Threshold>(), pollRate,
                             *interfacePath, readState);
                         sensor->setupRead();
                     }
diff --git a/src/HwmonTempSensor.cpp b/src/HwmonTempSensor.cpp
index eaaa9e8..e1e8f6c 100644
--- a/src/HwmonTempSensor.cpp
+++ b/src/HwmonTempSensor.cpp
@@ -32,7 +32,6 @@
 #include <string>
 #include <vector>
 
-static constexpr unsigned int sensorPollMs = 500;
 static constexpr unsigned int sensorScaleFactor = 1000;
 static constexpr size_t warnAfterErrorCount = 10;
 
@@ -44,13 +43,14 @@
     sdbusplus::asio::object_server& objectServer,
     std::shared_ptr<sdbusplus::asio::connection>& conn,
     boost::asio::io_service& io, const std::string& sensorName,
-    std::vector<thresholds::Threshold>&& _thresholds,
+    std::vector<thresholds::Threshold>&& _thresholds, const float pollRate,
     const std::string& sensorConfiguration, const PowerState powerState) :
     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
            std::move(_thresholds), sensorConfiguration, objectType, maxReading,
            minReading, conn, powerState),
     std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
-    inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path)
+    inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
+    sensorPollMs(pollRate * 1000)
 {
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/temperature/" + name,