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