Skip to main content

lunalink (Python node)

A LunaLink node for Python 3.9+: it connects to a LunaLink master, publishes tag values with durable, ordered, duplicate-free delivery, answers remote writes idempotently and speaks revision 2.0 of the LunaLink protocol. It is meant for Raspberry Pi gateways, quick integrations and protocol test tooling.

pip install lunalink

Dependencies: msgpack, lz4. Everything else is the standard library (asyncio, ssl, sqlite3).

Quick start

import asyncio, uuid
from lunalink import LunaLinkNode, DataType

TEMP = uuid.UUID("00112233-4455-6677-8899-aabbccddeeff")

async def main():
    node = LunaLinkNode("master.example", 7788, node_id="pi-1", db_path="lunalink.db",
                        auth_token="secret")
    await node.start()                       # connects in the background, reconnects forever
    node.publish(TEMP, 21.5, DataType.FLOAT32, tag_name="Temperature")   # durable when it returns
    await node.wait_delivered(30)            # until the master acknowledged everything
    await node.close()                       # Goodbye (if negotiated), then close

asyncio.run(main())

A complete example is in examples/node_example.py.

API

Item Purpose
LunaLinkNode(host, port, node_id=, db_path=, ...) The node. endpoints=[(host, port), ...] enables master failover order.
await node.start() / await node.close() Start the connection task / send Goodbye and stop.
node.publish(tag_id, value, data_type=None, ...) Persist one value as a batch. Returns its NodeSequence. Thread safe.
node.publish_batch([TagValue, ...]) Several values, one NodeSequence.
node.publish_event(message, severity=, ...) Event delivered through the outbox (needs events-v1).
await node.wait_delivered(timeout) Wait until the outbox is empty.
on_write= Callback (WriteRequest) -> bool. Called at most once per CommandId. Raise WriteOutcomeUnknown when the result cannot be determined.
on_remote_values=, on_event_ack=, on_error= Master to node values (TagDelta, SnapshotResp), EventAck, Error.
browse_tags= Callable returning TagInventory items; answers BrowseReq.
Features(...) What to offer in Hello (compression, max-payload, window, goodbye, events-v1, ...).
TlsConfig(ca_file=, cert_file=, key_file=) TLS 1.3, optional client certificate (mutual TLS). verify=False is for testing only.

Value types: pass a DataType (0 to 13) or let it be inferred (bool, int, float, str, bytes, lists). GUIDs are uuid.UUID, their text form, or the 16 wire bytes; they are converted to .NET byte order.

Delivery

State lives in one SQLite file (synchronous=FULL): the outbox, the persistent NodeSequence, the last value of every tag, queued events and the result of every write command. A batch is deleted only after TagDeltaAck with Accepted = true. With window=<n> granted by the master, up to n batches are in flight with go-back-N on rejection. If the master expects a sequence the outbox no longer holds (or the backlog reaches 1024 batches) the node sends a resync baseline that also carries the events of the batches it replaces. Without a granted window it uses stop-and-wait.

Tests

pip install -e ".[test]"
pytest                                   # unit tests, scripted-master tests, interop (if dotnet is present)
dotnet build interop/Harness -c Release -o interop/Harness/bin   # once, for the interop tests
  • tests/test_vectors.py: Appendix A vectors byte for byte (CRC, GUID, Float32, Ping frame, Hello, TagDelta, Ack, TagWriteAck, compression containers) plus codec edge cases.
  • tests/test_node.py: the node against an in-process scripted master (tests/fake_master.py).
  • tests/test_interop.py: the node against a real .NET master. interop/Harness is a small console program that references the NuGet package LunaLink 1.2.0, runs LunaLinkServer and prints every received data point. The tests check that values arrive in order without loss across forced reconnects and a master restart.

What is only unit-tested until the 2.0 master is released

The released master (1.2.0) negotiates only no-compression and max-payload. It ignores every other token, so these 2.0 features are covered by tests against the scripted master only, not against the .NET reference:

lz4-block compression in both directions, window=<n> and go-back-N, goodbye, events-v1 (EventItem, EventAck), browse-v1, arrays-v1 (types 10 to 13), master-delta, no-crc, the Error code names beyond what 1.2.0 sends, the SupportedVersions field, leased-by-other-master failover. K4os LZ4Pickler decompression is unit-tested with containers built to the spec (Appendix A.8 layout), not yet against compressed frames from a real master. The 1.x behaviours (stop-and-wait, TagDeltaAck, heartbeat, scalar types 0 to 9, reconnect, master restart) are tested against the real 1.2.0 master. Writes (TagWriteCmd) are tested against the scripted master only, because the 1.2.0 server has no public API to send them from the harness. TLS 1.3 with a client certificate is implemented with the standard ssl module but has no automated test yet.

License

MIT, see LICENSE.

Metadata

Release files for lunalink 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lunalink 0.1.0
File Size Uploaded Provenance Actions
lunalink-0.1.0.tar.gz 34.5 kB With provenance Details for lunalink-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lunalink 0.1.0
File Interpreter ABI Platform Actions
lunalink-0.1.0-py3-none-any.whl Python 3 none any Details for lunalink-0.1.0-py3-none-any.whl

Total release size: 60.6 kB

Security for lunalink 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page