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/PSUSensor.cpp b/src/PSUSensor.cpp
index 96b710c..2996b6f 100644
--- a/src/PSUSensor.cpp
+++ b/src/PSUSensor.cpp
@@ -41,13 +41,12 @@
                      std::vector<thresholds::Threshold>&& _thresholds,
                      const std::string& sensorConfiguration,
                      std::string& sensorTypeName, unsigned int factor,
-                     double max, double min) :
+                     double max, double min, const std::string& label,
+                     size_t tSize) :
     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
            std::move(_thresholds), sensorConfiguration, objectType, max, min),
     objServer(objectServer), inputDev(io), waitTimer(io), path(path),
-    errCount(0),
-
-    sensorFactor(factor)
+    errCount(0), sensorFactor(factor)
 {
     if constexpr (DEBUG)
     {
@@ -85,7 +84,14 @@
     // This should be called before initializing association.
     // createInventoryAssoc() does add more associations before doing
     // register and initialize "Associations" property.
-    setInitialProperties(conn);
+    if (label.empty() || tSize == _thresholds.size())
+    {
+        setInitialProperties(conn);
+    }
+    else
+    {
+        setInitialProperties(conn, label, tSize);
+    }
 
     association = objectServer.add_interface(dbusPath, association::interface);
 
@@ -97,6 +103,7 @@
 {
     waitTimer.cancel();
     inputDev.close();
+    objServer.remove_interface(association);
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(thresholdInterfaceWarning);
     objServer.remove_interface(thresholdInterfaceCritical);
@@ -115,7 +122,7 @@
 {
     if (err == boost::system::errc::bad_file_descriptor)
     {
-        std::cerr << "Bad file descriptor from " << path << "\n";
+        std::cerr << "Bad file descriptor from\n";
         return;
     }
     std::istream responseStream(&readBuf);
@@ -134,14 +141,13 @@
         }
         catch (const std::invalid_argument&)
         {
-            std::cerr << "Could not parse " << response << " from path " << path
-                      << "\n";
+            std::cerr << "Could not parse " << response << "\n";
             errCount++;
         }
     }
     else
     {
-        std::cerr << "System error " << err << " from path " << path << "\n";
+        std::cerr << "System error " << err << "\n";
         errCount++;
     }
 
@@ -149,8 +155,7 @@
     {
         if (errCount == warnAfterErrorCount)
         {
-            std::cerr << "Failure to read sensor " << name << " at " << path
-                      << "\n";
+            std::cerr << "Failure to read sensor " << name << "\n";
         }
         updateValue(0);
         errCount++;
@@ -161,7 +166,7 @@
     waitTimer.async_wait([&](const boost::system::error_code& ec) {
         if (ec == boost::asio::error::operation_aborted)
         {
-            std::cerr << "Failed to reschedule wait for " << path << "\n";
+            std::cerr << "Failed to reschedule\n";
             return;
         }
         setupRead();