-
Notifications
You must be signed in to change notification settings - Fork 2
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
How to implement callbacks? #5
Comments
Hey! I have not yet thought this through in depth. But I think a good approach could be to implements a new component which is responsible for scanning in the background and filter nuki devices (as like your prototype code above). But the only thing this component should do is to inform about state-changes and not reading the state after detecting a change! Why? Possibly a user of this library is only interested in the "it changed something" notification. And if someone is interested in "what changed", they could do an ReadState own his own after informing about the change. What you would have to check is if you can scan and send/receive commands at the same time. If not you have to save the ble-actions - for example via sync.mutex - (so only one go-routine can use ble-device at the same time). Here a fix coding "prototype":
For better usage you can think about to "register" Clients:
In that case the observer can check which type the device is and which address the device has. PS: Yeah we know that we assume that you know the MAC because in our project at this point we know these MAC and therefore a feature for discovering was not necessary. But for the lib it would be a great idea! :) |
Thanks for your suggestions! :-)
Yes, that was my thought as well. Maybe at a later point there could be a helper for the standard case where a script reacts on the "state changed" event by reading the latest state and doing something with it.
Just did a test. Empirical observation, which might just be true for my device (Intel NUC 11 Pro Kit with integrated AX201): So your example wouldn't work exactly like that, but it's anyway not desired to keep the connection established all the time or it would drain the battery. Instead the connection should be established in the callback and it should be closed once the state has been read. It would be a bit easier if the device address was passed to |
Still WIP, but this is simple to use and working fine: In my test callback, I just establish a connection (authentication details saved before starting to monitor), read the states and close. I could also read the latest log entry to get more information, but that won't reset the flag in the advertisement. |
I'm trying to implement a MQTT service based on this library so I can monitor and control my smart lock via ioBroker.
One key requirement for this is to get informed by the lock that the state has changed, so it would be worth to call
ReadStates()
. This can be checked via the advertisements (explanation). Based on this example, I was able to implement the followingNow the big question is: What could be a good way to add a new library function to subscribe to "state of device x has changed", i,e.
updated
has changed fromfalse
totrue
? It would involve setting up the scan and running it in the background, providing a callback function, the ability to cancel the scan (which might be necessary to connect, not sure?!?), and probably other things.Any ideas where to start, which patterns or libraries to use? Keep in mind that waiting for an update shouldn't block the whole process, because at any time there could be an MQTT command to unlock the door.
By the way, this library currently assumes that you already know the device's MAC address. Maybe some functions to discover available devices (using code similar to the above) would be a nice addition.
The text was updated successfully, but these errors were encountered: