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/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)