blob: c9bca4d650f9ab9535505c9b62b992da3cf0aa36 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +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 Dompkef763c9e2021-03-12 09:19:22 +01009
10#include <boost/asio/steady_timer.hpp>
11
12#include <chrono>
13#include <memory>
14#include <vector>
15
16class DiscreteThreshold :
17 public interfaces::Threshold,
18 public interfaces::SensorListener,
19 public std::enable_shared_from_this<DiscreteThreshold>
20{
21 public:
22 DiscreteThreshold(
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000023 boost::asio::io_context& ioc, Sensors sensors,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010024 std::vector<std::string> sensorNames,
25 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
Krzysztof Grobelnyfbeb5bf2022-01-03 09:41:29 +010026 Milliseconds dwellTime, double thresholdValue, const std::string& name);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010027 DiscreteThreshold(const DiscreteThreshold&) = delete;
28 DiscreteThreshold(DiscreteThreshold&&) = delete;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010029
30 void initialize() override;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010031 void sensorUpdated(interfaces::Sensor&, Milliseconds) override;
32 void sensorUpdated(interfaces::Sensor&, Milliseconds, double) override;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010033
34 private:
35 boost::asio::io_context& ioc;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000036 const Sensors sensors;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010037 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000038 const Milliseconds dwellTime;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010039 const double thresholdValue;
40 const std::string name;
41
42 struct ThresholdDetail
43 {
44 std::string sensorName;
45 bool dwell;
46 boost::asio::steady_timer timer;
47
48 ThresholdDetail(const std::string& name, bool dwell,
49 boost::asio::io_context& ioc) :
50 sensorName(name),
51 dwell(dwell), timer(ioc)
52 {}
53 };
54 std::vector<ThresholdDetail> details;
55
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010056 void startTimer(interfaces::Sensor&, Milliseconds, double);
57 void commit(const std::string&, Milliseconds, double);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058 ThresholdDetail& getDetails(interfaces::Sensor& sensor);
59};