SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
smart_switch_controller.h
Go to the documentation of this file.
1#ifndef _smart_switch_controller_h
2#define _smart_switch_controller_h
3
10
11namespace sensesp {
12
47 public:
63 SmartSwitchController(bool auto_initialize = true, String config_path = "",
64 const char* sk_sync_paths[] = NULL);
65
66 // For reading and writing the configuration of this transformation
67 virtual bool to_json(JsonObject& doc) override;
68 virtual bool from_json(const JsonObject& config) override;
69
70 public:
72 if (!ClickType::is_click(new_value)) {
73 // Ignore button presses (we only want interpreted clicks)
74 return;
75 }
76
77 if (new_value == ClickTypes::UltraLongSingleClick) {
78 // Long clicks reboot the system...
79 ESP.restart();
80 return;
81 }
82
83 // All other click types toggle the current state...
84 this->is_on_ = !this->is_on_;
85 this->emit(this->is_on_);
86
87 if (new_value == ClickTypes::DoubleClick) {
88 // Sync any specified sync paths...
89 for (auto& path : sync_paths_) {
90 ESP_LOGD(__FILENAME__, "Sync status to %s", path.sk_sync_path_.c_str());
91 path.put_request_->set(this->is_on_);
92 }
93 }
94 }};
95
97 if (TextToTruth::is_valid_true(new_value)) {
98 this->is_on_ = true;
99 } else if (TextToTruth::is_valid_false(new_value)) {
100 this->is_on_ = false;
101 } else {
102 // All other values simply toggle...
103 this->is_on_ = !this->is_on_;
104 }
105 this->emit(this->is_on_);
106 }};
107
109 this->is_on_ = value;
110 this->emit(is_on_);
111 }};
112
114 class SyncPath {
115 public:
117 std::shared_ptr<BoolSKPutRequest> put_request_;
118
119 SyncPath();
120 SyncPath(String sk_sync_path);
121
122 friend bool operator<(const SyncPath& lhs, const SyncPath& rhs) {
123 return lhs.sk_sync_path_ < rhs.sk_sync_path_;
124 }
125 };
126
127 protected:
128 bool is_on_ = false;
130 std::set<SyncPath> sync_paths_;
131};
132
133inline const String ConfigSchema(const SmartSwitchController& obj) {
134 return R"({"type":"object","properties":{"sync_paths":{"title":"Sync on double click","type":"array","items":{"type":"string"}}} })";
135}
136
137} // namespace sensesp
138
139#endif
static bool is_click(ClickTypes value)
Provides an easy way of calling a function based on the output of any ValueProducer.
Used to store configuration internally.
friend bool operator<(const SyncPath &lhs, const SyncPath &rhs)
std::shared_ptr< BoolSKPutRequest > put_request_
A high level transform designed to control a digital output (such as a relay) via manual button press...
LambdaConsumer< String > truthy_string_consumer_
LambdaConsumer< ClickTypes > click_consumer_
virtual bool to_json(JsonObject &doc) override
virtual bool from_json(const JsonObject &config) override
SmartSwitchController(bool auto_initialize=true, String config_path="", const char *sk_sync_paths[]=NULL)
static bool is_valid_false(const String &value)
static bool is_valid_true(const String &value)
Definition truth_text.cpp:7
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const bool &new_value)
const String ConfigSchema(const SmartSwitchController &obj)