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
4
#include "
sensesp/signalk/signalk_put_request.h
"
5
#include "
sensesp/system/lambda_consumer.h
"
6
#include "
sensesp/system/valueconsumer.h
"
7
#include "
sensesp/transforms/click_type.h
"
8
#include "
sensesp/transforms/truth_text.h
"
9
#include "
sensesp/ui/config_item.h
"
10
11
namespace
sensesp
{
12
46
class
SmartSwitchController
:
public
ValueProducer
<bool>,
FileSystemSaveable
{
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
:
71
LambdaConsumer<ClickTypes>
click_consumer_
{[
this
](
ClickTypes
new_value) {
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
96
LambdaConsumer<String>
truthy_string_consumer_
{[
this
](String new_value) {
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
108
LambdaConsumer<bool>
swich_consumer_
{[
this
](
bool
value) {
109
this->
is_on_
= value;
110
this->
emit
(
is_on_
);
111
}};
112
114
class
SyncPath
{
115
public
:
116
String
sk_sync_path_
;
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
;
129
bool
auto_initialize_
;
130
std::set<SyncPath>
sync_paths_
;
131
};
132
133
inline
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
sensesp::ClickType::is_click
static bool is_click(ClickTypes value)
Definition
click_type.cpp:28
sensesp::FileSystemSaveable
Definition
saveable.h:61
sensesp::LambdaConsumer
Provides an easy way of calling a function based on the output of any ValueProducer.
Definition
lambda_consumer.h:25
sensesp::SmartSwitchController::SyncPath
Used to store configuration internally.
Definition
smart_switch_controller.h:114
sensesp::SmartSwitchController::SyncPath::sk_sync_path_
String sk_sync_path_
Definition
smart_switch_controller.h:116
sensesp::SmartSwitchController::SyncPath::SyncPath
SyncPath()
Definition
smart_switch_controller.cpp:51
sensesp::SmartSwitchController::SyncPath::operator<
friend bool operator<(const SyncPath &lhs, const SyncPath &rhs)
Definition
smart_switch_controller.h:122
sensesp::SmartSwitchController::SyncPath::put_request_
std::shared_ptr< BoolSKPutRequest > put_request_
Definition
smart_switch_controller.h:117
sensesp::SmartSwitchController
A high level transform designed to control a digital output (such as a relay) via manual button press...
Definition
smart_switch_controller.h:46
sensesp::SmartSwitchController::truthy_string_consumer_
LambdaConsumer< String > truthy_string_consumer_
Definition
smart_switch_controller.h:96
sensesp::SmartSwitchController::click_consumer_
LambdaConsumer< ClickTypes > click_consumer_
Definition
smart_switch_controller.h:71
sensesp::SmartSwitchController::is_on_
bool is_on_
Definition
smart_switch_controller.h:128
sensesp::SmartSwitchController::to_json
virtual bool to_json(JsonObject &doc) override
Definition
smart_switch_controller.cpp:30
sensesp::SmartSwitchController::swich_consumer_
LambdaConsumer< bool > swich_consumer_
Definition
smart_switch_controller.h:108
sensesp::SmartSwitchController::auto_initialize_
bool auto_initialize_
Definition
smart_switch_controller.h:129
sensesp::SmartSwitchController::sync_paths_
std::set< SyncPath > sync_paths_
Definition
smart_switch_controller.h:130
sensesp::SmartSwitchController::from_json
virtual bool from_json(const JsonObject &config) override
Definition
smart_switch_controller.cpp:38
sensesp::SmartSwitchController::SmartSwitchController
SmartSwitchController(bool auto_initialize=true, String config_path="", const char *sk_sync_paths[]=NULL)
Definition
smart_switch_controller.cpp:8
sensesp::TextToTruth::is_valid_false
static bool is_valid_false(const String &value)
Definition
truth_text.cpp:25
sensesp::TextToTruth::is_valid_true
static bool is_valid_true(const String &value)
Definition
truth_text.cpp:7
sensesp::ValueProducer
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
Definition
valueproducer.h:25
sensesp::ValueProducer< bool >::emit
void emit(const bool &new_value)
Definition
valueproducer.h:91
click_type.h
config_item.h
lambda_consumer.h
sensesp
Definition
sensesp.cpp:7
sensesp::ConfigSchema
const String ConfigSchema(const SmartSwitchController &obj)
Definition
smart_switch_controller.h:133
sensesp::ClickTypes
ClickTypes
Definition
click_type.h:15
sensesp::ClickTypes::UltraLongSingleClick
@ UltraLongSingleClick
sensesp::ClickTypes::DoubleClick
@ DoubleClick
signalk_put_request.h
truth_text.h
valueconsumer.h
src
sensesp
controllers
smart_switch_controller.h
Generated by
1.12.0