-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rules] [Feature Request] Adding support for substring and hex parsing #2828
Conversation
* substring * strtol (useful for converting hex or binary numbers) * 100ths division in eventvalue parsing. Using this modification I am able to handle the messages sent from Opentherm Gateway ( http://otgw.tclcode.com/ ) and to set variables of a dummy device in rules. Example: * Message coming from the serial interface: T101813C0 ** The B denotes that the message is from the boiler. ** The next 4 bytes (actually 2bytes hex encoded) denote the status and type of the message. ** the last 4 bytes (actually 2bytes hex encoded) denote the payload. * Message that ends up in rules when using ser2net (P020) and Generic handling: !Serial#BT101813C0 The room temperature in this sample is 19.75°C Rule used to parse this and set a variable in a dummy device: // Room temperature on !Serial#T1018* do TaskValueSet 2,1,[strtol:16:[substring:13:15:%eventvalue%]].[div100ths:[strtol:16:[substring:15:17:%eventvalue%]]:256] endon Now the variable can be used in the rules/controller/log/whatever.
… the actual wildcard and not up to the wildcard.
Merged the functions as suggested by TD-er
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in case it gets dismissed: I fixed another issue regarding the wildcard matching in crathje@031888f
Checking for closing character ]. Switched to if else if. Copied command string to get a lowercase representation.
I fixed a merge conflict, created documentation and will very likely merge it tomorrow. For any future pull request you may want to make. |
OK, I'm now testing it a bit more and these transforms do not work well on |
Hmm this looks like it is causing a major rewrite of the rules parsing engine. So what should be done here is something like this: (just written here as memory dump, not tested) String parseline(const String& input, int start, int& end, bool useURLencode) {
int newStart = input.indexOf('[', start + 1);
if (newStart != -1) {
int newEnd = end;
String subResult = parseline(input, newStart, newEnd, useURLencode);
int sectionEnd = input.indexOf(']', newEnd);
if (sectionEnd != -1) {
// Found a closing bracket, so we have to interpret the content of this section.
end = sectionEnd;
String section = input.substring(start, newStart);
section += subResult;
section += input.substring(newEnd, sectionEnd);
// interpret content
return parseTemplate_padded(section, useURLencode);
}
addLog(LOG_LEVEL_ERROR, F("Error: bracket mismatch");
}
return input.substring(start, end);
} This is just a dump of what I'm now thinking of and it does also need quite a bit of rework on the parseTemplate function as well, since we're essentially making the parser quite a bit more flexible. N.B. this suggested Edit: |
Just couldn't let it go, so I just made it possible to use nested string commands. So this is now also possible:
And some debug log for illustrative purposes:
|
@TD-er Probably a more general solution should be to create a "Opentherm Gateway" plugin. These are my 2 cents, of course... |
I was also a bit confused about this one: So in that respect I do share your concern about this one. The pro for such a command is to make sure the result is a float in between steps, or else you will loose a lot of resolution. For now, I will remove this specific one (comment out) as it may indeed lead to a lot of these. The reason I put some work in this was also because I suddenly thought of a way to make parsing with nested brackets without using recursive calls and lots of memory allocation. Apart from that there have been a number of requests to split longer numbers, like in the processing of RFID etc, so that was also very nice to see implemented by this PR. But thanks for the critical look on the div100ths, as that's a bit too specific indeed. |
@TD-er maybe change [ ] to {} or <> |
Just curious, why? |
because we use these chars already for something else and now your example line is completely unreadable |
That's a valid point. |
{} left untouched maybe later we can add some user variables dedicated for strings manipulation? |
Well the % notation I made for some standard conversions are also a bit unreadable, so maybe something like |
Adding support for
in eventvalue parsing.
Using this modification I am able to handle the messages sent from Opentherm Gateway ( http://otgw.tclcode.com/ ) and to set variables of a dummy device in rules.
Example:
The room temperature in this sample is 19.75°C
To get this I need to access the last four bytes in packs of two bytes:
[substring:13:15:%eventvalue%]
and
[substring:15:17:%eventvalue%]
Parsing them to decimal representation each (using a base 16 call to strtol):
[strtol:16:[substring:13:15:%eventvalue%]]
and
[strtol:16:[substring:15:17:%eventvalue%]]
Last but not least the fraction is not correct, it needs to be divided by 256 (and multiplied by 100) with a call to div100ths:
[div100ths:[strtol:16:[substring:15:17:%eventvalue%]]:256]
And now a complete rule that I use to parse this and set a variable in a dummy device:
Now the variable can be used in the rules/controller/log/whatever.