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 | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 6 | Sensors sensorsIn, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 7 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn) : |
| 8 | sensors(std::move(sensorsIn)), |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 9 | actions(std::move(actionsIn)) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 10 | {} |
| 11 | |
| 12 | void OnChangeThreshold::initialize() |
| 13 | { |
| 14 | for (auto& sensor : sensors) |
| 15 | { |
| 16 | sensor->registerForUpdates(weak_from_this()); |
| 17 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 18 | initialized = true; |
| 19 | } |
| 20 | |
| 21 | void OnChangeThreshold::updateSensors(Sensors newSensors) |
| 22 | { |
| 23 | Sensors oldSensors = sensors; |
| 24 | |
| 25 | for (const auto& sensor : newSensors) |
| 26 | { |
| 27 | auto it = |
| 28 | std::find_if(oldSensors.begin(), oldSensors.end(), |
| 29 | [&sensor](const auto& s) { return sensor == s; }); |
| 30 | if (it != oldSensors.end()) |
| 31 | { |
| 32 | oldSensors.erase(it); |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | if (initialized) |
| 37 | { |
| 38 | sensor->registerForUpdates(weak_from_this()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (initialized) |
| 43 | { |
| 44 | for (auto& sensor : oldSensors) |
| 45 | { |
| 46 | sensor->unregisterFromUpdates(weak_from_this()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | sensors = std::move(newSensors); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void OnChangeThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 54 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 55 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 56 | commit(sensor.getName(), timestamp, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void OnChangeThreshold::commit(const std::string& sensorName, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 60 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 61 | { |
| 62 | for (const auto& action : actions) |
| 63 | { |
| 64 | action->commit(sensorName, timestamp, value); |
| 65 | } |
| 66 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 67 | |
| 68 | LabeledThresholdParam OnChangeThreshold::getThresholdParam() const |
| 69 | { |
| 70 | return {}; |
| 71 | } |