Skip to main content

UnetSocket: A high-level socket interface for UnetStack in Python

Project description

unetpy

unetpy is the modern Python wrapper for fjagepy. It exposes the low-level fjåge primitives alongside a batteries-included UnetSocket, Unet-specific message classes, and coordinate utilities.

Installation

From PyPI

pip install unetpy

From source

Clone the repository and install it in editable mode:

pip install -e '.[dev]'

This uses the pyproject.toml-based build powered by the standard setuptools backend.

Usage

Import the high-level API just like in unet.js:

from unetpy import (
    Gateway,
    AgentID,
    Performative,
    Message,
    MessageClass,
    UnetSocket,
    Protocol,
    Services,
    to_gps,
    to_local,
)
  • All fjåge primitives continue to be available, so legacy from unetpy import * snippets keep working.
  • Pre-defined message classes (DatagramReq, DatagramNtf, etc.) can be imported directly.
  • to_gps()/to_local() replicate the helper coordinate math from unet.js.

Pre-defined Messages and Services

Message classes mirror the UnetStack class hierarchy, so inheritance behaves exactly like it does on the modem. That means helper checks such as isinstance(rx, DatagramNtf) succeed even when rx is an RxFrameNtf. You can construct requests without manually calling MessageClass:

from unetpy import Gateway, Services, DatagramReq

gw = Gateway("localhost", 1100)
req = DatagramReq()
req.to = 31
req.protocol = 0
req.data = [1, 2, 3]
req.recipient = gw.agentForService(Services.DATAGRAM)
gw.send(req)

Working with UnetSocket

UnetSocket mirrors the JS implementation from unet.js, handling subscriptions, default addresses, and blocking receives on top of fjåge’s gateway:

from unetpy import Protocol, Services, DatagramNtf, UnetSocket

with UnetSocket("localhost", 1101) as sock:
    sock.bind(Protocol.USER)
    sock.connect(31, Protocol.USER)
    sock.send([0x01, 0x02, 0x03])

    sock.setTimeout(2000)
    ntf = sock.receive()
    if isinstance(ntf, DatagramNtf):
        print(f"Got datagram from {ntf.from_}: {ntf.data}")

    # Drop down to fjåge when you need shell access or raw agents
    gw = sock.getGateway()
    shell = gw.agentForService(Services.SHELL)
    print(shell.language)

Coordinate helpers

The coordinate math matches what unet.js exposes through toGps()/toLocal(), so you can use the same mission-planning tutorials in Python:

from unetpy import to_gps, to_local

origin = (1.25, 103.88)  # latitude, longitude
lat, lon = to_gps(origin, x=120.0, y=-45.0)
print(lat, lon)
print(to_local(origin, lat, lon))

Development

Run the unit tests with:

pytest

Documentation

Detailed documentation is available in the docs/ directory.

License

This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unetpy-4.0.0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unetpy-4.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file unetpy-4.0.0.tar.gz.

File metadata

  • Download URL: unetpy-4.0.0.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for unetpy-4.0.0.tar.gz
Algorithm Hash digest
SHA256 342074214201a66b46ee669027ea1048cf819d34a687ea5c5fae064acda4ab0b
MD5 68c8d9b4f6cce5279285162d7373827e
BLAKE2b-256 08ffb625955484b0b709b6e46c25b1e3a484b9cd8f85667abaa7f0c82ea275e6

See more details on using hashes here.

File details

Details for the file unetpy-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: unetpy-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for unetpy-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30928f1f5bc6274cbb56829dab0641f2045ad84d7360068fa6142b1d6ae5bb8e
MD5 cec5d4eb762f520e792f1fe36b3a6a83
BLAKE2b-256 bd9b38d859be344dcbf833435cd30868498cd7d3840595ef3f0e7bb88c8a7993

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page