Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "interfaces/sensor.hpp" |
| 4 | #include "interfaces/sensor_listener.hpp" |
| 5 | #include "interfaces/threshold.hpp" |
| 6 | #include "interfaces/trigger_action.hpp" |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 7 | #include "types/duration_types.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 8 | #include "types/trigger_types.hpp" |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 9 | #include "utils/threshold_operations.hpp" |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 10 | |
| 11 | #include <boost/asio/steady_timer.hpp> |
| 12 | |
| 13 | #include <chrono> |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 14 | #include <map> |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | #include <vector> |
| 17 | |
| 18 | class DiscreteThreshold : |
| 19 | public interfaces::Threshold, |
| 20 | public interfaces::SensorListener, |
| 21 | public std::enable_shared_from_this<DiscreteThreshold> |
| 22 | { |
| 23 | public: |
| 24 | DiscreteThreshold( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 25 | boost::asio::io_context& ioc, Sensors sensors, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 26 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 27 | Milliseconds dwellTime, double thresholdValue, const std::string& name, |
| 28 | const discrete::Severity severity); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 29 | DiscreteThreshold(const DiscreteThreshold&) = delete; |
| 30 | DiscreteThreshold(DiscreteThreshold&&) = delete; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 31 | |
| 32 | void initialize() override; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 33 | void sensorUpdated(interfaces::Sensor&, Milliseconds) override; |
| 34 | void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 35 | LabeledThresholdParam getThresholdParam() const override; |
| 36 | void updateSensors(Sensors newSensors) override; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | boost::asio::io_context& ioc; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 40 | const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 41 | const Milliseconds dwellTime; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 42 | const double thresholdValue; |
| 43 | const std::string name; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 44 | const discrete::Severity severity; |
| 45 | bool initialized = false; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 46 | |
| 47 | struct ThresholdDetail |
| 48 | { |
| 49 | std::string sensorName; |
| 50 | bool dwell; |
| 51 | boost::asio::steady_timer timer; |
| 52 | |
| 53 | ThresholdDetail(const std::string& name, bool dwell, |
| 54 | boost::asio::io_context& ioc) : |
| 55 | sensorName(name), |
| 56 | dwell(dwell), timer(ioc) |
| 57 | {} |
| 58 | }; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 59 | using SensorDetails = |
| 60 | std::unordered_map<std::shared_ptr<interfaces::Sensor>, |
| 61 | std::shared_ptr<ThresholdDetail>>; |
| 62 | SensorDetails sensorDetails; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 63 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 64 | friend ThresholdOperations; |
| 65 | |
| 66 | void startTimer(ThresholdDetail&, Milliseconds, double); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 67 | void commit(const std::string&, Milliseconds, double); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame^] | 68 | ThresholdDetail& getDetails(const interfaces::Sensor& sensor); |
| 69 | std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 70 | }; |