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

Example for using this as library #46

Open
hujiko opened this issue Mar 4, 2024 · 3 comments
Open

Example for using this as library #46

hujiko opened this issue Mar 4, 2024 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@hujiko
Copy link

hujiko commented Mar 4, 2024

Hey,

I tried using this library through the CLI and it worked fine.

Now as I would like to further process the information from my device, I would like to write my own python script and use your library as a library.

But sadly I don't really understand how to, based on the example given in the readme.
The readme says:

from victron_ble.devices import detect_device_type

data = <ble advertisement data>
parser = detect_device_type(data)
parsed_data = parser(<key>).parse(<ble advertisement data>)

But what's not clear to me, is: Where to get the BLE Advertisement Data from?
I tried using ID / MAC-Adress ("AA:BB:CC:DD:EE:FF") as ble advertisement data and the encryption key as "", but apparently that did not work.

In the end, I would like to end up with the same JSON / Object, that I get from the CLI, so that I can persist that information in a database.

@hujiko hujiko added enhancement New feature or request question Further information is requested labels Mar 4, 2024
@DannySkjodt1
Copy link

You can just mod the current binary for that, if you add quit() after the print line in line 114 it will quit after printing the json, you also have to comment out logger on line 73. Then you can call it from other stuff to grab your info.

@TemperedEnterprises
Copy link

You can just mod the current binary for that, if you add quit() after the print line in line 114 it will quit after printing the json, you also have to comment out logger on line 73. Then you can call it from other stuff to grab your info.

I did that and it still keeps running.. i put those changes in scanner.py

@mystery-moss
Copy link

I wanted to do the same thing. I believe the readme assumes that you have acquired the Bluetooth advertisement data separately (e.g. with the Bleak library), and now want to parse it.

But if, like me, you don't care to learn how Bleak works, here's a hacky script to grab the data:

import asyncio
import json
from victron_ble.exceptions import AdvertisementKeyMissingError
from victron_ble.scanner import Scanner, DeviceDataEncoder

class ScannerWithRead(Scanner):
    """Scanner with the ability to read the latest received
    advertisement data to a variable.
    """
    def __init__(self, device_keys):
        super().__init__(device_keys)
        self.latest_data = None

    async def read(self):
        """Wait indefinitely until `callback` has populated
        `self.latest_data`
        """
        while self.latest_data is None:
            await asyncio.sleep(0.1)
        data = self.latest_data
        self.latest_data = None
        return data

    def callback(self, ble_device, raw_data):
        """Override `Scanner` callback to store data rather
        than printing it.
        """
        try:
            device = self.get_device(ble_device, raw_data)
        except AdvertisementKeyMissingError:
            return
        self.latest_data = device.parse(raw_data)


loop = asyncio.get_event_loop()

# Using MAC address and encryption key extracted as described in readme
scanner = ScannerWithRead({"AA:BB:CC:DD:EE:FF": "1234567891234567891234567891234"})
asyncio.ensure_future(scanner.start())

data = loop.run_until_complete(scanner.read())
data_dict = json.dumps(data, cls=DeviceDataEncoder)
print(data_dict)

loop.run_until_complete(scanner.stop())

Note that while this should work with v0.9.0, it is grubbing into the guts of victron_ble, so future updates may break it. I'm also not familiar with asyncio; this is likely not the ideal way to go about this, but it does work.

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

No branches or pull requests

4 participants