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)
55 {}
56 
57 void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
58                                       Milliseconds timestamp, double value)
59 {
60     commit(sensor.getName(), timestamp, value);
61 }
62 
63 void OnChangeThreshold::commit(const std::string& sensorName,
64                                Milliseconds timestamp, double value)
65 {
66     for (const auto& action : actions)
67     {
68         action->commit(sensorName, timestamp, value);
69     }
70 }
71 
72 LabeledThresholdParam OnChangeThreshold::getThresholdParam() const
73 {
74     return {};
75 }
76