blob: e3334a03ff7bd6005ce6b1bbc01d5026a6be73e8 [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#pragma once
2
3#include "interfaces/sensor.hpp"
4#include "interfaces/sensor_listener.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00005#include "types/milliseconds.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02006#include "utils/unique_call.hpp"
7
8#include <boost/asio/high_resolution_timer.hpp>
9#include <sdbusplus/asio/connection.hpp>
10
11#include <memory>
12
13class Sensor final :
14 public interfaces::Sensor,
15 public std::enable_shared_from_this<Sensor>
16{
17 using ValueVariant = std::variant<std::monostate, double>;
18
19 public:
20 Sensor(interfaces::Sensor::Id sensorId, boost::asio::io_context& ioc,
21 const std::shared_ptr<sdbusplus::asio::connection>& bus);
22
23 Sensor(const Sensor&) = delete;
24 Sensor& operator=(const Sensor&) = delete;
25
26 static Id makeId(std::string_view service, std::string_view path);
27
28 Id id() const override;
29 void registerForUpdates(
30 const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
31
32 private:
33 static std::optional<double> readValue(const ValueVariant& v);
34 static void signalProc(const std::weak_ptr<Sensor>& weakSelf,
35 sdbusplus::message::message&);
36
37 void async_read();
38 void async_read(std::shared_ptr<utils::UniqueCall::Lock>);
39 void makeSignalMonitor();
40 void updateValue(double);
41
42 interfaces::Sensor::Id sensorId;
43 boost::asio::io_context& ioc;
44 std::shared_ptr<sdbusplus::asio::connection> bus;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000045 Milliseconds timerInterval = Milliseconds(0);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020046 std::optional<boost::asio::high_resolution_timer> timer;
47
48 utils::UniqueCall uniqueCall;
49 std::vector<std::weak_ptr<interfaces::SensorListener>> listeners;
50 uint64_t timestamp = 0;
51 std::optional<double> value;
52 std::unique_ptr<sdbusplus::bus::match::match> signalMonitor;
53};