blob: 23c3d5c6aa213843e92f544235b3ff9c582a118a [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;
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020031 void unregisterFromUpdates(
32 const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020033
34 private:
35 static std::optional<double> readValue(const ValueVariant& v);
36 static void signalProc(const std::weak_ptr<Sensor>& weakSelf,
37 sdbusplus::message::message&);
38
39 void async_read();
40 void async_read(std::shared_ptr<utils::UniqueCall::Lock>);
41 void makeSignalMonitor();
42 void updateValue(double);
43
44 interfaces::Sensor::Id sensorId;
45 boost::asio::io_context& ioc;
46 std::shared_ptr<sdbusplus::asio::connection> bus;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000047 Milliseconds timerInterval = Milliseconds(0);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020048 std::optional<boost::asio::high_resolution_timer> timer;
49
50 utils::UniqueCall uniqueCall;
51 std::vector<std::weak_ptr<interfaces::SensorListener>> listeners;
52 uint64_t timestamp = 0;
53 std::optional<double> value;
54 std::unique_ptr<sdbusplus::bus::match::match> signalMonitor;
55};