1 #pragma once 2 3 #include "interfaces/clock.hpp" 4 #include "interfaces/sensor.hpp" 5 #include "interfaces/sensor_listener.hpp" 6 #include "interfaces/threshold.hpp" 7 #include "interfaces/trigger_action.hpp" 8 #include "types/duration_types.hpp" 9 #include "types/trigger_types.hpp" 10 #include "utils/threshold_operations.hpp" 11 12 #include <boost/asio/steady_timer.hpp> 13 14 #include <chrono> 15 #include <map> 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( 26 boost::asio::io_context& ioc, const std::string& triggerId, 27 Sensors sensors, 28 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions, 29 Milliseconds dwellTime, const std::string& thresholdValue, 30 const std::string& name, const discrete::Severity severity, 31 std::unique_ptr<interfaces::Clock> clock); 32 DiscreteThreshold(const DiscreteThreshold&) = delete; 33 DiscreteThreshold(DiscreteThreshold&&) = delete; 34 35 void initialize() override; 36 void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override; 37 LabeledThresholdParam getThresholdParam() const override; 38 void updateSensors(Sensors newSensors) override; 39 40 private: 41 boost::asio::io_context& ioc; 42 const std::string& triggerId; 43 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; 44 const Milliseconds dwellTime; 45 const std::string thresholdValue; 46 const double numericThresholdValue; 47 const discrete::Severity severity; 48 const std::string name; 49 bool initialized = false; 50 std::unique_ptr<interfaces::Clock> clock; 51 52 struct ThresholdDetail 53 { 54 bool dwell = false; 55 boost::asio::steady_timer timer; 56 57 ThresholdDetail(const std::string& sensorNameIn, 58 boost::asio::io_context& ioc) : 59 timer(ioc), 60 sensorName(sensorNameIn) 61 {} 62 ThresholdDetail(const ThresholdDetail&) = delete; 63 ThresholdDetail(ThresholdDetail&&) = delete; 64 65 const std::string& getSensorName() 66 { 67 return sensorName; 68 } 69 70 private: 71 std::string sensorName; 72 }; 73 using SensorDetails = 74 std::unordered_map<std::shared_ptr<interfaces::Sensor>, 75 std::shared_ptr<ThresholdDetail>>; 76 SensorDetails sensorDetails; 77 78 friend ThresholdOperations; 79 80 void startTimer(ThresholdDetail&, double); 81 void commit(const std::string&, double); 82 ThresholdDetail& getDetails(const interfaces::Sensor& sensor); 83 std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); 84 std::string getNonEmptyName(const std::string& nameIn) const; 85 }; 86