1+ // The MIT License (MIT)
2+ //
3+ // Copyright (c) 2017 THINK BIG LABS SL
4+ // Author: [email protected] (Alvaro Luis Bustamante)5+ //
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files (the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ // copies of the Software, and to permit persons to whom the Software is
11+ // furnished to do so, subject to the following conditions:
12+ //
13+ // The above copyright notice and this permission notice shall be included in
14+ // all copies or substantial portions of the Software.
15+ //
16+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+ // THE SOFTWARE.
23+
24+ #ifndef THINGER_ESP32_WEBCONFIG_H
25+ #define THINGER_ESP32_WEBCONFIG_H
26+ #define FORMAT_SPIFFS_IF_FAILED true
27+
28+ #include < FS.h>
29+ #include < SPIFFS.h>
30+ #include " ThingerESP32.h"
31+ #include " ThingerWebConfig.h"
32+
33+
34+ class pson_spiffs_decoder : public protoson ::pson_decoder{
35+ public:
36+ pson_spiffs_decoder (File& file) : file_(file)
37+ {
38+ }
39+
40+ protected:
41+ virtual bool read (void * buffer, size_t size){
42+ size_t read = file_.readBytes ((char *)buffer, size);
43+ protoson::pson_decoder::read (buffer, read);
44+ return read == size;
45+ }
46+
47+ private:
48+ File& file_;
49+ };
50+
51+ class pson_spiffs_encoder : public protoson ::pson_encoder{
52+ public:
53+ pson_spiffs_encoder (File& file) : file_(file)
54+ {
55+ }
56+
57+ protected:
58+ virtual bool write (const void * buffer, size_t size){
59+ size_t wrote = file_.write ((const uint8_t *)buffer, size);
60+ protoson::pson_encoder::write (buffer, wrote);
61+ return wrote == size;
62+ }
63+
64+ private:
65+ File& file_;
66+ };
67+
68+ class ThingerESP32WebConfig : public ThingerWebConfig <ThingerESP32>{
69+
70+ public:
71+ ThingerESP32WebConfig (const char * user=" " , const char * device=" " , const char * credential=" " ) :
72+ ThingerWebConfig<ThingerESP32>(user, device, credential) {
73+
74+ }
75+
76+ bool clean_credentials () override {
77+ THINGER_DEBUG (" _CONFIG" , " Cleaning credentials..." );
78+ // clean Thinger.io credentials from file system
79+ if (SPIFFS.begin (FORMAT_SPIFFS_IF_FAILED)) {
80+ THINGER_DEBUG (" _CONFIG" , " FS Mounted!" );
81+ if (SPIFFS.exists (CONFIG_FILE)) {
82+ THINGER_DEBUG (" _CONFIG" , " Removing Config File..." );
83+ if (SPIFFS.remove (CONFIG_FILE)){
84+ THINGER_DEBUG (" _CONFIG" , " Config file removed!" );
85+ return true ;
86+ }else {
87+ THINGER_DEBUG (" _CONFIG" , " Cannot delete config file!" );
88+ }
89+ }else {
90+ THINGER_DEBUG (" _CONFIG" , " No config file to delete!" );
91+ }
92+ SPIFFS.end ();
93+ }
94+ return false ;
95+ }
96+
97+
98+ protected:
99+
100+ bool save_configuration (pson& config) override {
101+ THINGER_DEBUG (" _CONFIG" , " Updating Device Info..." );
102+ if (SPIFFS.begin (FORMAT_SPIFFS_IF_FAILED)) {
103+ File configFile = SPIFFS.open (CONFIG_FILE, " w" );
104+ if (configFile) {
105+ pson_spiffs_encoder encoder (configFile);
106+ encoder.encode (config);
107+ configFile.close ();
108+ THINGER_DEBUG (" _CONFIG" , " Done!" );
109+ return true ;
110+ } else {
111+ THINGER_DEBUG (" _CONFIG" , " Failed to open config file for writing!" );
112+ }
113+ SPIFFS.end ();
114+ }
115+ return false ;
116+ }
117+
118+ bool load_configuration (pson& config) override {
119+ THINGER_DEBUG (" _CONFIG" , " Mounting FS..." );
120+ if (SPIFFS.begin (FORMAT_SPIFFS_IF_FAILED)) {
121+ THINGER_DEBUG (" _CONFIG" , " FS Mounted!" );
122+ if (SPIFFS.exists (CONFIG_FILE)) {
123+ // file exists, reading and loading
124+ THINGER_DEBUG (" _CONFIG" , " Opening Config File..." );
125+ File configFile = SPIFFS.open (CONFIG_FILE, " r" );
126+ if (configFile){
127+ THINGER_DEBUG (" _CONFIG" , " Config File is Open!" );
128+ pson_spiffs_decoder decoder (configFile);
129+ bool result = decoder.decode (config);
130+ configFile.close ();
131+ return result;
132+ }else {
133+ THINGER_DEBUG (" _CONFIG" , " Config File is Not Available!" );
134+ }
135+ }
136+ // close SPIFFS
137+ SPIFFS.end ();
138+ } else {
139+ THINGER_DEBUG (" _CONFIG" , " Failed to Mount FS!" );
140+ }
141+ return false ;
142+ }
143+
144+ };
145+
146+
147+
148+ #endif
0 commit comments