blob: 206deb247bfa4fa188772b0a2c55155947a16b22 [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 Dompkeb7b7e1b2022-05-19 10:15:48 +02006 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),
10 sensors(std::move(sensorsIn)), actions(std::move(actionsIn)),
11 clock(std::move(clockIn))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010012{}
13
14void OnChangeThreshold::initialize()
15{
16 for (auto& sensor : sensors)
17 {
18 sensor->registerForUpdates(weak_from_this());
19 }
Szymon Dompke94f71c52021-12-10 07:16:33 +010020 initialized = true;
21}
22
23void OnChangeThreshold::updateSensors(Sensors newSensors)
24{
25 Sensors oldSensors = sensors;
26
27 for (const auto& sensor : newSensors)
28 {
29 auto it =
30 std::find_if(oldSensors.begin(), oldSensors.end(),
31 [&sensor](const auto& s) { return sensor == s; });
32 if (it != oldSensors.end())
33 {
34 oldSensors.erase(it);
35 continue;
36 }
37
38 if (initialized)
39 {
40 sensor->registerForUpdates(weak_from_this());
41 }
42 }
43
44 if (initialized)
45 {
46 for (auto& sensor : oldSensors)
47 {
48 sensor->unregisterFromUpdates(weak_from_this());
49 }
50 }
51
52 sensors = std::move(newSensors);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010053}
54
55void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010056 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010057{
Szymon Dompke620c65a2022-03-23 21:09:27 +010058 if (isFirstReading)
59 {
60 isFirstReading = false;
61 return;
62 }
63
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020064 commit(sensor.getName(), value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010065}
66
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020067void OnChangeThreshold::commit(const std::string& sensorName, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010068{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020069 Milliseconds timestamp = clock->systemTimestamp();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010070 for (const auto& action : actions)
71 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020072 action->commit(triggerId, std::nullopt, sensorName, timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010073 }
74}
Szymon Dompke94f71c52021-12-10 07:16:33 +010075
76LabeledThresholdParam OnChangeThreshold::getThresholdParam() const
77{
78 return {};
79}