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