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