Add external write hook to Sensor
Adds a member variable to Sensor, a lambda,
initialized to an empty function by default.
By assigning this variable to their own lambda,
a user of Sensor can receive notifications whenever an external write
happens, without having to modify any code within Sensor itself.
This is necessary to support ExternalSensor, but is generic enough
to help other dbus-sensors daemons, should the need ever arise.
Tested: In conjunction with ExternalSensor, works for me,
external writes correctly cause the lambda to be executed.
Signed-off-by: Josh Lehan <krellan@google.com>
Change-Id: I67571327d870e7c718fa4f25e91a6598daabcf3d
diff --git a/include/sensor.hpp b/include/sensor.hpp
index 0f90118..dc91fc1 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -77,6 +77,12 @@
size_t errCount;
std::unique_ptr<SensorInstrumentation> instrumentation;
+ // This member variable provides a hook that can be used to receive
+ // notification whenever this Sensor's value is externally set via D-Bus.
+ // If interested, assign your own lambda to this variable, during
+ // construction of your Sensor subclass. See ExternalSensor for example.
+ std::function<void()> externalSetHook;
+
void updateInstrumentation(double readValue)
{
// Do nothing if this feature is not enabled
@@ -172,6 +178,12 @@
// check thresholds for external set
value = newValue;
checkThresholds();
+
+ // Trigger the hook, as an external set has just happened
+ if (externalSetHook)
+ {
+ externalSetHook();
+ }
}
else if (!overriddenState)
{