Ejemplo n.º 1
0
    def _connect_inner(self, link):
        try:
            if self.address is '':
                ports = link.scan()
                if not link.scanned.is_set():
                    link.scanned.wait(link.timeout)
                if not ports:
                    raise Exception('No devices found')
                if isinstance(ports[0], tuple):
                    self.address = ports[0][1]
                else:
                    self.address = ports[0]
                link.open(self.address)
            elif self.address is not '':
                link.open(self.address)

            transport = None
            if self.transport == 'ninebot':
                from py9b.transport.ninebot import NinebotTransport
                transport = NinebotTransport(link)
            elif self.transport == 'xiaomi':
                from py9b.transport.xiaomi import XiaomiTransport
                transport = XiaomiTransport(link)

                if transport.execute(
                        ReadRegs(BT.ESC, 0x68,
                                 "<H"))[0] > 0x081 and self.link is ('ble'):
                    transport.keys = link.fetch_keys()
                    transport.recover_keys()
                    tprint('Keys recovered')
                    tprint('functionality may be limited for now')

            self._tran = transport
            self._link = link

            if not self._link.connected.is_set():
                self._link.connected.wait(self._link.timeout * 1.5)

            if self._link.connected.is_set():
                self.update_state('connected')
            else:
                self.disconnect()

            return transport

        except Exception as exc:
            self.dispatch('on_error', repr(exc))
            raise exc
Ejemplo n.º 2
0
    def __enter__(self):
        link = None
        if self.link == 'bleak':
            from py9b.link.bleak import BleakLink
            link = BleakLink()
        elif self.link == 'tcp':
            from py9b.link.tcp import TCPLink
            link = TCPLink()
        elif self.link == 'serial':
            from py9b.link.serial import SerialLink
            link = SerialLink(timeout=1.0)

        link.__enter__()

        if not self.address:
            ports = link.scan()
            if not ports:
                raise Exception('No devices found')
            self.address = ports[0]

        link.open(self.address)

        transport = None
        if self.transport == 'ninebot':
            from py9b.transport.ninebot import NinebotTransport
            transport = NinebotTransport(link)

        elif self.transport == 'xiaomi':
            from py9b.transport.xiaomi import XiaomiTransport
            transport = XiaomiTransport(link)

            if transport.execute(
                    ReadRegs(BT.ESC, 0x68,
                             "<H"))[0] > 0x081 and self.link.startswith('ble'):
                transport.keys = link.fetch_keys_pro()
                transport.recover_keys()
                print('Keys recovered')

        self._transport = transport
        self._link = link

        return transport
Ejemplo n.º 3
0
    def _connect_inner(self, link):
        try:
            if not self.address:
                if platform != 'android':
                    ports = link.scan()
                    if not ports:
                        raise Exception('No devices found')
                    if isinstance(ports[0], tuple):
                        self.address = ports[0][1]
                    else:
                        self.address = ports[0]
                link.open(self.address)
            elif self.address:
                link.open(self.address)

            transport = None
            if self.transport == 'ninebot':
                from py9b.transport.ninebot import NinebotTransport
                transport = NinebotTransport(link)
            elif self.transport == 'xiaomi':
                from py9b.transport.xiaomi import XiaomiTransport
                transport = XiaomiTransport(link)

                if transport.execute(
                        ReadRegs(BT.ESC, 0x68,
                                 "<H"))[0] > 0x081 and self.link is ('ble'):
                    transport.keys = link.fetch_keys_pro()
                    transport.recover_keys()
                    tprint('Keys recovered')

            self._tran = transport
            self._link = link

            return transport

        except Exception as exc:
            self.update_state('disconnected')
            self.dispatch('on_error', repr(exc))
            raise exc