Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +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 | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 7 | #include "types/milliseconds.hpp" |
| 8 | #include "types/trigger_types.hpp" |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 9 | |
| 10 | #include <boost/asio/steady_timer.hpp> |
| 11 | |
| 12 | #include <chrono> |
| 13 | #include <memory> |
| 14 | #include <vector> |
| 15 | |
| 16 | class NumericThreshold : |
| 17 | public interfaces::Threshold, |
| 18 | public interfaces::SensorListener, |
| 19 | public std::enable_shared_from_this<NumericThreshold> |
| 20 | { |
| 21 | public: |
| 22 | NumericThreshold( |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 23 | boost::asio::io_context& ioc, Sensors sensors, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 24 | std::vector<std::string> sensorNames, |
| 25 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 26 | Milliseconds dwellTime, numeric::Direction direction, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 27 | double thresholdValue); |
| 28 | ~NumericThreshold(); |
| 29 | |
| 30 | void initialize() override; |
| 31 | void sensorUpdated(interfaces::Sensor&, uint64_t) override; |
| 32 | void sensorUpdated(interfaces::Sensor&, uint64_t, double) override; |
| 33 | |
| 34 | private: |
| 35 | boost::asio::io_context& ioc; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 36 | const Sensors sensors; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 37 | const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 38 | const Milliseconds dwellTime; |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 39 | const numeric::Direction direction; |
| 40 | const double thresholdValue; |
| 41 | |
| 42 | struct ThresholdDetail |
| 43 | { |
| 44 | std::string sensorName; |
| 45 | double prevValue; |
| 46 | bool dwell; |
| 47 | boost::asio::steady_timer timer; |
| 48 | |
| 49 | ThresholdDetail(const std::string& name, double prevValue, bool dwell, |
| 50 | boost::asio::io_context& ioc) : |
| 51 | sensorName(name), |
| 52 | prevValue(prevValue), dwell(dwell), timer(ioc) |
| 53 | {} |
| 54 | }; |
| 55 | std::vector<ThresholdDetail> details; |
| 56 | |
| 57 | void startTimer(const std::string&, uint64_t, double, bool&, |
| 58 | boost::asio::steady_timer&); |
| 59 | void commit(const std::string&, uint64_t, double); |
| 60 | ThresholdDetail& getDetails(interfaces::Sensor& sensor); |
| 61 | }; |