blob: 01f4c02c8f1a7ccdfcdecf225fd2c11d80b978ee [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
7/**
8 * Abstract base class for all sensors.
9 */
10class Sensor
11{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012 public:
Patrick Venturea510ea22019-02-08 09:30:49 -080013 /**
14 * Given a sensor's type, return the default timeout value.
15 * A timeout of 0 means there isn't a timeout for this sensor.
16 * By default a fan sensor isn't checked for a timeout, whereas
17 * any of sensor is meant to be sampled once per second. By default.
18 *
19 * @param[in] type - the sensor type (e.g. fan)
20 * @return the default timeout for that type (in seconds).
21 */
22 static int64_t getDefaultTimeout(const std::string& type)
23 {
24 return (type == "fan") ? 0 : 2;
25 }
26
Patrick Venturedf766f22018-10-13 09:30:58 -070027 Sensor(const std::string& name, int64_t timeout) :
28 _name(name), _timeout(timeout)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070029 {}
Patrick Venture863b9242018-03-08 08:29:23 -080030
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031 virtual ~Sensor()
Patrick Venturea83a3ec2020-08-04 09:52:05 -070032 {}
Patrick Venture863b9242018-03-08 08:29:23 -080033
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034 virtual ReadReturn read(void) = 0;
35 virtual void write(double value) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070036 virtual bool getFailed(void)
37 {
38 return false;
39 };
Patrick Venture863b9242018-03-08 08:29:23 -080040
Patrick Venture563a3562018-10-30 09:31:26 -070041 std::string getName(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042 {
43 return _name;
44 }
Patrick Venture863b9242018-03-08 08:29:23 -080045
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070046 /* Returns the configurable timeout period
47 * for this sensor in seconds (undecorated).
48 */
Patrick Venture563a3562018-10-30 09:31:26 -070049 int64_t getTimeout(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070050 {
51 return _timeout;
52 }
Patrick Venture863b9242018-03-08 08:29:23 -080053
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070054 private:
55 std::string _name;
56 int64_t _timeout;
Patrick Venture863b9242018-03-08 08:29:23 -080057};