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 | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 5 | #include "types/duration_types.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: |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 21 | Sensor(interfaces::Sensor::Id sensorId, const std::string& sensorMetadata, |
| 22 | boost::asio::io_context& ioc, |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 23 | const std::shared_ptr<sdbusplus::asio::connection>& bus); |
| 24 | |
| 25 | Sensor(const Sensor&) = delete; |
| 26 | Sensor& operator=(const Sensor&) = delete; |
| 27 | |
| 28 | static Id makeId(std::string_view service, std::string_view path); |
| 29 | |
| 30 | Id id() const override; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 31 | std::string metadata() const override; |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 32 | std::string getName() const override; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 33 | void registerForUpdates( |
| 34 | const std::weak_ptr<interfaces::SensorListener>& weakListener) override; |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 35 | void unregisterFromUpdates( |
| 36 | const std::weak_ptr<interfaces::SensorListener>& weakListener) override; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 37 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 38 | LabeledSensorInfo getLabeledSensorInfo() const override; |
| 39 | |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 40 | private: |
| 41 | static std::optional<double> readValue(const ValueVariant& v); |
| 42 | static void signalProc(const std::weak_ptr<Sensor>& weakSelf, |
Patrick Williams | 39cc6ac | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 43 | sdbusplus::message_t&); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 44 | |
| 45 | void async_read(); |
| 46 | void async_read(std::shared_ptr<utils::UniqueCall::Lock>); |
| 47 | void makeSignalMonitor(); |
| 48 | void updateValue(double); |
| 49 | |
| 50 | interfaces::Sensor::Id sensorId; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 51 | std::string sensorMetadata; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 52 | boost::asio::io_context& ioc; |
| 53 | std::shared_ptr<sdbusplus::asio::connection> bus; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 54 | Milliseconds timerInterval = Milliseconds(0); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 55 | std::optional<boost::asio::high_resolution_timer> timer; |
| 56 | |
| 57 | utils::UniqueCall uniqueCall; |
| 58 | std::vector<std::weak_ptr<interfaces::SensorListener>> listeners; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 59 | Milliseconds timestamp = Milliseconds{0u}; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 60 | std::optional<double> value; |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 61 | std::unique_ptr<sdbusplus::bus::match_t> signalMonitor; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 62 | }; |