xref: /openbmc/telemetry/src/on_change_threshold.cpp (revision f535cad6545d39fe58f50d0f23074359f43f8a03)
1 #include "on_change_threshold.hpp"
2 
3 #include <phosphor-logging/log.hpp>
4 
OnChangeThreshold(const std::string & triggerIdIn,Sensors sensorsIn,std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,std::unique_ptr<interfaces::Clock> clockIn)5 OnChangeThreshold::OnChangeThreshold(
6     const std::string& triggerIdIn, Sensors sensorsIn,
7     std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
8     std::unique_ptr<interfaces::Clock> clockIn) :
9     triggerId(triggerIdIn), sensors(std::move(sensorsIn)),
10     actions(std::move(actionsIn)), clock(std::move(clockIn))
11 {}
12 
initialize()13 void OnChangeThreshold::initialize()
14 {
15     for (auto& sensor : sensors)
16     {
17         sensor->registerForUpdates(weak_from_this());
18     }
19     initialized = true;
20 }
21 
updateSensors(Sensors newSensors)22 void OnChangeThreshold::updateSensors(Sensors newSensors)
23 {
24     Sensors oldSensors = sensors;
25 
26     for (const auto& sensor : newSensors)
27     {
28         auto it =
29             std::find_if(oldSensors.begin(), oldSensors.end(),
30                          [&sensor](const auto& s) { return sensor == s; });
31         if (it != oldSensors.end())
32         {
33             oldSensors.erase(it);
34             continue;
35         }
36 
37         if (initialized)
38         {
39             sensor->registerForUpdates(weak_from_this());
40         }
41     }
42 
43     if (initialized)
44     {
45         for (auto& sensor : oldSensors)
46         {
47             sensor->unregisterFromUpdates(weak_from_this());
48         }
49     }
50 
51     sensors = std::move(newSensors);
52 }
53 
sensorUpdated(interfaces::Sensor & sensor,Milliseconds timestamp,double value)54 void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
55                                       Milliseconds timestamp, double value)
56 {
57     if (isFirstReading)
58     {
59         isFirstReading = false;
60         return;
61     }
62 
63     commit(sensor.getName(), value);
64 }
65 
commit(const std::string & sensorName,double value)66 void OnChangeThreshold::commit(const std::string& sensorName, double value)
67 {
68     Milliseconds timestamp = clock->systemTimestamp();
69     for (const auto& action : actions)
70     {
71         action->commit(triggerId, std::nullopt, sensorName, timestamp, value);
72     }
73 }
74 
getThresholdParam() const75 LabeledThresholdParam OnChangeThreshold::getThresholdParam() const
76 {
77     return {};
78 }
79