Оптимизируем работу со строками в файлах SerialPrint и StringUtils

This commit is contained in:
2022-10-28 17:16:53 +03:00
parent dfd24dc1f6
commit a292f17285
4 changed files with 47 additions and 42 deletions

View File

@@ -3,4 +3,4 @@
#include "utils/TimeUtils.h" #include "utils/TimeUtils.h"
#include "classes/IoTItem.h" #include "classes/IoTItem.h"
void SerialPrint(String errorLevel, String module, String msg, String itemId = ""); void SerialPrint(const String& errorLevel, const String& module, const String& msg, const String& itemId = "");

View File

@@ -8,29 +8,29 @@ void hex2string(byte array[], unsigned int len, char buffer[]);
int string2hex(const char* str, unsigned char* bytes); int string2hex(const char* str, unsigned char* bytes);
uint8_t hexStringToUint8(String hex); uint8_t hexStringToUint8(const String& hex);
uint16_t hexStringToUint16(String hex); uint16_t hexStringToUint16(const String& hex);
String selectToMarkerLast(String str, String found); String selectToMarkerLast(String str, const String& found);
String selectToMarker(String str, String found); String selectToMarker(String str, const String& found);
String extractInner(String str); String extractInner(String str);
String deleteAfterDelimiter(String str, String found); String deleteAfterDelimiter(String str, const String& found);
String deleteBeforeDelimiter(String str, String found); String deleteBeforeDelimiter(String str, const String& found);
String deleteBeforeDelimiterTo(String str, String found); String deleteBeforeDelimiterTo(String str, const String& found);
String deleteToMarkerLast(String str, String found); String deleteToMarkerLast(String str, const String& found);
String selectFromMarkerToMarker(String str, String found, int number); String selectFromMarkerToMarker(String str, const String& found, int number);
size_t itemsCount2(String str, const String& separator); size_t itemsCount2(String str, const String& separator);
char* stringToChar(String& str); char* stringToChar(const String& str);
//size_t itemsCount(String& str, const char* delim); //size_t itemsCount(String& str, const char* delim);
@@ -42,4 +42,4 @@ String prettyBytes(size_t size);
String uint64ToString(uint64_t input, uint8_t base = 10); String uint64ToString(uint64_t input, uint8_t base = 10);
String cleanString(String str); void cleanString(String& str);

View File

@@ -1,23 +1,16 @@
#include "utils/SerialPrint.h" #include "utils/SerialPrint.h"
void SerialPrint(String errorLevel, String module, String msg, String itemId) { void SerialPrint(const String& errorLevel, const String& module, const String& msg, const String& itemId) {
String tosend = prettyMillis(millis()); String tosend = prettyMillis(millis());
tosend = tosend + " [" + errorLevel + "] [" + module + "] " + msg; tosend += " [";
tosend += errorLevel;
tosend += "] [";
tosend += module;
tosend += "] ";
tosend += msg;
Serial.println(tosend); Serial.println(tosend);
if (errorLevel == "E") {
msg = cleanString(msg);
// создаем событие об ошибке для возможной реакции в сценарии
if (itemId != "") {
IoTItems.push_back((IoTItem *)new externalVariable("{\"id\":\"" + itemId + "_onError\",\"val\":\"" + msg + "\",\"int\":1}"));
generateEvent(itemId + "_onError", "1");
} else {
IoTItems.push_back((IoTItem *)new externalVariable("{\"id\":\"onError\",\"val\":\"" + module + " " + msg + "\",\"int\":1}"));
generateEvent("onError", "1");
}
}
if (isNetworkActive()) { if (isNetworkActive()) {
if (jsonReadInt(settingsFlashJson, F("log")) != 0) { if (jsonReadInt(settingsFlashJson, F("log")) != 0) {
// String pl = "/log|" + tosend; // String pl = "/log|" + tosend;
@@ -25,4 +18,18 @@ void SerialPrint(String errorLevel, String module, String msg, String itemId) {
sendStringToWs("corelg", tosend, -1); sendStringToWs("corelg", tosend, -1);
} }
} }
if (errorLevel == "E") {
cleanString(tosend);
// создаем событие об ошибке для возможной реакции в сценарии
if (itemId != "") {
IoTItems.push_back((IoTItem *)new externalVariable("{\"id\":\"" + itemId + "_onError\",\"val\":\"" + tosend + "\",\"int\":1}"));
generateEvent(itemId + "_onError", "1");
} else {
IoTItems.push_back((IoTItem *)new externalVariable("{\"id\":\"onError\",\"val\":\"" + module + " " + tosend + "\",\"int\":1}"));
generateEvent("onError", "1");
}
}
} }

View File

@@ -8,12 +8,12 @@ void writeUint8tToString(uint8_t* payload, size_t length, size_t headerLenth, St
} }
} }
String selectToMarkerLast(String str, String found) { String selectToMarkerLast(String str, const String& found) {
int p = str.lastIndexOf(found); int p = str.lastIndexOf(found);
return str.substring(p + found.length()); return str.substring(p + found.length());
} }
String selectToMarker(String str, String found) { String selectToMarker(String str, const String& found) {
int p = str.indexOf(found); int p = str.indexOf(found);
return str.substring(0, p); return str.substring(0, p);
} }
@@ -24,32 +24,32 @@ String extractInner(String str) {
return str.substring(p1 + 1, p2); return str.substring(p1 + 1, p2);
} }
String deleteAfterDelimiter(String str, String found) { String deleteAfterDelimiter(String str, const String& found) {
int p = str.indexOf(found); int p = str.indexOf(found);
return str.substring(0, p); return str.substring(0, p);
} }
String deleteBeforeDelimiter(String str, String found) { String deleteBeforeDelimiter(String str, const String& found) {
int p = str.indexOf(found) + found.length(); int p = str.indexOf(found) + found.length();
return str.substring(p); return str.substring(p);
} }
String deleteBeforeDelimiterTo(String str, String found) { String deleteBeforeDelimiterTo(String str, const String& found) {
int p = str.indexOf(found); int p = str.indexOf(found);
return str.substring(p); return str.substring(p);
} }
String deleteToMarkerLast(String str, String found) { String deleteToMarkerLast(String str, const String& found) {
int p = str.lastIndexOf(found); int p = str.lastIndexOf(found);
return str.substring(0, p); return str.substring(0, p);
} }
String selectToMarkerPlus(String str, String found, int plus) { String selectToMarkerPlus(String str, const String& found, int plus) {
int p = str.indexOf(found); int p = str.indexOf(found);
return str.substring(0, p + plus); return str.substring(0, p + plus);
} }
String selectFromMarkerToMarker(String str, String tofind, int number) { String selectFromMarkerToMarker(String str, const String& tofind, int number) {
if (str.indexOf(tofind) == -1) { if (str.indexOf(tofind) == -1) {
return "not found"; return "not found";
} }
@@ -98,14 +98,14 @@ int string2hex(const char* str, unsigned char* bytes) {
return i; return i;
} }
uint8_t hexStringToUint8(String hex) { uint8_t hexStringToUint8(const String& hex) {
uint8_t tmp = strtol(hex.c_str(), NULL, 0); uint8_t tmp = strtol(hex.c_str(), NULL, 0);
if (tmp >= 0x00 && tmp <= 0xFF) { if (tmp >= 0x00 && tmp <= 0xFF) {
return tmp; return tmp;
} }
} }
uint16_t hexStringToUint16(String hex) { uint16_t hexStringToUint16(const String& hex) {
uint16_t tmp = strtol(hex.c_str(), NULL, 0); uint16_t tmp = strtol(hex.c_str(), NULL, 0);
if (tmp >= 0x0000 && tmp <= 0xFFFF) { if (tmp >= 0x0000 && tmp <= 0xFFFF) {
return tmp; return tmp;
@@ -141,7 +141,7 @@ size_t itemsCount2(String str, const String& separator) {
// return cnt; // return cnt;
// } // }
char* stringToChar(String& str) { char* stringToChar(const String& str) {
char* mychar = new char[str.length() + 1]; char* mychar = new char[str.length() + 1];
strcpy(mychar, str.c_str()); strcpy(mychar, str.c_str());
return mychar; return mychar;
@@ -199,11 +199,9 @@ String uint64ToString(uint64_t input, uint8_t base) {
return result; return result;
} }
String cleanString(String str) { void cleanString(String& str) {
String clearStr = ""; const String allowedChars = F("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя.!-+ ");
const String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя.!-+ ";
for (size_t i = 0; i < str.length(); i++) { for (size_t i = 0; i < str.length(); i++) {
if (allowedChars.indexOf(str.charAt(i)) != -1) clearStr += str.charAt(i); if (allowedChars.indexOf(str.charAt(i)) == -1) str.setCharAt(i, ' ');
} }
return clearStr;
} }