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