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> |
| 10 | |
| 11 | #include <memory> |
| 12 | |
| 13 | class 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 Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 45 | Milliseconds timerInterval = Milliseconds(0); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 46 | 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 | }; |