blob: 3b0d1018398ffa511ee25aa94d2cb15b9479a408 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#pragma once
2
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02003#include "interfaces/clock.hpp"
Szymon Dompkef763c9e2021-03-12 09:19:22 +01004#include "interfaces/sensor.hpp"
5#include "interfaces/sensor_listener.hpp"
6#include "interfaces/threshold.hpp"
7#include "interfaces/trigger_action.hpp"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01008#include "types/duration_types.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00009#include "types/trigger_types.hpp"
Szymon Dompke94f71c52021-12-10 07:16:33 +010010#include "utils/threshold_operations.hpp"
Szymon Dompkef763c9e2021-03-12 09:19:22 +010011
12#include <boost/asio/steady_timer.hpp>
13
14#include <chrono>
Szymon Dompke94f71c52021-12-10 07:16:33 +010015#include <map>
Szymon Dompkef763c9e2021-03-12 09:19:22 +010016#include <memory>
17#include <vector>
18
19class DiscreteThreshold :
20 public interfaces::Threshold,
21 public interfaces::SensorListener,
22 public std::enable_shared_from_this<DiscreteThreshold>
23{
24 public:
25 DiscreteThreshold(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020026 boost::asio::io_context& ioc, const std::string& triggerId,
27 Sensors sensors,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010028 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
Szymon Dompkeaa572362022-03-23 16:31:24 +010029 Milliseconds dwellTime, const std::string& thresholdValue,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020030 const std::string& name, const discrete::Severity severity,
31 std::unique_ptr<interfaces::Clock> clock);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010032 DiscreteThreshold(const DiscreteThreshold&) = delete;
33 DiscreteThreshold(DiscreteThreshold&&) = delete;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010034
35 void initialize() override;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010036 void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override;
Szymon Dompke94f71c52021-12-10 07:16:33 +010037 LabeledThresholdParam getThresholdParam() const override;
38 void updateSensors(Sensors newSensors) override;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010039
40 private:
41 boost::asio::io_context& ioc;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020042 const std::string& triggerId;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010043 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000044 const Milliseconds dwellTime;
Szymon Dompkeaa572362022-03-23 16:31:24 +010045 const std::string thresholdValue;
46 const double numericThresholdValue;
Szymon Dompke94f71c52021-12-10 07:16:33 +010047 const discrete::Severity severity;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020048 const std::string name;
Szymon Dompke94f71c52021-12-10 07:16:33 +010049 bool initialized = false;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020050 std::unique_ptr<interfaces::Clock> clock;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010051
52 struct ThresholdDetail
53 {
Szymon Dompke162b9762022-06-07 12:45:48 +020054 bool dwell = false;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010055 boost::asio::steady_timer timer;
56
Szymon Dompke162b9762022-06-07 12:45:48 +020057 ThresholdDetail(const std::string& sensorNameIn,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058 boost::asio::io_context& ioc) :
Szymon Dompke162b9762022-06-07 12:45:48 +020059 timer(ioc),
60 sensorName(sensorNameIn)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010061 {}
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020062 ThresholdDetail(const ThresholdDetail&) = delete;
63 ThresholdDetail(ThresholdDetail&&) = delete;
Szymon Dompke162b9762022-06-07 12:45:48 +020064
65 const std::string& getSensorName()
66 {
67 return sensorName;
68 }
69
70 private:
71 std::string sensorName;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010072 };
Szymon Dompke94f71c52021-12-10 07:16:33 +010073 using SensorDetails =
74 std::unordered_map<std::shared_ptr<interfaces::Sensor>,
75 std::shared_ptr<ThresholdDetail>>;
76 SensorDetails sensorDetails;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010077
Szymon Dompke94f71c52021-12-10 07:16:33 +010078 friend ThresholdOperations;
79
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020080 void startTimer(ThresholdDetail&, double);
81 void commit(const std::string&, double);
Szymon Dompke94f71c52021-12-10 07:16:33 +010082 ThresholdDetail& getDetails(const interfaces::Sensor& sensor);
83 std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020084 std::string getNonEmptyName(const std::string& nameIn) const;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010085};