blob: 70b2a6a202d12a478aca080c66c74409eb7265aa [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 Grobelnydcc4e192021-03-08 09:09:34 +00007#include "types/milliseconds.hpp"
8#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 Grobelnydcc4e192021-03-08 09:09:34 +000026 Milliseconds dwellTime, double thresholdValue, std::string name);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010027 DiscreteThreshold(const DiscreteThreshold&) = delete;
28 DiscreteThreshold(DiscreteThreshold&&) = delete;
29 ~DiscreteThreshold()
30 {}
31
32 void initialize() override;
33 void sensorUpdated(interfaces::Sensor&, uint64_t) override;
34 void sensorUpdated(interfaces::Sensor&, uint64_t, double) override;
35 const char* getName() const;
36
37 private:
38 boost::asio::io_context& ioc;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000039 const Sensors sensors;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000041 const Milliseconds dwellTime;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010042 const double thresholdValue;
43 const std::string name;
44
45 struct ThresholdDetail
46 {
47 std::string sensorName;
48 bool dwell;
49 boost::asio::steady_timer timer;
50
51 ThresholdDetail(const std::string& name, bool dwell,
52 boost::asio::io_context& ioc) :
53 sensorName(name),
54 dwell(dwell), timer(ioc)
55 {}
56 };
57 std::vector<ThresholdDetail> details;
58
59 void startTimer(interfaces::Sensor&, uint64_t, double);
60 void commit(const std::string&, uint64_t, double);
61 ThresholdDetail& getDetails(interfaces::Sensor& sensor);
62};