blob: c3a71036153acdb6a830ce29e2a811465ab37e27 [file] [log] [blame]
Josh Lehan2a40e932020-09-02 11:48:14 -07001#pragma once
2
3#include "Thresholds.hpp"
4#include "sensor.hpp"
5
6#include <sdbusplus/asio/object_server.hpp>
7
Josh Lehan72432172021-03-17 13:35:43 -07008#include <chrono>
Josh Lehan2a40e932020-09-02 11:48:14 -07009#include <string>
10#include <vector>
11
12class ExternalSensor :
13 public Sensor,
14 public std::enable_shared_from_this<ExternalSensor>
15{
16 public:
Patrick Williams2aaf7172024-08-16 15:20:40 -040017 ExternalSensor(
18 const std::string& objectType,
19 sdbusplus::asio::object_server& objectServer,
20 std::shared_ptr<sdbusplus::asio::connection>& conn,
21 const std::string& sensorName, const std::string& sensorUnits,
22 std::vector<thresholds::Threshold>&& thresholdsIn,
23 const std::string& sensorConfiguration, double maxReading,
24 double minReading, double timeoutSecs, const PowerState& powerState);
Ed Tanous74cffa82022-01-25 13:00:28 -080025 ~ExternalSensor() override;
Josh Lehan03627382021-03-17 13:35:43 -070026
27 // Call this immediately after calling the constructor
28 void initWriteHook(
Josh Lehan72432172021-03-17 13:35:43 -070029 std::function<void(std::chrono::steady_clock::time_point now)>&&
30 writeHookIn);
Josh Lehan2a40e932020-09-02 11:48:14 -070031
Josh Lehan72432172021-03-17 13:35:43 -070032 // Returns true if sensor has external Value that is subject to timeout
Ed Tanous201a1012024-04-03 18:07:28 -070033 bool isAliveAndPerishable() const;
Josh Lehan72432172021-03-17 13:35:43 -070034
35 // Returns true if AliveAndPerishable and timeout has not yet happened
36 bool
37 isAliveAndFresh(const std::chrono::steady_clock::time_point& now) const;
38
39 // Marks the time when Value successfully received from external source
40 void writeBegin(const std::chrono::steady_clock::time_point& now);
41
42 // Marks sensor as timed out, replacing Value with floating-point "NaN"
Ed Tanous201a1012024-04-03 18:07:28 -070043 void writeInvalidate();
Josh Lehan72432172021-03-17 13:35:43 -070044
45 // Returns amount of time elapsed since last writeBegin() happened
46 std::chrono::steady_clock::duration
47 ageElapsed(const std::chrono::steady_clock::time_point& now) const;
48
49 // Returns amount of time remaining until sensor timeout will happen
50 std::chrono::steady_clock::duration
51 ageRemaining(const std::chrono::steady_clock::time_point& now) const;
52
Josh Lehan2a40e932020-09-02 11:48:14 -070053 private:
54 sdbusplus::asio::object_server& objServer;
55
Josh Lehan72432172021-03-17 13:35:43 -070056 std::chrono::steady_clock::time_point writeLast;
57 std::chrono::steady_clock::duration writeTimeout;
Ed Tanousb429f312022-06-27 16:09:53 -070058 bool writeAlive{false};
Josh Lehan72432172021-03-17 13:35:43 -070059 bool writePerishable;
60 std::function<void(const std::chrono::steady_clock::time_point& now)>
61 writeHook;
62
Ed Tanous201a1012024-04-03 18:07:28 -070063 void checkThresholds() override;
64 void externalSetTrigger();
Josh Lehan2a40e932020-09-02 11:48:14 -070065};