blob: 72aedc5cdfcac1e6f03460fa718d1375d77b4b98 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "on_change_threshold.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5OnChangeThreshold::OnChangeThreshold(
Szymon Dompke94f71c52021-12-10 07:16:33 +01006 Sensors sensorsIn,
Szymon Dompkef763c9e2021-03-12 09:19:22 +01007 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn) :
8 sensors(std::move(sensorsIn)),
Szymon Dompke94f71c52021-12-10 07:16:33 +01009 actions(std::move(actionsIn))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010010{}
11
12void OnChangeThreshold::initialize()
13{
14 for (auto& sensor : sensors)
15 {
16 sensor->registerForUpdates(weak_from_this());
17 }
Szymon Dompke94f71c52021-12-10 07:16:33 +010018 initialized = true;
19}
20
21void 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);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010051}
52
53void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010054 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010055{
Szymon Dompke94f71c52021-12-10 07:16:33 +010056 commit(sensor.getName(), timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010057}
58
59void OnChangeThreshold::commit(const std::string& sensorName,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010060 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010061{
62 for (const auto& action : actions)
63 {
64 action->commit(sensorName, timestamp, value);
65 }
66}
Szymon Dompke94f71c52021-12-10 07:16:33 +010067
68LabeledThresholdParam OnChangeThreshold::getThresholdParam() const
69{
70 return {};
71}