import asyncio

async def flows_client(flows, repeat, delay):
    reader, writer = await asyncio.open_connection('127.0.0.1', 58288)

    for i in range(repeat):
        for message in flows:
            print(f'S=> {message!r}')
            writer.write(message.encode())
            await writer.drain()

            for x in range(len(message.split('\n')) - 1):
                data = await reader.readline()
                reply = data.decode()
                if reply == '':
                    break
                else:
                    print('<=R %r' % reply)
            
            await asyncio.sleep(delay)

    await asyncio.sleep(1)
    print('Close the connection')
    writer.close()
    await writer.wait_closed()

if __name__ == '__main__':

    vtc_flows = [
        'ScanForDevices()\n',
        'VMM-0A:10:GetFWVersion()\n',
        'VMM-0A:GetModbusID()\n',
        'VMM-0A:GetHardwareID()\n',
        'VMM-0A:GetBoardName()\n',
        'VMM-0A:GetBoardSN()\n',
        'VMM-0A:GetUUID()\n',
        'VMM-0A:GetSystemStatus()\n',
        'VMM-0A:GetTargetTemp(1)\n',
        'VMM-0A:SetTargetTemp(1,25.0,2.0)\n',
        'VMM-0A:GetDriverStatus(123)\n',
        'VMM-0A:GetDriverStatus(456)\n',
        'VMM-0A:GetLEDStatus()\n',
        'VMM-0A:SetAutoOffTimer(1,100)\n'
    ]

    try:
        asyncio.run(flows_client(vtc_flows, 1, 0.1))
    except Exception as ex:
        print(ex)
