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