Skip to content

Lablink API reference

The reference for the pypalmsens.lablink module: connecting to Lablink instances, claiming instruments, running measurements, and fetching data. All methods that talk to the server are async. See the lablink introduction for how to get started and concepts for the object model.

Functions

  • discover – Discover lablink instances on the network

Classes

Exceptions

pypalmsens.lablink.discover async

discover(ip_version: IPVersion = All, timeout: float = 10) -> list[dict[str, Any]]

Browse the local network for LabLink devices for a fixed duration.

Uses zeroconf to browse for the _lablink._tcp.local. service type. Each discovered device is resolved into a dictionary of properties (hostname, addresses, TXT record fields) and appended to the results list.

Parameters:

  • ip_version

    (IPVersion, default: All ) –

    IP protocol version for browsing/name resolution. Defaults to zeroconf.IPVersion.All.

  • timeout

    (float, default: 10 ) –

    How many seconds to browse before stopping. Default is 10.

Returns:

  • list[dict] –

    One dictionary per discovered LabLink device. Each dict contains:

    • server: fully qualified host name (e.g. "lablink-01.local.")
    • addresses: parsed scoped IP address strings
    • other TXT record fields as-is, e.g. name, serial, version

    The list is empty if no devices are discovered within the timeout.

Examples:

>>> import asyncio
>>> devices = asyncio.run(discover(timeout=10))
>>> device, _* = devices
>>> devices["name"]
'lablink-01'
>>> devices["addresses"]
['192.168.1.42', ...]
Source code in src/pypalmsens/lablink/_discover.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
async def discover(
    ip_version: IPVersion = IPVersion.All,
    timeout: float = 10,
) -> list[dict[str, Any]]:
    """
    Browse the local network for LabLink devices for a fixed duration.

    Uses zeroconf to browse for the `_lablink._tcp.local.` service type.
    Each discovered device is resolved into a dictionary of properties
    (hostname, addresses, TXT record fields) and appended to the results list.

    Parameters
    ----------
    ip_version : IPVersion
        IP protocol version for browsing/name resolution.
        Defaults to `zeroconf.IPVersion.All`.
    timeout : float
        How many seconds to browse before stopping. Default is 10.

    Returns
    -------
    list[dict]
        One dictionary per discovered LabLink device. Each dict contains:

        * server: fully qualified host name (e.g. "lablink-01.local.")
        * addresses: parsed scoped IP address strings
        * other TXT record fields as-is, e.g. name, serial, version

        The list is empty if no devices are discovered within the timeout.

    Examples
    --------
    >>> import asyncio
    >>> devices = asyncio.run(discover(timeout=10))
    >>> device, _* = devices
    >>> devices["name"]
    'lablink-01'
    >>> devices["addresses"]
    ['192.168.1.42', ...]
    """
    discovered: list[dict[str, Any]] = []
    tasks: set[asyncio.Task] = set()

    def on_service_state_change(
        zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
    ) -> None:
        logger.info('%s: %s', state_change.name, name)
        if state_change is ServiceStateChange.Added:
            task = asyncio.create_task(
                _resolve_service_info(zeroconf, service_type, name, discovered)
            )
            tasks.add(task)
            task.add_done_callback(tasks.discard)

    aiozc = AsyncZeroconf(ip_version=ip_version)
    browser = AsyncServiceBrowser(
        aiozc.zeroconf, '_lablink._tcp.local.', handlers=[on_service_state_change]
    )
    logger.info('browsing for _lablink._tcp.local (timeout=%s s).', timeout)

    try:
        await asyncio.sleep(timeout)
        _ = await asyncio.gather(*tasks, return_exceptions=True)
    finally:
        await browser.async_cancel()
        await aiozc.async_close()

    return discovered