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); |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 32 | NumericThreshold(const NumericThreshold&) = delete; |
| 33 | NumericThreshold(NumericThreshold&&) = delete; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 34 | |
| 35 | void initialize() override; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 36 | void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 37 | LabeledThresholdParam getThresholdParam() const override; |
| 38 | void updateSensors(Sensors newSensors) override; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | boost::asio::io_context& ioc; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 42 | const std::string& triggerId; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 43 | const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 44 | const Milliseconds dwellTime; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 45 | const numeric::Direction direction; |
| 46 | const double thresholdValue; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 47 | const numeric::Type type; |
| 48 | bool initialized = false; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 49 | std::unique_ptr<interfaces::Clock> clock; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 50 | |
| 51 | struct ThresholdDetail |
| 52 | { |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 53 | std::optional<double> prevValue = std::nullopt; |
| 54 | numeric::Direction prevDirection = numeric::Direction::either; |
| 55 | bool dwell = false; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 56 | boost::asio::steady_timer timer; |
| 57 | |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 58 | ThresholdDetail(const std::string& sensorNameIn, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 59 | boost::asio::io_context& ioc) : |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 60 | timer(ioc), |
| 61 | sensorName(sensorNameIn) |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 62 | {} |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 63 | ThresholdDetail(const ThresholdDetail&) = delete; |
| 64 | ThresholdDetail(ThresholdDetail&&) = delete; |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 65 | |
| 66 | const std::string& getSensorName() |
| 67 | { |
| 68 | return sensorName; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | std::string sensorName; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 73 | }; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 74 | using SensorDetails = |
| 75 | std::unordered_map<std::shared_ptr<interfaces::Sensor>, |
| 76 | std::shared_ptr<ThresholdDetail>>; |
| 77 | SensorDetails sensorDetails; |
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 | friend ThresholdOperations; |
| 80 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 81 | void startTimer(ThresholdDetail&, double); |
| 82 | void commit(const std::string&, double); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 83 | ThresholdDetail& getDetails(const interfaces::Sensor& sensor); |
| 84 | std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 85 | }; |