1#ifndef SENSESP_SRC_SENSESP_NET_WEB_CONFIG_ITEM_H_
2#define SENSESP_SRC_SENSESP_NET_WEB_CONFIG_ITEM_H_
10#include "ArduinoJson.h"
67std::shared_ptr<ConfigItemT<T>>
ConfigItem(std::shared_ptr<T>);
71 :
virtual public std::enable_shared_from_this<ConfigItemBase> {
147 static std::unique_ptr<std::vector<std::shared_ptr<ConfigItemBase>>>
149 std::unique_ptr<std::vector<std::shared_ptr<ConfigItemBase>>>
150 sorted_config_items(
new std::vector<std::shared_ptr<ConfigItemBase>>());
153 sorted_config_items->push_back(it.second);
155 std::sort(sorted_config_items->begin(), sorted_config_items->end(),
156 [](std::shared_ptr<ConfigItemBase> a,
157 std::shared_ptr<ConfigItemBase> b) {
158 return a->get_sort_order() < b->get_sort_order();
161 return sorted_config_items;
167 virtual bool to_json(JsonObject& config)
const = 0;
168 virtual bool from_json(
const JsonObject& config)
const = 0;
191 template <
typename T>
192 friend std::shared_ptr<ConfigItemT<T>>
ConfigItem(std::shared_ptr<T>);
203 static_assert(std::is_base_of<Serializable, T>::value,
204 "T must inherit from Serializable");
205 static_assert(std::is_base_of<Saveable, T>::value,
206 "T must inherit from Saveable");
212 virtual bool to_json(JsonObject& config)
const override {
216 virtual bool from_json(
const JsonObject& config)
const override {
289std::shared_ptr<ConfigItemT<T>>
ConfigItem(std::shared_ptr<T> config_object) {
290 ESP_LOGD(__FILENAME__,
"Registering ConfigItemT with path %s",
291 config_object->get_config_path().c_str());
292 auto config_item = std::make_shared<ConfigItemT<T>>(config_object);
293 auto base_sptr = std::static_pointer_cast<ConfigItemBase>(config_item);
300std::shared_ptr<ConfigItemT<T>>
ConfigItem(T* config_object) {
301 auto config_object_sptr = std::shared_ptr<T>(config_object);
String description_
Description of the ConfigItemT to be displayed on the web UI.
ConfigItemBase * set_description(const String &description)
Set the description of the ConfigItemT.
virtual const String get_config_schema() const
ConfigItemBase * set_sort_order(int sort_order)
ConfigItemBase * set_requires_restart(bool requires_restart)
Set the requires_restart flag.
virtual const String get_default_config_schema() const =0
friend std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
String config_path_
The path of the ConfigItemT. This is used to identify the ConfigItemT.
virtual bool from_json(const JsonObject &config) const =0
int get_sort_order() const
virtual const String & get_config_path() const =0
bool requires_restart() const
Return true if the ConfigItemT requires restart after saving for the changes to take effect.
String title_
Title of the ConfigItemT to be displayed on the web UI.
static std::map< String, std::shared_ptr< ConfigItemBase > > config_items_
const String & get_description() const
const String & get_title() const
virtual bool to_json(JsonObject &config) const =0
int sort_order_
The sort order of ConfigItemT. Lower numbers have precedence.
static std::shared_ptr< ConfigItemBase > get_config_item(const String key)
Get a single ConfigItemT by key.
ConfigItemBase * set_title(const String &title)
static std::unique_ptr< std::vector< std::shared_ptr< ConfigItemBase > > > get_config_items()
Get all config items as a vector.
Class providing sufficient data for rendering a config card in the frontend.
virtual bool from_json(const JsonObject &config) const override
virtual bool refresh() override
T * get_config_object()
Get the object that this ConfigItemT is managing.
virtual bool save() override
ConfigItemT< T > * set_description(const String &description)
virtual bool to_json(JsonObject &config) const override
ConfigItemT(std::shared_ptr< T > config_object)
ConfigItemT< T > * set_config_schema(const String &config_schema)
ConfigItemT< T > * set_requires_restart(bool requires_restart)
virtual bool load() override
std::shared_ptr< T > config_object_
virtual const String & get_config_path() const override
ConfigItemT< T > * set_title(const String &title)
ConfigItemT< T > * set_sort_order(int sort_order)
const String get_default_config_schema() const
Get the default configuration schema.
const String ConfigSchema(const SmartSwitchController &obj)
const char * get_schema_type_string(const int)
bool ConfigRequiresRestart(const HTTPServer &obj)
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.