SensESP 3.0.1
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 String hash_path = String("/") + Base64Sha1(config_path_);
16
17 String filename;
18 if (!find_config_file(config_path_, filename)) {
19 return false;
20 }
21
22 File f = SPIFFS.open(filename, "r");
23 JsonDocument json_doc;
24 auto error = deserializeJson(json_doc, f);
25 f.close();
26 if (error) {
27 ESP_LOGW(__FILENAME__, "Could not parse configuration for %s",
28 config_path_.c_str());
29 return false;
30 } //
31 JsonObject obj = json_doc.as<JsonObject>();
32 if (!from_json(obj)) {
33 ESP_LOGW(__FILENAME__, "Could not convert configuration to Json for %s",
34 config_path_.c_str());
35 return false;
36 }
37 ESP_LOGD(__FILENAME__, "Configuration loaded for %s", config_path_.c_str());
38 return true;
39}
40
42 if (config_path_ == "") {
43 return false;
44 }
45 String hash_path = String("/") + Base64Sha1(config_path_);
46
47 // Delete any existing configuration files
48 String filename;
49 if (find_config_file(config_path_, filename)) {
50 SPIFFS.remove(filename);
51 }
52
53 JsonDocument json_doc;
54 JsonObject obj = json_doc.to<JsonObject>();
55 if (!to_json(obj)) {
56 ESP_LOGW(__FILENAME__, "Could not get configuration from json for %s",
57 config_path_.c_str());
58 return false;
59 }
60 File f = SPIFFS.open(hash_path, "w");
61 serializeJson(obj, f);
62 f.close();
63
64 String str;
65 serializeJson(obj, str);
66 ESP_LOGV(__FILENAME__, "Configuration saved for %s: %s", config_path_.c_str(),
67 str.c_str());
68
69 return true;
70}
71
73 if (config_path_ == "") {
74 return false;
75 }
76
77 String filename;
78 if (!find_config_file(config_path_, filename)) {
79 return true;
80 }
81
82 String hash_path = String("/") + Base64Sha1(config_path_);
83
84 if (SPIFFS.exists(hash_path)) {
85 SPIFFS.remove(hash_path);
86 }
87 return true;
88}
89
90bool FileSystemSaveable::find_config_file(const String& config_path,
91 String& filename) {
92 String hash_path = String("/") + Base64Sha1(config_path);
93 String paths_to_check[] = {hash_path, hash_path + "\n"};
94
95 // Check for SensESP v2.4.0 and earlier versions
96 for (const auto& path : paths_to_check) {
97 if (SPIFFS.exists(path)) {
98 filename = path;
99 return true;
100 }
101 }
102
103 // Check for SensESP v2.1.0 and earlier versions
104 if (config_path.length() < 32 && SPIFFS.exists(config_path)) {
105 filename = config_path;
106 return true;
107 }
108
109 return false;
110}
111
112} // namespace sensesp
virtual bool clear() override
Delete the data from a persistent storage.
Definition saveable.cpp:72
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:41
bool find_config_file(const String &config_path, String &filename)
Definition saveable.cpp:90
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:72