Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "Thresholds.hpp" |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame] | 4 | #include "Utils.hpp" |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 5 | #include "sensor.hpp" |
| 6 | |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame] | 7 | #include <sdbusplus/asio/connection.hpp> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/object_server.hpp> |
| 9 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 10 | #include <chrono> |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame] | 11 | #include <functional> |
| 12 | #include <memory> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | class ExternalSensor : |
| 17 | public Sensor, |
| 18 | public std::enable_shared_from_this<ExternalSensor> |
| 19 | { |
| 20 | public: |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 21 | ExternalSensor( |
| 22 | const std::string& objectType, |
| 23 | sdbusplus::asio::object_server& objectServer, |
| 24 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 25 | const std::string& sensorName, const std::string& sensorUnits, |
| 26 | std::vector<thresholds::Threshold>&& thresholdsIn, |
| 27 | const std::string& sensorConfiguration, double maxReading, |
| 28 | double minReading, double timeoutSecs, const PowerState& powerState); |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 29 | ~ExternalSensor() override; |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 30 | |
| 31 | // Call this immediately after calling the constructor |
| 32 | void initWriteHook( |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 33 | std::function<void(std::chrono::steady_clock::time_point now)>&& |
| 34 | writeHookIn); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 35 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 36 | // Returns true if sensor has external Value that is subject to timeout |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 37 | bool isAliveAndPerishable() const; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 38 | |
| 39 | // Returns true if AliveAndPerishable and timeout has not yet happened |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 40 | bool isAliveAndFresh( |
| 41 | const std::chrono::steady_clock::time_point& now) const; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 42 | |
| 43 | // Marks the time when Value successfully received from external source |
| 44 | void writeBegin(const std::chrono::steady_clock::time_point& now); |
| 45 | |
| 46 | // Marks sensor as timed out, replacing Value with floating-point "NaN" |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 47 | void writeInvalidate(); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 48 | |
| 49 | // Returns amount of time elapsed since last writeBegin() happened |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 50 | std::chrono::steady_clock::duration ageElapsed( |
| 51 | const std::chrono::steady_clock::time_point& now) const; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 52 | |
| 53 | // Returns amount of time remaining until sensor timeout will happen |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 54 | std::chrono::steady_clock::duration ageRemaining( |
| 55 | const std::chrono::steady_clock::time_point& now) const; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 56 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 57 | private: |
| 58 | sdbusplus::asio::object_server& objServer; |
| 59 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 60 | std::chrono::steady_clock::time_point writeLast; |
| 61 | std::chrono::steady_clock::duration writeTimeout; |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 62 | bool writeAlive{false}; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 63 | bool writePerishable; |
| 64 | std::function<void(const std::chrono::steady_clock::time_point& now)> |
| 65 | writeHook; |
| 66 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 67 | void checkThresholds() override; |
| 68 | void externalSetTrigger(); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 69 | }; |