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/CPUSensor.hpp b/include/CPUSensor.hpp
index e76ec89..29cd54a 100644
--- a/include/CPUSensor.hpp
+++ b/include/CPUSensor.hpp
@@ -49,9 +49,6 @@
void handleResponse(const boost::system::error_code& err);
void checkThresholds(void) override;
void updateMinMaxValues(void);
- bool areDifferent(const double& lVal, const double& rVal);
- void genericUpdateValue(double& oldValue, const double& newValue,
- const char* dbusParamName);
};
extern boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
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;
+ }
};
diff --git a/src/CPUSensor.cpp b/src/CPUSensor.cpp
index 801138a..afc4706 100644
--- a/src/CPUSensor.cpp
+++ b/src/CPUSensor.cpp
@@ -142,19 +142,20 @@
if (auto newVal =
readFile(attrPath, CPUSensor::sensorScaleFactor))
{
- genericUpdateValue(oldValue, *newVal, dbusName);
+ updateProperty(sensorInterface, oldValue, *newVal,
+ dbusName);
}
else
{
if (isPowerOn())
{
- genericUpdateValue(oldValue, 0, dbusName);
+ updateProperty(sensorInterface, oldValue, 0, dbusName);
}
else
{
- genericUpdateValue(
- oldValue, std::numeric_limits<double>::quiet_NaN(),
- dbusName);
+ updateProperty(sensorInterface, oldValue,
+ std::numeric_limits<double>::quiet_NaN(),
+ dbusName);
}
}
}
@@ -162,34 +163,6 @@
}
}
-bool CPUSensor::areDifferent(const double& lVal, const double& rVal)
-{
- if (std::isnan(lVal) || std::isnan(rVal))
- {
- return !(std::isnan(lVal) && std::isnan(rVal));
- }
- double diff = std::abs(lVal - rVal);
- if (diff > hysteresisPublish)
- {
- return true;
- }
- return false;
-}
-
-void CPUSensor::genericUpdateValue(double& oldValue, const double& newValue,
- const char* dbusParamName)
-{
- if (areDifferent(oldValue, newValue))
- {
- oldValue = newValue;
- if (!(sensorInterface->set_property(dbusParamName, newValue)))
- {
- std::cerr << "Error setting property " << dbusParamName << " to "
- << newValue << "\n";
- }
- }
-}
-
void CPUSensor::handleResponse(const boost::system::error_code& err)
{
if (err == boost::system::errc::bad_file_descriptor)