Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #include "on_change_threshold.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | OnChangeThreshold::OnChangeThreshold( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 6 | const std::string& triggerIdIn, Sensors sensorsIn, |
| 7 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn, |
| 8 | std::unique_ptr<interfaces::Clock> clockIn) : |
Patrick Williams | f535cad | 2024-08-16 15:21:20 -0400 | [diff] [blame] | 9 | triggerId(triggerIdIn), sensors(std::move(sensorsIn)), |
| 10 | actions(std::move(actionsIn)), clock(std::move(clockIn)) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 11 | {} |
| 12 | |
| 13 | void OnChangeThreshold::initialize() |
| 14 | { |
| 15 | for (auto& sensor : sensors) |
| 16 | { |
| 17 | sensor->registerForUpdates(weak_from_this()); |
| 18 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 19 | initialized = true; |
| 20 | } |
| 21 | |
| 22 | void 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 Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 55 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 56 | { |
Szymon Dompke | 620c65a | 2022-03-23 21:09:27 +0100 | [diff] [blame] | 57 | if (isFirstReading) |
| 58 | { |
| 59 | isFirstReading = false; |
| 60 | return; |
| 61 | } |
| 62 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 63 | commit(sensor.getName(), value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 66 | void OnChangeThreshold::commit(const std::string& sensorName, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 67 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 68 | Milliseconds timestamp = clock->systemTimestamp(); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 69 | for (const auto& action : actions) |
| 70 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 71 | action->commit(triggerId, std::nullopt, sensorName, timestamp, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 74 | |
| 75 | LabeledThresholdParam OnChangeThreshold::getThresholdParam() const |
| 76 | { |
| 77 | return {}; |
| 78 | } |