blob: 4b861a381e439dbe59613c6c9c287a65a8cb4ce1 [file] [log] [blame]
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01001#pragma once
2
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02003#include "interfaces/clock.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +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"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010011
12#include <boost/asio/steady_timer.hpp>
13
14#include <chrono>
Szymon Dompke94f71c52021-12-10 07:16:33 +010015#include <map>
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010016#include <memory>
17#include <vector>
18
19class NumericThreshold :
20 public interfaces::Threshold,
21 public interfaces::SensorListener,
22 public std::enable_shared_from_this<NumericThreshold>
23{
24 public:
25 NumericThreshold(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020026 boost::asio::io_context& ioc, const std::string& triggerId,
27 Sensors sensors,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010028 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000029 Milliseconds dwellTime, numeric::Direction direction,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020030 double thresholdValue, numeric::Type type,
31 std::unique_ptr<interfaces::Clock> clock);
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020032 NumericThreshold(const NumericThreshold&) = delete;
33 NumericThreshold(NumericThreshold&&) = delete;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +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;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010039
40 private:
41 boost::asio::io_context& ioc;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020042 const std::string& triggerId;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010043 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000044 const Milliseconds dwellTime;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010045 const numeric::Direction direction;
46 const double thresholdValue;
Szymon Dompke94f71c52021-12-10 07:16:33 +010047 const numeric::Type type;
48 bool initialized = false;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020049 std::unique_ptr<interfaces::Clock> clock;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010050
51 struct ThresholdDetail
52 {
Szymon Dompke162b9762022-06-07 12:45:48 +020053 std::optional<double> prevValue = std::nullopt;
54 numeric::Direction prevDirection = numeric::Direction::either;
55 bool dwell = false;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010056 boost::asio::steady_timer timer;
57
Szymon Dompke162b9762022-06-07 12:45:48 +020058 ThresholdDetail(const std::string& sensorNameIn,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010059 boost::asio::io_context& ioc) :
Szymon Dompke162b9762022-06-07 12:45:48 +020060 timer(ioc),
61 sensorName(sensorNameIn)
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010062 {}
Krzysztof Grobelny41fa80d2022-06-09 13:27:16 +020063 ThresholdDetail(const ThresholdDetail&) = delete;
64 ThresholdDetail(ThresholdDetail&&) = delete;
Szymon Dompke162b9762022-06-07 12:45:48 +020065
66 const std::string& getSensorName()
67 {
68 return sensorName;
69 }
70
71 private:
72 std::string sensorName;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010073 };
Szymon Dompke94f71c52021-12-10 07:16:33 +010074 using SensorDetails =
75 std::unordered_map<std::shared_ptr<interfaces::Sensor>,
76 std::shared_ptr<ThresholdDetail>>;
77 SensorDetails sensorDetails;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010078
Szymon Dompke94f71c52021-12-10 07:16:33 +010079 friend ThresholdOperations;
80
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020081 void startTimer(ThresholdDetail&, double);
82 void commit(const std::string&, double);
Szymon Dompke94f71c52021-12-10 07:16:33 +010083 ThresholdDetail& getDetails(const interfaces::Sensor& sensor);
84 std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName);
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010085};