SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
smart_switch_controller.cpp
Go to the documentation of this file.
2
4#include "sensesp_base_app.h"
5
6namespace sensesp {
7
9 String config_path,
10 const char* sk_sync_paths[])
11 : FileSystemSaveable(config_path), auto_initialize_{auto_initialize} {
12 if (sk_sync_paths != NULL) {
13 sync_paths_.clear();
14 int i = 0;
15 while (strlen(sk_sync_paths[i]) > 0) {
16 SyncPath path(sk_sync_paths[i]);
17 sync_paths_.insert(path);
18 i++;
19 } // while
20 }
21
22 load();
23
24 // Emit the initial state once the event loop starts
25 if (auto_initialize_) {
26 event_loop()->onDelay(0, [this]() { this->emit(is_on_); });
27 }
28}
29
30bool SmartSwitchController::to_json(JsonObject& root) {
31 JsonArray jPaths = root["sync_paths"].to<JsonArray>();
32 for (auto& path : sync_paths_) {
33 jPaths.add(path.sk_sync_path_);
34 }
35 return true;
36}
37
38bool SmartSwitchController::from_json(const JsonObject& config) {
39 JsonArray arr = config["sync_paths"];
40 if (arr.size() > 0) {
41 sync_paths_.clear();
42 for (String sk_path : arr) {
43 SyncPath path(sk_path);
44 sync_paths_.insert(path);
45 }
46 }
47
48 return true;
49}
50
52
54 : sk_sync_path_{sk_sync_path} {
55 ESP_LOGD(__FILENAME__, "DoubleClick will also sync %s", sk_sync_path.c_str());
56 this->put_request_ = std::make_shared<BoolSKPutRequest>(sk_sync_path, "", false);
57}
58
59} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Used to store configuration internally.
std::shared_ptr< BoolSKPutRequest > put_request_
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)
void emit(const bool &new_value)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9