blob: 02110fe6958f1ba74f27db7bd2824ff7a552953a [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 Lehan72432172021-03-17 13:35:43 -070017 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& sensorMeasure,
22 std::vector<thresholds::Threshold>&& thresholdsIn,
23 const std::string& sensorConfiguration, double maxReading,
24 double minReading, double timeoutSecs, const PowerState& powerState,
25 std::function<void(std::chrono::steady_clock::time_point now)>&&
26 writeHookIn);
Josh Lehan2a40e932020-09-02 11:48:14 -070027 virtual ~ExternalSensor();
28
Josh Lehan72432172021-03-17 13:35:43 -070029 // Returns true if sensor has external Value that is subject to timeout
30 bool isAliveAndPerishable(void) const;
31
32 // Returns true if AliveAndPerishable and timeout has not yet happened
33 bool
34 isAliveAndFresh(const std::chrono::steady_clock::time_point& now) const;
35
36 // Marks the time when Value successfully received from external source
37 void writeBegin(const std::chrono::steady_clock::time_point& now);
38
39 // Marks sensor as timed out, replacing Value with floating-point "NaN"
40 void writeInvalidate(void);
41
42 // Returns amount of time elapsed since last writeBegin() happened
43 std::chrono::steady_clock::duration
44 ageElapsed(const std::chrono::steady_clock::time_point& now) const;
45
46 // Returns amount of time remaining until sensor timeout will happen
47 std::chrono::steady_clock::duration
48 ageRemaining(const std::chrono::steady_clock::time_point& now) const;
49
Josh Lehan2a40e932020-09-02 11:48:14 -070050 private:
51 sdbusplus::asio::object_server& objServer;
52
Josh Lehan72432172021-03-17 13:35:43 -070053 std::chrono::steady_clock::time_point writeLast;
54 std::chrono::steady_clock::duration writeTimeout;
55 bool writeAlive;
56 bool writePerishable;
57 std::function<void(const std::chrono::steady_clock::time_point& now)>
58 writeHook;
59
Josh Lehan2a40e932020-09-02 11:48:14 -070060 void checkThresholds(void) override;
Josh Lehan72432172021-03-17 13:35:43 -070061 void externalSetTrigger(void);
Josh Lehan2a40e932020-09-02 11:48:14 -070062};