SensESP 3.3.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
saveable.cpp
Go to the documentation of this file.
1#include "saveable.h"
2
3#include "SPIFFS.h"
5
6namespace sensesp {
7
9 if (config_path_ == "") {
10 ESP_LOGI(__FILENAME__,
11 "Not loading configuration: no config_path specified: %s",
12 config_path_.c_str());
13 return false;
14 }
15
16 String filename;
17 if (!find_config_file(config_path_, filename)) {
18 return false;
19 }
20
21 File f = SPIFFS.open(filename, "r");
22 JsonDocument json_doc;
23 auto error = deserializeJson(json_doc, f);
24 f.close();
25 if (error) {
26 ESP_LOGW(__FILENAME__, "Could not parse configuration for %s",
27 config_path_.c_str());
28 return false;
29 } //
30 JsonObject obj = json_doc.as<JsonObject>();
31 if (!from_json(obj)) {
32 ESP_LOGW(__FILENAME__, "Could not parse configuration from Json for %s",
33 config_path_.c_str());
34 return false;
35 }
36 ESP_LOGD(__FILENAME__, "Configuration loaded for %s", config_path_.c_str());
37 return true;
38}
39
41 if (config_path_ == "") {
42 return false;
43 }
44 String hash_path = String("/") + Base64Sha1(config_path_);
45
46 // Delete any existing configuration files
47 String filename;
48 if (find_config_file(config_path_, filename)) {
49 SPIFFS.remove(filename);
50 }
51
52 JsonDocument json_doc;
53 JsonObject obj = json_doc.to<JsonObject>();
54 if (!to_json(obj)) {
55 ESP_LOGW(__FILENAME__, "Could not get configuration from json for %s",
56 config_path_.c_str());
57 return false;
58 }
59 File f = SPIFFS.open(hash_path, "w");
60 serializeJson(obj, f);
61 f.close();
62
63 String str;
64 serializeJson(obj, str);
65 ESP_LOGV(__FILENAME__, "Configuration saved for %s: %s", config_path_.c_str(),
66 str.c_str());
67
68 return true;
69}
70
72 if (config_path_ == "") {
73 return false;
74 }
75
76 String filename;
77 if (!find_config_file(config_path_, filename)) {
78 return true;
79 }
80
81 String hash_path = String("/") + Base64Sha1(config_path_);
82
83 if (SPIFFS.exists(hash_path)) {
84 SPIFFS.remove(hash_path);
85 }
86 return true;
87}
88
89bool FileSystemSaveable::find_config_file(const String& config_path,
90 String& filename) {
91 String hash_path = String("/") + Base64Sha1(config_path);
92 String paths_to_check[] = {hash_path, hash_path + "\n"};
93
94 // Check for SensESP v2.4.0 and earlier versions
95 for (const auto& path : paths_to_check) {
96 if (SPIFFS.exists(path)) {
97 filename = path;
98 return true;
99 }
100 }
101
102 // Check for SensESP v2.1.0 and earlier versions
103 if (config_path.length() < 32 && SPIFFS.exists(config_path)) {
104 filename = config_path;
105 return true;
106 }
107
108 return false;
109}
110
111} // namespace sensesp
virtual bool clear() override
Delete the data from a persistent storage.
Definition saveable.cpp:71
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
virtual bool save() override
Save the object to a persistent storage.
Definition saveable.cpp:40
bool find_config_file(const String &config_path, String &filename)
Definition saveable.cpp:89
const String config_path_
Definition saveable.h:58
virtual bool from_json(const JsonObject &root)
virtual bool to_json(JsonObject &root)
String Base64Sha1(const String &payload_str)
A base64-encoded SHA-1 hash function.
Definition hash.cpp:80