SensESP 3.0.0-beta.6
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
ui_controls.h
Go to the documentation of this file.
1#ifndef SENSESP_UI_UI_CONTROLS_H
2#define SENSESP_UI_UI_CONTROLS_H
3
4#include "sensesp.h"
5
7
8namespace sensesp {
9
11 public:
12 StringConfig(String& value, String& config_path)
13 : FileSystemSaveable(config_path), value_(value) {
14 load();
15 }
16
17 virtual bool to_json(JsonObject& doc) override;
18 virtual bool from_json(const JsonObject& config) override;
19
20 String get_value() { return value_; }
21
22 protected:
23 String value_;
24 String title_ = "Value";
25};
26
27inline const String ConfigSchema(const StringConfig& obj) {
28 return R"({"type":"object","properties":{"value":{"title":"Value","type":"string"}}})";
29}
30
32 public:
33 NumberConfig(float& value, String& config_path)
34 : FileSystemSaveable(config_path), value_(value) {
35 load();
36 }
37
38 virtual bool to_json(JsonObject& doc) override;
39 virtual bool from_json(const JsonObject& config) override;
40 float get_value() { return value_; }
41
42 protected:
43 float value_;
44 String title_ = "Value";
45
46 friend const String ConfigSchema(const NumberConfig& obj);
47};
48
49inline const String ConfigSchema(const NumberConfig& obj) {
50 String schema = R"({"type":"object","properties":{"value":{"title":"{{title}}","type":"number"}}})";
51 schema.replace("{{title}}", obj.title_);
52 return schema.c_str();
53}
54
56 public:
57 CheckboxConfig(bool value, String title, String config_path)
58 : FileSystemSaveable(config_path), value_(value), title_(title) {
59 load();
60 }
61
62 virtual bool to_json(JsonObject& doc) override;
63 virtual bool from_json(const JsonObject& config) override;
64
65 bool get_value() { return value_; }
66
67 protected:
68 bool value_ = false;
69 String title_ = "Enable";
70
71 friend const String ConfigSchema(const CheckboxConfig& obj);
72};
73
74inline const String ConfigSchema(const CheckboxConfig& obj) {
75 String schema = R"({"type":"object","properties":{"value":{"title":"{{title}}","type":"boolean"}}})";
76 schema.replace("{{title}}", obj.title_);
77 return schema.c_str();
78}
79
80enum class SelectType {
83 kSelect,
84};
85
87 public:
88 SelectConfig(String value, String title, String config_path,
89 std::vector<String> options, SelectType format)
90 : FileSystemSaveable(config_path),
91 value_(value),
92 title_(title),
93 options_(options),
94 format_(format) {
95 load();
96 }
97
98 virtual bool to_json(JsonObject& doc) override;
99 virtual bool from_json(const JsonObject& config) override;
100
101 String get_value() { return value_; }
102
103 protected:
104 String value_;
105 String title_ = "Value";
106 std::vector<String> options_;
108
109 friend const String ConfigSchema(const SelectConfig& obj);
110};
111
112inline const String ConfigSchema(const SelectConfig& obj) {
113 String schema = R"({"type":"object","properties":{"value":{"title":"<<title>>","type":"array","format":"<<format>>","uniqueItems":true,"items":{"type":"string","enum":[<<options>>]}}}})";
114 schema.replace("<<title>>", obj.title_);
115 String options;
116 for (size_t i = 0; i < obj.options_.size(); i++) {
117 options += "\"" + obj.options_[i] + "\"";
118 if (i < obj.options_.size() - 1) {
119 options += ",";
120 }
121 }
122 schema.replace("<<options>>", options);
123 String format;
124 if (obj.format_ == SelectType::kUndefined) {
125 format = "undefined";
126 } else if (obj.format_ == SelectType::kCheckbox) {
127 format = "checkbox";
128 } else if (obj.format_ == SelectType::kSelect) {
129 format = "select";
130 }
131 schema.replace("<<format>>", format);
132 return schema.c_str();
133}
134
135} // namespace sensesp
136
137#endif // USERS_MAIRAS_SRC_SIGNALK_SENSESP_SRC_SENSESP_UI_UI_CONTROLS_H
virtual bool from_json(const JsonObject &config) override
friend const String ConfigSchema(const CheckboxConfig &obj)
Definition ui_controls.h:74
virtual bool to_json(JsonObject &doc) override
CheckboxConfig(bool value, String title, String config_path)
Definition ui_controls.h:57
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
virtual bool to_json(JsonObject &doc) override
friend const String ConfigSchema(const NumberConfig &obj)
Definition ui_controls.h:49
NumberConfig(float &value, String &config_path)
Definition ui_controls.h:33
virtual bool from_json(const JsonObject &config) override
SelectConfig(String value, String title, String config_path, std::vector< String > options, SelectType format)
Definition ui_controls.h:88
friend const String ConfigSchema(const SelectConfig &obj)
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &doc) override
std::vector< String > options_
virtual bool from_json(const JsonObject &config) override
StringConfig(String &value, String &config_path)
Definition ui_controls.h:12
virtual bool to_json(JsonObject &doc) override
const String ConfigSchema(const SmartSwitchController &obj)