blob: 49489db88ee2063fd22ef00802fd9e426a90f499 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
Patrick Venture863b9242018-03-08 08:29:23 -08003#include "interfaces.hpp"
4
Ed Tanousf8b6e552025-06-27 13:27:50 -07005#include <cstdint>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -08007
Patrick Venturea0764872020-08-08 07:48:43 -07008namespace pid_control
9{
10
Patrick Venture863b9242018-03-08 08:29:23 -080011/**
12 * Abstract base class for all sensors.
13 */
14class Sensor
15{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070016 public:
Patrick Venturea510ea22019-02-08 09:30:49 -080017 /**
18 * Given a sensor's type, return the default timeout value.
19 * A timeout of 0 means there isn't a timeout for this sensor.
20 * By default a fan sensor isn't checked for a timeout, whereas
21 * any of sensor is meant to be sampled once per second. By default.
22 *
23 * @param[in] type - the sensor type (e.g. fan)
24 * @return the default timeout for that type (in seconds).
25 */
26 static int64_t getDefaultTimeout(const std::string& type)
27 {
28 return (type == "fan") ? 0 : 2;
29 }
30
Potin Laie1fa8592025-08-29 15:27:08 +080031 Sensor(const std::string& name, int64_t timeout,
32 bool ignoreFailIfHostOff = false) :
33 _name(name), _timeout(timeout),
34 _ignoreFailIfHostOff(ignoreFailIfHostOff)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070035 {}
Patrick Venture863b9242018-03-08 08:29:23 -080036
Ed Tanousd2768c52025-06-26 11:42:57 -070037 virtual ~Sensor() = default;
Patrick Venture863b9242018-03-08 08:29:23 -080038
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039 virtual ReadReturn read(void) = 0;
40 virtual void write(double value) = 0;
Josh Lehan2400ce42020-10-01 01:50:39 -070041
42 virtual void write(double value, bool force, int64_t* written)
43 {
44 (void)force;
45 (void)written;
46 return write(value);
47 }
48
James Feist36b7d8e2018-10-05 15:39:01 -070049 virtual bool getFailed(void)
50 {
51 return false;
52 };
Patrick Venture863b9242018-03-08 08:29:23 -080053
Harvey Wua4270072024-05-29 16:11:13 +080054 virtual std::string getFailReason(void)
55 {
56 return "Unimplemented";
57 }
58
Patrick Venture563a3562018-10-30 09:31:26 -070059 std::string getName(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 {
61 return _name;
62 }
Patrick Venture863b9242018-03-08 08:29:23 -080063
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 /* Returns the configurable timeout period
65 * for this sensor in seconds (undecorated).
66 */
Patrick Venture563a3562018-10-30 09:31:26 -070067 int64_t getTimeout(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 {
69 return _timeout;
70 }
Patrick Venture863b9242018-03-08 08:29:23 -080071
Potin Laie1fa8592025-08-29 15:27:08 +080072 bool getIgnoreFailIfHostOff(void) const
73 {
74 return _ignoreFailIfHostOff;
75 }
76
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070077 private:
78 std::string _name;
79 int64_t _timeout;
Potin Laie1fa8592025-08-29 15:27:08 +080080 bool _ignoreFailIfHostOff;
Patrick Venture863b9242018-03-08 08:29:23 -080081};
Patrick Venturea0764872020-08-08 07:48:43 -070082
83} // namespace pid_control