escape characters instead of angle brackets

This commit is contained in:
suks.ae
2026-07-31 12:10:16 +02:00
parent 45476312ab
commit a85eb30659
3 changed files with 18 additions and 9 deletions
+5 -9
View File
@@ -632,8 +632,6 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
}
UserVar.setFloat(event->TaskIndex, x, doublePayload); // Save the new value
// Generate event for rules processing - proposed by TridentTD
if (Settings.UseRules && P037_SEND_EVENTS) {
@@ -661,11 +659,10 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
}
P037_addEventToQueue(event, RuleEvent);
}
else
{ // Generate event of all non-json topic/payloads
else // Generate event of all non-json topic/payloads
{
String tmp = unparsedPayload;
tmp.replace('}', '>'); // Replace curly braces by angle brackets to avoid problems with string processing in rules
tmp.replace('{', '<');
addEscapeCharacters(tmp); // Add escape characters to avoid problems with rules processing if a JSON message is received
String RuleEvent = strformat(F("%s#%s=%s"),
getTaskDeviceName(event->TaskIndex).c_str(),
@@ -697,9 +694,8 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
RuleEvent += toString(doublePayload, ExtraTaskSettings.TaskDeviceValueDecimals[x]);
} else {
String tmp = Payload;
tmp.replace('}', '>'); // Replace curly braces by angle brackets to avoid problems with
tmp.replace('{', '<'); // string processing in rules if a JSON message is received
addEscapeCharacters(tmp); // Add escape characters to avoid problems with rules processing if a JSON message is received
RuleEvent += wrapWithQuotesIfContainsParameterSeparatorChar(tmp);
}
P037_addEventToQueue(event, RuleEvent);
+11
View File
@@ -46,6 +46,17 @@ void stripEscapeCharacters(String& str)
}
}
void addEscapeCharacters(String& str)
{
const char braces[] = { '%', '[', ']', '{', '}', '(', ')' };
constexpr uint8_t nrbraces = NR_ELEMENTS(braces);
for (uint8_t i = 0; i < nrbraces; ++i) {
const String s(concat(F("\\"), braces[i]));
str.replace(s.substring(1), s);
}
}
#if FEATURE_STRING_VARIABLES
String parseTemplateAndCalculate(String& tmpString) {
stripEscapeCharacters(tmpString);
+2
View File
@@ -15,6 +15,8 @@ bool hasEscapedCharacter(String& str, const char EscapeChar);
// So far \\% \\[ \\] \\{ \\} \\( and \\) are used (all with single backslash!)
void stripEscapeCharacters(String& str);
void addEscapeCharacters(String& str);
#if FEATURE_STRING_VARIABLES
String parseTemplateAndCalculate(String& tmpString);
uint8_t getDerivedValueCountForTask(taskIndex_t taskIndex);