blob: 0675815126d39e5e22d659496d6b4f5855639630 [file] [log] [blame]
Josh Lehan2a40e932020-09-02 11:48:14 -07001#pragma once
2
3#include "Thresholds.hpp"
Ed Tanous18b61862025-01-30 10:56:28 -08004#include "Utils.hpp"
Josh Lehan2a40e932020-09-02 11:48:14 -07005#include "sensor.hpp"
6
Ed Tanous18b61862025-01-30 10:56:28 -08007#include <sdbusplus/asio/connection.hpp>
Josh Lehan2a40e932020-09-02 11:48:14 -07008#include <sdbusplus/asio/object_server.hpp>
9
Josh Lehan72432172021-03-17 13:35:43 -070010#include <chrono>
Ed Tanous18b61862025-01-30 10:56:28 -080011#include <functional>
12#include <memory>
Josh Lehan2a40e932020-09-02 11:48:14 -070013#include <string>
14#include <vector>
15
16class ExternalSensor :
17 public Sensor,
18 public std::enable_shared_from_this<ExternalSensor>
19{
20 public:
Patrick Williams2aaf7172024-08-16 15:20:40 -040021 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 Tanous74cffa82022-01-25 13:00:28 -080029 ~ExternalSensor() override;
Josh Lehan03627382021-03-17 13:35:43 -070030
31 // Call this immediately after calling the constructor
32 void initWriteHook(
Josh Lehan72432172021-03-17 13:35:43 -070033 std::function<void(std::chrono::steady_clock::time_point now)>&&
34 writeHookIn);
Josh Lehan2a40e932020-09-02 11:48:14 -070035
Josh Lehan72432172021-03-17 13:35:43 -070036 // Returns true if sensor has external Value that is subject to timeout
Ed Tanous201a1012024-04-03 18:07:28 -070037 bool isAliveAndPerishable() const;
Josh Lehan72432172021-03-17 13:35:43 -070038
39 // Returns true if AliveAndPerishable and timeout has not yet happened
Patrick Williams556e04b2025-02-01 08:22:22 -050040 bool isAliveAndFresh(
41 const std::chrono::steady_clock::time_point& now) const;
Josh Lehan72432172021-03-17 13:35:43 -070042
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 Tanous201a1012024-04-03 18:07:28 -070047 void writeInvalidate();
Josh Lehan72432172021-03-17 13:35:43 -070048
49 // Returns amount of time elapsed since last writeBegin() happened
Patrick Williams556e04b2025-02-01 08:22:22 -050050 std::chrono::steady_clock::duration ageElapsed(
51 const std::chrono::steady_clock::time_point& now) const;
Josh Lehan72432172021-03-17 13:35:43 -070052
53 // Returns amount of time remaining until sensor timeout will happen
Patrick Williams556e04b2025-02-01 08:22:22 -050054 std::chrono::steady_clock::duration ageRemaining(
55 const std::chrono::steady_clock::time_point& now) const;
Josh Lehan72432172021-03-17 13:35:43 -070056
Josh Lehan2a40e932020-09-02 11:48:14 -070057 private:
58 sdbusplus::asio::object_server& objServer;
59
Josh Lehan72432172021-03-17 13:35:43 -070060 std::chrono::steady_clock::time_point writeLast;
61 std::chrono::steady_clock::duration writeTimeout;
Ed Tanousb429f312022-06-27 16:09:53 -070062 bool writeAlive{false};
Josh Lehan72432172021-03-17 13:35:43 -070063 bool writePerishable;
64 std::function<void(const std::chrono::steady_clock::time_point& now)>
65 writeHook;
66
Ed Tanous201a1012024-04-03 18:07:28 -070067 void checkThresholds() override;
68 void externalSetTrigger();
Josh Lehan2a40e932020-09-02 11:48:14 -070069};