blob: b05f4a4a144ff1664f69d46d852d078830df7ff1 [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) :
Patrick Williamsf535cad2024-08-16 15:21:20 -04009 triggerId(triggerIdIn), sensors(std::move(sensorsIn)),
10 actions(std::move(actionsIn)), clock(std::move(clockIn))
Szymon Dompkef763c9e2021-03-12 09:19:22 +010011{}
12
13void OnChangeThreshold::initialize()
14{
15 for (auto& sensor : sensors)
16 {
17 sensor->registerForUpdates(weak_from_this());
18 }
Szymon Dompke94f71c52021-12-10 07:16:33 +010019 initialized = true;
20}
21
22void 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);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010052}
53
54void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010055 Milliseconds timestamp, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010056{
Szymon Dompke620c65a2022-03-23 21:09:27 +010057 if (isFirstReading)
58 {
59 isFirstReading = false;
60 return;
61 }
62
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020063 commit(sensor.getName(), value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064}
65
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020066void OnChangeThreshold::commit(const std::string& sensorName, double value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010067{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020068 Milliseconds timestamp = clock->systemTimestamp();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010069 for (const auto& action : actions)
70 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020071 action->commit(triggerId, std::nullopt, sensorName, timestamp, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010072 }
73}
Szymon Dompke94f71c52021-12-10 07:16:33 +010074
75LabeledThresholdParam OnChangeThreshold::getThresholdParam() const
76{
77 return {};
78}