Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 3 | #include "interfaces/clock.hpp" |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +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" |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +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> |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <vector> |
| 18 | |
| 19 | class DiscreteThreshold : |
| 20 | public interfaces::Threshold, |
| 21 | public interfaces::SensorListener, |
| 22 | public std::enable_shared_from_this<DiscreteThreshold> |
| 23 | { |
| 24 | public: |
| 25 | DiscreteThreshold( |
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, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 28 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions, |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 29 | Milliseconds dwellTime, const std::string& thresholdValue, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 30 | const std::string& name, const discrete::Severity severity, |
| 31 | std::unique_ptr<interfaces::Clock> clock); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 32 | DiscreteThreshold(const DiscreteThreshold&) = delete; |
| 33 | DiscreteThreshold(DiscreteThreshold&&) = delete; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +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; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +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; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +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; |
Szymon Dompke | aa57236 | 2022-03-23 16:31:24 +0100 | [diff] [blame] | 45 | const std::string thresholdValue; |
| 46 | const double numericThresholdValue; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 47 | const discrete::Severity severity; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 48 | const std::string name; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 49 | bool initialized = false; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 50 | std::unique_ptr<interfaces::Clock> clock; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 51 | |
| 52 | struct ThresholdDetail |
| 53 | { |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 54 | bool dwell = false; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 55 | boost::asio::steady_timer timer; |
| 56 | |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 57 | ThresholdDetail(const std::string& sensorNameIn, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 58 | boost::asio::io_context& ioc) : |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 59 | timer(ioc), |
| 60 | sensorName(sensorNameIn) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 61 | {} |
Krzysztof Grobelny | 41fa80d | 2022-06-09 13:27:16 +0200 | [diff] [blame] | 62 | ThresholdDetail(const ThresholdDetail&) = delete; |
| 63 | ThresholdDetail(ThresholdDetail&&) = delete; |
Szymon Dompke | 162b976 | 2022-06-07 12:45:48 +0200 | [diff] [blame] | 64 | |
| 65 | const std::string& getSensorName() |
| 66 | { |
| 67 | return sensorName; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::string sensorName; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 72 | }; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 73 | using SensorDetails = |
| 74 | std::unordered_map<std::shared_ptr<interfaces::Sensor>, |
| 75 | std::shared_ptr<ThresholdDetail>>; |
| 76 | SensorDetails sensorDetails; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 77 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 78 | friend ThresholdOperations; |
| 79 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 80 | void startTimer(ThresholdDetail&, double); |
| 81 | void commit(const std::string&, double); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 82 | ThresholdDetail& getDetails(const interfaces::Sensor& sensor); |
| 83 | std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 84 | std::string getNonEmptyName(const std::string& nameIn) const; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 85 | }; |