xref: /openbmc/telemetry/src/numeric_threshold.hpp (revision f7ea2997ae6b4fad0dc35f9afa9b488d774f2ee1)
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"
7 #include "types/duration_types.hpp"
8 #include "types/trigger_types.hpp"
9 #include "utils/threshold_operations.hpp"
10 
11 #include <boost/asio/steady_timer.hpp>
12 
13 #include <chrono>
14 #include <map>
15 #include <memory>
16 #include <vector>
17 
18 class NumericThreshold :
19     public interfaces::Threshold,
20     public interfaces::SensorListener,
21     public std::enable_shared_from_this<NumericThreshold>
22 {
23   public:
24     NumericThreshold(
25         boost::asio::io_context& ioc, Sensors sensors,
26         std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
27         Milliseconds dwellTime, numeric::Direction direction,
28         double thresholdValue, numeric::Type type);
29     ~NumericThreshold()
30     {}
31 
32     void initialize() override;
33     void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override;
34     LabeledThresholdParam getThresholdParam() const override;
35     void updateSensors(Sensors newSensors) override;
36 
37   private:
38     boost::asio::io_context& ioc;
39     const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
40     const Milliseconds dwellTime;
41     const numeric::Direction direction;
42     const double thresholdValue;
43     const numeric::Type type;
44     bool initialized = false;
45 
46     struct ThresholdDetail
47     {
48         std::string sensorName;
49         double prevValue;
50         bool dwell;
51         boost::asio::steady_timer timer;
52 
53         ThresholdDetail(const std::string& name, double prevValue, bool dwell,
54                         boost::asio::io_context& ioc) :
55             sensorName(name),
56             prevValue(prevValue), dwell(dwell), timer(ioc)
57         {}
58     };
59     using SensorDetails =
60         std::unordered_map<std::shared_ptr<interfaces::Sensor>,
61                            std::shared_ptr<ThresholdDetail>>;
62     SensorDetails sensorDetails;
63 
64     friend ThresholdOperations;
65 
66     void startTimer(ThresholdDetail&, Milliseconds, double);
67     void commit(const std::string&, Milliseconds, double);
68     ThresholdDetail& getDetails(const interfaces::Sensor& sensor);
69     std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName);
70 };
71