pid: reuse the code for processing sensors input

The code for processing sensors' inputs are the same in updateFanTelemetry() and
updateSensors(). This patch extract the similar code out as a private
function and make it be called in these two functions.

Tested:
- Can still build phosphor-pid-control.
- Copy built image to the system and it works fine.

Change-Id: I6249053e788bfa14bb7bdf2a880be5403c20029b
Signed-off-by: Tom Tung <shes050117@gmail.com>
diff --git a/pid/zone.cpp b/pid/zone.cpp
index e9aaafc..c88f41b 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -348,7 +348,7 @@
      * is disabled?  I think it's a waste to try and log things even if the
      * data is just being dropped though.
      */
-    tstamp now = std::chrono::high_resolution_clock::now();
+    const auto now = std::chrono::high_resolution_clock::now();
     if (loggingEnabled)
     {
         _log << std::chrono::duration_cast<std::chrono::milliseconds>(
@@ -358,67 +358,7 @@
         _log << "," << _maximumSetPointName;
     }
 
-    for (const auto& f : _fanInputs)
-    {
-        auto sensor = _mgr.getSensor(f);
-        ReadReturn r = sensor->read();
-        _cachedValuesByName[f] = {r.value, r.unscaled};
-        int64_t timeout = sensor->getTimeout();
-        tstamp then = r.updated;
-
-        auto duration =
-            std::chrono::duration_cast<std::chrono::seconds>(now - then)
-                .count();
-        auto period = std::chrono::seconds(timeout).count();
-        /*
-         * TODO(venture): We should check when these were last read.
-         * However, these are the fans, so if I'm not getting updated values
-         * for them... what should I do?
-         */
-        if (loggingEnabled)
-        {
-            const auto& v = _cachedValuesByName[f];
-            _log << "," << v.scaled << "," << v.unscaled;
-            const auto& p = _cachedFanOutputs[f];
-            _log << "," << p.scaled << "," << p.unscaled;
-        }
-
-        if (debugEnabled)
-        {
-            std::cerr << f << " fan sensor reading: " << r.value << "\n";
-        }
-
-        // check if fan fail.
-        if (sensor->getFailed())
-        {
-            _failSafeSensors.insert(f);
-            if (debugEnabled)
-            {
-                std::cerr << f << " fan sensor get failed\n";
-            }
-        }
-        else if (timeout != 0 && duration >= period)
-        {
-            _failSafeSensors.insert(f);
-            if (debugEnabled)
-            {
-                std::cerr << f << " fan sensor timeout\n";
-            }
-        }
-        else
-        {
-            // Check if it's in there: remove it.
-            auto kt = _failSafeSensors.find(f);
-            if (kt != _failSafeSensors.end())
-            {
-                if (debugEnabled)
-                {
-                    std::cerr << f << " is erased from failsafe sensor set\n";
-                }
-                _failSafeSensors.erase(kt);
-            }
-        }
-    }
+    processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
 
     if (loggingEnabled)
     {
@@ -434,59 +374,8 @@
 
 void DbusPidZone::updateSensors(void)
 {
-    using namespace std::chrono;
-    /* margin and temp are stored as temp */
-    tstamp now = high_resolution_clock::now();
-
-    for (const auto& t : _thermalInputs)
-    {
-        auto sensor = _mgr.getSensor(t);
-        ReadReturn r = sensor->read();
-        int64_t timeout = sensor->getTimeout();
-
-        _cachedValuesByName[t] = {r.value, r.unscaled};
-        tstamp then = r.updated;
-
-        auto duration = duration_cast<std::chrono::seconds>(now - then).count();
-        auto period = std::chrono::seconds(timeout).count();
-
-        if (debugEnabled)
-        {
-            std::cerr << t << " temperature sensor reading: " << r.value
-                      << "\n";
-        }
-
-        if (sensor->getFailed())
-        {
-            _failSafeSensors.insert(t);
-            if (debugEnabled)
-            {
-                std::cerr << t << " temperature sensor get failed\n";
-            }
-        }
-        else if (timeout != 0 && duration >= period)
-        {
-            // std::cerr << "Entering fail safe mode.\n";
-            _failSafeSensors.insert(t);
-            if (debugEnabled)
-            {
-                std::cerr << t << " temperature sensor get timeout\n";
-            }
-        }
-        else
-        {
-            // Check if it's in there: remove it.
-            auto kt = _failSafeSensors.find(t);
-            if (kt != _failSafeSensors.end())
-            {
-                if (debugEnabled)
-                {
-                    std::cerr << t << " is erased from failsafe sensor set\n";
-                }
-                _failSafeSensors.erase(kt);
-            }
-        }
-    }
+    processSensorInputs</* fanSensorLogging */ false>(
+        _thermalInputs, std::chrono::high_resolution_clock::now());
 
     return;
 }
diff --git a/pid/zone.hpp b/pid/zone.hpp
index 0504a6d..b985f5f 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -13,6 +13,7 @@
 #include <xyz/openbmc_project/Control/Mode/server.hpp>
 
 #include <fstream>
+#include <iostream>
 #include <map>
 #include <memory>
 #include <set>
@@ -99,6 +100,78 @@
     bool failSafe() const override;
 
   private:
+    template <bool fanSensorLogging>
+    void processSensorInputs(const std::vector<std::string>& sensorInputs,
+                             std::chrono::high_resolution_clock::time_point now)
+    {
+        for (const auto& sensorInput : sensorInputs)
+        {
+            auto sensor = _mgr.getSensor(sensorInput);
+            ReadReturn r = sensor->read();
+            _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
+            int64_t timeout = sensor->getTimeout();
+            std::chrono::high_resolution_clock::time_point then = r.updated;
+
+            auto duration =
+                std::chrono::duration_cast<std::chrono::seconds>(now - then)
+                    .count();
+            auto period = std::chrono::seconds(timeout).count();
+            /*
+             * TODO(venture): We should check when these were last read.
+             * However, these are the fans, so if I'm not getting updated values
+             * for them... what should I do?
+             */
+            if constexpr (fanSensorLogging)
+            {
+                if (loggingEnabled)
+                {
+                    const auto& v = _cachedValuesByName[sensorInput];
+                    _log << "," << v.scaled << "," << v.unscaled;
+                    const auto& p = _cachedFanOutputs[sensorInput];
+                    _log << "," << p.scaled << "," << p.unscaled;
+                }
+            }
+
+            if (debugEnabled)
+            {
+                std::cerr << sensorInput << " sensor reading: " << r.value
+                          << "\n";
+            }
+
+            // check if fan fail.
+            if (sensor->getFailed())
+            {
+                _failSafeSensors.insert(sensorInput);
+                if (debugEnabled)
+                {
+                    std::cerr << sensorInput << " sensor get failed\n";
+                }
+            }
+            else if (timeout != 0 && duration >= period)
+            {
+                _failSafeSensors.insert(sensorInput);
+                if (debugEnabled)
+                {
+                    std::cerr << sensorInput << " sensor timeout\n";
+                }
+            }
+            else
+            {
+                // Check if it's in there: remove it.
+                auto kt = _failSafeSensors.find(sensorInput);
+                if (kt != _failSafeSensors.end())
+                {
+                    if (debugEnabled)
+                    {
+                        std::cerr << sensorInput
+                                  << " is erased from failsafe sensor set\n";
+                    }
+                    _failSafeSensors.erase(kt);
+                }
+            }
+        }
+    }
+
     std::ofstream _log;
 
     const int64_t _zoneId;