1f763c9e3SSzymon Dompke #pragma once
2f763c9e3SSzymon Dompke 
3b7b7e1b6SSzymon Dompke #include "interfaces/clock.hpp"
4f763c9e3SSzymon Dompke #include "interfaces/sensor.hpp"
5f763c9e3SSzymon Dompke #include "interfaces/sensor_listener.hpp"
6f763c9e3SSzymon Dompke #include "interfaces/threshold.hpp"
7f763c9e3SSzymon Dompke #include "interfaces/trigger_action.hpp"
851f0fd50SKrzysztof Grobelny #include "types/duration_types.hpp"
9dcc4e193SKrzysztof Grobelny #include "types/trigger_types.hpp"
1094f71c51SSzymon Dompke #include "utils/threshold_operations.hpp"
11f763c9e3SSzymon Dompke 
12f763c9e3SSzymon Dompke #include <boost/asio/steady_timer.hpp>
13f763c9e3SSzymon Dompke 
14f763c9e3SSzymon Dompke #include <chrono>
1594f71c51SSzymon Dompke #include <map>
16f763c9e3SSzymon Dompke #include <memory>
17f763c9e3SSzymon Dompke #include <vector>
18f763c9e3SSzymon Dompke 
19f763c9e3SSzymon Dompke class DiscreteThreshold :
20f763c9e3SSzymon Dompke     public interfaces::Threshold,
21f763c9e3SSzymon Dompke     public interfaces::SensorListener,
22f763c9e3SSzymon Dompke     public std::enable_shared_from_this<DiscreteThreshold>
23f763c9e3SSzymon Dompke {
24f763c9e3SSzymon Dompke   public:
25f763c9e3SSzymon Dompke     DiscreteThreshold(
26b7b7e1b6SSzymon Dompke         boost::asio::io_context& ioc, const std::string& triggerId,
27b7b7e1b6SSzymon Dompke         Sensors sensors,
28f763c9e3SSzymon Dompke         std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
29aa572361SSzymon Dompke         Milliseconds dwellTime, const std::string& thresholdValue,
30b7b7e1b6SSzymon Dompke         const std::string& name, const discrete::Severity severity,
31b7b7e1b6SSzymon Dompke         std::unique_ptr<interfaces::Clock> clock);
32f763c9e3SSzymon Dompke     DiscreteThreshold(const DiscreteThreshold&) = delete;
33f763c9e3SSzymon Dompke     DiscreteThreshold(DiscreteThreshold&&) = delete;
34f763c9e3SSzymon Dompke 
35f763c9e3SSzymon Dompke     void initialize() override;
3651f0fd50SKrzysztof Grobelny     void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override;
3794f71c51SSzymon Dompke     LabeledThresholdParam getThresholdParam() const override;
3894f71c51SSzymon Dompke     void updateSensors(Sensors newSensors) override;
39f763c9e3SSzymon Dompke 
40f763c9e3SSzymon Dompke   private:
41f763c9e3SSzymon Dompke     boost::asio::io_context& ioc;
42b7b7e1b6SSzymon Dompke     const std::string& triggerId;
43f763c9e3SSzymon Dompke     const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
44dcc4e193SKrzysztof Grobelny     const Milliseconds dwellTime;
45aa572361SSzymon Dompke     const std::string thresholdValue;
46aa572361SSzymon Dompke     const double numericThresholdValue;
4794f71c51SSzymon Dompke     const discrete::Severity severity;
48b7b7e1b6SSzymon Dompke     const std::string name;
4994f71c51SSzymon Dompke     bool initialized = false;
50b7b7e1b6SSzymon Dompke     std::unique_ptr<interfaces::Clock> clock;
51f763c9e3SSzymon Dompke 
52f763c9e3SSzymon Dompke     struct ThresholdDetail
53f763c9e3SSzymon Dompke     {
54f763c9e3SSzymon Dompke         std::string sensorName;
55f763c9e3SSzymon Dompke         bool dwell;
56f763c9e3SSzymon Dompke         boost::asio::steady_timer timer;
57f763c9e3SSzymon Dompke 
58f763c9e3SSzymon Dompke         ThresholdDetail(const std::string& name, bool dwell,
59f763c9e3SSzymon Dompke                         boost::asio::io_context& ioc) :
60f763c9e3SSzymon Dompke             sensorName(name),
61f763c9e3SSzymon Dompke             dwell(dwell), timer(ioc)
62f763c9e3SSzymon Dompke         {}
63*41fa80d8SKrzysztof Grobelny         ThresholdDetail(const ThresholdDetail&) = delete;
64*41fa80d8SKrzysztof Grobelny         ThresholdDetail(ThresholdDetail&&) = delete;
65f763c9e3SSzymon Dompke     };
6694f71c51SSzymon Dompke     using SensorDetails =
6794f71c51SSzymon Dompke         std::unordered_map<std::shared_ptr<interfaces::Sensor>,
6894f71c51SSzymon Dompke                            std::shared_ptr<ThresholdDetail>>;
6994f71c51SSzymon Dompke     SensorDetails sensorDetails;
70f763c9e3SSzymon Dompke 
7194f71c51SSzymon Dompke     friend ThresholdOperations;
7294f71c51SSzymon Dompke 
73b7b7e1b6SSzymon Dompke     void startTimer(ThresholdDetail&, double);
74b7b7e1b6SSzymon Dompke     void commit(const std::string&, double);
7594f71c51SSzymon Dompke     ThresholdDetail& getDetails(const interfaces::Sensor& sensor);
7694f71c51SSzymon Dompke     std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName);
77b7b7e1b6SSzymon Dompke     std::string getNonEmptyName(const std::string& nameIn) const;
78f763c9e3SSzymon Dompke };
79