SensESP 3.0.1
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
21template <typename C>
22class ThresholdTransform : public Transform<C, bool> {
23 public:
24
25 ThresholdTransform(C min_value, C max_value, bool in_range,
26 String config_path = "")
27 : Transform<C, bool>(config_path),
28 min_value_{min_value},
29 max_value_{max_value},
30 in_range_{in_range} {
31 this->load();
32 };
33
34 virtual void set(const C& new_value) override {
35 if (new_value >= min_value_ && new_value <= max_value_) {
36 this->output_ = in_range_;
37 } else {
38 this->output_ = !in_range_;
39 }
40
41 this->notify();
42 }
43
44 bool from_json(const JsonObject& root) override {
45 String expected[] = {"min", "max", "in_range", "out_range"};
46 for (auto str : expected) {
47
48 if (!root[str].is<JsonVariant>()) {
49 return false;
50 }
51 }
52 min_value_ = root["min"];
53 max_value_ = root["max"];
54 in_range_ = root["in_range"];
55 return true;
56 }
57
58 bool to_json(JsonObject& root) override {
59 root["min"] = min_value_;
60 root["max"] = max_value_;
61 root["in_range"] = in_range_;
62 return true;
63 }
64
65 protected:
69};
70
71const String ConfigSchema(const ThresholdTransform<float>& obj);
72const String ConfigSchema(const ThresholdTransform<int>& obj);
73
76
77} // namespace sensesp
78#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:22
bool from_json(const JsonObject &root) override
Definition threshold.h:44
virtual void set(const C &new_value) override
Definition threshold.h:34
bool to_json(JsonObject &root) override
Definition threshold.h:58
ThresholdTransform(C min_value, C max_value, bool in_range, String config_path="")
Definition threshold.h:25
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
const String ConfigSchema(const SmartSwitchController &obj)
ThresholdTransform< int > IntThreshold
Definition threshold.h:75
ThresholdTransform< float > FloatThreshold
Definition threshold.h:74