SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
constant_sensor.h
Go to the documentation of this file.
1#ifndef CONSTANT_SENSOR_H_
2#define CONSTANT_SENSOR_H_
3
4#include "Arduino.h"
6
7namespace sensesp {
8
42static const char SCHEMA_CONSTANT_SENSOR[] PROGMEM = R"###({
43 "type": "object",
44 "properties": {
45 "value": { "title": "Constant Value", "type": "number", "description": "Constant value" }
46 }
47 })###";
48
52template <class T>
53class ConstantSensor : public SensorT<T> {
54 public:
59 void start() override {
60 ReactESP::app->onRepeat(send_interval_ * 1000,
61 [this]() { this->emit(value_); });
62 }
63
64 void set_value(T value) { value_ = value; }
65 T get_value() { return value_; }
66
67 protected:
68 virtual void get_configuration(JsonObject &doc) override {
69 doc["value"] = value_;
70 doc["interval"] = send_interval_;
71 }
72 virtual bool set_configuration(const JsonObject &config) override {
73 // Neither of the configuration parameters are mandatory
74 if (config.containsKey("value")) {
75 value_ = config["value"];
76 }
77 if (config.containsKey("interval")) {
78 send_interval_ = config["interval"];
79 }
80 return true;
81 }
82 virtual String get_config_schema() override {
84 }
85 void update() { this->emit(value_); }
86
88 int send_interval_; // seconds
89};
90
91// ..........................................
92// constant value sensors
93// ..........................................
94
99
100} // namespace sensesp
101
102#endif
virtual void load_configuration()
Base class for constant value sensors.
virtual void get_configuration(JsonObject &doc) override
ConstantSensor(T value, int send_interval=30, String config_path="")
virtual String get_config_schema() override
virtual bool set_configuration(const JsonObject &config) override
Construct a new transform based on a single function.
Sensor template class for any sensor producing actual values.
Definition sensor.h:45
void emit(T new_value)
const uint8_t PAGE_css_bootstrap[] PROGMEM
ConstantSensor< int > IntConstantSensor
ConstantSensor< String > StringConstantSensor
ConstantSensor< float > FloatConstantSensor
ConstantSensor< bool > BoolConstantSensor