SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
constant_sensor.h
Go to the documentation of this file.
1#ifndef SENSESP_SENSORS_CONSTANT_SENSOR_H_
2#define SENSESP_SENSORS_CONSTANT_SENSOR_H_
3
4#include "Arduino.h"
7#include "sensesp_base_app.h"
8
9namespace sensesp {
10
48template <class T>
49class ConstantSensor : public Sensor<T> {
50 public:
51 ConstantSensor(T value, int send_interval = 30, String config_path = "")
52 : Sensor<T>(config_path), value_{value}, send_interval_{send_interval} {
53 this->load();
54
55 // Emit the initial value once to set the output
56 event_loop()->onDelay(0, [this]() { this->emit(value_); });
57 // Then, emit the value at the specified interval
58 event_loop()->onRepeat(send_interval_ * 1000,
59 [this]() { this->emit(value_); });
60 }
61
62 void set(T value) { value_ = value; }
63
64 virtual bool to_json(JsonObject& doc) override {
65 doc["value"] = value_;
66 return true;
67 }
68 virtual bool from_json(const JsonObject& config) override {
69 // Neither of the configuration parameters are mandatory
70 if (config["value"].is<T>()) {
71 value_ = config["value"].as<T>();
72 }
73 return true;
74 }
75
76 protected:
77 void update() { this->emit(value_); }
78
80 int send_interval_; // seconds
81};
82
83template <typename T>
84const String ConfigSchemaSensorType(const T& obj) {
85 return "";
86}
87template <>
88const String ConfigSchemaSensorType<int>(const int& obj) {
89 return "integer";
90}
91template <>
92const String ConfigSchemaSensorType<float>(const float& obj) {
93 return "number";
94}
95template <>
96const String ConfigSchemaSensorType<String>(const String& obj) {
97 return "string";
98}
99template <>
100const String ConfigSchemaSensorType<bool>(const bool& obj) {
101 return "boolean";
102}
103
104template <typename T>
105const String ConfigSchema(const ConstantSensor<T>& obj) {
106 String schema = R"###({"type":"object","properties":{"value":{"title":"Constant Value","type":"%TYPE%","description":"Constant value"}} })###";
107 ;
108 schema.replace("%TYPE%", ConfigSchemaSensorType(obj));
109 return schema.c_str();
110}
111
112// ..........................................
113// constant value sensors
114// ..........................................
115
120
121} // namespace sensesp
122
123#endif
ConstantSensor is the base class for virtual sensors that periodically emit a constant value.
virtual bool from_json(const JsonObject &config) override
ConstantSensor(T value, int send_interval=30, String config_path="")
virtual bool to_json(JsonObject &doc) override
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Sensor template class for any sensor producing actual values.
Definition sensor.h:40
void emit(const T &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
const String ConfigSchemaSensorType< String >(const String &obj)
ConstantSensor< int > IntConstantSensor
ConstantSensor< String > StringConstantSensor
const String ConfigSchemaSensorType(const T &obj)
const String ConfigSchemaSensorType< int >(const int &obj)
const String ConfigSchemaSensorType< bool >(const bool &obj)
ConstantSensor< float > FloatConstantSensor
const String ConfigSchemaSensorType< float >(const float &obj)
ConstantSensor< bool > BoolConstantSensor