1 #pragma once 2 3 #include "Thresholds.hpp" 4 #include "Utils.hpp" 5 #include "sensor.hpp" 6 7 #include <sdbusplus/asio/connection.hpp> 8 #include <sdbusplus/asio/object_server.hpp> 9 10 #include <chrono> 11 #include <functional> 12 #include <memory> 13 #include <string> 14 #include <vector> 15 16 class ExternalSensor : 17 public Sensor, 18 public std::enable_shared_from_this<ExternalSensor> 19 { 20 public: 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); 29 ~ExternalSensor() override; 30 31 // Call this immediately after calling the constructor 32 void initWriteHook( 33 std::function<void(std::chrono::steady_clock::time_point now)>&& 34 writeHookIn); 35 36 // Returns true if sensor has external Value that is subject to timeout 37 bool isAliveAndPerishable() const; 38 39 // Returns true if AliveAndPerishable and timeout has not yet happened 40 bool isAliveAndFresh( 41 const std::chrono::steady_clock::time_point& now) const; 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" 47 void writeInvalidate(); 48 49 // Returns amount of time elapsed since last writeBegin() happened 50 std::chrono::steady_clock::duration ageElapsed( 51 const std::chrono::steady_clock::time_point& now) const; 52 53 // Returns amount of time remaining until sensor timeout will happen 54 std::chrono::steady_clock::duration ageRemaining( 55 const std::chrono::steady_clock::time_point& now) const; 56 57 private: 58 sdbusplus::asio::object_server& objServer; 59 60 std::chrono::steady_clock::time_point writeLast; 61 std::chrono::steady_clock::duration writeTimeout; 62 bool writeAlive{false}; 63 bool writePerishable; 64 std::function<void(const std::chrono::steady_clock::time_point& now)> 65 writeHook; 66 67 void checkThresholds() override; 68 void externalSetTrigger(); 69 }; 70