| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 3 | #include "interfaces/clock.hpp" |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 4 | #include "interfaces/sensor.hpp" |
| 5 | #include "interfaces/sensor_listener.hpp" |
| 6 | #include "interfaces/threshold.hpp" |
| 7 | #include "interfaces/trigger_action.hpp" |
| Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 8 | #include "types/duration_types.hpp" |
| Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 9 | #include "types/trigger_types.hpp" |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 10 | #include "utils/threshold_operations.hpp" |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 11 | |
| 12 | #include <boost/asio/steady_timer.hpp> |
| 13 | |
| 14 | #include <chrono> |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 15 | #include <map> |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <vector> |
| 18 | |
| 19 | class NumericThreshold : |
| 20 | public interfaces::Threshold, |
| 21 | public interfaces::SensorListener, |
| 22 | public std::enable_shared_from_this<NumericThreshold> |
| 23 | { |
| 24 | public: |
| 25 | NumericThreshold( |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 26 | boost::asio::io_context& ioc, const std::string& triggerId, |
| 27 | Sensors sensors, |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 28 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions, |
| Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 29 | Milliseconds dwellTime, numeric::Direction direction, |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 30 | double thresholdValue, numeric::Type type, |
| 31 | std::unique_ptr<interfaces::Clock> clock); |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 32 | ~NumericThreshold() = default; |
| Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 33 | NumericThreshold(const NumericThreshold&) = delete; |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 34 | NumericThreshold& operator=(const NumericThreshold&) = delete; |
| Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 35 | NumericThreshold(NumericThreshold&&) = delete; |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 36 | NumericThreshold& operator=(NumericThreshold&&) = delete; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 37 | |
| 38 | void initialize() override; |
| Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 39 | void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override; |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 40 | LabeledThresholdParam getThresholdParam() const override; |
| 41 | void updateSensors(Sensors newSensors) override; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 42 | |
| 43 | private: |
| 44 | boost::asio::io_context& ioc; |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 45 | const std::string& triggerId; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 46 | const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
| Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 47 | const Milliseconds dwellTime; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 48 | const numeric::Direction direction; |
| 49 | const double thresholdValue; |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 50 | const numeric::Type type; |
| 51 | bool initialized = false; |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 52 | std::unique_ptr<interfaces::Clock> clock; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 53 | |
| 54 | struct ThresholdDetail |
| 55 | { |
| Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 56 | std::optional<double> prevValue = std::nullopt; |
| 57 | numeric::Direction prevDirection = numeric::Direction::either; |
| 58 | bool dwell = false; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 59 | boost::asio::steady_timer timer; |
| 60 | |
| Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 61 | ThresholdDetail(const std::string& sensorNameIn, |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 62 | boost::asio::io_context& ioc) : |
| Patrick Williams | f535cad | 2024-08-16 15:21:20 -0400 | [diff] [blame] | 63 | timer(ioc), sensorName(sensorNameIn) |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 64 | {} |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 65 | ~ThresholdDetail() = default; |
| Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 66 | ThresholdDetail(const ThresholdDetail&) = delete; |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 67 | ThresholdDetail& operator=(const ThresholdDetail&) = delete; |
| Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 68 | ThresholdDetail(ThresholdDetail&&) = delete; |
| Piotr Sulewski | c1dbac1 | 2025-11-12 14:14:09 +0100 | [diff] [blame^] | 69 | ThresholdDetail& operator=(ThresholdDetail&&) = delete; |
| Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 70 | |
| 71 | const std::string& getSensorName() |
| 72 | { |
| 73 | return sensorName; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::string sensorName; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 78 | }; |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 79 | using SensorDetails = |
| 80 | std::unordered_map<std::shared_ptr<interfaces::Sensor>, |
| 81 | std::shared_ptr<ThresholdDetail>>; |
| 82 | SensorDetails sensorDetails; |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 83 | |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 84 | friend ThresholdOperations; |
| 85 | |
| Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 86 | void startTimer(ThresholdDetail&, double); |
| 87 | void commit(const std::string&, double); |
| Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 88 | ThresholdDetail& getDetails(const interfaces::Sensor& sensor); |
| 89 | std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); |
| Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 90 | }; |