Пример #1
0
    def connect(self):
        self.update_state('connecting')
        try:
            link = None
            if self.link == 'ble':
                if platform == 'android':
                    from py9b.link.droidble import BLELink
                    link = BLELink()
                else:
                    from py9b.link.bleak import BLELink
                    if self.loop is None:
                        self.loop = asyncio.get_event_loop()
                    link = BLELink(loop=self.loop)
            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)
            elif self.link == 'mock':
                from mocklink import MockLink
                link = MockLink()

            link.__enter__()

            # This is split into two parts due to some link implementations
            # (namely droidble) requiring some initalization in main thread...
            self._connect_inner(link)
        except Exception as exc:
            self.update_state('disconnected')
            self.dispatch('on_error', repr(exc))
            raise exc
Пример #2
0
    def Flash(self, fwfilep):
        if self.device == 'extbms' and self.protocol != 'ninebot':
            exit('Only Ninebot supports External BMS !')
        self.setfwfilep(fwfilep)
        file = open(fwfilep, 'rb')
        dev = self.devices.get(self.device)
        if self.interface == 'ble':
            if platform != 'android':
                try:
                    from py9b.link.ble import BLELink
                except:
                    exit('BLE is not yet working with your configuration !')
            elif platform == 'android':
                try:
                    from py9b.link.able import BLELink
                except:
                    exit('BLE is not yet supported on Android !')
            else:
                exit('BLE is not supported on your system !')
            link = BLELink()
        elif self.interface == 'tcp':
            from py9b.link.tcp import TCPLink
            link = TCPLink()
        elif self.interface == 'serial':
            if platform == 'android':
                exit('Serial is not yet supported on Android !')
            from py9b.link.serial import SerialLink
            link = SerialLink()
        else:
            exit('!!! BUG !!! Unknown interface selected: ' + interface)

        with link:
            tran = self.protocols.get(self.protocol)(link)

            if self.address != '':
                addr = self.address
            elif self.interface != 'ble':
                print('Scanning...')
                ports = link.scan()
                if not ports:
                    exit("No interfaces found !")
                print('Connecting to', ports[0][0])
                addr = ports[0][1]
            else:
                raise LinkOpenException

            link.open(addr)
            print('Connected')
            try:
                UpdateFirmware(link, tran, dev, file)
            except Exception as e:
                print('Error:', e)
                raise
Пример #3
0
    def connect(self):
        self.update_state('connecting')
        try:
            link = None
            if self.link == 'ble':
                if platform == 'android':
                    from py9b.link.droidble import BLELink
                    link = BLELink()
                else:
                    from py9b.link.bleak import BLELink
                    if self.loop is None:
                        self.loop = asyncio.get_event_loop()
                    link = BLELink(loop=self.loop)

            elif self.link == 'tcp':
                from py9b.link.tcp import TCPLink
                link = TCPLink()

            elif self.link == 'serial':
                if platform == 'android':
                    tprint('Serial on Android is only available in Pro')
                else:
                    from py9b.link.serial import SerialLink
                    link = SerialLink(timeout=1.0)

            elif self.link == 'mock':
                from mocklink import MockLink
                link = MockLink()

            elif self.link == None:
                tprint('select interface first')
                self.disconnect()

            if self.transport is '':
                self.disconnect()
                tprint('select protocol first')

            if self.transport is not '' and self.link is not None:
                link.__enter__()
                # This is split into two parts due to some link implementations
                # (namely droidble) requiring some initalization in main thread...
                self._connect_inner(link)
            else:
                tprint('One or more parameters are not set')

        except Exception as exc:
            self.dispatch('on_error', repr(exc))
            raise exc
Пример #4
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
Пример #5
0
args = parser.parse_args()

if args.device == 'extbms' and args.protocol != 'ninebot':
    exit('Only Ninebot supports External BMS !')

dev = devices.get(args.device)

if args.interface == 'ble':
    try:
        from py9b.link.ble import BLELink
    except:
        exit('BLE is not supported on your system !')
    link = BLELink()
elif args.interface == 'tcp':
    from py9b.link.tcp import TCPLink
    link = TCPLink()
elif args.interface == 'serial':
    from py9b.link.serial import SerialLink
    link = SerialLink()
else:
    exit('!!! BUG !!! Unknown interface selected: ' + args.interface)

with link:
    tran = protocols.get(args.protocol)(link)

    if args.address:
        addr = args.address
    else:
        print('Scanning...')
        ports = link.scan()
        if not ports:
Пример #6
0
#!python2-32
from py9b.link.base import LinkOpenException, LinkTimeoutException
from py9b.link.tcp import TCPLink

# from py9b.link.ble import BLELink
from py9b.link.serial import SerialLink
from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT

# from py9b.transport.xiaomi import XiaomiTransport
from py9b.transport.ninebot import NinebotTransport
from py9b.command.regio import ReadRegs, WriteRegs

# link = SerialLink(dump=True)
link = TCPLink()
# link = BLELink()

with link:
    print("Scanning...")
    ports = link.scan()
    print(ports)

    # tran = XiaomiTransport(link)
    tran = NinebotTransport(link)

    link.open(("127.0.0.20:6000"))
    # 	link.open(ports[0][1])
    print("Connected")

    print("Locking...")
    tran.execute(WriteRegs(BT.ESC, 0x70, "<H", 0x0001))
Пример #7
0
#!python2-32
from py9b.link.base import LinkOpenException, LinkTimeoutException
from py9b.link.tcp import TCPLink
from py9b.link.ble import BLELink
from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT
from py9b.transport.xiaomi import XiaomiTransport

#link = SerialLink()
link = TCPLink()
#link = BLELink()

with link:
	print "Scanning..."
	ports = link.scan()
	print ports

	tran = XiaomiTransport(link)

	link.open(("192.168.1.45", 6000))
	#link.open(ports[0][1])
	print "Connected"

	req = PKT(src=BT.HOST, dst=BT.ESC, cmd=0x02, arg=0x41, data="\xCE\xAB\x00\x00")

	tran.send(req)
	try:
		rsp = tran.recv()
	finally:
		link.close()
Пример #8
0
#!python2-32
from py9b.link.base import LinkOpenException, LinkTimeoutException
from py9b.link.tcp import TCPLink
from py9b.link.ble import BLELink
from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT
from py9b.transport.xiaomi import XiaomiTransport

# link = SerialLink()
link = TCPLink()
# link = BLELink()

with link:
    print("Scanning...")
    ports = link.scan()
    print(ports)

    tran = XiaomiTransport(link)

    link.open(("127.0.0.1", 6000))
    # link.open(ports[0][1])
    print("Connected")

    req = PKT(src=BT.HOST,
              dst=BT.ESC,
              cmd=0x02,
              arg=0x41,
              data="\xCE\xAB\x00\x00")

    tran.send(req)
    try:
Пример #9
0
from py9b.link.base import LinkOpenException, LinkTimeoutException
from py9b.link.tcp import TCPLink
from py9b.transport.base import BaseTransport as BT
from py9b.transport.packet import BasePacket as PKT
from py9b.transport.xiaomi import XiaomiTransport

#link = SerialLink()
with TCPLink() as link:
    ports = link.scan()
    print ports

    tran = XiaomiTransport(link)

    #link.open(("192.168.1.45", 6000))
    link.open(ports[0][1])

    #req = PKT(src=BT.HOST, dst=BT.ESC, cmd=0x01, arg=0x10, data="\x10")
    req = PKT(src=BT.HOST, dst=BT.BMS, cmd=0x01, arg=0x10, data="\x10")

    while raw_input("Press ENTER to send...") != "q":
        tran.send(req)
        try:
            rsp = tran.recv()
        except LinkTimeoutException:
            print "No response"
            continue
        print rsp

    link.close()