mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-09-11 09:04:53 +00:00
Merge pull request #5597 from SuksAE/P037_MQTTImport_improvement
Change in P037_MQTTImport to enable processing of JSON messages in rules
This commit is contained in:
+26
-20
@@ -632,25 +632,6 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
|
||||
}
|
||||
UserVar.setFloat(event->TaskIndex, x, doublePayload); // Save the new value
|
||||
|
||||
if (!checkJson && P037_SEND_EVENTS && Settings.UseRules) { // Generate event of all non-json topic/payloads
|
||||
String RuleEvent = strformat(F("%s#%s=%s"),
|
||||
getTaskDeviceName(event->TaskIndex).c_str(),
|
||||
event->String1.c_str(),
|
||||
wrapWithQuotesIfContainsParameterSeparatorChar(unparsedPayload).c_str());
|
||||
P037_addEventToQueue(event, RuleEvent);
|
||||
}
|
||||
|
||||
// Log the event
|
||||
# if !defined(P037_LIMIT_BUILD_SIZE) || defined(P037_OVERRIDE)
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
|
||||
addLog(LOG_LEVEL_INFO, strformat(F("IMPT : [%s#%s] : %s"),
|
||||
getTaskDeviceName(event->TaskIndex).c_str(),
|
||||
checkJson ? key.c_str() : getTaskValueName(event->TaskIndex, x).c_str(),
|
||||
toString(doublePayload, ExtraTaskSettings.TaskDeviceValueDecimals[x]).c_str()));
|
||||
}
|
||||
# endif // if !defined(P037_LIMIT_BUILD_SIZE) || defined(P037_OVERRIDE)
|
||||
|
||||
// Generate event for rules processing - proposed by TridentTD
|
||||
|
||||
if (Settings.UseRules && P037_SEND_EVENTS) {
|
||||
@@ -678,6 +659,28 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
|
||||
}
|
||||
P037_addEventToQueue(event, RuleEvent);
|
||||
}
|
||||
else // Generate event of all non-json topic/payloads
|
||||
{
|
||||
String tmp = unparsedPayload;
|
||||
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(),
|
||||
event->String1.c_str(),
|
||||
wrapWithQuotesIfContainsParameterSeparatorChar(tmp).c_str());
|
||||
P037_addEventToQueue(event, RuleEvent);
|
||||
}
|
||||
|
||||
// Log the event
|
||||
# if !defined(P037_LIMIT_BUILD_SIZE) || defined(P037_OVERRIDE)
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
|
||||
addLog(LOG_LEVEL_INFO, strformat(F("IMPT : [%s#%s] : %s"),
|
||||
getTaskDeviceName(event->TaskIndex).c_str(),
|
||||
checkJson ? key.c_str() : getTaskValueName(event->TaskIndex, x).c_str(),
|
||||
toString(doublePayload, ExtraTaskSettings.TaskDeviceValueDecimals[x]).c_str()));
|
||||
}
|
||||
# endif // if !defined(P037_LIMIT_BUILD_SIZE) || defined(P037_OVERRIDE)
|
||||
|
||||
// (Always) Generate <Taskname>#<Valuename>=<Payload> event
|
||||
String RuleEvent;
|
||||
@@ -690,7 +693,10 @@ boolean Plugin_037(uint8_t function, struct EventStruct *event, String& string)
|
||||
if (numericPayload) {
|
||||
RuleEvent += toString(doublePayload, ExtraTaskSettings.TaskDeviceValueDecimals[x]);
|
||||
} else {
|
||||
RuleEvent += wrapWithQuotesIfContainsParameterSeparatorChar(Payload);
|
||||
String tmp = Payload;
|
||||
addEscapeCharacters(tmp); // Add escape characters to avoid problems with rules processing if a JSON message is received
|
||||
|
||||
RuleEvent += wrapWithQuotesIfContainsParameterSeparatorChar(tmp);
|
||||
}
|
||||
P037_addEventToQueue(event, RuleEvent);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user