blob: ee54eba0d36662bed55d6520e0fa0cc0854fec5e [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#pragma once
2
3#include "interfaces/sensor.hpp"
4#include "interfaces/sensor_listener.hpp"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01005#include "types/duration_types.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02006#include "utils/unique_call.hpp"
7
8#include <boost/asio/high_resolution_timer.hpp>
9#include <sdbusplus/asio/connection.hpp>
Patrick Williams3a62ee12021-12-03 10:13:25 -060010#include <sdbusplus/bus/match.hpp>
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020011
12#include <memory>
13
14class 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 Grobelnyb8cc78d2021-11-29 15:54:53 +010021 Sensor(interfaces::Sensor::Id sensorId, const std::string& sensorMetadata,
22 boost::asio::io_context& ioc,
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020023 const std::shared_ptr<sdbusplus::asio::connection>& bus);
24
Piotr Sulewskic1dbac12025-11-12 14:14:09 +010025 ~Sensor() = default;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020026 Sensor(const Sensor&) = delete;
27 Sensor& operator=(const Sensor&) = delete;
Piotr Sulewskic1dbac12025-11-12 14:14:09 +010028 Sensor(Sensor&&) = delete;
29 Sensor& operator=(Sensor&&) = delete;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020030
31 static Id makeId(std::string_view service, std::string_view path);
32
33 Id id() const override;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010034 std::string metadata() const override;
Szymon Dompke94f71c52021-12-10 07:16:33 +010035 std::string getName() const override;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020036 void registerForUpdates(
37 const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020038 void unregisterFromUpdates(
39 const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020040
Szymon Dompke94f71c52021-12-10 07:16:33 +010041 LabeledSensorInfo getLabeledSensorInfo() const override;
42
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020043 private:
44 static std::optional<double> readValue(const ValueVariant& v);
45 static void signalProc(const std::weak_ptr<Sensor>& weakSelf,
Patrick Williams39cc6ac2022-07-22 19:26:56 -050046 sdbusplus::message_t&);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020047
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;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010054 std::string sensorMetadata;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020055 boost::asio::io_context& ioc;
56 std::shared_ptr<sdbusplus::asio::connection> bus;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000057 Milliseconds timerInterval = Milliseconds(0);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020058 std::optional<boost::asio::high_resolution_timer> timer;
59
60 utils::UniqueCall uniqueCall;
61 std::vector<std::weak_ptr<interfaces::SensorListener>> listeners;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010062 Milliseconds timestamp = Milliseconds{0u};
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020063 std::optional<double> value;
Patrick Williams3a62ee12021-12-03 10:13:25 -060064 std::unique_ptr<sdbusplus::bus::match_t> signalMonitor;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020065};