| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_VIRTUAL_SENSOR_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_VIRTUAL_SENSOR_H_ |
| |
| #include <memory> |
| #include <optional> |
| #include <string> |
| |
| #include "absl/container/flat_hash_map.h" |
| #include "absl/functional/any_invocable.h" |
| #include "absl/status/statusor.h" |
| #include "boost/asio.hpp" //NOLINT: boost::asio is commonly used in BMC |
| #include "virtual_sensor_config.pb.h" |
| #include "tlbmc/expression/expression.h" |
| #include "sensor.pb.h" |
| #include "tlbmc/sensors/sensor.h" |
| |
| namespace milotic_tlbmc { |
| |
| class VirtualSensor : public Sensor, |
| public std::enable_shared_from_this<VirtualSensor> { |
| private: |
| class Token; |
| |
| public: |
| static absl::StatusOr<std::shared_ptr<VirtualSensor>> Create( |
| const VirtualSensorConfig& config, |
| const std::shared_ptr<boost::asio::io_context>& io_context, |
| const absl::flat_hash_map<std::string, std::shared_ptr<Sensor>>& |
| sensors_map, |
| std::unique_ptr<Expression> expression, |
| std::optional<NotificationCb> on_batch_notify = std::nullopt); |
| |
| VirtualSensor(Token token, const std::string& sensor_name, |
| SensorUnit sensor_unit, |
| const EntityCommonConfig& entity_common_config, |
| const ThresholdConfigs& threshold_configs, |
| const ReadingRangeConfigs& reading_range_configs, |
| const absl::flat_hash_map<std::string, std::shared_ptr<Sensor>>& |
| sensors_map, |
| std::unique_ptr<Expression> expression, |
| const std::shared_ptr<boost::asio::io_context>& io_context, |
| std::optional<NotificationCb> on_batch_notify = std::nullopt); |
| |
| ~VirtualSensor() override = default; |
| |
| void RefreshOnceAsync( |
| absl::AnyInvocable<void(const std::shared_ptr<const SensorValue>&)> |
| callback) override; |
| |
| protected: |
| absl::StatusOr<double> GetSensorValue(); |
| |
| private: |
| class Token { |
| private: |
| explicit Token() = default; |
| friend VirtualSensor; |
| }; |
| |
| std::shared_ptr<boost::asio::io_context> io_context_; |
| // The sensors that are used to calculate the virtual sensor value. |
| absl::flat_hash_map<std::string, std::shared_ptr<Sensor>> sensors_map_; |
| std::unique_ptr<Expression> expression_; |
| const std::string sensor_name_; |
| }; |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_VIRTUAL_SENSOR_H_ |