-
Notifications
You must be signed in to change notification settings - Fork 81
Bluegiga adapter support #126
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
Open
paulianttila
wants to merge
16
commits into
ttu:master
Choose a base branch
from
paulianttila:bluegiga
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0714ec3
Bluegiga support
paulianttila d10b04d
Readme update
paulianttila 259d9f4
Disable active scan and make reset optional with env variable
paulianttila 2238d60
Fixed scan result handling
paulianttila ca4470f
Readme update
paulianttila 7c7f19e
Use default scan window and interval
paulianttila f1607a5
Prevent endless loops
paulianttila 1127a2c
Improved error handling
paulianttila ed03ecd
Merge branch 'master' into bluegiga
ttu d3bb8c0
Merge branch 'master' into bluegiga
ttu 8f3fc5c
Rename get_data methods to match current implementation
ttu ebd061c
Format raw data in adapter
ttu ce79c8e
Add missing types
ttu 12b5012
Merge remote-tracking branch 'origin/master' into pr/126
ttu 580ad8d
Linter fixes
ttu 2936e93
Use ruuvi_types in adapter
ttu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import logging | ||
| import os | ||
| import sys | ||
| import time | ||
| import pygatt | ||
| import binascii | ||
| import threading | ||
| import os | ||
|
|
||
| from multiprocessing import Manager | ||
| from queue import Queue | ||
|
|
||
| from ruuvitag_sensor.adapters import BleCommunication | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| class BleCommunicationBluegiga(BleCommunication): | ||
| """Bluetooth LE communication for Bluegiga""" | ||
|
|
||
| @staticmethod | ||
| def get_data(mac, bt_device=''): | ||
| if not bt_device: | ||
| adapter = pygatt.BGAPIBackend() | ||
| else: | ||
| adapter = pygatt.BGAPIBackend(bt_device) | ||
|
|
||
| def scan_received(devices, addr, packet_type): | ||
| if mac and mac == addr: | ||
| log.debug('Received data from device: %s %s', addr, packet_type) | ||
| return True # stop scan | ||
|
|
||
| reset = False if os.environ.get('BLUEGIGA_RESET', '').upper() == 'FALSE' else True | ||
| adapter.start(reset=reset) | ||
| log.debug('Start receiving broadcasts (device %s)', bt_device) | ||
| try: | ||
| devices = adapter.scan(timeout=60, scan_interval=1, scan_window=100, active=False, scan_cb=scan_received) | ||
| for dev in devices: | ||
| if mac and mac == dev['address']: | ||
| log.debug('Result found for device %s', mac ) | ||
| rawdata = dev['packet_data']['non-connectable_advertisement_packet']['manufacturer_specific_data'] | ||
| hexa = binascii.hexlify(rawdata).decode("ascii").upper() | ||
| log.debug('Data found: %s', hexa) | ||
| return hexa | ||
| finally: | ||
| adapter.stop() | ||
|
|
||
| @staticmethod | ||
| def get_datas(blacklist=[], bt_device=''): | ||
| m = Manager() | ||
| q = m.Queue() | ||
|
|
||
| # Use Manager dict to share data between processes | ||
| shared_data = m.dict() | ||
| shared_data['blacklist'] = blacklist | ||
| shared_data['stop'] = False | ||
|
|
||
| # Start background process | ||
| scanner = threading.Thread( | ||
| target=BleCommunicationBluegiga._run_get_data_background, | ||
| args=[q, shared_data, bt_device]) | ||
| scanner.start() | ||
|
|
||
| try: | ||
| while True: | ||
| while not q.empty(): | ||
| data = q.get() | ||
| log.debug('Found data: %s', data) | ||
| yield data | ||
| time.sleep(0.1) | ||
| except GeneratorExit: | ||
| pass | ||
| except KeyboardInterrupt as ex: | ||
| pass | ||
| except Exception as ex: | ||
| log.info(ex) | ||
|
|
||
| log.debug('Stop') | ||
| shared_data['stop'] = True | ||
| scanner.join() | ||
| log.debug('Exit') | ||
| return | ||
|
|
||
| @staticmethod | ||
| def _run_get_data_background(queue, shared_data, bt_device): | ||
| """ | ||
| Attributes: | ||
| device (string): BLE device (default auto) | ||
| """ | ||
|
|
||
| if bt_device: | ||
| adapter = pygatt.BGAPIBackend(bt_device) | ||
| else: | ||
| adapter = pygatt.BGAPIBackend() | ||
|
|
||
| reset = False if os.environ.get('BLUEGIGA_RESET', '').upper() == 'FALSE' else True | ||
| adapter.start(reset=reset) | ||
| try: | ||
| while True: | ||
| try: | ||
| if shared_data['stop']: | ||
| break | ||
| devices = adapter.scan(timeout=0.5, scan_interval=1, scan_window=100, active=False, ) | ||
| for dev in devices: | ||
| log.debug('received: %s', dev) | ||
| mac = str(dev['address']) | ||
| if mac and mac in shared_data['blacklist']: | ||
| log.debug('MAC blacklisted: %s', mac) | ||
| continue | ||
| try: | ||
| rawdata = dev['packet_data']['non-connectable_advertisement_packet']['manufacturer_specific_data'] | ||
| log.debug('Received manufacturer data from %s: %s', mac, rawdata) | ||
| hexa = binascii.hexlify(rawdata).decode("ascii").upper() | ||
| queue.put((mac, hexa)) | ||
| except KeyError: | ||
| pass | ||
| except GeneratorExit: | ||
| return | ||
| except KeyboardInterrupt as ex: | ||
| return | ||
| finally: | ||
| log.debug('Stop scan') | ||
| adapter.stop() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.