Skip to content
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

support connect/disconnect, support multiple callbacks #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fhempy
Copy link

@fhempy fhempy commented Oct 6, 2020

support multiple hci interfaces:
Tries to connect via all available interfaces and sticks to one if the connection works.

support keep connection alive:
Allows the user to keep the connection alive and re-establishes the connection if it dropped inbetween.

support multiple callbacks:
Allow to register multiple callbacks.

support keep connection alive
support multiple callbacks
Copy link
Owner

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I'd really prefer to not add any new mandatory dependencies, so this is going to need some refactoring.

if adapter is None:
continue
iface_list.append(re.search(r'\d+$', path)[0])
return iface_list
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would introduce hard dependency for dbus (which may or may not be available on all environments), so it's a no go. Could you please create a separate PR for adding optional support for multiple interfaces?

Also, are you sure bluepy does not provide an interface for enumerating over existing adapters? That would be the preferred way over manually scraping bluez responses.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately bluepy doesn't provide such a functionality, that was the first place I searched for. I could find this one from bluez which is the functionality I implemented:
https://github.com/pauloborges/bluez/blob/master/test/test-adapter

Another possible solution would be to remove this one from eq3bt and add an additional parameter "iface".

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the best solution is to refactor the connection handling (so that connect can be done separately outside the __enter__) and expose iface parameter for it as you propose. The eq3bt cli tool could also optionally expose --interface option, and the downstream users can use whatever they want (like dbus) to locate available interfaces.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure that we are on the same page:

  • add connect(iface) method
  • add iface parameter to Thermostat
  • remove dbus and get adapters completely

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

  • connect(iface) will be useful for your use case, where you want to fall back to other interfaces if the connection fails
  • Adding optional iface to Thermostat allows forcing to use specific controller (e.g., via cli) during the initialization phase, in case someone wants to do that.

The real fix for obtaining interfaces should be done inside bluepy, but I think the library is not very actively maintained, notwithstanding the problem with dbus availability (iirc there was also some problems getting that library easily pip-installable due to some compiling needed during the setup process). Last time I did some dbus interfacing, I used python-dbus-next which is pure python only library.

raise
conn_state = self._conn.getState()
except:
self._conn = None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will lose the reference to the potentially already existing _conn object, is there something else that should be done in cases where getState errors on already existing connection?

Anycase, please don't use bare excepts but catch only exceptions you are expecting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can change that to catch only the errors which indicate that the connection was already lost and therefore a disconnect() call isn't needed.

_LOGGER.debug("Second connection try to %s using ifaces %s failed: %s", self._mac, self._ifaces[self._iface_idx], ex2)
if self.next_iface() is False:
# tried all ifaces, raise exception
raise
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure feels very error-prone, I think the correct way would be refactoring the connection code away from __enter__ (it's not really exposed anywhere), and create separate connect() method that does the connection using the given iface.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'll refactor and add a connect() method with iface as parameter.

add connect(iface) for keep_connection usage
@fhempy
Copy link
Author

fhempy commented Oct 8, 2020

Hi,
I updated the PR. Please keep in mind that I haven't tested it yet, as I can't do that right now. Nevertheless, it would be great if you could let me know your opinion about the changes.
I'll let you know as soon as I have tested it.
Thx!

fix keep_connected
@fhempy fhempy changed the title use all hci ifaces, support keep_connection, support multiple callbacks support connect/disconnect, support multiple callbacks Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants