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 ThresholdDetailDiscreteThreshold::ThresholdDetail57 ThresholdDetail(const std::string& sensorNameIn, 58 boost::asio::io_context& ioc) : 59 timer(ioc), sensorName(sensorNameIn) 60 {} 61 ThresholdDetail(const ThresholdDetail&) = delete; 62 ThresholdDetail(ThresholdDetail&&) = delete; 63 getSensorNameDiscreteThreshold::ThresholdDetail64 const std::string& getSensorName() 65 { 66 return sensorName; 67 } 68 69 private: 70 std::string sensorName; 71 }; 72 using SensorDetails = 73 std::unordered_map<std::shared_ptr<interfaces::Sensor>, 74 std::shared_ptr<ThresholdDetail>>; 75 SensorDetails sensorDetails; 76 77 friend ThresholdOperations; 78 79 void startTimer(ThresholdDetail&, double); 80 void commit(const std::string&, double); 81 ThresholdDetail& getDetails(const interfaces::Sensor& sensor); 82 std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName); 83 std::string getNonEmptyName(const std::string& nameIn) const; 84 }; 85