| 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; | 
| Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 31 | void unregisterFromUpdates( | 
|  | 32 | const std::weak_ptr<interfaces::SensorListener>& weakListener) override; | 
| Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 33 |  | 
|  | 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 Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 47 | Milliseconds timerInterval = Milliseconds(0); | 
| Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 48 | 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 | }; |