Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #include "discrete_threshold.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | DiscreteThreshold::DiscreteThreshold( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 6 | boost::asio::io_context& ioc, Sensors sensorsIn, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 7 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn, |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 8 | Milliseconds dwellTimeIn, double thresholdValueIn, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 9 | const std::string& nameIn, const discrete::Severity severityIn) : |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 10 | ioc(ioc), |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 11 | actions(std::move(actionsIn)), dwellTime(dwellTimeIn), |
| 12 | thresholdValue(thresholdValueIn), name(nameIn), severity(severityIn) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 13 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 14 | for (const auto& sensor : sensorsIn) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 15 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 16 | sensorDetails.emplace(sensor, makeDetails(sensor->getName())); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 17 | } |
| 18 | } |
| 19 | |
| 20 | void DiscreteThreshold::initialize() |
| 21 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 22 | ThresholdOperations().initialize(this); |
| 23 | } |
| 24 | |
| 25 | void DiscreteThreshold::updateSensors(Sensors newSensors) |
| 26 | { |
| 27 | ThresholdOperations().updateSensors(this, std::move(newSensors)); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | DiscreteThreshold::ThresholdDetail& |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 31 | DiscreteThreshold::getDetails(const interfaces::Sensor& sensor) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 32 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 33 | return ThresholdOperations().getDetails(this, sensor); |
| 34 | } |
| 35 | |
| 36 | std::shared_ptr<DiscreteThreshold::ThresholdDetail> |
| 37 | DiscreteThreshold::makeDetails(const std::string& sensorName) |
| 38 | { |
| 39 | return std::make_shared<ThresholdDetail>(sensorName, false, ioc); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 43 | Milliseconds timestamp) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 44 | {} |
| 45 | |
| 46 | void DiscreteThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 47 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 48 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 49 | auto& details = getDetails(sensor); |
| 50 | auto& [sensorName, dwell, timer] = details; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 51 | |
| 52 | if (thresholdValue) |
| 53 | { |
| 54 | if (dwell && value != thresholdValue) |
| 55 | { |
| 56 | timer.cancel(); |
| 57 | dwell = false; |
| 58 | } |
| 59 | else if (value == thresholdValue) |
| 60 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 61 | startTimer(details, timestamp, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 66 | void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 67 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 68 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 69 | const auto& sensorName = details.sensorName; |
| 70 | auto& dwell = details.dwell; |
| 71 | auto& timer = details.timer; |
| 72 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 73 | if (dwellTime == Milliseconds::zero()) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 74 | { |
| 75 | commit(sensorName, timestamp, value); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | dwell = true; |
| 80 | timer.expires_after(dwellTime); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 81 | timer.async_wait([this, &sensorName, &dwell, timestamp, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 82 | value](const boost::system::error_code ec) { |
| 83 | if (ec) |
| 84 | { |
| 85 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 86 | "Timer has been canceled"); |
| 87 | return; |
| 88 | } |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 89 | commit(sensorName, timestamp, value); |
| 90 | dwell = false; |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void DiscreteThreshold::commit(const std::string& sensorName, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 96 | Milliseconds timestamp, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 97 | { |
| 98 | for (const auto& action : actions) |
| 99 | { |
| 100 | action->commit(sensorName, timestamp, value); |
| 101 | } |
| 102 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 103 | |
| 104 | LabeledThresholdParam DiscreteThreshold::getThresholdParam() const |
| 105 | { |
| 106 | return discrete::LabeledThresholdParam(name, severity, dwellTime.count(), |
| 107 | std::to_string(thresholdValue)); |
| 108 | } |