-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
187 lines (177 loc) · 5.34 KB
/
Util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include "Util.h"
//CallWifiManager to connect wifi
bool CallWiFiManager(bool isNew)
{
Serial.println("WiFi Manager is started...");
WiFiManager wifiManager;
if (isNew)
wifiManager.resetSettings(); //reset settings - for testing
wifiManager.setAPStaticIPConfig(WeMo::_IP, WeMo::_GW, WeMo::_SN);
wifiManager.setTimeout(360);
if (!wifiManager.autoConnect(WeMo::AP_Name.c_str()))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("local ip");
Serial.println(WiFi.localIP());
#ifdef WEMO_SWITCH
WeMo::wifiConnected = true;
#endif
return true;
}
/**Reading Config File*/
bool ReadConfigFile()
{
Serial.println("\nmounting FS for reading ");
if (SPIFFS.begin())
{
Serial.println("mounted file system");
if (SPIFFS.exists(ConfigFileName))
{
Serial.println("reading config file");
// Open file for reading
File configFile = SPIFFS.open(ConfigFileName, "r");
StaticJsonDocument<512> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, configFile);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonDocument to the Config
for (int i = 0; i < WeMo::NoOfRelay; i++)
{
WeMo::RelayStatus[i] = doc[WeMo::RelayNames[i]] | 0;
WeMo::isRelayOn[i] = doc[WeMo::RelayNames[i] + WeMo::RelayPins[i]] | false;
}
Serial.println("values set");
// Close the file (Curiously, File's destructor doesn't close the file)
configFile.close();
return true;
}
else
{
Serial.println("Config File not found...");
return false;
}
}
else
{
Serial.println("Failed To read and mount Filesystem..");
return false;
}
} // end of ReadConfigFile
// Saves the configuration to a file
bool saveConfiguration()
{
Serial.println("mounting FS...for saving");
if (SPIFFS.begin())
{
Serial.println("mounted file system");
if (SPIFFS.exists(ConfigFileName))
{
SPIFFS.remove(ConfigFileName);
}
File file = SPIFFS.open(ConfigFileName, "w");
if (!file)
{
Serial.println(F("Failed to create file"));
return false;
}
StaticJsonDocument<256> doc;
for (int i = 0; i < WeMo::NoOfRelay; i++)
{
doc[WeMo::RelayNames[i]] = WeMo::RelayStatus[i];
doc[WeMo::RelayNames[i] + WeMo::RelayPins[i]] = WeMo::isRelayOn[i];
}
// Serialize JSON to file
if (serializeJson(doc, file) == 0)
{
Serial.println(F("Failed to write to file"));
return false;
}
Serial.println("file saved");
// Close the file
file.close();
return true;
}
else
{
Serial.println("mounted file system failed");
return false;
}
}
// Prints the content of a file to the Serial
void printFile(const char *filename)
{
if (SPIFFS.begin())
{
if (SPIFFS.exists(ConfigFileName))
{
File file = SPIFFS.open(filename, "r");
if (!file)
{
Serial.println(F("Failed to read file"));
return;
}
// Extract each characters by one by one
while (file.available())
{
Serial.print((char)file.read());
}
Serial.println();
// Close the file
file.close();
}
else
{
Serial.println("File doesnt exisit");
}
}
else
{
Serial.println("File sys mounting failed");
}
}
//Saving Spefic Setting
bool saveRelayConfiguration(String sName, int value, String sName2, bool value2)
{
//TODO: Have to make specific value either one or two. need to check with retroSwitch
Serial.println("mounting FS...for saving");
if (SPIFFS.begin())
{
Serial.println("mounted file system");
if (!SPIFFS.exists(ConfigFileName))
{
Serial.println("File Doesnt exist...");
}
File file = SPIFFS.open(ConfigFileName, "w");
if (!file)
{
Serial.println(F("Failed to create file"));
return false;
}
StaticJsonDocument<256> doc;
doc[sName] = value;
doc[sName2] = value2;
// Serialize JSON to file
if (serializeJson(doc, file) == 0)
{
Serial.println(F("Failed to write to file"));
return false;
}
Serial.println("file saved");
// Close the file
file.close();
return true;
}
else
{
Serial.println("mounted file system failed");
return false;
}
}