Replace some duplicate codes about time setting

- Replace duplicate codes in pid/buildjson.cpp and dbusconfiguration.cpp

TEST:
D-Bus config =>
        {
            "FailSafePercent": 90.0,
            "MinThermalOutput": 0.0,
            "CycleIntervalTimeMS": 101,
            "UpdateThermalsTimeMS": 1000,
            "Name": "Zone 0",
            "Type": "Pid.Zone",
            "ZoneIndex": 0
        },
        {
            "FailSafePercent": 90.0,
            "MinThermalOutput": 0.0,
            "CycleIntervalTimeMS": 100,
            "Name": "Zone 1",
            "Type": "Pid.Zone",
            "ZoneIndex": 1
        },
        {
            "FailSafePercent": 100.0,
            "MinThermalOutput": 0.0,
            "UpdateThermalsTimeMS": -1000,
            "Name": "Zone 2",
            "Type": "Pid.Zone",
            "ZoneIndex": 2
        },

pid journal log =>
Mar 09 04:49:23 qbmc swampd[4823]: Zone 1: UpdateThermalsTimeMS cannot
    find setting. Use default 1000 ms
Mar 09 04:49:24 qbmc swampd[4823]: Zone 2: CycleIntervalTimeMS cannot
    find setting. Use default 100 ms
Mar 09 04:49:24 qbmc swampd[4823]: Zone 2: UpdateThermalsTimeMS is
    invalid. Use default 1000 ms

Static JSON =>
        {
            "id": 0,
            "minThermalOutput": 0.0,
            "failsafePercent": 100.0,
            "cycleIntervalTimeMS": 100,
            "updateThermalsTimeMS": 1000,
        ...
        ...
        {
            "id": 1,
            "minThermalOutput": 0.0,
            "failsafePercent": 100.0,
            "updateThermalsTimeMS": 0,

pid journal log =>
Mar 09 04:38:44 qbmc swampd[10646]: Zone 1: cycleIntervalTimeMS cannot
    find setting. Use default 100 ms
Mar 09 04:38:44 qbmc swampd[10646]: Zone 1: updateThermalsTimeMS is
    invalid. Use default 1000 ms

Change-Id: I75d272d9458978790d2b8fe2def35a0e6ba126b0
Signed-off-by: Harvey Wu <Harvey.Wu@quantatw.com>
diff --git a/dbus/dbusconfiguration.cpp b/dbus/dbusconfiguration.cpp
index bbe87d4..66ef7f8 100644
--- a/dbus/dbusconfiguration.cpp
+++ b/dbus/dbusconfiguration.cpp
@@ -257,6 +257,32 @@
     return search->second;
 }
 
+inline void getCycleTimeSetting(
+    const std::unordered_map<std::string, DbusVariantType>& zone,
+    const int zoneIndex, const std::string& attributeName, uint64_t& value)
+{
+    auto findAttributeName = zone.find(attributeName);
+    if (findAttributeName != zone.end())
+    {
+        double tmpAttributeValue =
+            std::visit(VariantToDoubleVisitor(), zone.at(attributeName));
+        if (tmpAttributeValue >= 1.0)
+        {
+            value = static_cast<uint64_t>(tmpAttributeValue);
+        }
+        else
+        {
+            std::cerr << "Zone " << zoneIndex << ": " << attributeName
+                      << " is invalid. Use default " << value << " ms\n";
+        }
+    }
+    else
+    {
+        std::cerr << "Zone " << zoneIndex << ": " << attributeName
+                  << " cannot find setting. Use default " << value << " ms\n";
+    }
+}
+
 void populatePidInfo(
     [[maybe_unused]] sdbusplus::bus_t& bus,
     const std::unordered_map<std::string, DbusVariantType>& base,
@@ -582,49 +608,10 @@
             details.failsafePercent = std::visit(VariantToDoubleVisitor(),
                                                  zone.at("FailSafePercent"));
 
-            auto findTimeInterval = zone.find("CycleIntervalTimeMS");
-            if (findTimeInterval != zone.end())
-            {
-                double tmp = 0.0;
-                auto ptrTimeInterval =
-                    std::get_if<double>(&(findTimeInterval->second));
-                if (ptrTimeInterval)
-                {
-                    tmp = *ptrTimeInterval;
-                }
-                if (tmp >= 1.0)
-                {
-                    details.cycleTime.cycleIntervalTimeMS = tmp;
-                }
-                else
-                {
-                    std::cerr << "CycleIntervalTimeMS cannot be 0. Use default "
-                              << details.cycleTime.cycleIntervalTimeMS
-                              << " ms\n";
-                }
-            }
-
-            auto findUpdateThermalsTime = zone.find("UpdateThermalsTimeMS");
-            if (findUpdateThermalsTime != zone.end())
-            {
-                double tmp = 0.0;
-                auto ptrUpdateThermalsTime =
-                    std::get_if<double>(&(findUpdateThermalsTime->second));
-                if (ptrUpdateThermalsTime)
-                {
-                    tmp = *ptrUpdateThermalsTime;
-                }
-                if (tmp >= 1.0)
-                {
-                    details.cycleTime.updateThermalsTimeMS = tmp;
-                }
-                else
-                {
-                    std::cerr
-                        << "UpdateThermalsTimeMS cannot be 0. Use default "
-                        << details.cycleTime.updateThermalsTimeMS << " ms\n";
-                }
-            }
+            getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
+                                details.cycleTime.cycleIntervalTimeMS);
+            getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
+                                details.cycleTime.updateThermalsTimeMS);
         }
         auto findBase = configuration.second.find(pidConfigurationInterface);
         // loop through all the PID configurations and fill out a sensor config
diff --git a/pid/buildjson.cpp b/pid/buildjson.cpp
index 47aa752..c37a5f6 100644
--- a/pid/buildjson.cpp
+++ b/pid/buildjson.cpp
@@ -129,6 +129,32 @@
 }
 } // namespace conf
 
+inline void getCycleTimeSetting(const auto& zone, const int id,
+                                const std::string& attributeName,
+                                uint64_t& value)
+{
+    auto findAttributeName = zone.find(attributeName);
+    if (findAttributeName != zone.end())
+    {
+        uint64_t tmpAttributeValue = 0;
+        findAttributeName->get_to(tmpAttributeValue);
+        if (tmpAttributeValue >= 1)
+        {
+            value = tmpAttributeValue;
+        }
+        else
+        {
+            std::cerr << "Zone " << id << ": " << attributeName
+                      << " is invalid. Use default " << value << " ms\n";
+        }
+    }
+    else
+    {
+        std::cerr << "Zone " << id << ": " << attributeName
+                  << " cannot find setting. Use default " << value << " ms\n";
+    }
+}
+
 std::pair<std::map<int64_t, conf::PIDConf>, std::map<int64_t, conf::ZoneConfig>>
     buildPIDsFromJson(const json& data)
 {
@@ -150,39 +176,10 @@
         thisZoneConfig.minThermalOutput = zone["minThermalOutput"];
         thisZoneConfig.failsafePercent = zone["failsafePercent"];
 
-        auto findTimeInterval = zone.find("cycleIntervalTimeMS");
-        if (findTimeInterval != zone.end())
-        {
-            uint64_t tmp;
-            findTimeInterval->get_to(tmp);
-            if (tmp != 0)
-            {
-                thisZoneConfig.cycleTime.cycleIntervalTimeMS = tmp;
-            }
-            else
-            {
-                std::cerr << "cycleIntervalTimeMS cannot be 0. Use default "
-                          << thisZoneConfig.cycleTime.cycleIntervalTimeMS
-                          << " ms\n";
-            }
-        }
-
-        auto findUpdateThermalsTime = zone.find("updateThermalsTimeMS");
-        if (findUpdateThermalsTime != zone.end())
-        {
-            uint64_t tmp;
-            findUpdateThermalsTime->get_to(tmp);
-            if (tmp != 0)
-            {
-                thisZoneConfig.cycleTime.updateThermalsTimeMS = tmp;
-            }
-            else
-            {
-                std::cerr << "updateThermalsTimeMS cannot be 0. Use default "
-                          << thisZoneConfig.cycleTime.updateThermalsTimeMS
-                          << " ms\n";
-            }
-        }
+        getCycleTimeSetting(zone, id, "cycleIntervalTimeMS",
+                            thisZoneConfig.cycleTime.cycleIntervalTimeMS);
+        getCycleTimeSetting(zone, id, "updateThermalsTimeMS",
+                            thisZoneConfig.cycleTime.updateThermalsTimeMS);
 
         auto pids = zone["pids"];
         for (const auto& pid : pids)