Fix PSU threshold mismatch issue

When trying to change thresholds on PSU sensors, PSU service will crash.
Some PSU in entity-manager will have threshold0 to threshodl5 which
contains all labels in one D-Bus path. But in current dbus sensors,
the presist threshold function do not support labels, it only check
severity. So when change a threshold in PSU sensor, it will change some
incorrect threshold in entity-manger. This patch adds label support to
presist threshold function, so that correct theshold in entity-manager
can be changed.

Tested:
After changed PSU1 Temp1 threshold by ipmitool, check the threshold again,
the value should be changed to the value we set. Check entity-manager,
the threshold in entity-manager should also change to the value we set.

Signed-off-by: Cheng C Yang <cheng.c.yang@linux.intel.com>
Change-Id: Ib1c8bb454cd42dff170ae33c4aa768c4b515bb44
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index 0367d2a..64f54da 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -218,7 +218,6 @@
 
     // TODO may need only modify the ones that need to be changed.
     sensors.clear();
-    combineEvents.clear();
     for (const char* type : sensorTypes)
     {
         if (!getSensorConfiguration(type, dbusConnection, sensorConfigs,
@@ -310,6 +309,7 @@
         const SensorData* sensorData = nullptr;
         const std::string* interfacePath = nullptr;
         const char* sensorType = nullptr;
+        size_t thresholdConfSize = 0;
 
         for (const std::pair<sdbusplus::message::object_path, SensorData>&
                  sensor : sensorConfigs)
@@ -360,6 +360,13 @@
                 continue;
             }
 
+            std::vector<thresholds::Threshold> confThresholds;
+            if (!parseThresholdsFromConfig(*sensorData, confThresholds))
+            {
+                std::cerr << "error populating totoal thresholds\n";
+            }
+            thresholdConfSize = confThresholds.size();
+
             interfacePath = &(sensor.first.str);
             break;
         }
@@ -441,9 +448,11 @@
             std::ifstream labelFile(labelPath);
             if (!labelFile.good())
             {
-                std::cerr << "Input file " << sensorPath
-                          << " has no corresponding label file\n";
-
+                if constexpr (DEBUG)
+                {
+                    std::cerr << "Input file " << sensorPath
+                              << " has no corresponding label file\n";
+                }
                 // hwmon *_input filename with number:
                 // temp1, temp2, temp3, ...
                 labelHead = sensorNameStr.substr(0, sensorNameStr.find("_"));
@@ -479,8 +488,11 @@
                 if (std::find(findLabels.begin(), findLabels.end(),
                               labelHead) == findLabels.end())
                 {
-                    std::cerr << "could not find " << labelHead
-                              << " in the Labels list\n";
+                    if constexpr (DEBUG)
+                    {
+                        std::cerr << "could not find " << labelHead
+                                  << " in the Labels list\n";
+                    }
                     continue;
                 }
             }
@@ -669,7 +681,6 @@
             }
 
             std::vector<thresholds::Threshold> sensorThresholds;
-
             if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
                                            &labelHead))
             {
@@ -724,7 +735,7 @@
                 sensorPathStr, sensorType, objectServer, dbusConnection, io,
                 sensorName, std::move(sensorThresholds), *interfacePath,
                 findSensorType->second, factor, psuProperty->maxReading,
-                psuProperty->minReading);
+                psuProperty->minReading, labelHead, thresholdConfSize);
 
             ++numCreated;
             if constexpr (DEBUG)