import asyncio

import click

from precon.devices_handlers.distance_sensor import show_distance as show_distance_func
from precon.remote_control import steer_vehicle, Screen

try:
    import RPi.GPIO as GPIO
except (RuntimeError, ModuleNotFoundError):
    import fake_rpi

    GPIO = fake_rpi.RPi.GPIO


@click.command(name="rc")
def remote_control() -> None:
    loop = asyncio.get_event_loop()
    try:
        with Screen() as screen:
            loop.run_until_complete(steer_vehicle(screen))
    except KeyboardInterrupt:
        print("Finishing remote control...")
    except Exception as e:
        print("Raised unexpected error: %s" % e)
    finally:
        GPIO.cleanup()


@click.command(name="show-distance")
def show_distance() -> None:
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(show_distance_func())
    except KeyboardInterrupt:
        print("Finishing measuring distance...")
    except Exception as e:
        print("Raised unexpected error: %s" % e)
    finally:
        GPIO.cleanup()

[(16, 'click.command', 'click.command', False, 'import click\n'), (30, 'click.command', 'click.command', False, 'import click\n'), (18, 'asyncio.get_event_loop', 'asyncio.get_event_loop', False, 'import asyncio\n'), (32, 'asyncio.get_event_loop', 'asyncio.get_event_loop', False, 'import asyncio\n'), (27, 'RPi.GPIO.cleanup', 'GPIO.cleanup', True, 'import RPi.GPIO as GPIO\n'), (40, 'RPi.GPIO.cleanup', 'GPIO.cleanup', True, 'import RPi.GPIO as GPIO\n'), (20, 'precon.remote_control.Screen', 'Screen', False, 'from precon.remote_control import steer_vehicle, Screen\n'), (34, 'precon.devices_handlers.distance_sensor.show_distance', 'show_distance_func', True, 'from precon.devices_handlers.distance_sensor import show_distance as show_distance_func\n'), (21, 'precon.remote_control.steer_vehicle', 'steer_vehicle', False, 'from precon.remote_control import steer_vehicle, Screen\n')]