Subscribe by Regex with option 'Do not subscribe all states on start'
Describe the bug
Not receiving messages after subscribing with regex. Option 'Do not subscribe all states on start' is true.
Similar to issue #1311
To Reproduce
Steps to reproduce the behavior:
- Go to setting adapter
- Set option 'Do not subscribe all states on start' as true.
- Create and run script (for example)
on(new RegExp("mqtt.0.zigbee2mqtt.*"), cs => {
log(cs.state.val); // never :(
});
- Change state mqtt.0.zigbee2mqtt.*
Expected behavior
Messages are coming.
Versions:
- Adapter version: 7.1.4
- JS-Controller version: 4.0.24
- Node version: 16
- Operating system: Linux
Mal unabhängig vom issue ... dir ist klar das man zb "." in Regex escapen muss? Was du dfa hast ist eher ein "Pattern" und weniger eine regex ...
Mal unabhängig vom issue ... dir ist klar das man zb "." in Regex escapen muss? Was du dfa hast ist eher ein "Pattern" und weniger eine regex ...
I understand. Just an example
new RegExp("mqtt\.0\.zigbee2mqtt\..*")
Is there a suggested workaround for this issue? Iterate all states by using $[...], registering a single callback for each of the matches?
Okay, a practical solution is the following code:
$("mqtt.0.zigbee2mqtt.*").on(function(cs) {
// Use this instead of the original function OR in addition if you rely on a more complex regexp
});
As soon as this is added, more complicated regular expressions also work - as long as they are covered by the wildcard in the workaround. So the problem might be due to js-controller not being able to work with regular expressions?
I think this is related to #525
The problem is, that the subscribeForeignState function doesn't support regular expressions.
https://github.com/ioBroker/ioBroker.javascript/blob/3a87abff627e341875333f122015423152abf4ad/lib/sandbox.js#L74
Thats indeed completely right.
So easiest solution is to "throw" an error if someone tries to subscribe regex when not all states are subscribed. In fact my main question would by why someone turned off the auto subscription ... this is "advanced feature" in my eyes which should be only used well-thought.
The alterative is very meehhh because it would require to somehow "convert" the given regex pattern into a "iobroker wildcard pattern string" (kind of only "*" and "?" allowed) ...
I turned the option off because the adapter started using up more and more resources. I have around 50-60 scripts running, from ones with only a couple of lines to ones with a couple of thousand lines. Turning all state subscription off lowers CPU and RAM usage by a factor of 5.
Turning all state subscription off lowers CPU and RAM usage by a factor of 5.
This is to be expected because with one central subscribe the adapter can handle it better with just some "downsides" like the risk to get stale data on getState if there is an update "in flight", but by using asny getState variant or with callback you can always requerst current value from remote