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

Feature/format chars #1

Merged
merged 17 commits into from
Jul 7, 2020
Merged

Feature/format chars #1

merged 17 commits into from
Jul 7, 2020

Conversation

Carglglz
Copy link
Owner

@Carglglz Carglglz commented Jul 6, 2020

Preview of hbldh#237

@Carglglz Carglglz added the enhancement New feature or request label Jul 6, 2020
@Carglglz
Copy link
Owner Author

Carglglz commented Jul 7, 2020

This adds the feature discussed in hbldh#237:
It is included in bleak.utils:

  • get_char_xml (function): to parse characteristic metadata
  • get_char_value (function): to unpack the raw bytes of read_gatt_char into the proper values
  • (optional) pformat_char_value, map_char_value, dict_char_value, pformat_char_flags: post-processing of the characteristic value

Example: service_explorer.py:

$ python3.8 service_explorer.py
Connected: True
[Service] 180A: Device Information
        [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb: (Handle: 12) (read) | Name: , Value: b'\x00\x03'
        [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb: (Handle: 14) (read) | Name: , Value: b'Espressif Incorporated'
        [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb: (Handle: 16) (read) | Name: , Value: b'esp32'
        [Characteristic] 00002a26-0000-1000-8000-00805f9b34fb: (Handle: 18) (read) | Name: , Value: b'micropython-1.12.0'
[Service] 180F: Battery Service
        [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb: (Handle: 21) (read) | Name: , Value: b'^'
        [Characteristic] 00002a1a-0000-1000-8000-00805f9b34fb: (Handle: 23) (read,notify) | Name: , Value: b'\xbf'
                [Descriptor] 2902: (Handle: 25) | Value: b'\x00\x00'
[Service] 181A: Environmental Sensing
        [Characteristic] 00002a6e-0000-1000-8000-00805f9b34fb: (Handle: 27) (read,notify) | Name: , Value: b'Z\x16'
                [Descriptor] 2902: (Handle: 29) | Value: b'\x00\x00'"""

Example: service_explorer.py with get_char_value and pformat_char_value

    for char in service.characteristics:
                if "read" in char.properties:
                    try:
                        value = bytes(await client.read_gatt_char(char.uuid))
                        value = get_char_value(value, char)
                        pretty_format = pformat_char_value(value,
                                                           one_line=True,
                                                           prnt=False, rtn=True)
                    except Exception as e:
                        print(e)
                        value = str(e).encode()
                        pretty_format = value
                else:
                    value = None
                    pretty_format = value
                log.info(
                    "\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} ".format(
                        char.uuid,
                        char.handle,
                        ",".join(char.properties),
                        uuidstr_to_str(char.uuid),
                        pretty_format,
                    )
                )
$ python3.8 service_explorer.py
Connected: True
[Service] 180A: Device Information

        [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb: (Handle: 12) (read) | Name: Appearance, Value: Category: Generic Thermometer
        [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb: (Handle: 14) (read) | Name: Manufacturer Name String, Value: Manufacturer Name: Espressif Incorporated
        [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb: (Handle: 16) (read) | Name: Model Number String, Value: Model Number: esp32
        [Characteristic] 00002a26-0000-1000-8000-00805f9b34fb: (Handle: 18) (read) | Name: Firmware Revision String, Value: Firmware Revision: micropython-1.12.0
[Service] 180F: Battery Service
        [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb: (Handle: 21) (read) | Name: Battery Level, Value: Level: 94 %
        [Characteristic] 00002a1a-0000-1000-8000-00805f9b34fb: (Handle: 23) (read,notify) | Name: Battery Power State, Value: State: {'BitGroup 0': 'Present', 'BitGroup 2': 'Discharging', 'BitGroup 4':
'Charging (Chargeable)', 'BitGroup 6': 'Good Level'}
                [Descriptor] 2902: (Handle: 25) | Value: b'\x00\x00'
[Service] 181A: Environmental Sensing
        [Characteristic] 00002a6e-0000-1000-8000-00805f9b34fb: (Handle: 27) (read,notify) | Name: Temperature, Value: Temperature: 57.22 °C
                [Descriptor] 2902: (Handle: 29) | Value: b'\x00\x00'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants