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); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 32 | ~NumericThreshold() |
| 33 | {} |
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 | { |
| 53 | std::string sensorName; |
| 54 | double prevValue; |
| 55 | bool dwell; |
| 56 | boost::asio::steady_timer timer; |
| 57 | |
| 58 | ThresholdDetail(const std::string& name, double prevValue, bool dwell, |
| 59 | boost::asio::io_context& ioc) : |
| 60 | sensorName(name), |
| 61 | prevValue(prevValue), dwell(dwell), timer(ioc) |
| 62 | {} |
| 63 | }; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 64 | using SensorDetails = |
| 65 | std::unordered_map<std::shared_ptr<interfaces::Sensor>, |
| 66 | std::shared_ptr<ThresholdDetail>>; |
| 67 | SensorDetails sensorDetails; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 68 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 69 | friend ThresholdOperations; |
| 70 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 71 | void startTimer(ThresholdDetail&, double); |
| 72 | void commit(const std::string&, double); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 73 | ThresholdDetail& getDetails(const interfaces::Sensor& sensor); |
| 74 | std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 75 | }; |