mainloop: move out the interface update method
Move the interface update method out of the read() loop.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I430d20ded4efd89c691eff368d005791f0138929
diff --git a/mainloop.cpp b/mainloop.cpp
index ab4af2e..a35c40e 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -73,6 +73,31 @@
Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
&CriticalObject::criticalAlarmHigh;
+void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value)
+{
+ for (auto& iface : ifaces)
+ {
+ switch (iface.first)
+ {
+ case InterfaceType::VALUE:
+ {
+ auto& valueIface =
+ std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
+ valueIface->value(value);
+ }
+ break;
+ case InterfaceType::WARN:
+ checkThresholds<WarningObject>(iface.second, value);
+ break;
+ case InterfaceType::CRIT:
+ checkThresholds<CriticalObject>(iface.second, value);
+ break;
+ default:
+ break;
+ }
+ }
+}
+
std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
{
std::string id;
@@ -413,29 +438,7 @@
value = sensor->adjustValue(value);
}
- for (auto& iface : obj)
- {
- switch (iface.first)
- {
- case InterfaceType::VALUE:
- {
- auto& valueIface =
- std::any_cast<std::shared_ptr<ValueObject>&>(
- iface.second);
- valueIface->value(value);
- }
- break;
- case InterfaceType::WARN:
- checkThresholds<WarningObject>(iface.second, value);
- break;
- case InterfaceType::CRIT:
- checkThresholds<CriticalObject>(iface.second,
- value);
- break;
- default:
- break;
- }
- }
+ updateSensorInterfaces(obj, value);
}
catch (const std::system_error& e)
{
diff --git a/mainloop.hpp b/mainloop.hpp
index 07128b5..d99ed91 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -147,3 +147,8 @@
std::optional<ObjectStateData>
getObject(SensorSet::container_t::const_reference sensor);
};
+
+/** @brief Given a value and map of interfaces, update values and check
+ * thresholds.
+ */
+void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value);