Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "interfaces/sensor.hpp" |
| 4 | #include "interfaces/sensor_listener.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 5 | #include "types/milliseconds.hpp" |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 6 | #include "utils/unique_call.hpp" |
| 7 | |
| 8 | #include <boost/asio/high_resolution_timer.hpp> |
| 9 | #include <sdbusplus/asio/connection.hpp> |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame^] | 10 | #include <sdbusplus/bus/match.hpp> |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 11 | |
| 12 | #include <memory> |
| 13 | |
| 14 | class Sensor final : |
| 15 | public interfaces::Sensor, |
| 16 | public std::enable_shared_from_this<Sensor> |
| 17 | { |
| 18 | using ValueVariant = std::variant<std::monostate, double>; |
| 19 | |
| 20 | public: |
| 21 | Sensor(interfaces::Sensor::Id sensorId, boost::asio::io_context& ioc, |
| 22 | const std::shared_ptr<sdbusplus::asio::connection>& bus); |
| 23 | |
| 24 | Sensor(const Sensor&) = delete; |
| 25 | Sensor& operator=(const Sensor&) = delete; |
| 26 | |
| 27 | static Id makeId(std::string_view service, std::string_view path); |
| 28 | |
| 29 | Id id() const override; |
| 30 | void registerForUpdates( |
| 31 | const std::weak_ptr<interfaces::SensorListener>& weakListener) override; |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 32 | void unregisterFromUpdates( |
| 33 | const std::weak_ptr<interfaces::SensorListener>& weakListener) override; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | static std::optional<double> readValue(const ValueVariant& v); |
| 37 | static void signalProc(const std::weak_ptr<Sensor>& weakSelf, |
| 38 | sdbusplus::message::message&); |
| 39 | |
| 40 | void async_read(); |
| 41 | void async_read(std::shared_ptr<utils::UniqueCall::Lock>); |
| 42 | void makeSignalMonitor(); |
| 43 | void updateValue(double); |
| 44 | |
| 45 | interfaces::Sensor::Id sensorId; |
| 46 | boost::asio::io_context& ioc; |
| 47 | std::shared_ptr<sdbusplus::asio::connection> bus; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 48 | Milliseconds timerInterval = Milliseconds(0); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 49 | std::optional<boost::asio::high_resolution_timer> timer; |
| 50 | |
| 51 | utils::UniqueCall uniqueCall; |
| 52 | std::vector<std::weak_ptr<interfaces::SensorListener>> listeners; |
| 53 | uint64_t timestamp = 0; |
| 54 | std::optional<double> value; |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame^] | 55 | std::unique_ptr<sdbusplus::bus::match_t> signalMonitor; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 56 | }; |