def _subscribe(self):
        #print_info(f"Trying to subscribe to {self.args['bmac']}")
        device = self.args["bmac"]
        data = self._transform_data(self.args["encode"], self.args["data"])
        type = self.args["type"]
        uuid_subscribe = self.args["uuid-subscribe"]
        uuid_write = self.args["uuid-write"]
        subs = False
        try:
            iface = int(self.args["iface"])
        except:
            iface = 0
        print_info(f"\nTrying to subscribe to {device}")
        ble_device = BLE(device, type, iface)

        for x in range(0, 6):
            try:
                ble_device.connect()
                print_ok("\nDevice connected...")
                ble_device.set_delegate(HomeSecurityDelegate)
                ble_device.set_subscribe(uuid_subscribe)
                subs = True
                break
            except KeyboardInterrupt:
                print("Module Interrupted")
                break
            except:
                sleep(3)

        ble_device.write_data(data, uuid_write)
        if subs:
            while True:
                try:
                    if (ble_device.device.waitForNotifications(8.0)):
                        break
                    sleep(3)
                except KeyboardInterrupt:
                    print("Module Interrupted")
                    return True
                except:
                    ble_device.disconnect()

        print("")
        if subs:
            print_error(f"Unsubscribed {device}")
        else:
            print_error(f"Unable to subscribe to {device}")
Exemple #2
0
    def _subscribe(self):
        #print_info(f"Trying to subscribe to {self.args['bmac']}")
        bmac = self.args["bmac"]
        data = self._transform_data(self.args["encode"], self.args["data"])
        subs = False
        conn = 0
        try:
            iface = int(self.args["iface"])
        except:
            iface = 0
        print_info(f"\nTrying to subscribe to {bmac}")
        ble_device = BLE(self.args["bmac"], self.args["type"], iface)
        while True:
            wait = False
            try:
                ble_device.connect()
                print_ok("\nDevice connected...")
                ble_device.set_subscribe(self.args["uuid"])
                subs = True
                wait = True
                ble_device.set_delegate(HomeSecurityDelegate)
            except KeyboardInterrupt:
                print("Module Interrupted")
                break
            except:
                sleep(3)
                conn += 1
                if conn == 5:
                    break
                continue

            if wait:
                ble_device.subscribe()

        print("")
        if subs:
            print_error(f"Unsubscribed {self.args['bmac']}")
        else:
            print_error(f"Unable to subscribe to {self.args['bmac']}")