mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-28 15:12:19 +03:00
FileUtils, some typos fixes
This commit is contained in:
56
src/Utils/FileUtils.cpp
Normal file
56
src/Utils/FileUtils.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "Utils/FileUtils.h"
|
||||
|
||||
String readFileString(const String& filename, const String& str_to_found) {
|
||||
String res = "failed";
|
||||
auto file = SPIFFS.open("/" + filename, "r");
|
||||
if (file && file.find(str_to_found.c_str())) {
|
||||
res = file.readStringUntil('\n');
|
||||
}
|
||||
file.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
String addFile(const String& fileName, const String& str) {
|
||||
auto file = SPIFFS.open("/" + fileName, "a");
|
||||
if (!file) {
|
||||
return "Failed to open file";
|
||||
}
|
||||
file.println(str);
|
||||
file.close();
|
||||
return "Write sucсess";
|
||||
}
|
||||
|
||||
String writeFile(const String& fileName, const String& str) {
|
||||
auto file = SPIFFS.open("/" + fileName, "w");
|
||||
if (!file) {
|
||||
return "Failed to open file";
|
||||
}
|
||||
file.print(str);
|
||||
file.close();
|
||||
return "Write sucсess";
|
||||
}
|
||||
|
||||
String readFile(const String& fileName, size_t len) {
|
||||
File file = SPIFFS.open("/" + fileName, "r");
|
||||
if (!file) {
|
||||
return "Failed";
|
||||
}
|
||||
size_t size = file.size();
|
||||
if (size > len) {
|
||||
file.close();
|
||||
return "Large";
|
||||
}
|
||||
String temp = file.readString();
|
||||
file.close();
|
||||
return temp;
|
||||
}
|
||||
|
||||
String sizeFile(const String& fileName) {
|
||||
auto file = SPIFFS.open("/" + fileName, "r");
|
||||
if (!file) {
|
||||
return "Failed";
|
||||
}
|
||||
size_t size = file.size();
|
||||
file.close();
|
||||
return String(size);
|
||||
}
|
||||
@@ -60,3 +60,28 @@ uint16_t hexStringToUint16(String hex) {
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
size_t itemsCount(String str, const String& separator) {
|
||||
// если строки поиск нет сразу выход
|
||||
if (str.indexOf(separator) == -1) {
|
||||
return 0;
|
||||
}
|
||||
// добавим для корректного поиска
|
||||
str += separator;
|
||||
size_t cnt = 0;
|
||||
while (str.length()) {
|
||||
// отбросим проверенный блок до разделителя
|
||||
str = deleteBeforeDelimiter(str, separator);
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
boolean isDigitStr(const String& str) {
|
||||
for (size_t i = 0; i < str.length(); i++) {
|
||||
if (!isDigit(str.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return str.length();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ const String prettyMillis(unsigned long time_ms) {
|
||||
}
|
||||
|
||||
void time_check() {
|
||||
if (GetTimeUnix() == "failed") {
|
||||
if (getTimeUnix() == "failed") {
|
||||
Serial.println("[i] Time is not synchronized, start synchronization");
|
||||
reconfigTime();
|
||||
}
|
||||
@@ -66,11 +66,11 @@ void reconfigTime() {
|
||||
delay(2000);
|
||||
//}
|
||||
#endif
|
||||
if (GetTimeUnix() != "failed") {
|
||||
if (getTimeUnix() != "failed") {
|
||||
Serial.print("[V] Time synchronized = ");
|
||||
Serial.print(GetDataDigital());
|
||||
Serial.print(" ");
|
||||
Serial.println(GetTime());
|
||||
Serial.println(getTime());
|
||||
} else {
|
||||
Serial.println("[E] Time server or internet connection error, will try again in 30 sec");
|
||||
}
|
||||
@@ -79,8 +79,7 @@ void reconfigTime() {
|
||||
}
|
||||
}
|
||||
|
||||
//Получаем время в формате linux gmt
|
||||
String GetTimeUnix() {
|
||||
String getTimeUnix() {
|
||||
time_t now = time(nullptr);
|
||||
if (now < 30000) {
|
||||
return "failed";
|
||||
@@ -89,7 +88,13 @@ String GetTimeUnix() {
|
||||
}
|
||||
}
|
||||
|
||||
String GetTime() {
|
||||
boolean getUnixTimeStr(String& res) {
|
||||
time_t now = time(nullptr);
|
||||
res = String(now);
|
||||
return now < 30000;
|
||||
}
|
||||
|
||||
String getTime() {
|
||||
time_t now = time(nullptr); // получаем время с помощью библиотеки time.h
|
||||
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||
now = now + zone;
|
||||
|
||||
Reference in New Issue
Block a user