SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
threshold.h
Go to the documentation of this file.
1#include "transform.h"
2#ifndef _threshold_h
3#define _threshold_h
4
5namespace sensesp {
6
20template <typename C>
21class ThresholdTransform : public Transform<C, bool> {
22 public:
23
24 ThresholdTransform(C min_value, C max_value, bool in_range,
25 String config_path = "")
26 : Transform<C, bool>(config_path),
27 min_value_{min_value},
28 max_value_{max_value},
29 in_range_{in_range} {
30 this->load();
31 };
32
33 virtual void set(const C& new_value) override {
34 if (new_value >= min_value_ && new_value <= max_value_) {
35 this->output_ = in_range_;
36 } else {
37 this->output_ = !in_range_;
38 }
39
40 this->notify();
41 }
42
43 bool from_json(const JsonObject& root) override {
44 String expected[] = {"min", "max", "in_range"};
45 for (auto str : expected) {
46
47 if (!root[str].is<JsonVariant>()) {
48 return false;
49 }
50 }
51 min_value_ = root["min"];
52 max_value_ = root["max"];
53 in_range_ = root["in_range"];
54 return true;
55 }
56
57 bool to_json(JsonObject& root) override {
58 root["min"] = min_value_;
59 root["max"] = max_value_;
60 root["in_range"] = in_range_;
61 return true;
62 }
63
64 protected:
68};
69
70const String ConfigSchema(const ThresholdTransform<float>& obj);
71const String ConfigSchema(const ThresholdTransform<int>& obj);
72
75
76} // namespace sensesp
77#endif
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
A Transform base class that translates the value of type C into boolean. Base class for classes Float...
Definition threshold.h:21
bool from_json(const JsonObject &root) override
Definition threshold.h:43
virtual void set(const C &new_value) override
Definition threshold.h:33
bool to_json(JsonObject &root) override
Definition threshold.h:57
ThresholdTransform(C min_value, C max_value, bool in_range, String config_path="")
Definition threshold.h:24
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:68
const String ConfigSchema(const SmartSwitchController &obj)
ThresholdTransform< int > IntThreshold
Definition threshold.h:74
ThresholdTransform< float > FloatThreshold
Definition threshold.h:73