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( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 8 | boost::asio::io_context& ioc, const std::string& triggerIdIn, |
| 9 | Sensors sensorsIn, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 10 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn, |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 11 | Milliseconds dwellTimeIn, const std::string& thresholdValueIn, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 12 | const std::string& nameIn, const discrete::Severity severityIn, |
| 13 | std::unique_ptr<interfaces::Clock> clockIn) : |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 14 | ioc(ioc), |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 15 | triggerId(triggerIdIn), actions(std::move(actionsIn)), |
| 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 | { |
| 46 | return std::make_shared<ThresholdDetail>(sensorName, false, 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); |
| 53 | auto& [sensorName, dwell, timer] = details; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 54 | |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 55 | if (dwell && value != numericThresholdValue) |
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 | timer.cancel(); |
| 58 | dwell = false; |
| 59 | } |
| 60 | else if (value == numericThresholdValue) |
| 61 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 62 | startTimer(details, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 66 | void DiscreteThreshold::startTimer(DiscreteThreshold::ThresholdDetail& details, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 67 | 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 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 75 | commit(sensorName, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 76 | } |
| 77 | else |
| 78 | { |
| 79 | dwell = true; |
| 80 | timer.expires_after(dwellTime); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 81 | timer.async_wait([this, &sensorName, &dwell, |
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 | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 89 | commit(sensorName, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 90 | dwell = false; |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 95 | void DiscreteThreshold::commit(const std::string& sensorName, double value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 96 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 97 | Milliseconds timestamp = clock->systemTimestamp(); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 98 | for (const auto& action : actions) |
| 99 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 100 | action->commit(triggerId, std::cref(name), sensorName, timestamp, |
| 101 | thresholdValue); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 104 | |
| 105 | LabeledThresholdParam DiscreteThreshold::getThresholdParam() const |
| 106 | { |
| 107 | return discrete::LabeledThresholdParam(name, severity, dwellTime.count(), |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 108 | thresholdValue); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 109 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 110 | |
| 111 | std::string DiscreteThreshold::getNonEmptyName(const std::string& nameIn) const |
| 112 | { |
| 113 | if (nameIn.empty()) |
| 114 | { |
| 115 | return discrete::severityToString(severity) + " condition"; |
| 116 | } |
| 117 | return nameIn; |
| 118 | } |