コード例 #1
0
    def run(self):
        bmac = self.args["bmac"]
        try:
            iface = int(self.args["iface"])
        except:
            iface = 0
        data = self._transform_data(self.args["encode"], self.args["data"])
        if not data:
            return

        attempt = 1
        success = False
        ble_device = BLE(bmac, self.args["type"], iface)
        while attempt <= 5 and not success:
            print_info(f"Trying to connect {bmac}. (Attempt: {attempt})")
            try:
                ble_device.connect()
                success = True
            except KeyboardInterrupt:
                print_info("Interrupted... exit run")
                return 
            except:
                attempt += 1
        if not success:
            print_error("Failed to connect")
            return
        ble_device.write_data(data, self.args["uuid"])
        try:
            sleep(int(self.args["wait"]))
        except:
            sleep(2)
            
        ble_device.disconnect()
コード例 #2
0
    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}")
コード例 #3
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.write_data(data, 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']}")