#!/usr/bin/env python3
#
# BTZen - library to asynchronously access Bluetooth devices.
#
# Copyright (C) 2015-2020 by Artur Wroblewski <wrobell@riseup.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import asyncio
import logging
import uvloop

import btzen

parser = argparse.ArgumentParser()
parser.add_argument(
    '--verbose', default=False, action='store_true',
    help='show debug log'
)
parser.add_argument('service', help='Bluetooth service UUID')
parser.add_argument(
    'devices', nargs='+',
    help='List of MAC addresses of devices to connect'
)
args = parser.parse_args()

level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(level=level)

class Device(btzen.Device):
    info = btzen.Info(args.service)

    async def enable(self): pass

# get the loop first
uvloop.install()
loop = asyncio.get_event_loop()

devices = (Device(mac) for mac in args.devices)

# create connection manager
manager = btzen.ConnectionManager()
manager.add(*devices)

try:
    loop.run_until_complete(manager)
finally:
    # connection manager closes all devices
    manager.close()

# vim: sw=4:et:ai
