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