mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
FileUtils, some typos fixes
This commit is contained in:
66
include/Errors.h
Normal file
66
include/Errors.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
enum ErrorType_t {
|
||||
ET_NONE,
|
||||
ET_FUNCTION,
|
||||
ET_MODULE,
|
||||
ET_SYSTEM
|
||||
};
|
||||
|
||||
enum ErrorLevel_t {
|
||||
EL_NONE,
|
||||
EL_INFO,
|
||||
EL_WARNING,
|
||||
EL_ERROR
|
||||
};
|
||||
|
||||
String getErrorLevelStr(ErrorLevel_t level);
|
||||
|
||||
class Error : public Printable {
|
||||
public:
|
||||
static Error OK() {
|
||||
return Error();
|
||||
}
|
||||
|
||||
static Error InfoMessage(const char *message) {
|
||||
return Error(EL_INFO, message);
|
||||
}
|
||||
|
||||
static Error ErrorMessage(const char *message) {
|
||||
return Error(EL_ERROR, message);
|
||||
}
|
||||
|
||||
public:
|
||||
Error() : _type{ET_NONE}, _level{EL_NONE} {};
|
||||
|
||||
Error(const ErrorLevel_t level, const char *message) : Error(ET_FUNCTION, level, message){};
|
||||
|
||||
Error(const ErrorType_t type, const ErrorLevel_t level, const char *message) : _type{type}, _level{level} {
|
||||
strncpy(_message, message, sizeof(_message));
|
||||
};
|
||||
|
||||
const ErrorLevel_t level() const { return _level; }
|
||||
|
||||
const ErrorType_t type() const { return _type; }
|
||||
|
||||
const char *message() const { return _message; }
|
||||
|
||||
operator bool() const { return _level != EL_NONE; }
|
||||
|
||||
const String toString() const {
|
||||
char buf[128];
|
||||
sprintf(buf, "[%s] %s", getErrorLevelStr(_level).c_str(), _message);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
virtual size_t printTo(Print &p) const {
|
||||
return p.println(toString().c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
char _message[128];
|
||||
ErrorType_t _type;
|
||||
ErrorLevel_t _level;
|
||||
};
|
||||
@@ -9,8 +9,10 @@
|
||||
#include "Consts.h"
|
||||
#include "ESP32_Spec.h"
|
||||
#include "ESP8266_Spec.h"
|
||||
#include "Errors.h"
|
||||
#include "GyverFilters.h"
|
||||
#include "UptimeInterval.h"
|
||||
#include "Utils\FileUtils.h"
|
||||
#include "Utils\JsonUtils.h"
|
||||
#include "Utils\StringUtils.h"
|
||||
#include "Utils\TimeUtils.h"
|
||||
@@ -173,15 +175,9 @@ extern void getMemoryLoad(String text);
|
||||
extern void saveConfig();
|
||||
extern String getURL(const String &urls);
|
||||
|
||||
extern String writeFile(String fileName, String strings);
|
||||
extern String readFile(String fileName, size_t len);
|
||||
extern String addFile(String fileName, String strings);
|
||||
|
||||
extern void servo_();
|
||||
extern boolean isDigitStr(String str);
|
||||
extern String jsonWriteStr(String &json, String name, String volume);
|
||||
|
||||
extern void led_blink(String satus);
|
||||
extern int count(String str, String found);
|
||||
|
||||
// Mqtt
|
||||
extern void MQTT_init();
|
||||
@@ -192,7 +188,6 @@ extern void sendSTATUS(String topik, String state);
|
||||
extern void sendCONTROL(String id, String topik, String state);
|
||||
extern void do_mqtt_connection();
|
||||
extern void handleMQTT();
|
||||
extern String selectFromMarkerToMarker(String str, String found, int number);
|
||||
|
||||
// WiFiUtils
|
||||
extern void WIFI_init();
|
||||
|
||||
30
include/Utils/FileUtils.h
Normal file
30
include/Utils/FileUtils.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <FS.h>
|
||||
|
||||
/*
|
||||
* Чтение строки из файла
|
||||
* возвращает стоку из файла в которой есть искомое слово found
|
||||
*/
|
||||
String readFileString(const String& filename, const String& str_to_found);
|
||||
|
||||
/*
|
||||
* Добовление строки в файл
|
||||
*/
|
||||
String addFile(const String& fileName, const String& str);
|
||||
|
||||
/*
|
||||
* Запись строки в файл
|
||||
*/
|
||||
String writeFile(const String& fileName, const String& str);
|
||||
|
||||
/*
|
||||
* Чтение файла в строку
|
||||
*/
|
||||
String readFile(const String& fileName, size_t len);
|
||||
|
||||
/*
|
||||
* Размер файла
|
||||
*/
|
||||
String sizeFile(const String& fileName);
|
||||
@@ -16,4 +16,8 @@ String deleteBeforeDelimiter(String str, String found);
|
||||
|
||||
String deleteBeforeDelimiterTo(String str, String found);
|
||||
|
||||
String selectFromMarkerToMarker(String str, String found, int number);
|
||||
String selectFromMarkerToMarker(String str, String found, int number);
|
||||
|
||||
size_t itemsCount(String str, const String& separator);
|
||||
|
||||
boolean isDigitStr(const String&);
|
||||
@@ -11,9 +11,17 @@ void reconfigTime();
|
||||
/*
|
||||
* Получение текущего времени
|
||||
*/
|
||||
String GetTime();
|
||||
String getTime();
|
||||
/*
|
||||
* Получаем время в формате linux gmt
|
||||
*/
|
||||
String getTimeUnix();
|
||||
|
||||
String GetTimeUnix();
|
||||
/*
|
||||
* Параметр время
|
||||
* Результат выполнения
|
||||
*/
|
||||
boolean getUnixTimeStr(String&);
|
||||
|
||||
String GetTimeWOsec();
|
||||
|
||||
|
||||
@@ -304,7 +304,7 @@ void timeSet() {
|
||||
void handle_time_init() {
|
||||
ts.add(
|
||||
TIME, 1000, [&](void *) {
|
||||
String tmp = GetTime();
|
||||
String tmp = getTime();
|
||||
jsonWriteStr(configLiveJson, "time", tmp);
|
||||
tmp.replace(":", "-");
|
||||
jsonWriteStr(configLiveJson, "timenow", tmp);
|
||||
@@ -332,7 +332,7 @@ void textSet() {
|
||||
if (text.indexOf("-time") >= 0) {
|
||||
text.replace("-time", "");
|
||||
text.replace("#", " ");
|
||||
String time = GetTime();
|
||||
String time = getTime();
|
||||
time.replace(":", ".");
|
||||
text = text + " " + GetDataDigital() + " " + time;
|
||||
}
|
||||
|
||||
25
src/Errors.cpp
Normal file
25
src/Errors.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "Errors.h"
|
||||
|
||||
static const char *str_info = "Info";
|
||||
static const char *str_warn = "Warn";
|
||||
static const char *str_error = "Error";
|
||||
static const char *str_unknown = "Unknown";
|
||||
|
||||
String getErrorLevelStr(ErrorLevel_t level) {
|
||||
const char *ptr;
|
||||
switch (level) {
|
||||
case EL_INFO:
|
||||
ptr = str_info;
|
||||
break;
|
||||
case EL_WARNING:
|
||||
ptr = str_warn;
|
||||
break;
|
||||
case EL_ERROR:
|
||||
ptr = str_error;
|
||||
break;
|
||||
default:
|
||||
ptr = str_unknown;
|
||||
break;
|
||||
}
|
||||
return String(ptr);
|
||||
}
|
||||
232
src/Logging.cpp
232
src/Logging.cpp
@@ -6,125 +6,135 @@ void sendLogData(String file, String topic);
|
||||
//===============================================Логирование============================================================
|
||||
//logging temp1 1 10 Температура Датчики 2
|
||||
void logging() {
|
||||
String value_name = sCmd.next();
|
||||
String period_min = sCmd.next();
|
||||
String maxCount = sCmd.next();
|
||||
String widget_name = sCmd.next();
|
||||
widget_name.replace("#", " ");
|
||||
String page_name = sCmd.next();
|
||||
String page_number = sCmd.next();
|
||||
logging_value_names_list += value_name + ",";
|
||||
enter_to_logging_counter++; //считаем количество входов в эту функцию
|
||||
jsonWriteStr(configOptionJson, value_name + "_c", maxCount); //создаем в файловой системе переменную количества точек на графике с отметкой _c что значит count
|
||||
createChart (widget_name, page_name, page_number, "widgets/widget.chart.json", value_name + "_ch", maxCount); //создаем график в приложении с топиком _ch /prefix/3234045-1589487/value_name_ch
|
||||
if (enter_to_logging_counter == LOG1) {
|
||||
ts.add(LOG1, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_1 = selectFromMarkerToMarker(logging_value_names_list, ",", 0);
|
||||
deleteOldDate("log." + tmp_buf_1 + ".txt", jsonReadInt(configOptionJson, tmp_buf_1 + "_c"), jsonReadStr(configLiveJson, tmp_buf_1));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_1 + "' done");
|
||||
}, nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG2) {
|
||||
ts.add(LOG2, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_2 = selectFromMarkerToMarker(logging_value_names_list, ",", 1);
|
||||
deleteOldDate("log." + tmp_buf_2 + ".txt", jsonReadInt(configOptionJson, tmp_buf_2 + "_c"), jsonReadStr(configLiveJson, tmp_buf_2));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_2 + "' done");
|
||||
}, nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG3) {
|
||||
ts.add(LOG3, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_3 = selectFromMarkerToMarker(logging_value_names_list, ",", 2);
|
||||
deleteOldDate("log." + tmp_buf_3 + ".txt", jsonReadInt(configOptionJson, tmp_buf_3 + "_c"), jsonReadStr(configLiveJson, tmp_buf_3));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_3 + "' done");
|
||||
}, nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG4) {
|
||||
ts.add(LOG4, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_4 = selectFromMarkerToMarker(logging_value_names_list, ",", 3);
|
||||
deleteOldDate("log." + tmp_buf_4 + ".txt", jsonReadInt(configOptionJson, tmp_buf_4 + "_c"), jsonReadStr(configLiveJson, tmp_buf_4));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_4 + "' done");
|
||||
}, nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG5) {
|
||||
ts.add(LOG5, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_5 = selectFromMarkerToMarker(logging_value_names_list, ",", 4);
|
||||
deleteOldDate("log." + tmp_buf_5 + ".txt", jsonReadInt(configOptionJson, tmp_buf_5 + "_c"), jsonReadStr(configLiveJson, tmp_buf_5));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_5 + "' done");
|
||||
}, nullptr, false);
|
||||
}
|
||||
String value_name = sCmd.next();
|
||||
String period_min = sCmd.next();
|
||||
String maxCount = sCmd.next();
|
||||
String widget_name = sCmd.next();
|
||||
widget_name.replace("#", " ");
|
||||
String page_name = sCmd.next();
|
||||
String page_number = sCmd.next();
|
||||
logging_value_names_list += value_name + ",";
|
||||
enter_to_logging_counter++; //считаем количество входов в эту функцию
|
||||
jsonWriteStr(configOptionJson, value_name + "_c", maxCount); //создаем в файловой системе переменную количества точек на графике с отметкой _c что значит count
|
||||
createChart(widget_name, page_name, page_number, "widgets/widget.chart.json", value_name + "_ch", maxCount); //создаем график в приложении с топиком _ch /prefix/3234045-1589487/value_name_ch
|
||||
if (enter_to_logging_counter == LOG1) {
|
||||
ts.add(
|
||||
LOG1, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_1 = selectFromMarkerToMarker(logging_value_names_list, ",", 0);
|
||||
deleteOldDate("log." + tmp_buf_1 + ".txt", jsonReadInt(configOptionJson, tmp_buf_1 + "_c"), jsonReadStr(configLiveJson, tmp_buf_1));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_1 + "' done");
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG2) {
|
||||
ts.add(
|
||||
LOG2, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_2 = selectFromMarkerToMarker(logging_value_names_list, ",", 1);
|
||||
deleteOldDate("log." + tmp_buf_2 + ".txt", jsonReadInt(configOptionJson, tmp_buf_2 + "_c"), jsonReadStr(configLiveJson, tmp_buf_2));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_2 + "' done");
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG3) {
|
||||
ts.add(
|
||||
LOG3, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_3 = selectFromMarkerToMarker(logging_value_names_list, ",", 2);
|
||||
deleteOldDate("log." + tmp_buf_3 + ".txt", jsonReadInt(configOptionJson, tmp_buf_3 + "_c"), jsonReadStr(configLiveJson, tmp_buf_3));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_3 + "' done");
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG4) {
|
||||
ts.add(
|
||||
LOG4, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_4 = selectFromMarkerToMarker(logging_value_names_list, ",", 3);
|
||||
deleteOldDate("log." + tmp_buf_4 + ".txt", jsonReadInt(configOptionJson, tmp_buf_4 + "_c"), jsonReadStr(configLiveJson, tmp_buf_4));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_4 + "' done");
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
if (enter_to_logging_counter == LOG5) {
|
||||
ts.add(
|
||||
LOG5, period_min.toInt() * 1000 * 60, [&](void*) {
|
||||
String tmp_buf_5 = selectFromMarkerToMarker(logging_value_names_list, ",", 4);
|
||||
deleteOldDate("log." + tmp_buf_5 + ".txt", jsonReadInt(configOptionJson, tmp_buf_5 + "_c"), jsonReadStr(configLiveJson, tmp_buf_5));
|
||||
Serial.println("[i] LOGGING for sensor '" + tmp_buf_5 + "' done");
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
}
|
||||
|
||||
//=========================================Удаление стрых данных и запись новых==================================================================
|
||||
void deleteOldDate(String file, int seted_number_of_lines, String date_to_add) {
|
||||
String log_date = readFile(file, 5000);
|
||||
int current_number_of_lines = count(log_date, "\r\n");
|
||||
Serial.println("=====> [i] in log file " + file + " " + current_number_of_lines + " lines");
|
||||
String log_date = readFile(file, 5000);
|
||||
int current_number_of_lines = itemsCount(log_date, "\r\n");
|
||||
Serial.println("=====> [i] in log file " + file + " " + current_number_of_lines + " lines");
|
||||
|
||||
if (current_number_of_lines > seted_number_of_lines + 1) {
|
||||
SPIFFS.remove("/" + file);
|
||||
current_number_of_lines = 0;
|
||||
}
|
||||
if (current_number_of_lines == 0) {
|
||||
SPIFFS.remove("/" + file);
|
||||
current_number_of_lines = 0;
|
||||
}
|
||||
if (current_number_of_lines > seted_number_of_lines) {
|
||||
log_date = deleteBeforeDelimiter(log_date, "\r\n");
|
||||
if (GetTimeUnix() != "failed") {
|
||||
log_date += GetTimeUnix() + " " + date_to_add + "\r\n";
|
||||
writeFile(file, log_date);
|
||||
if (current_number_of_lines > seted_number_of_lines + 1) {
|
||||
SPIFFS.remove("/" + file);
|
||||
current_number_of_lines = 0;
|
||||
}
|
||||
} else {
|
||||
if (GetTimeUnix() != "failed") {
|
||||
addFile(file, GetTimeUnix() + " " + date_to_add);
|
||||
if (current_number_of_lines == 0) {
|
||||
SPIFFS.remove("/" + file);
|
||||
current_number_of_lines = 0;
|
||||
}
|
||||
}
|
||||
log_date = "";
|
||||
if (current_number_of_lines > seted_number_of_lines) {
|
||||
log_date = deleteBeforeDelimiter(log_date, "\r\n");
|
||||
if (getTimeUnix() != "failed") {
|
||||
log_date += getTimeUnix() + " " + date_to_add + "\r\n";
|
||||
writeFile(file, log_date);
|
||||
}
|
||||
} else {
|
||||
if (getTimeUnix() != "failed") {
|
||||
addFile(file, getTimeUnix() + " " + date_to_add);
|
||||
}
|
||||
}
|
||||
log_date = "";
|
||||
}
|
||||
|
||||
//=========================================Выбор какие данные отправлять==================================================================
|
||||
void choose_log_date_and_send() {
|
||||
String all_line = logging_value_names_list;
|
||||
while (all_line.length() != 0) {
|
||||
String tmp = selectToMarker (all_line, ",");
|
||||
sendLogData("log." + tmp + ".txt", tmp + "_ch");
|
||||
all_line = deleteBeforeDelimiter(all_line, ",");
|
||||
}
|
||||
all_line = "";
|
||||
String all_line = logging_value_names_list;
|
||||
while (all_line.length() != 0) {
|
||||
String tmp = selectToMarker(all_line, ",");
|
||||
sendLogData("log." + tmp + ".txt", tmp + "_ch");
|
||||
all_line = deleteBeforeDelimiter(all_line, ",");
|
||||
}
|
||||
all_line = "";
|
||||
}
|
||||
//=========================================Отправка данных===================================================================================
|
||||
void sendLogData(String file, String topic) {
|
||||
String log_date = readFile(file, 5000);
|
||||
if (log_date != "Failed") {
|
||||
log_date.replace("\r\n", "\n");
|
||||
log_date.replace("\r", "\n");
|
||||
String buf = "{}";
|
||||
String json_array;
|
||||
String unix_time;
|
||||
String value;
|
||||
while (log_date.length() != 0) {
|
||||
String tmp = selectToMarker (log_date, "\n");
|
||||
log_date = deleteBeforeDelimiter(log_date, "\n");
|
||||
unix_time = selectToMarker (tmp, " ");
|
||||
jsonWriteInt(buf, "x", unix_time.toInt());
|
||||
value = deleteBeforeDelimiter(tmp, " ");
|
||||
jsonWriteFloat(buf, "y1", value.toFloat());
|
||||
if (log_date.length() < 3) {
|
||||
json_array += buf;
|
||||
} else {
|
||||
json_array += buf + ",";
|
||||
}
|
||||
buf = "{}";
|
||||
String log_date = readFile(file, 5000);
|
||||
if (log_date != "Failed") {
|
||||
log_date.replace("\r\n", "\n");
|
||||
log_date.replace("\r", "\n");
|
||||
String buf = "{}";
|
||||
String json_array;
|
||||
String unix_time;
|
||||
String value;
|
||||
while (log_date.length() != 0) {
|
||||
String tmp = selectToMarker(log_date, "\n");
|
||||
log_date = deleteBeforeDelimiter(log_date, "\n");
|
||||
unix_time = selectToMarker(tmp, " ");
|
||||
jsonWriteInt(buf, "x", unix_time.toInt());
|
||||
value = deleteBeforeDelimiter(tmp, " ");
|
||||
jsonWriteFloat(buf, "y1", value.toFloat());
|
||||
if (log_date.length() < 3) {
|
||||
json_array += buf;
|
||||
} else {
|
||||
json_array += buf + ",";
|
||||
}
|
||||
buf = "{}";
|
||||
}
|
||||
unix_time = "";
|
||||
value = "";
|
||||
log_date = "";
|
||||
json_array = "{\"status\":[" + json_array + "]}";
|
||||
Serial.println(json_array);
|
||||
sendCHART(topic, json_array);
|
||||
json_array = "";
|
||||
getMemoryLoad("[i] after send log date");
|
||||
}
|
||||
unix_time = "";
|
||||
value = "";
|
||||
log_date = "";
|
||||
json_array = "{\"status\":[" + json_array + "]}";
|
||||
Serial.println(json_array);
|
||||
sendCHART(topic, json_array);
|
||||
json_array = "";
|
||||
getMemoryLoad("[i] after send log date");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -146,12 +156,12 @@ void sendLogData(String file, String topic) {
|
||||
*/
|
||||
//=========================================Очистка данных===================================================================================
|
||||
void clean_log_date() {
|
||||
String all_line = logging_value_names_list;
|
||||
while (all_line.length() != 0) {
|
||||
String tmp = selectToMarker (all_line, ",");
|
||||
SPIFFS.remove("/log." + tmp + ".txt");
|
||||
all_line = deleteBeforeDelimiter(all_line, ",");
|
||||
}
|
||||
all_line = "";
|
||||
String all_line = logging_value_names_list;
|
||||
while (all_line.length() != 0) {
|
||||
String tmp = selectToMarker(all_line, ",");
|
||||
SPIFFS.remove("/log." + tmp + ".txt");
|
||||
all_line = deleteBeforeDelimiter(all_line, ",");
|
||||
}
|
||||
all_line = "";
|
||||
}
|
||||
#endif
|
||||
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;
|
||||
|
||||
80
src/main.cpp
80
src/main.cpp
@@ -4,27 +4,6 @@ void saveConfig() {
|
||||
writeFile("config.json", configSetupJson);
|
||||
}
|
||||
|
||||
//--------------------Посчитать -----------------------------------------------------------------------------------
|
||||
int count(String str, String found) {
|
||||
if (str.indexOf(found) == -1) return 0; // если строки поиск нет сразу выход
|
||||
str += found; // добавим для корректного поиска
|
||||
uint8_t i = 0; // Индекс перебора
|
||||
while (str.length() != 0) {
|
||||
str = deleteBeforeDelimiter(str, found); // отбросим проверенный блок до разделителя
|
||||
i++; // увеличим индекс
|
||||
}
|
||||
return i; // Достигли пустой строки и ничего не нашли
|
||||
}
|
||||
|
||||
boolean isDigitStr(String str) {
|
||||
for (size_t i = 0; i < str.length(); i++) {
|
||||
if (!isDigit(str.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return str.length();
|
||||
}
|
||||
|
||||
String getURL(const String& urls) {
|
||||
String res = "";
|
||||
HTTPClient http;
|
||||
@@ -52,65 +31,6 @@ void safeDataToFile(String data, String Folder) {
|
||||
jsonWriteStr(configLiveJson, "test", fileName);
|
||||
}
|
||||
|
||||
// ------------- Чтение файла в строку -------------------------------------------------------------------------------
|
||||
String readFile(String fileName, size_t len) {
|
||||
File configFile = SPIFFS.open("/" + fileName, "r");
|
||||
if (!configFile) {
|
||||
return "Failed";
|
||||
}
|
||||
size_t size = configFile.size();
|
||||
if (size > len) {
|
||||
configFile.close();
|
||||
return "Large";
|
||||
}
|
||||
String temp = configFile.readString();
|
||||
configFile.close();
|
||||
return temp;
|
||||
}
|
||||
// ------------- Размер файла ----------------------------------------------------------------------------------------
|
||||
String sizeFile(String fileName) {
|
||||
File configFile = SPIFFS.open("/" + fileName, "r");
|
||||
if (!configFile) {
|
||||
return "Failed";
|
||||
}
|
||||
size_t size = configFile.size();
|
||||
configFile.close();
|
||||
return String(size);
|
||||
}
|
||||
// ------------- Запись строки в файл ---------------------------------------------------------------------------------
|
||||
String writeFile(String fileName, String strings) {
|
||||
File configFile = SPIFFS.open("/" + fileName, "w");
|
||||
if (!configFile) {
|
||||
return "Failed to open config file";
|
||||
}
|
||||
configFile.print(strings);
|
||||
//strings.printTo(configFile);
|
||||
configFile.close();
|
||||
return "Write sucsses";
|
||||
}
|
||||
// ------------- Добовление строки в файл ------------------------------------------------------------------------------
|
||||
String addFile(String fileName, String strings) {
|
||||
File configFile = SPIFFS.open("/" + fileName, "a");
|
||||
if (!configFile) {
|
||||
return "Failed to open config file";
|
||||
}
|
||||
configFile.println(strings);
|
||||
configFile.close();
|
||||
return "Write sucsses";
|
||||
}
|
||||
// ------------- Чтение строки из файла ---------------------------------------------------------------------------------
|
||||
//возвращает стоку из файла в которой есть искомое слово found
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void sendCONFIG(String topik, String widgetConfig, String key, String date) {
|
||||
yield();
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/status";
|
||||
|
||||
Reference in New Issue
Block a user