blob: 9bd39b93ddd29ebd5cea0bf38949389510215ef2 [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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -08006
Patrick Venturea0764872020-08-08 07:48:43 -07007namespace pid_control
8{
9
Patrick Venture863b9242018-03-08 08:29:23 -080010/**
11 * Abstract base class for all sensors.
12 */
13class Sensor
14{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070015 public:
Patrick Venturea510ea22019-02-08 09:30:49 -080016 /**
17 * Given a sensor's type, return the default timeout value.
18 * A timeout of 0 means there isn't a timeout for this sensor.
19 * By default a fan sensor isn't checked for a timeout, whereas
20 * any of sensor is meant to be sampled once per second. By default.
21 *
22 * @param[in] type - the sensor type (e.g. fan)
23 * @return the default timeout for that type (in seconds).
24 */
25 static int64_t getDefaultTimeout(const std::string& type)
26 {
27 return (type == "fan") ? 0 : 2;
28 }
29
Patrick Venturedf766f22018-10-13 09:30:58 -070030 Sensor(const std::string& name, int64_t timeout) :
31 _name(name), _timeout(timeout)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070032 {}
Patrick Venture863b9242018-03-08 08:29:23 -080033
Patrick Williams8c051122023-05-10 07:50:59 -050034 virtual ~Sensor() {}
Patrick Venture863b9242018-03-08 08:29:23 -080035
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036 virtual ReadReturn read(void) = 0;
37 virtual void write(double value) = 0;
Josh Lehan2400ce42020-10-01 01:50:39 -070038
39 virtual void write(double value, bool force, int64_t* written)
40 {
41 (void)force;
42 (void)written;
43 return write(value);
44 }
45
James Feist36b7d8e2018-10-05 15:39:01 -070046 virtual bool getFailed(void)
47 {
48 return false;
49 };
Patrick Venture863b9242018-03-08 08:29:23 -080050
Harvey Wua4270072024-05-29 16:11:13 +080051 virtual std::string getFailReason(void)
52 {
53 return "Unimplemented";
54 }
55
Patrick Venture563a3562018-10-30 09:31:26 -070056 std::string getName(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 {
58 return _name;
59 }
Patrick Venture863b9242018-03-08 08:29:23 -080060
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061 /* Returns the configurable timeout period
62 * for this sensor in seconds (undecorated).
63 */
Patrick Venture563a3562018-10-30 09:31:26 -070064 int64_t getTimeout(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 {
66 return _timeout;
67 }
Patrick Venture863b9242018-03-08 08:29:23 -080068
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070069 private:
70 std::string _name;
71 int64_t _timeout;
Patrick Venture863b9242018-03-08 08:29:23 -080072};
Patrick Venturea0764872020-08-08 07:48:43 -070073
74} // namespace pid_control