Example #1
0
async def request_spacekeys(s: aioserial.AioSerial):
    spacekeys = []
    s.write(b'stats spacekey\r\n')
    line = None
    while not (line and line.endswith('stats spacekey\r\n')):
        line = await serial_fetch_line(s)
        print(line, end='', flush=True)
    while line != 'done\r\n':
        line = await serial_fetch_line(s)
        spacekey = _parse_spacekey(line)
        if spacekey:
            spacekeys.append(spacekey)
    return spacekeys
Example #2
0
async def request_bonds(s: aioserial.AioSerial):
    bonds = []
    s.write(b'stats bonds\r\n')
    line = None
    while not (line and line.endswith('stats bonds\r\n')):
        line = await serial_fetch_line(s)
        print(line, end='', flush=True)
    while line != 'done\r\n':
        line = await serial_fetch_line(s)
        bond = _parse_bond(line)
        if bond:
            bonds.append(bond)
    return bonds
Example #3
0
 async def write_serial(self, aioserial_instance: aioserial.AioSerial,
                        message):
     aioserial_instance.write(b'%b' % message.encode('utf8'))
class SerialConnection:
    def __init__(self,
                 port=None,
                 baudrate=9600,
                 timeout=1,
                 inter_byte_delay=None):
        self.ser = AioSerial(port, baudrate, timeout=timeout)
        self.inter_byte_delay = inter_byte_delay

    def open(self):
        """
        Open serial port connection
        """
        if not self.ser.is_open:
            self.ser.open()

    def readline(self):
        """
        Read line from serial port
        :return: One line from serial stream
        """
        try:
            output = self.ser.readline()
            return output
        except SerialException as se:
            log.error('Serial connection read error: {}'.format(se))
            return None

    async def readline_async(self):
        """
        Asynchronously read line from serial port
        :return: One line from serial stream
        """
        try:
            output = await self.ser.readline_async()
            return output
        except asyncio.CancelledError:
            log.error(f'readline_async() future cancelled')
            return None

    def write(self, data):
        """
        Write data to serial port
        :param data: Data to send
        """
        try:
            if self.inter_byte_delay:
                for byte in data:
                    self.ser.write(bytes([byte]))
                    sleep(self.inter_byte_delay)
            else:
                self.ser.write(data)
        except SerialException as se:
            log.error('Serial connection write error: {}'.format(se))

    async def write_async(self, data):
        """
        Asynchronously write data to serial port
        :param data: Data to send
        """
        try:
            if self.inter_byte_delay:
                for byte in data:
                    await self.ser.write_async(bytes([byte]))
                    await asyncio.sleep(self.inter_byte_delay)
            else:
                await self.ser.write_async(data)
        except asyncio.CancelledError:
            log.error(f'write_async() future cancelled')

    def send_break(self, duration=0.25):
        """
        Send break condition to serial port
        :param duration: Break duration
        """
        try:
            self.ser.send_break(duration)
        except SerialException as se:
            log.error('Serial connection send break error: {}'.format(se))

    def close(self):
        """
        Close serial port connection
        """
        self.ser.close()
async def write_serial(aioserial_instance: aioserial.AioSerial):
    aioserial_instance.write(b"print 0 1\n")
    return "SEND OPERATION: OK\n"
Example #6
-2
async def manage_serial(s: aioserial.AioSerial):
    s.write(b'ble_start\r\n')

    if not args.skip_sanity_checks:
        config_identity, coin_list = read_db()
        bonds = await request_bonds(s)
        spacekeys = await request_spacekeys(s)
        assert len(bonds) == len(spacekeys) == len(coin_list), "number of coins does not match"
        for i in zip(bonds, spacekeys, coin_list):
            assert i[0][0] == i[1][0], "addresses must match"
            assert i[2][0] == i[0][0], "addresses must match"
            assert i[2][3][:2] == i[1][1], "spacekey must match"

    battery_level = 0
    coin_address = ""
    # main event loop
    while True:
        line = await serial_fetch_line(s)
        print(line, end='', flush=True)
        k, v = parse_status(line)
        if not args.skip_sanity_checks:
            if k == StatusType.IDENTITY:
                assert v[0].upper() == config_identity[0], "central identity must match"
        if k == StatusType.AUTHENTICATED:
            confirm_authentication(coin_address, battery_level)
        elif k == StatusType.BATTERY_LEVEL:
            battery_level = v[0]
        elif k == StatusType.CONNECTED:
            coin_address = v[0]
        elif k == StatusType.DISCONNECTED:
            battery_level = 0
            coin_address = ""