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 | { |
| 42 | return std::make_shared<ThresholdDetail>(sensorName, thresholdValue, false, |
| 43 | ioc); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 47 | Milliseconds timestamp, double value) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 48 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 49 | auto& details = getDetails(sensor); |
| 50 | auto& [sensorName, prevValue, dwell, timer] = details; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 51 | bool decreasing = thresholdValue < prevValue && thresholdValue > value; |
| 52 | bool increasing = thresholdValue > prevValue && thresholdValue < value; |
| 53 | |
| 54 | if (dwell && (increasing || decreasing)) |
| 55 | { |
| 56 | timer.cancel(); |
| 57 | dwell = false; |
| 58 | } |
| 59 | if ((direction == numeric::Direction::decreasing && decreasing) || |
| 60 | (direction == numeric::Direction::increasing && increasing) || |
| 61 | (direction == numeric::Direction::either && (increasing || decreasing))) |
| 62 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 63 | startTimer(details, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | prevValue = value; |
| 67 | } |
| 68 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 69 | void NumericThreshold::startTimer(NumericThreshold::ThresholdDetail& details, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 70 | double value) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 71 | { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 72 | const auto& sensorName = details.sensorName; |
| 73 | auto& dwell = details.dwell; |
| 74 | auto& timer = details.timer; |
| 75 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 76 | if (dwellTime == Milliseconds::zero()) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 77 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 78 | commit(sensorName, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 79 | } |
| 80 | else |
| 81 | { |
| 82 | dwell = true; |
| 83 | timer.expires_after(dwellTime); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 84 | timer.async_wait([this, &sensorName, &dwell, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 85 | value](const boost::system::error_code ec) { |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 86 | if (ec) |
| 87 | { |
| 88 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 89 | "Timer has been canceled"); |
| 90 | return; |
| 91 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 92 | commit(sensorName, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 93 | dwell = false; |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 98 | void NumericThreshold::commit(const std::string& sensorName, double value) |
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 | Milliseconds timestamp = clock->systemTimestamp(); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 101 | for (const auto& action : actions) |
| 102 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 103 | action->commit(triggerId, std::nullopt, sensorName, timestamp, value); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 106 | |
| 107 | LabeledThresholdParam NumericThreshold::getThresholdParam() const |
| 108 | { |
| 109 | return numeric::LabeledThresholdParam(type, dwellTime.count(), direction, |
| 110 | thresholdValue); |
| 111 | } |