SensESP
2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
smart_switch_controller.cpp
Go to the documentation of this file.
1
#include "
smart_switch_controller.h
"
2
3
#include "
sensesp/transforms/truth_text.h
"
4
5
namespace
sensesp
{
6
7
SmartSwitchController::SmartSwitchController
(
bool
auto_initialize
,
8
String
config_path
,
9
const
char
*
sk_sync_paths
[])
10
:
BooleanTransform
(
config_path
), auto_initialize_{
auto_initialize
} {
11
if
(
sk_sync_paths
!=
NULL
) {
12
sync_paths
.clear();
13
int
i
= 0;
14
while
(
strlen
(
sk_sync_paths
[
i
]) > 0) {
15
SyncPath
path
(
sk_sync_paths
[
i
]);
16
sync_paths
.insert(
path
);
17
i
++;
18
}
// while
19
}
20
21
load_configuration
();
22
}
23
24
void
SmartSwitchController::start
() {
25
if
(
auto_initialize_
) {
26
this->
emit
(
is_on
);
27
}
28
}
29
30
void
SmartSwitchController::set_input
(
bool
new_value
,
uint8_t
input_channel
) {
31
is_on
=
new_value
;
32
this->
emit
(
is_on
);
33
}
34
35
void
SmartSwitchController::set_input
(
ClickTypes
new_value
,
36
uint8_t
input_channel
) {
37
if
(!
ClickType::is_click
(
new_value
)) {
38
// Ignore button presses (we only want interpreted clicks)
39
return
;
40
}
41
42
if
(
new_value
==
ClickTypes::UltraLongSingleClick
) {
43
// Long clicks reboot the system...
44
ESP
.restart();
45
return
;
46
}
47
48
// All other click types toggle the current state...
49
is_on
= !
is_on
;
50
this->
emit
(
is_on
);
51
52
if
(new_value ==
ClickTypes::DoubleClick
) {
53
// Sync any specified sync paths...
54
for
(
auto
&
path
:
sync_paths
) {
55
debugD
(
"Sync status to %s"
,
path
.sk_sync_path.c_str());
56
path
.put_request->
set_input
(
is_on
);
57
}
58
}
59
}
60
61
void
SmartSwitchController::set_input
(
String
new_value
,
uint8_t
input_channel
) {
62
if
(
TextToTruth::is_valid_true
(
new_value
)) {
63
is_on
=
true
;
64
}
else
if
(
TextToTruth::is_valid_false
(
new_value
)) {
65
is_on
=
false
;
66
}
else
{
67
// All other values simply toggle...
68
is_on
= !
is_on
;
69
}
70
this->
emit
(
is_on
);
71
}
72
73
void
SmartSwitchController::get_configuration
(
JsonObject
&
root
) {
74
JsonArray
jPaths
=
root
.createNestedArray(
"sync_paths"
);
75
for
(
auto
&
path
:
sync_paths
) {
76
jPaths
.add(
path
.sk_sync_path);
77
}
78
}
79
80
static
const
char
SCHEMA
[] PROGMEM = R
"({
81
"type": "object",
82
"properties": {
83
"sync_paths": { "title": "Sync on double click",
84
"type": "array",
85
"items": { "type": "string"}
86
}
87
}
88
})";
89
90
String
SmartSwitchController::get_config_schema
() {
return
FPSTR
(
SCHEMA
); }
91
92
bool
SmartSwitchController::set_configuration
(
const
JsonObject
&
config
) {
93
JsonArray
arr
=
config
[
"sync_paths"
];
94
if
(
arr
.size() > 0) {
95
sync_paths
.clear();
96
for
(
String
sk_path :
arr
) {
97
SyncPath
path
(sk_path);
98
sync_paths
.insert(
path
);
99
}
100
}
101
102
return
true
;
103
}
104
105
SmartSwitchController::SyncPath::SyncPath
() {}
106
107
SmartSwitchController::SyncPath::SyncPath
(
String
sk_sync_path)
108
: sk_sync_path{sk_sync_path} {
109
debugD
(
"DoubleClick will also sync %s"
,
sk_sync_path
.c_str());
110
this->
put_request
=
new
BoolSKPutRequest
(sk_sync_path,
""
,
false
);
111
}
112
113
}
// namespace sensesp
sensesp::ClickType::is_click
static bool is_click(ClickTypes value)
Definition
click_type.cpp:27
sensesp::Configurable::load_configuration
virtual void load_configuration()
Definition
configurable.cpp:40
sensesp::LambdaTransform
Construct a new transform based on a single function.
Definition
lambda_transform.h:71
sensesp::LambdaTransform::set_input
void set_input(IN input, uint8_t input_channel=0) override
Definition
lambda_transform.h:180
sensesp::SmartSwitchController::SyncPath
Used to store configuration internally.
Definition
smart_switch_controller.h:76
sensesp::SmartSwitchController::SyncPath::sk_sync_path
String sk_sync_path
Definition
smart_switch_controller.h:78
sensesp::SmartSwitchController::SyncPath::put_request
BoolSKPutRequest * put_request
Definition
smart_switch_controller.h:79
sensesp::SmartSwitchController::SyncPath::SyncPath
SyncPath()
Definition
smart_switch_controller.cpp:105
sensesp::SmartSwitchController::sync_paths
std::set< SyncPath > sync_paths
Definition
smart_switch_controller.h:92
sensesp::SmartSwitchController::set_input
void set_input(bool new_value, uint8_t input_channel=0) override
Definition
smart_switch_controller.cpp:30
sensesp::SmartSwitchController::auto_initialize_
bool auto_initialize_
Definition
smart_switch_controller.h:91
sensesp::SmartSwitchController::get_configuration
virtual void get_configuration(JsonObject &doc) override
Definition
smart_switch_controller.cpp:73
sensesp::SmartSwitchController::is_on
bool is_on
Definition
smart_switch_controller.h:90
sensesp::SmartSwitchController::start
void start() override
Definition
smart_switch_controller.cpp:24
sensesp::SmartSwitchController::SmartSwitchController
SmartSwitchController(bool auto_initialize=true, String config_path="", const char *sk_sync_paths[]=NULL)
Definition
smart_switch_controller.cpp:7
sensesp::SmartSwitchController::set_configuration
virtual bool set_configuration(const JsonObject &config) override
Definition
smart_switch_controller.cpp:92
sensesp::SmartSwitchController::get_config_schema
virtual String get_config_schema() override
Definition
smart_switch_controller.cpp:90
sensesp::SymmetricTransform
A common type of transform that consumes, transforms, then outputs values of the same data type.
Definition
transform.h:95
sensesp::TextToTruth::is_valid_false
static bool is_valid_false(String value)
Definition
truth_text.cpp:23
sensesp::TextToTruth::is_valid_true
static bool is_valid_true(String value)
Definition
truth_text.cpp:5
sensesp::ValueProducer::emit
void emit(T new_value)
Definition
valueproducer.h:109
debugD
#define debugD(fmt,...)
Definition
local_debug.h:47
sensesp
Definition
sensesp.h:22
sensesp::ClickTypes
ClickTypes
Definition
click_type.h:14
sensesp::ClickTypes::UltraLongSingleClick
@ UltraLongSingleClick
sensesp::ClickTypes::DoubleClick
@ DoubleClick
sensesp::BoolSKPutRequest
SKPutRequest< bool > BoolSKPutRequest
Definition
signalk_put_request.h:173
smart_switch_controller.h
truth_text.h
src
sensesp
controllers
smart_switch_controller.cpp
Generated by
1.9.8