Metadata-Version: 2.4
Name: base-aprs-client
Version: 0.0.1
Summary: Base layer for building more complex APRS interactions with python.
Author-email: Masen Furer KF7HVM <kf7hvm@0x26.net>
License: MIT License
        
        Copyright (c) 2022 python-aprs
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/python-aprs/base-aprs-client
Keywords: Ham Radio,APRS
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Topic :: Communications :: Ham Radio
Classifier: Framework :: AsyncIO
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aprs3>=8.2.0
Requires-Dist: ax253>=0.2.1
Requires-Dist: kiss3>=8.1.1
Requires-Dist: attrs>20
Provides-Extra: db
Requires-Dist: aiosqlite>=0.20; extra == "db"
Requires-Dist: sqlalchemy[asyncio]>=2.0.30; extra == "db"
Dynamic: license-file

# base-aprs-client

Base layer for building APRS client applications in Python, on top of
[aprs3](https://github.com/python-aprs/aprs3),
[kiss3](https://github.com/python-aprs/kiss3) and
[ax253](https://github.com/python-aprs/ax253).

`AsyncClient` (asyncio) and `Client` (its synchronous wrapper) implement
the APRS station semantics that every application needs:

* sending messages, with acknowledgement and timed retries
* acknowledging incoming messages addressed to `mycall`, and delivering
  each one once even when the sender retries
* [reply-acks](http://www.aprs.org/aprs11/replyacks.txt), on by default
* splitting long messages on word boundaries, or truncating them
* sending status and position reports
* reconnecting with backoff when the connection drops, keeping
  unacknowledged messages
* with a `PacketStore`, logging every frame and sending queued frames when
  they are due, even across restarts

It works over APRS-IS, KISS-over-TCP (e.g. Dire Wolf) or a serial KISS TNC.

## Example

```python
import asyncio
from base_aprs_client import AsyncClient


async def main():
    async with AsyncClient.aprsis(
        "N0CALL-1",
        passcode="-1",  # use a real passcode to transmit
        filter="r/46.1/-122.9/50",
    ) as client:
        client.send_status("Hello from base-aprs-client")
        await client.send_message("N0CALL-2", "hi", ack=True)  # waits for the ack
        async for frame in client:
            print(frame)


asyncio.run(main())
```

The synchronous `Client` runs the same client on a background thread:

```python
from base_aprs_client import Client

with Client.kiss_tcp("N0CALL-1", "localhost", 8001) as client:
    ack = client.send_message("N0CALL-2", "hi", ack=True)
    print(ack.result(timeout=60))
    for frame in client.read(min_frames=1):
        print(frame)
```

`Client(mycall, sync_frame_io)` with an already started `aprs.TCP`,
`aprs.TCPKISS` or `aprs.SerialKISS` still works as before; its event loop
only runs inside the client's calls.

## Local testing without a radio

`APRSISSimulator` is a tiny APRS-IS-like relay: every line one client sends
is forwarded to all the others.

```python
from aprs import TCP
from base_aprs_client import APRSISSimulator, Client

with APRSISSimulator() as sim:
    with TCP(host=sim.host, port=sim.port) as t1, TCP(host=sim.host, port=sim.port) as t2:
        sim.wait_for_clients(2)
        alice = Client("N0CALL-1", t1)
        bob = Client("N0CALL-2", t2)
        alice.send_message(bob.mycall, "hi bob")
        print(bob.read(min_frames=1))
```

Run it standalone on port 14588 with `python -m base_aprs_client.simulator`.

## Examples

The [examples](examples) directory has two scripts that work over APRS-IS
(`--aprsis host:port`), a KISS TNC on TCP (`--tcp host:port`) or a serial
TNC (`--serial device`):

* `send_message.py` sends one message and waits for the ack, splitting long
  text into parts. It uses `Client`.
* `chat.py` is a terminal chat: each line you type goes to `--to`, and
  messages addressed to you are printed as they arrive. It uses
  `AsyncClient`.

Start `python -m base_aprs_client.simulator` and point two copies of
`chat.py` at `--aprsis localhost:14588` to try them without a radio.

## Packet log and send queue

`base_aprs_client.store.PacketStore` keeps a log of frames heard and sent,
and a queue of frames to transmit later. It uses SQLAlchemy 2.0 with an
async session, so it never blocks the event loop; SQLite through aiosqlite
is the default, and any async SQLAlchemy URL works. Install with
`pip install base-aprs-client[db]`.

Give it to a client to log everything it hears and sends, and to transmit
frames scheduled with `client.schedule(frame, delay=...)`:

```python
from aprs import InformationField
from base_aprs_client import AsyncClient
from base_aprs_client.store import PacketStore

async with PacketStore("sqlite+aiosqlite:///N0CALL.db") as store:
    async with AsyncClient.aprsis("N0CALL-1", passcode="12345", store=store) as client:
        await client.schedule(InformationField.from_bytes(b">QRT soon"), delay=600)
```

The store can also be used on its own:

```python
from base_aprs_client.store import PacketStore

async with PacketStore("sqlite+aiosqlite:///N0CALL.db") as store:
    await store.record_received(frame)
    await store.schedule(reply, delay=10)
    for record in await store.due():
        if await store.claim(record):  # False if another task already sent it
            transport.write(record.frame)
```

## Development

```
uv sync --all-extras
uv run pytest
```

CI runs the suite on every supported Python (3.11+). The KISS tests run a
serial TNC on a pseudo-terminal, and send through
[Dire Wolf](https://github.com/wb2osz/direwolf) when it is installed
(`apt install direwolf`); they are skipped otherwise. Lint and format with
`uv run ruff check` and `uv run ruff format`, type-check with `uv run ty check`.
