giving loop to other classes may keep the other loops from crashing/becoming un-useable.

This commit is contained in:
Lucas Oskorep
2020-03-06 10:32:43 -06:00
parent d58229f0a1
commit f9c9b9fc8c
3 changed files with 25 additions and 26 deletions
+3 -9
View File
@@ -4,13 +4,14 @@ from wand import Wand
class Shop(object):
"""A scanner class to connect to wands
"""
def __init__(self, wand_class=Wand, debug=False):
def __init__(self, shop_loop, wand_class=Wand, debug=False):
"""Create a new scanner
Keyword Arguments:
wand_class {class} -- Class to use when connecting to wand (default: {Wand})
debug {bool} -- Print debug messages (default: {False})
"""
self.shop_loop = shop_loop
self.wand_class = wand_class
self.debug = debug
self._name = None
@@ -39,27 +40,20 @@ class Shop(object):
except AssertionError as e:
print("Either a name, prefix, or mac address must be provided to find a wand")
raise e
if prefix is not None:
self._prefix = prefix
elif mac is not None:
self._mac = mac
self.wands = []
devices = await discover(timeout= timeout)
print(devices)
if self._prefix:
devices = list(filter(lambda x : x.name.startswith(self._prefix), devices))
if self._mac:
devices = list(filter(lambda x : x.address == self.mac, devices))
print(devices)
self.wands = [Wand(d.address, d.name) for d in devices]
self.wands = [Wand(d.address, d.name, self.shop_loop) for d in devices]
print(self.wands)
if connect:
for wand in self.wands:
+16 -8
View File
@@ -10,7 +10,7 @@ class Wand(object):
"""A wand class to interact with the Kano wand
"""
def __init__(self, device_addr, name, debug=True):
def __init__(self, device_addr, name, bot_loop, debug=True):
"""Create a new wand
Arguments:
@@ -23,6 +23,7 @@ class Wand(object):
self.debug = debug
self._dev = BleakClient(device_addr)
self.name = name
self.bot_loop = bot_loop
if debug:
print("Wand: {}\n\rWand Mac: {}".format(self.name, self._dev.address))
@@ -532,7 +533,7 @@ class Wand(object):
callback(val)
def on_button(self, value):
async def on_button(self, value):
"""Function called on button notification
Arguments:
@@ -566,7 +567,7 @@ class Wand(object):
pass
def _on_battery(self, data):
async def _on_battery(self, data):
"""Private function for battery notification
Arguments:
@@ -577,12 +578,12 @@ class Wand(object):
if self.debug:
print("Battery: {}".format(val))
self.on_battery(val)
await self.on_battery(val)
for callback in self._battery_callbacks.values():
callback(val)
await callback(val)
def on_battery(self, value):
async def on_battery(self, value):
"""Function called on battery notification
Arguments:
@@ -590,7 +591,6 @@ class Wand(object):
"""
pass
def handle_notification(self, sender, data):
"""Handle notifications subscribed to
@@ -598,11 +598,19 @@ class Wand(object):
cHandle {int} -- Handle of notification
data {bytes} -- Data from device
"""
import asyncio
future = None
print("Handle notification")
if sender == SENSOR.QUATERNIONS_CHAR.value:
self._on_position(data)
elif sender == IO.USER_BUTTON_CHAR.value:
self._on_button(data)
# self._on_button(data)
future = asyncio.run_coroutine_threadsafe(self._on_button(data), self.bot_loop)
elif sender == SENSOR.TEMP_CHAR.value:
self._on_temperature(data)
elif sender == IO.BATTERY_CHAR.value:
self._on_battery(data)
if future != None:
print("future result is - ", future.result())