blob: ecf3d6a9de0b9eac70d14a5fa859cfd6a58f1407 [file] [log] [blame]
Wludzik, Jozef1477fe62021-01-02 11:56:10 +01001#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"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01007#include "types/duration_types.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00008#include "types/trigger_types.hpp"
Szymon Dompke94f71c52021-12-10 07:16:33 +01009#include "utils/threshold_operations.hpp"
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010010
11#include <boost/asio/steady_timer.hpp>
12
13#include <chrono>
Szymon Dompke94f71c52021-12-10 07:16:33 +010014#include <map>
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010015#include <memory>
16#include <vector>
17
18class NumericThreshold :
19 public interfaces::Threshold,
20 public interfaces::SensorListener,
21 public std::enable_shared_from_this<NumericThreshold>
22{
23 public:
24 NumericThreshold(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000025 boost::asio::io_context& ioc, Sensors sensors,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010026 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000027 Milliseconds dwellTime, numeric::Direction direction,
Szymon Dompke94f71c52021-12-10 07:16:33 +010028 double thresholdValue, numeric::Type type);
29 ~NumericThreshold()
30 {}
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010031
32 void initialize() override;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010033 void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override;
Szymon Dompke94f71c52021-12-10 07:16:33 +010034 LabeledThresholdParam getThresholdParam() const override;
35 void updateSensors(Sensors newSensors) override;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010036
37 private:
38 boost::asio::io_context& ioc;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010039 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000040 const Milliseconds dwellTime;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010041 const numeric::Direction direction;
42 const double thresholdValue;
Szymon Dompke94f71c52021-12-10 07:16:33 +010043 const numeric::Type type;
44 bool initialized = false;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010045
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 };
Szymon Dompke94f71c52021-12-10 07:16:33 +010059 using SensorDetails =
60 std::unordered_map<std::shared_ptr<interfaces::Sensor>,
61 std::shared_ptr<ThresholdDetail>>;
62 SensorDetails sensorDetails;
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010063
Szymon Dompke94f71c52021-12-10 07:16:33 +010064 friend ThresholdOperations;
65
66 void startTimer(ThresholdDetail&, Milliseconds, double);
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010067 void commit(const std::string&, Milliseconds, double);
Szymon Dompke94f71c52021-12-10 07:16:33 +010068 ThresholdDetail& getDetails(const interfaces::Sensor& sensor);
69 std::shared_ptr<ThresholdDetail> makeDetails(const std::string& sensorName);
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010070};