Skip to content

Commit

Permalink
Add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed May 1, 2021
1 parent 29a4851 commit 887e826
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 3 deletions.
193 changes: 190 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ description = "A multiplex TCP server that provides communication with a serial
authors = ["José Sánchez-Gallego <[email protected]>"]
license = "BSD-3-Clause"

[tool.poetry.scripts]
com-server = "simple_com_server.__main__:com_server"

[tool.poetry.dependencies]
python = "^3.9"
pyserial = "^3.5"
pyserial-asyncio = "^0.5"
sdsstools = "^0.4.10"
daemonocle = "^1.2.3"

[tool.poetry.dev-dependencies]
ipython = ">=7.11.0"
Expand Down
56 changes: 56 additions & 0 deletions src/simple_com_server/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego ([email protected])
# @Date: 2021-04-30
# @Filename: __main__.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

import asyncio

import click
from sdsstools.daemonizer import DaemonGroup, cli_coro

from simple_com_server.server import TCPServer


@click.group(cls=DaemonGroup, prog="com-server")
@click.option(
"--device",
multiple=True,
type=str,
required=True,
help="Path to the serial device. Can be called multiple times for several devices.",
)
@click.option(
"--port",
multiple=True,
type=int,
required=True,
help="Port on which to serve. Can be called multiple times for several devices.",
)
@click.option(
"--timeout",
type=float,
default=1.0,
help="Time to wait for a reply from the serial device.",
)
@cli_coro()
async def com_server(device, port, timeout):
"""Start a TCP-to-COM server."""

assert len(device) == len(port), "Number of devices must match number of ports."
assert len(set(port)) == len(port), "Ports must be unique"

servers = await asyncio.gather(
*[
TCPServer(
str(device[ii]),
port[ii],
timeout=timeout,
).start()
for ii in range(len(port))
]
)

await servers[0].server.serve_forever()

0 comments on commit 887e826

Please sign in to comment.