blob: 0365ef40b26d12bfa7d05ef16f0bc2de02564f8c [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(
6 std::vector<std::shared_ptr<interfaces::Sensor>> sensorsIn,
7 std::vector<std::string> sensorNamesIn,
8 std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn) :
9 sensors(std::move(sensorsIn)),
10 sensorNames(std::move(sensorNamesIn)), actions(std::move(actionsIn))
11{}
12
13void OnChangeThreshold::initialize()
14{
15 for (auto& sensor : sensors)
16 {
17 sensor->registerForUpdates(weak_from_this());
18 }
19}
20
21void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
22 uint64_t timestamp)
23{}
24
25void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor,
26 uint64_t timestamp, double value)
27{
28 auto it =
29 std::find_if(sensors.begin(), sensors.end(),
30 [&sensor](const auto& x) { return &sensor == x.get(); });
31 auto index = std::distance(sensors.begin(), it);
32 commit(sensorNames.at(index), timestamp, value);
33}
34
35void OnChangeThreshold::commit(const std::string& sensorName,
36 uint64_t timestamp, double value)
37{
38 for (const auto& action : actions)
39 {
40 action->commit(sensorName, timestamp, value);
41 }
42}