1 #pragma once 2 3 #include "interfaces/sensor.hpp" 4 #include "interfaces/sensor_listener.hpp" 5 #include "types/duration_types.hpp" 6 #include "utils/unique_call.hpp" 7 8 #include <boost/asio/high_resolution_timer.hpp> 9 #include <sdbusplus/asio/connection.hpp> 10 #include <sdbusplus/bus/match.hpp> 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, const std::string& sensorMetadata, 22 boost::asio::io_context& ioc, 23 const std::shared_ptr<sdbusplus::asio::connection>& bus); 24 25 ~Sensor() = default; 26 Sensor(const Sensor&) = delete; 27 Sensor& operator=(const Sensor&) = delete; 28 Sensor(Sensor&&) = delete; 29 Sensor& operator=(Sensor&&) = delete; 30 31 static Id makeId(std::string_view service, std::string_view path); 32 33 Id id() const override; 34 std::string metadata() const override; 35 std::string getName() const override; 36 void registerForUpdates( 37 const std::weak_ptr<interfaces::SensorListener>& weakListener) override; 38 void unregisterFromUpdates( 39 const std::weak_ptr<interfaces::SensorListener>& weakListener) override; 40 41 LabeledSensorInfo getLabeledSensorInfo() const override; 42 43 private: 44 static std::optional<double> readValue(const ValueVariant& v); 45 static void signalProc(const std::weak_ptr<Sensor>& weakSelf, 46 sdbusplus::message_t&); 47 48 void async_read(); 49 void async_read(std::shared_ptr<utils::UniqueCall::Lock>); 50 void makeSignalMonitor(); 51 void updateValue(double); 52 53 interfaces::Sensor::Id sensorId; 54 std::string sensorMetadata; 55 boost::asio::io_context& ioc; 56 std::shared_ptr<sdbusplus::asio::connection> bus; 57 Milliseconds timerInterval = Milliseconds(0); 58 std::optional<boost::asio::high_resolution_timer> timer; 59 60 utils::UniqueCall uniqueCall; 61 std::vector<std::weak_ptr<interfaces::SensorListener>> listeners; 62 Milliseconds timestamp = Milliseconds{0u}; 63 std::optional<double> value; 64 std::unique_ptr<sdbusplus::bus::match_t> signalMonitor; 65 }; 66