Change in P037_MQTTImport to enable processing of JSON messages in rules - #5597
Conversation
|
Rules (and commands) already have the feature that some special characters, like percent, square- and curly- braces, can be escaped by preceding them with a backslash ( |
|
changed according description. please review |
|
|
||
| for (uint8_t i = 0; i < nrbraces; ++i) { | ||
| const String s(concat(F("\\"), braces[i])); | ||
| str.replace(s.substring(1), s); |
There was a problem hiding this comment.
Did you test this?
Not sure if F("\\") will be 1 or 2 characters long.
Maybe better to use '\\' and see whether the compiler accepts it?
There was a problem hiding this comment.
I did test it, I got following output in the console:
EVENT: MQTTImport#devices/rpc/request='\{"method":"setDo0","params":true\}'
and processing in rules also works.
It is the same code as in stripEscapeCharacters(), just the other way around...
|
This got me inspired to be able to UnEscape and Escape (JSON) strings in rules, so I added that as feature, based on this PR: (File removed, feature will be in a new PR) Unzip the file, place the content
I also added the colon ( You can commit and push these changes in this PR, keeping it together👍 NB: These functions are only available when String Variables are included in the build. giving this output:
|
Currently the processing of JSON formated messages can only be done via the MQTTImport device.
As this device has certain limitations in processing of JSON messages, it is beneficial if it would also be possible to process JSON messages in rules. This is currently not possible, because the curly brackets used in JSON messages are handled as special characters by the rules engine.
I propose to substitute all curly brackets in unprocessed JSON messages with angle brackets before the data is put into the event queue by the MQTTImport device. So, JSON messages do not interfere with string manipulation commands in rules.
example:
Note the outcome at the last line at before and after...
rule:
before:
after: