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