From 07cb03b74980f4c50c6d61654b6946716997e153 Mon Sep 17 00:00:00 2001 From: Lucas Oskorep Date: Tue, 11 Feb 2020 16:34:41 -0600 Subject: [PATCH] Adding filtering to the shop. --- kano-wand-2/shop.py | 54 ++++++++++----------------------------------- 1 file changed, 12 insertions(+), 42 deletions(-) diff --git a/kano-wand-2/shop.py b/kano-wand-2/shop.py index 2e17fa8..2e3be51 100644 --- a/kano-wand-2/shop.py +++ b/kano-wand-2/shop.py @@ -17,7 +17,7 @@ class Shop(object): self._prefix = None self._mac = None - async def scan(self, name=None, prefix="Kano-Wand", mac=None, timeout=2.0, connect=False): + async def scan(self, prefix="Kano-Wand", mac=None, timeout=2.0, connect=False): """Scan for devices Keyword Arguments: @@ -33,17 +33,15 @@ class Shop(object): if self.debug: print("Scanning for {} seconds...".format(timeout)) try: - name_check = not (name is None) prefix_check = not (prefix is None) mac_check = not (mac is None) - assert name_check or prefix_check or mac_check + assert prefix_check or mac_check except AssertionError as e: print("Either a name, prefix, or mac address must be provided to find a wand") raise e - if name is not None: - self._name = name - elif prefix is not None: + + if prefix is not None: self._prefix = prefix elif mac is not None: self._mac = mac @@ -52,46 +50,18 @@ class Shop(object): devices = await discover(timeout= timeout) - #TODO: Add in wand filtering. + 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)) - # if isNewDev: - # # Perform initial detection attempt - # mode = 0 - # found = 0 - # name = device.getValueText(9) - # if self._name is not None: - # mode += 1 - # if name == self._name: - # found += 1 - # if self._prefix is not None: - # mode += 1 - # if name is not None and name.startswith(self._prefix): - # found += 1 - # if self._mac is not None: - # mode += 1 - # if device.addr == self._mac: - # found += 1 - # - # if found >= mode: - # self.wands.append(self.wand_class(device, debug=self.debug)) - # elif self.debug: - # if name != "None": - # print("Mac: {}\tCommon Name: {}".format(device.addr, name)) - # else: - # print("Mac: {}".format(device.addr)) - print(devices) - devices = list(filter(lambda x : x.name.startswith("kano"), devices)) print(devices) + self.wands = [Wand(d.address, d.name) for d in devices] - # for d in devices: - # #Convert to device making here. - # print(d) - # print(d.name, d.address) print(self.wands) - - #TODO: Process device list here for the shop - > not all BLE devices are necessary - # client = BleakClient(self.wand_address, loop=self.loop) if connect: for wand in self.wands: - wand.connect() + await wand.connect() return self.wands