Refactoring of the updateValue method
Purpose of these changes is to make the method created to update any dbus
parameters, not only the `Value`, available for all classes that inherit
from the Sensor class.
This commit does following changes:
* method areDifferent is moved from the CPUSensor class to the Sensor.
* method genericUpdateValue is renamed to updateProperty and moved from
the CPUSensor class to the Sensor.
* method updateValue is changed to utilize the updateProperty method.
Tested:
Manual tests where done on CPUSensor. No differences detected comparing
to results taken before these changes.
Signed-off-by: Zbigniew Kurzynski <zbigniew.kurzynski@intel.com>
Change-Id: I736526c0a128ffe094e156018fca6a883659f82a
diff --git a/include/sensor.hpp b/include/sensor.hpp
index ff272b5..6bf0eba 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -165,44 +165,9 @@
return;
}
- bool isChanged = false;
-
- // Avoid floating-point equality comparison,
- // by instead comparing against a very small hysteresis range.
- if (std::isnan(value) || std::isnan(newValue))
- {
- // If one or the other is NAN,
- // either we are intentionally invalidating a sensor reading,
- // or initializing for the very first time,
- // either way we should always publish this.
- isChanged = true;
- }
- else
- {
- // This essentially does "if (value != newValue)",
- // but safely against floating-point background noise.
- double diff = std::abs(value - newValue);
- if (diff > hysteresisPublish)
- {
- isChanged = true;
- }
- }
-
- // Ignore if the change is so small as to be deemed unchanged
- if (!isChanged)
- {
- return;
- }
-
- // The value will be changed, keep track of it for next time
- value = newValue;
-
// Indicate that it is internal set call
internalSet = true;
- if (!(sensorInterface->set_property("Value", newValue)))
- {
- std::cerr << "error setting property to " << newValue << "\n";
- }
+ updateProperty(sensorInterface, value, newValue, "Value");
internalSet = false;
// Always check thresholds after changing the value,
@@ -212,4 +177,33 @@
// in all current implementations of sensors that have thresholds.
checkThresholds();
}
+
+ void updateProperty(
+ std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
+ double& oldValue, const double& newValue, const char* dbusPropertyName)
+ {
+ if (requiresUpdate(oldValue, newValue))
+ {
+ oldValue = newValue;
+ if (!(interface->set_property(dbusPropertyName, newValue)))
+ {
+ std::cerr << "error setting property " << dbusPropertyName
+ << " to " << newValue << "\n";
+ }
+ }
+ }
+
+ bool requiresUpdate(const double& lVal, const double& rVal)
+ {
+ if (std::isnan(lVal) || std::isnan(rVal))
+ {
+ return true;
+ }
+ double diff = std::abs(lVal - rVal);
+ if (diff > hysteresisPublish)
+ {
+ return true;
+ }
+ return false;
+ }
};