blob: a447ae0a3eeaf2ca8f901ba7182e965b6695c3c0 [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 <chrono>
6#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -08007
8/**
9 * Abstract base class for all sensors.
10 */
11class Sensor
12{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070013 public:
Patrick Venturea510ea22019-02-08 09:30:49 -080014 /**
15 * Given a sensor's type, return the default timeout value.
16 * A timeout of 0 means there isn't a timeout for this sensor.
17 * By default a fan sensor isn't checked for a timeout, whereas
18 * any of sensor is meant to be sampled once per second. By default.
19 *
20 * @param[in] type - the sensor type (e.g. fan)
21 * @return the default timeout for that type (in seconds).
22 */
23 static int64_t getDefaultTimeout(const std::string& type)
24 {
25 return (type == "fan") ? 0 : 2;
26 }
27
Patrick Venturedf766f22018-10-13 09:30:58 -070028 Sensor(const std::string& name, int64_t timeout) :
29 _name(name), _timeout(timeout)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070030 {}
Patrick Venture863b9242018-03-08 08:29:23 -080031
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032 virtual ~Sensor()
Patrick Venturea83a3ec2020-08-04 09:52:05 -070033 {}
Patrick Venture863b9242018-03-08 08:29:23 -080034
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035 virtual ReadReturn read(void) = 0;
36 virtual void write(double value) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070037 virtual bool getFailed(void)
38 {
39 return false;
40 };
Patrick Venture863b9242018-03-08 08:29:23 -080041
Patrick Venture563a3562018-10-30 09:31:26 -070042 std::string getName(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043 {
44 return _name;
45 }
Patrick Venture863b9242018-03-08 08:29:23 -080046
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047 /* Returns the configurable timeout period
48 * for this sensor in seconds (undecorated).
49 */
Patrick Venture563a3562018-10-30 09:31:26 -070050 int64_t getTimeout(void) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070051 {
52 return _timeout;
53 }
Patrick Venture863b9242018-03-08 08:29:23 -080054
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070055 private:
56 std::string _name;
57 int64_t _timeout;
Patrick Venture863b9242018-03-08 08:29:23 -080058};