mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
A bit of refactoring
This commit is contained in:
parent
8b970c0792
commit
c11e8fbd18
@ -46,6 +46,14 @@ class Connection(object):
|
|||||||
def enable_notifications(self):
|
def enable_notifications(self):
|
||||||
self.write(ENABLE_NOTIFICATIONS_HANDLE, ENABLE_NOTIFICATIONS_VALUE)
|
self.write(ENABLE_NOTIFICATIONS_HANDLE, ENABLE_NOTIFICATIONS_VALUE)
|
||||||
|
|
||||||
|
def _is_device_matched(self, address, name, hub_mac):
|
||||||
|
log.debug("Checking device name: %s, MAC: %s", name, address)
|
||||||
|
if address != "00:00:00:00:00:00":
|
||||||
|
if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac.lower() == address.lower():
|
||||||
|
log.info("Found %s at %s", name, address)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class DebugServer(object):
|
class DebugServer(object):
|
||||||
"""
|
"""
|
||||||
|
@ -4,7 +4,7 @@ from threading import Thread, Event
|
|||||||
|
|
||||||
from bluepy import btle
|
from bluepy import btle
|
||||||
|
|
||||||
from pylgbst.comms import Connection, LEGO_MOVE_HUB
|
from pylgbst.comms import Connection
|
||||||
from pylgbst.utilities import str2hex, queue
|
from pylgbst.utilities import str2hex, queue
|
||||||
|
|
||||||
log = logging.getLogger('comms-bluepy')
|
log = logging.getLogger('comms-bluepy')
|
||||||
@ -100,13 +100,10 @@ class BluepyConnection(Connection):
|
|||||||
address = dev.addr
|
address = dev.addr
|
||||||
addressType = dev.addrType
|
addressType = dev.addrType
|
||||||
name = dev.getValueText(COMPLETE_LOCAL_NAME_ADTYPE)
|
name = dev.getValueText(COMPLETE_LOCAL_NAME_ADTYPE)
|
||||||
log.debug("Found dev, name: {}, address: {}".format(name, address))
|
|
||||||
|
|
||||||
if address != "00:00:00:00:00:00":
|
if self._is_device_matched(address, name, hub_mac):
|
||||||
if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address:
|
self._peripheral = BluepyThreadedPeripheral(address, addressType, self._controller)
|
||||||
logging.info("Found %s at %s", name, address)
|
break
|
||||||
self._peripheral = BluepyThreadedPeripheral(address, addressType, self._controller)
|
|
||||||
break
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ from time import sleep
|
|||||||
|
|
||||||
import gatt
|
import gatt
|
||||||
|
|
||||||
from pylgbst.comms import Connection, LEGO_MOVE_HUB, MOVE_HUB_HW_UUID_SERV, MOVE_HUB_HW_UUID_CHAR, \
|
from pylgbst.comms import Connection, MOVE_HUB_HW_UUID_SERV, MOVE_HUB_HW_UUID_CHAR, \
|
||||||
MOVE_HUB_HARDWARE_HANDLE
|
MOVE_HUB_HARDWARE_HANDLE
|
||||||
from pylgbst.utilities import str2hex
|
from pylgbst.utilities import str2hex
|
||||||
|
|
||||||
@ -100,12 +100,9 @@ class GattConnection(Connection):
|
|||||||
for dev in devices:
|
for dev in devices:
|
||||||
address = dev.mac_address
|
address = dev.mac_address
|
||||||
name = dev.alias()
|
name = dev.alias()
|
||||||
logging.debug("Device %s at %s", name, address)
|
if self._is_device_matched(address, name, hub_mac):
|
||||||
if address != "00:00:00:00:00:00":
|
self._device = CustomDevice(address, self._manager)
|
||||||
if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address:
|
break
|
||||||
logging.info("Found %s at %s", name, address)
|
|
||||||
self._device = CustomDevice(address, self._manager)
|
|
||||||
break
|
|
||||||
|
|
||||||
if not self._device:
|
if not self._device:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
@ -5,7 +5,7 @@ from threading import Thread
|
|||||||
|
|
||||||
from gattlib import DiscoveryService, GATTRequester
|
from gattlib import DiscoveryService, GATTRequester
|
||||||
|
|
||||||
from pylgbst.comms import Connection, LEGO_MOVE_HUB
|
from pylgbst.comms import Connection
|
||||||
from pylgbst.utilities import queue, str2hex
|
from pylgbst.utilities import queue, str2hex
|
||||||
|
|
||||||
log = logging.getLogger('comms-gattlib')
|
log = logging.getLogger('comms-gattlib')
|
||||||
@ -70,11 +70,9 @@ class GattLibConnection(Connection):
|
|||||||
log.debug("Devices: %s", devices)
|
log.debug("Devices: %s", devices)
|
||||||
|
|
||||||
for address, name in devices.items():
|
for address, name in devices.items():
|
||||||
if address != "00:00:00:00:00:00":
|
if self._is_device_matched(address, name, hub_mac):
|
||||||
if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address:
|
self.requester = Requester(address, True, self._iface)
|
||||||
logging.info("Found %s at %s", name, address)
|
break
|
||||||
self.requester = Requester(address, True, self._iface)
|
|
||||||
break
|
|
||||||
|
|
||||||
if self.requester:
|
if self.requester:
|
||||||
break
|
break
|
||||||
|
@ -33,11 +33,9 @@ class GattoolConnection(Connection):
|
|||||||
for dev in devices:
|
for dev in devices:
|
||||||
address = dev['address']
|
address = dev['address']
|
||||||
name = dev['name']
|
name = dev['name']
|
||||||
if address != "00:00:00:00:00:00":
|
if self._is_device_matched(address, name, hub_mac):
|
||||||
if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address:
|
self._conn_hnd = adapter.connect(address)
|
||||||
logging.info("Found %s at %s", name, address)
|
break
|
||||||
self._conn_hnd = adapter.connect(address)
|
|
||||||
break
|
|
||||||
|
|
||||||
if self._conn_hnd:
|
if self._conn_hnd:
|
||||||
break
|
break
|
||||||
@ -58,6 +56,7 @@ class GattoolConnection(Connection):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BlueGigaConnection(GattoolConnection):
|
class BlueGigaConnection(GattoolConnection):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(BlueGigaConnection, self).__init__()
|
super(BlueGigaConnection, self).__init__()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user