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