blob: 6f5b3dbbf22cab372417812b4ae3fb0ea8f0567f [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:
Josh Lehan03627382021-03-17 13:35:43 -070017 ExternalSensor(const std::string& objectType,
18 sdbusplus::asio::object_server& objectServer,
19 std::shared_ptr<sdbusplus::asio::connection>& conn,
20 const std::string& sensorName,
Ed Tanous2049bd22022-07-09 07:20:26 -070021 const std::string& sensorUnits,
Josh Lehan03627382021-03-17 13:35:43 -070022 std::vector<thresholds::Threshold>&& thresholdsIn,
23 const std::string& sensorConfiguration, double maxReading,
24 double minReading, double timeoutSecs,
25 const PowerState& powerState);
Ed Tanous74cffa82022-01-25 13:00:28 -080026 ~ExternalSensor() override;
Josh Lehan03627382021-03-17 13:35:43 -070027
28 // Call this immediately after calling the constructor
29 void initWriteHook(
Josh Lehan72432172021-03-17 13:35:43 -070030 std::function<void(std::chrono::steady_clock::time_point now)>&&
31 writeHookIn);
Josh Lehan2a40e932020-09-02 11:48:14 -070032
Josh Lehan72432172021-03-17 13:35:43 -070033 // Returns true if sensor has external Value that is subject to timeout
Ed Tanous201a1012024-04-03 18:07:28 -070034 bool isAliveAndPerishable() const;
Josh Lehan72432172021-03-17 13:35:43 -070035
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"
Ed Tanous201a1012024-04-03 18:07:28 -070044 void writeInvalidate();
Josh Lehan72432172021-03-17 13:35:43 -070045
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 Lehan2a40e932020-09-02 11:48:14 -070054 private:
55 sdbusplus::asio::object_server& objServer;
56
Josh Lehan72432172021-03-17 13:35:43 -070057 std::chrono::steady_clock::time_point writeLast;
58 std::chrono::steady_clock::duration writeTimeout;
Ed Tanousb429f312022-06-27 16:09:53 -070059 bool writeAlive{false};
Josh Lehan72432172021-03-17 13:35:43 -070060 bool writePerishable;
61 std::function<void(const std::chrono::steady_clock::time_point& now)>
62 writeHook;
63
Ed Tanous201a1012024-04-03 18:07:28 -070064 void checkThresholds() override;
65 void externalSetTrigger();
Josh Lehan2a40e932020-09-02 11:48:14 -070066};