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