blob: 0951956ab28eda114b0d61d4d69fa1c2f05a02cd [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"
7#include "interfaces/trigger_types.hpp"
8
9#include <boost/asio/steady_timer.hpp>
10
11#include <chrono>
12#include <memory>
13#include <vector>
14
15class NumericThreshold :
16 public interfaces::Threshold,
17 public interfaces::SensorListener,
18 public std::enable_shared_from_this<NumericThreshold>
19{
20 public:
21 NumericThreshold(
22 boost::asio::io_context& ioc,
23 std::vector<std::shared_ptr<interfaces::Sensor>> sensors,
24 std::vector<std::string> sensorNames,
25 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions,
26 std::chrono::milliseconds dwellTime, numeric::Direction direction,
27 double thresholdValue);
28 ~NumericThreshold();
29
30 void initialize() override;
31 void sensorUpdated(interfaces::Sensor&, uint64_t) override;
32 void sensorUpdated(interfaces::Sensor&, uint64_t, double) override;
33
34 private:
35 boost::asio::io_context& ioc;
36 const std::vector<std::shared_ptr<interfaces::Sensor>> sensors;
37 const std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
38 const std::chrono::milliseconds dwellTime;
39 const numeric::Direction direction;
40 const double thresholdValue;
41
42 struct ThresholdDetail
43 {
44 std::string sensorName;
45 double prevValue;
46 bool dwell;
47 boost::asio::steady_timer timer;
48
49 ThresholdDetail(const std::string& name, double prevValue, bool dwell,
50 boost::asio::io_context& ioc) :
51 sensorName(name),
52 prevValue(prevValue), dwell(dwell), timer(ioc)
53 {}
54 };
55 std::vector<ThresholdDetail> details;
56
57 void startTimer(const std::string&, uint64_t, double, bool&,
58 boost::asio::steady_timer&);
59 void commit(const std::string&, uint64_t, double);
60 ThresholdDetail& getDetails(interfaces::Sensor& sensor);
61};