# Path: mysensors/cli/helper.py
# def common_gateway_options(func):
#     """Supply common gateway options."""
#     func = click.option(
#         "-v",
#         "--protocol_version",
#         help="Protocol version of the gateway.",
#         default="2.2",
#         show_default=True,
#     )(func)
#     func = click.option(
#         "-s", "--persistence", help="Turn on persistence.", is_flag=True
#     )(func)
#     return func
# 
# def handle_msg(msg):
#     """Handle mysensors updates."""
#     _LOGGER.info("Received message: %s", msg.encode().strip())
# 
# def run_async_gateway(gateway, stop_task=None):
#     """Run an async gateway."""
#     try:
#         gateway.tasks.loop.run_until_complete(gateway.start_persistence())
#         gateway.tasks.loop.run_until_complete(gateway.start())
#         gateway.tasks.loop.run_forever()
#     except KeyboardInterrupt:
#         gateway.tasks.loop.run_until_complete(gateway.stop())
#         if stop_task:
#             gateway.tasks.loop.run_until_complete(stop_task)
#         gateway.tasks.loop.close()
# 
# def run_gateway(gateway):
#     """Run a sync gateway."""
#     gateway.start_persistence()
#     gateway.start()
#     try:
#         while True:
#             time.sleep(0.5)
#     except KeyboardInterrupt:
#         gateway.stop()
# 
# Path: mysensors/gateway_tcp.py
# class AsyncTCPGateway(BaseAsyncGateway, BaseTCPGateway):
#     """MySensors async TCP gateway."""
# 
#     def __init__(self, *args, loop=None, **kwargs):
#         """Set up TCP gateway."""
#         self.cancel_check_conn = None
#         protocol = AsyncTCPMySensorsProtocol
#         transport = AsyncTransport(
#             self, async_connect, loop=loop, protocol=protocol, **kwargs
#         )
#         super().__init__(transport, *args, loop=loop, **kwargs)
# 
#     def check_connection(self):
#         """Check if connection is alive every reconnect_timeout seconds."""
#         try:
#             super().check_connection()
#         except OSError as exc:
#             _LOGGER.error(exc)
#             self.tasks.transport.protocol.transport.close()
#             self.tasks.transport.protocol.conn_lost_callback()
#             return
#         task = self.tasks.loop.call_later(
#             self.tasks.transport.reconnect_timeout + 0.1, self.check_connection
#         )
#         self.cancel_check_conn = task.cancel
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         mac = await self.tasks.loop.run_in_executor(None, self._get_gateway_id)
#         return mac
# 
# class TCPGateway(BaseSyncGateway, BaseTCPGateway):
#     """MySensors TCP gateway."""
# 
#     def __init__(self, *args, **kwargs):
#         """Set up TCP gateway."""
#         transport = SyncTransport(self, sync_connect, **kwargs)
#         super().__init__(transport, *args, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


import click
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_tcp import AsyncTCPGateway, TCPGateway



def common_tcp_options(func):
    """Supply common tcp gateway options."""
    func = click.option(
        "-p",
        "--port",
        default=5003,
        show_default=True,
        type=int,
        help="TCP port of the connection.",
    )(func)
    func = click.option(
        "-H", "--host", required=True, help="TCP address of the gateway."
    )(func)
    return func


@click.command(options_metavar="<options>")
@common_tcp_options
@common_gateway_options
def tcp_gateway(**kwargs):
    """Start a tcp gateway."""
    gateway = TCPGateway(event_callback=handle_msg, **kwargs)
    run_gateway(gateway)


@click.command(options_metavar="<options>")
@common_tcp_options

# Path: mysensors/cli/helper.py
# def common_gateway_options(func):
#     """Supply common gateway options."""
#     func = click.option(
#         "-v",
#         "--protocol_version",
#         help="Protocol version of the gateway.",
#         default="2.2",
#         show_default=True,
#     )(func)
#     func = click.option(
#         "-s", "--persistence", help="Turn on persistence.", is_flag=True
#     )(func)
#     return func
# 
# def handle_msg(msg):
#     """Handle mysensors updates."""
#     _LOGGER.info("Received message: %s", msg.encode().strip())
# 
# def run_async_gateway(gateway, stop_task=None):
#     """Run an async gateway."""
#     try:
#         gateway.tasks.loop.run_until_complete(gateway.start_persistence())
#         gateway.tasks.loop.run_until_complete(gateway.start())
#         gateway.tasks.loop.run_forever()
#     except KeyboardInterrupt:
#         gateway.tasks.loop.run_until_complete(gateway.stop())
#         if stop_task:
#             gateway.tasks.loop.run_until_complete(stop_task)
#         gateway.tasks.loop.close()
# 
# def run_gateway(gateway):
#     """Run a sync gateway."""
#     gateway.start_persistence()
#     gateway.start()
#     try:
#         while True:
#             time.sleep(0.5)
#     except KeyboardInterrupt:
#         gateway.stop()
# 
# Path: mysensors/gateway_tcp.py
# class AsyncTCPGateway(BaseAsyncGateway, BaseTCPGateway):
#     """MySensors async TCP gateway."""
# 
#     def __init__(self, *args, loop=None, **kwargs):
#         """Set up TCP gateway."""
#         self.cancel_check_conn = None
#         protocol = AsyncTCPMySensorsProtocol
#         transport = AsyncTransport(
#             self, async_connect, loop=loop, protocol=protocol, **kwargs
#         )
#         super().__init__(transport, *args, loop=loop, **kwargs)
# 
#     def check_connection(self):
#         """Check if connection is alive every reconnect_timeout seconds."""
#         try:
#             super().check_connection()
#         except OSError as exc:
#             _LOGGER.error(exc)
#             self.tasks.transport.protocol.transport.close()
#             self.tasks.transport.protocol.conn_lost_callback()
#             return
#         task = self.tasks.loop.call_later(
#             self.tasks.transport.reconnect_timeout + 0.1, self.check_connection
#         )
#         self.cancel_check_conn = task.cancel
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         mac = await self.tasks.loop.run_in_executor(None, self._get_gateway_id)
#         return mac
# 
# class TCPGateway(BaseSyncGateway, BaseTCPGateway):
#     """MySensors TCP gateway."""
# 
#     def __init__(self, *args, **kwargs):
#         """Set up TCP gateway."""
#         transport = SyncTransport(self, sync_connect, **kwargs)
#         super().__init__(transport, *args, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


# Path: mysensors/cli/gateway_tcp.py
import click
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_tcp import AsyncTCPGateway, TCPGateway




def common_tcp_options(func):
    """Supply common tcp gateway options."""
    func = click.option(
        "-p",
        "--port",
        default=5003,
        show_default=True,
        type=int,
        help="TCP port of the connection.",
    )(func)
    func = click.option(
        "-H", "--host", required=True, help="TCP address of the gateway."
    )(func)
    return func


@click.command(options_metavar="<options>")
@common_tcp_options
@common_gateway_options
def tcp_gateway(**kwargs):
    """Start a tcp gateway."""
    gateway = TCPGateway(event_callback=handle_msg, **kwargs)
    run_gateway(gateway)


@click.command(options_metavar="<options>")
@common_tcp_options

@common_gateway_options