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

Narrowing down errors with nested try/exceptions #9

Open
pippim opened this issue Jan 8, 2025 · 0 comments
Open

Narrowing down errors with nested try/exceptions #9

pippim opened this issue Jan 8, 2025 · 0 comments

Comments

@pippim
Copy link

pippim commented Jan 8, 2025

Firstly, great application. I was able to get it up and running on Ubuntu 16.04 LTS Plus in just a few hours. In the connect() function, I had to make these changes to narrow down my problems:

def connect(MAC, reset_on_start=True):
    """
    Create and start a new backend adapter and connect it to a device.

    When connecting to multiple devices at the same time make sure to set reset_on_start
    to False after the first connection is made, otherwise all connections made before are
    invalidated.

    :param string MAC: MAC address of the device to connect to.
    :param bool reset_on_start: Perhaps due to a bug in gatttool or pygatt,
        but if the bluez backend isn't restarted, it can sometimes lock up
        the computer when trying to make a connection to HCI device.
    """
    try:
        adapter = pygatt.GATTToolBackend()
        try:
            adapter.start(reset_on_start=reset_on_start)
            try:
                device = adapter.connect(MAC)
            except pygatt.exceptions.NotConnectedError:
                raise pygatt.exceptions.NotConnectedError("Device not connected!")
        except pygatt.exceptions.NotConnectedError:
            raise pygatt.exceptions.NotConnectedError("Adapter cannot start!")
    except pygatt.exceptions.NotConnectedError:
        raise pygatt.exceptions.NotConnectedError("No Backend Tool for GATT!")
        # This is the true error on 2025-01-07 but only happens intermediately
        #   File "./homa.py", line 2214, in __init__
        #     device = tc.connect("36:46:3E:F3:09:5E")
        #   File "/home/rick/HomA/trionesControl/trionesControl.py", line 35, in connect
        #     raise pygatt.exceptions.NotConnectedError("No Backend Tool for GATT!")
        # pygatt.exceptions.NotConnectedError: No Backend Tool for GATT!

    ''' ORIGINAL CODE: 
    try:
        adapter = pygatt.GATTToolBackend()
        adapter.start(reset_on_start=reset_on_start)
        device = adapter.connect(MAC)
    except pygatt.exceptions.NotConnectedError:
        raise pygatt.exceptions.NotConnectedError("Device nor connected!")
    '''

    log.info("Device connected")
    return device

Before narrowing down the exact error I was only seeing "Device nor connected".

Digging into gatttool.py I had to make these changes to fix the REAL error:

    @at_most_one_device
    def char_write_handle(self, handle, value, wait_for_response=True,
                          timeout=30):
        """
        Writes a value to a given characteristic handle.

        :param handle:
        :param string value:
        :param wait_for_response: If true, performs an attribute write. If
            false, sends a command and expects no acknowledgement from the
            device.
        :param timeout:
        """

        # 2025-01-07 debug error:
        #     ''.join("{0:02x}".format(byte) for byte in value),
        # ValueError: Unknown format code 'x' for object of type 'str'

        cmd = 'char-write-{0} 0x{1:02x} {2}'.format(
            'req' if wait_for_response else 'cmd',
            handle,
            #''.join("{0:02x}".format(byte) for byte in value),  # 2025-01-07
            ''.join("{0:02x}".format(ord(byte)) for byte in value),
        )

(... snip ...)

I was pleasantly surprised how easy it was to turn on the "Happy Lighting" LED light strip automatically from Linux. Note it's Python 2.7.12.

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

No branches or pull requests

1 participant