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