Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 1 | #include "numeric_threshold.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | NumericThreshold::NumericThreshold( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 6 | boost::asio::io_context& ioc, const std::string& triggerIdIn, |
| 7 | Sensors sensorsIn, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 8 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 9 | Milliseconds dwellTimeIn, numeric::Direction directionIn, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 10 | double thresholdValueIn, numeric::Type typeIn, |
| 11 | std::unique_ptr<interfaces::Clock> clockIn) : |
Patrick Williams | f535cad | 2024-08-16 15:21:20 -0400 | [diff] [blame] | 12 | ioc(ioc), triggerId(triggerIdIn), actions(std::move(actionsIn)), |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 13 | dwellTime(dwellTimeIn), direction(directionIn), |
| 14 | thresholdValue(thresholdValueIn), type(typeIn), clock(std::move(clockIn)) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 15 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 16 | for (const auto& sensor : sensorsIn) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 17 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 18 | sensorDetails.emplace(sensor, makeDetails(sensor->getName())); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 19 | } |
| 20 | } |
| 21 | |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 22 | void NumericThreshold::initialize() |
| 23 | { |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 24 | ThresholdOperations::initialize(this); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void NumericThreshold::updateSensors(Sensors newSensors) |
| 28 | { |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 29 | ThresholdOperations::updateSensors(this, std::move(newSensors)); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | NumericThreshold::ThresholdDetail& |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 33 | NumericThreshold::getDetails(const interfaces::Sensor& sensor) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 34 | { |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 35 | return ThresholdOperations::getDetails(this, sensor); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | std::shared_ptr<NumericThreshold::ThresholdDetail> |
| 39 | NumericThreshold::makeDetails(const std::string& sensorName) |
| 40 | { |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 41 | return std::make_shared<ThresholdDetail>(sensorName, ioc); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 45 | Milliseconds timestamp, double value) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 46 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 47 | auto& details = getDetails(sensor); |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 48 | auto& prevValue = details.prevValue; |
| 49 | auto& prevDirection = details.prevDirection; |
| 50 | auto& dwell = details.dwell; |
| 51 | auto& timer = details.timer; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 52 | |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 53 | if (!prevValue) |
| 54 | { |
| 55 | prevValue = value; |
| 56 | return; |
| 57 | } |
| 58 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 59 | bool crossedDecreasing = thresholdValue < prevValue && |
| 60 | thresholdValue > value; |
| 61 | bool crossedIncreasing = thresholdValue > prevValue && |
| 62 | thresholdValue < value; |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 63 | |
| 64 | if (!crossedDecreasing && !crossedIncreasing && thresholdValue == prevValue) |
| 65 | { |
| 66 | crossedDecreasing = prevDirection == numeric::Direction::decreasing && |
| 67 | thresholdValue > value; |
| 68 | crossedIncreasing = prevDirection == numeric::Direction::increasing && |
| 69 | thresholdValue < value; |
| 70 | } |
| 71 | |
| 72 | if (dwell && (crossedIncreasing || crossedDecreasing)) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 73 | { |
| 74 | timer.cancel(); |
| 75 | dwell = false; |
| 76 | } |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 77 | if ((direction == numeric::Direction::decreasing && crossedDecreasing) || |
| 78 | (direction == numeric::Direction::increasing && crossedIncreasing) || |
| 79 | (direction == numeric::Direction::either && |
| 80 | (crossedIncreasing || crossedDecreasing))) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 81 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 82 | startTimer(details, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 85 | prevDirection = value > prevValue ? numeric::Direction::increasing |
| 86 | : value < prevValue ? numeric::Direction::decreasing |
| 87 | : numeric::Direction::either; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 88 | prevValue = value; |
| 89 | } |
| 90 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 91 | void NumericThreshold::startTimer(NumericThreshold::ThresholdDetail& details, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 92 | double value) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 93 | { |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 94 | auto& sensorName = details.getSensorName(); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 95 | auto& dwell = details.dwell; |
| 96 | auto& timer = details.timer; |
| 97 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 98 | if (dwellTime == Milliseconds::zero()) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 99 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 100 | commit(sensorName, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
| 104 | dwell = true; |
| 105 | timer.expires_after(dwellTime); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 106 | timer.async_wait([this, &sensorName, &dwell, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 107 | value](const boost::system::error_code ec) { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 108 | if (ec) |
| 109 | { |
| 110 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 111 | "Timer has been canceled"); |
| 112 | return; |
| 113 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 114 | commit(sensorName, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 115 | dwell = false; |
| 116 | }); |
| 117 | } |
| 118 | } |
| 119 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 120 | void NumericThreshold::commit(const std::string& sensorName, double value) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 121 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 122 | Milliseconds timestamp = clock->systemTimestamp(); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 123 | for (const auto& action : actions) |
| 124 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 125 | action->commit(triggerId, std::nullopt, sensorName, timestamp, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 128 | |
| 129 | LabeledThresholdParam NumericThreshold::getThresholdParam() const |
| 130 | { |
| 131 | return numeric::LabeledThresholdParam(type, dwellTime.count(), direction, |
| 132 | thresholdValue); |
| 133 | } |