deviceregistry
plantimager.commons.deviceregistry Link
DeviceRegistry Link
DeviceRegistry(context, addr='*', port='5555')
Bases: Thread
A thread-based Device Registry for managing registering and unregistering devices.
This class provides a thread-based mechanism to handle device registration and unregistration events. It communicates using ZeroMQ sockets and fires appropriate callbacks when devices are added or removed.
Attributes:
| Name | Type | Description |
|---|---|---|
addr |
str
|
Address on which the registry will bind. |
port |
str
|
Port on which the registry will listen for messages. |
context |
Context
|
ZeroMQ context used for setting up the communication socket. |
devices |
dict of str to tuple of (str, str)
|
Dictionary mapping device names to a tuple of (device_type, address). |
_new_device_callbacks |
list of Callable
|
List of callback functions to execute when a new device is added. |
_device_removed_callbacks |
list of Callable
|
List of callback functions to execute when a device is removed. |
_stop_flag |
bool
|
Control flag to indicate whether the registry thread should stop. |
_callback_events_to_process |
dict of str (Literal["added", "removed"]) to list of tuple
|
Dictionary containing lists of registered or removed devices to process callbacks for. |
Source code in plantimager/commons/deviceregistry.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
add_device_removed_callback Link
add_device_removed_callback(callback)
Register a callback function to be executed when a device is removed.
Source code in plantimager/commons/deviceregistry.py
348 349 350 351 352 | |
add_new_device_callback Link
add_new_device_callback(callback)
Register a callback function to be executed when a new device is registered.
Source code in plantimager/commons/deviceregistry.py
342 343 344 345 346 | |
get_devices Link
get_devices()
Returns the registered devices.
Returns:
| Type | Description |
|---|---|
dict[str, tuple[str, str]]
|
A dictionary where the keys are device identifiers as strings and the values are tuples containing the name and type of the device as strings. |
Source code in plantimager/commons/deviceregistry.py
354 355 356 357 358 359 360 361 362 363 364 365 | |
run Link
run()
Runs the main loop for the registry service, handling device registration, unregistration, and device-related event callbacks. The service listens for messages using ZeroMQ (zmq) and processes the events accordingly.
Source code in plantimager/commons/deviceregistry.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
stop Link
stop()
Stop the registry thread.
Source code in plantimager/commons/deviceregistry.py
116 117 118 119 120 | |
register_device Link
register_device(context, device_type, addr, name, registry_url, overwrite=False)
Register device of type device_type and of address addr to registry at registry_url.
Proposes name to registry.
Returns accepted device name and the uuid of the device if successful, otherwise an empty string. The uuid must be kept to unregister the device later.
Source code in plantimager/commons/deviceregistry.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
send_alive_check Link
send_alive_check(context, uuid, registry_url, alive_timeout=ALIVE_TIMEOUT)
Check the liveness of a service with the registry.
Send an EventType.CHECK_ALIVE request to the registry and wait for an
EventType.ACK reply. The function opens a REQ socket on the provided
ZeroMQ context, sends the payload containing uuid and alive_timeout,
and returns True only when an acknowledgement is received within the
socket poll timeout (5s). Any other reply or the lack of a reply results in
False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Context
|
ZeroMQ :class: |
required |
uuid
|
str
|
Unique identifier of the service performing the alive check. |
required |
registry_url
|
str
|
URL of the registry (e.g., |
required |
alive_timeout
|
int
|
Timeout (in seconds) that the service claims it will stay alive. The
value is included in the request payload. Defaults to |
ALIVE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
str
|
'ok' if alive check successfull 'unreachable' if the check attempt timed out 'unknown' if the registry does not know this service; might have already been removed |
Raises:
| Type | Description |
|---|---|
ZMQError
|
Propagated if ZeroMQ encounters an error while creating the socket, connecting, sending, or receiving messages. |
Notes
- The socket is created inside a
withblock, guaranteeing that it is closed when the block exits. The explicitsocket.close()calls are retained for clarity but are not strictly required. - The function uses a fixed poll timeout of 5 seconds; this value is not configurable via the public API.
alive_timeoutis merely echoed back to the registry and is not used by this function to enforce any timing constraints.
See Also
EventType – enumeration of supported event types.
Source code in plantimager/commons/deviceregistry.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
unregister_device Link
unregister_device(context, uuid, registry_addr)
Unregister the device of the given uuid from the registry at registry_addr.
Returns True if the device was unregistered successfully.
Source code in plantimager/commons/deviceregistry.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |