SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
configurable.cpp
Go to the documentation of this file.
1#include "configurable.h"
2
3#include "SPIFFS.h"
4#include "sensesp.h"
6
7namespace sensesp {
8
9// Define a global configurable map. Rationale for a global variable:
10// Every Configurable with an id gets registered, and carrying an object
11// reference around would unnecessarily reduce readability of the code.
12std::map<String, Configurable*> configurables;
13
15 int sort_order)
16 : config_path_{config_path},
17 description_{description},
18 sort_order_{sort_order} {
19 if (config_path != "") {
20 auto it = configurables.find(config_path);
21 if (it != configurables.end()) {
22 debugW("WARNING: Overriding id %s", config_path.c_str());
23 }
25 }
26}
27
29 debugW("WARNING: get_configuration not defined");
30}
31
32// Sets and saves the configuration
34 debugW("WARNING: set_configuration not defined for this Class");
35 return false;
36}
37
39
41 if (config_path_ == "") {
42 debugI("Not loading configuration: no config_path specified: %s",
43 config_path_.c_str());
44 return;
45 }
47
48 const String* filename;
49
50 if (SPIFFS.exists(hash_path)) {
52 debugD("Loading configuration for path '%s' from '%s'",
53 config_path_.c_str(), hash_path.c_str());
54 } else if (SPIFFS.exists(hash_path + "\n")) {
55 // Up to SensESP v2.4.0, the config path hash had an accidental newline
56 // appended to it.
57 hash_path += "\n";
59 debugD("Loading configuration for path '%s' from '%s'",
60 config_path_.c_str(), hash_path.c_str());
61 } else if (config_path_.length() < 32 && SPIFFS.exists(config_path_)) {
62 // Prior to SensESP v2.1.0, the config path was a plain filename.
64 debugD("Loading configuration for path %s", config_path_.c_str());
65 } else {
66 debugI("Could not find configuration for path %s", config_path_.c_str());
67 return;
68 }
69
70 File f = SPIFFS.open(*filename, "r");
73 if (error) {
74 debugW("WARNING: Could not parse configuration for %s",
75 config_path_.c_str());
76 return;
77 } //
79 debugW("WARNING: Could not set configuration for %s", config_path_.c_str());
80 }
81 f.close();
82}
83
85 if (config_path_ == "") {
86 debugI("WARNING: Could not save configuration (config_path not set)");
87 }
89
90 if (config_path_.length() < 32 && SPIFFS.exists(config_path_)) {
91 debugD("Deleting legacy configuration file %s", config_path_.c_str());
92 SPIFFS.remove(config_path_);
93 }
94
95 debugD("Saving configuration path %s to file %s", config_path_.c_str(),
96 hash_path.c_str());
97
99 JsonObject obj = jsonDoc.createNestedObject("root");
101 File f = SPIFFS.open(hash_path, "w");
103 f.close();
104}
105
106} // namespace sensesp
virtual bool set_configuration(const JsonObject &config)
virtual String get_config_schema()
virtual void save_configuration()
virtual void get_configuration(JsonObject &configObject)
virtual void load_configuration()
const String config_path_
Configurable(String config_path="", String description="", int sort_order=1000)
Construct a new transform based on a single function.
String Base64Sha1(String payload_str)
A base64-encoded SHA-1 hash function.
Definition hash.cpp:46
#define debugI(fmt,...)
Definition local_debug.h:48
#define debugD(fmt,...)
Definition local_debug.h:47
#define debugW(fmt,...)
Definition local_debug.h:49
std::map< String, Configurable * > configurables