bec-lib 4.6.1__tar.gz → 4.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bec_lib-4.6.1 → bec_lib-4.7.0}/PKG-INFO +20 -1
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/device.py +9 -3
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/devicemanager.py +70 -22
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/endpoints.py +3 -2
- bec_lib-4.7.0/bec_lib/zmq_transports/__init__.py +6 -0
- bec_lib-4.7.0/bec_lib/zmq_transports/adzmq.py +276 -0
- bec_lib-4.7.0/bec_lib/zmq_transports/protocol.py +165 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/pyproject.toml +3 -1
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_device_manager.py +298 -1
- bec_lib-4.7.0/tests/test_zmq_transports.py +370 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/.gitignore +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/README.md +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/_print_versions.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/acl_login.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/actors.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/alarm_handler.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/atlas_models.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/bec_errors.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/bec_service.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/bec_yaml_loader.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/bl_state_manager.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/bl_states.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/builtin_actor_hli.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/callback_handler.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/channel_monitor.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/client.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/codecs.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/config_helper.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/config_values.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/configs/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/configs/demo_config.yaml +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/configs/openapi_schema.json +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/connector.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/dap_plugin_objects.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/dap_plugins.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/file_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/live_scan_data.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/lmfit_serializer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/logger.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/macro_update_handler.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/messages.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/messaging_hooks.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/messaging_services.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/metadata_schema.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/numpy_encoder.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/observer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/path_optimization.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/pdf_writer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/plugin_helper.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/position_generators.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/procedures/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/procedures/helper.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/procedures/hli.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/procedures/status.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/queue_items.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/constants.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/hli.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/managed_redis_connection.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/streams.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/redis_connector/validation.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/request_context.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/request_items.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_args.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_data_container.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_history.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_input_validator.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_items.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_manager.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_number_container.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_preview.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_repeat.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scan_report.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scans.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/scibec_validator.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/script_executor.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/serialization.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/serialization_registry.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/service_config.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/signature_serializer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/tests/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/tests/fixtures.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/tests/test_config.yaml +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/tests/test_service_config.yaml +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/tests/utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/user_macros.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/copier_jinja_filters.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/deprecation.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/import_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/json_extended.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/_constants.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/_util.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/create/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/create/device.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/create/scan.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/create/templates/scan.py.jinja +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/plugin_manager/main.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/proxy.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/pydantic_pretty_print.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/rpc_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/scan_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/threading_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/bec_lib/utils/user_acls_test.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/__init__.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/_additional_for_signature_serializer_test.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/conftest.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_acl_login.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_alarm_handler.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_atlas_models.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_beamline_states.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_bec_logger.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_bec_messages.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_bec_service.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_callback_handler.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_channel_monitor.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_client.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_config_helper.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_core_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_dap_plugins.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_data_container.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_deprecation.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_device_hashing.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_devices.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_file_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_hli_connector_fakeredis.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_hli_redis_connector.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_import_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_jinja_plugins.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_lmfit_serializer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_managed_config_values.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_managed_redis_connection.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_messaging_hooks.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_messaging_service.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_metadata_schema.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_observer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_path_optimization.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_pdf_writer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_plugin_helper.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_plugin_manager.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_plugin_system.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_position_generators.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_procedure_submission.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_pydantic_pretty_print.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_queue_items.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_redis_connector_fakeredis.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_roi_endpoints.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_roi_messages.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_context.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_data.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_history.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_items.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_manager.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_object.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_preview.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_repeat.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scan_report.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_scibec_validator.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_script_executor.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_serializer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_service_config.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_signature_serializer.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_signature_serializer_with_future_import.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_stream_subs.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_user_macros.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_utils.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/tests/test_yaml_loader.py +0 -0
- {bec_lib-4.6.1 → bec_lib-4.7.0}/util_scripts/init_config.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: bec_lib
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.7.0
|
|
4
4
|
Summary: BEC library
|
|
5
5
|
Project-URL: Bug Tracker, https://github.com/bec-project/bec/issues
|
|
6
6
|
Project-URL: Homepage, https://github.com/bec-project/bec
|
|
@@ -35,6 +35,25 @@ Requires-Dist: scipy~=1.12
|
|
|
35
35
|
Requires-Dist: tomli>=2.0.1,~=2.0
|
|
36
36
|
Requires-Dist: toolz<2.0,>=0.12
|
|
37
37
|
Requires-Dist: typeguard>=4.1.5,~=4.1
|
|
38
|
+
Provides-Extra: adzmq
|
|
39
|
+
Requires-Dist: pillow<14,>=10; extra == 'adzmq'
|
|
40
|
+
Requires-Dist: pyzmq<28,>=25; extra == 'adzmq'
|
|
41
|
+
Provides-Extra: all
|
|
42
|
+
Requires-Dist: black~=26.0; extra == 'all'
|
|
43
|
+
Requires-Dist: copier~=9.7; extra == 'all'
|
|
44
|
+
Requires-Dist: coverage~=7.0; extra == 'all'
|
|
45
|
+
Requires-Dist: fakeredis>=2.23.2,~=2.23; extra == 'all'
|
|
46
|
+
Requires-Dist: isort>=5.13.2,~=5.13; extra == 'all'
|
|
47
|
+
Requires-Dist: jinja2~=3.1; extra == 'all'
|
|
48
|
+
Requires-Dist: pandas<4,>=2; extra == 'all'
|
|
49
|
+
Requires-Dist: pillow<14,>=10; extra == 'all'
|
|
50
|
+
Requires-Dist: pylint<5,>=3; extra == 'all'
|
|
51
|
+
Requires-Dist: pytest-random-order~=1.1; extra == 'all'
|
|
52
|
+
Requires-Dist: pytest-redis<6,>=3; extra == 'all'
|
|
53
|
+
Requires-Dist: pytest-timeout~=2.2; extra == 'all'
|
|
54
|
+
Requires-Dist: pytest<10,>=8; extra == 'all'
|
|
55
|
+
Requires-Dist: pyzmq<28,>=25; extra == 'all'
|
|
56
|
+
Requires-Dist: typer~=0.15; extra == 'all'
|
|
38
57
|
Provides-Extra: ci
|
|
39
58
|
Requires-Dist: bec-testing-plugin; extra == 'ci'
|
|
40
59
|
Provides-Extra: dev
|
|
@@ -11,7 +11,9 @@ import threading
|
|
|
11
11
|
import time
|
|
12
12
|
import uuid
|
|
13
13
|
from collections import defaultdict, namedtuple
|
|
14
|
+
from contextlib import nullcontext
|
|
14
15
|
from contextvars import ContextVar
|
|
16
|
+
from copy import deepcopy
|
|
15
17
|
from datetime import datetime
|
|
16
18
|
from typing import TYPE_CHECKING, Any, Callable, Iterable
|
|
17
19
|
|
|
@@ -1140,6 +1142,12 @@ class Device(OphydInterfaceBase):
|
|
|
1140
1142
|
"""
|
|
1141
1143
|
Provides a summary of the device, all associated signals and their type.
|
|
1142
1144
|
"""
|
|
1145
|
+
lock = getattr(self.root.parent, "_config_lock", None)
|
|
1146
|
+
with lock if lock is not None else nullcontext():
|
|
1147
|
+
signals = deepcopy(self._info.get("signals", {}))
|
|
1148
|
+
Console().print(self._build_summary_table(signals))
|
|
1149
|
+
|
|
1150
|
+
def _build_summary_table(self, signals: dict) -> Table:
|
|
1143
1151
|
table = Table(title=f"{self.name} - Summary of Available Signals")
|
|
1144
1152
|
table.add_column("Name")
|
|
1145
1153
|
table.add_column("Data Name")
|
|
@@ -1148,7 +1156,6 @@ class Device(OphydInterfaceBase):
|
|
|
1148
1156
|
table.add_column("Type")
|
|
1149
1157
|
table.add_column("Labels")
|
|
1150
1158
|
table.add_column("Description")
|
|
1151
|
-
signals = self._info.get("signals", {})
|
|
1152
1159
|
signals_grouped = defaultdict(dict)
|
|
1153
1160
|
for kind in ["hinted", "normal", "config", "omitted"]:
|
|
1154
1161
|
signals_grouped[kind] = {
|
|
@@ -1197,8 +1204,7 @@ class Device(OphydInterfaceBase):
|
|
|
1197
1204
|
signal_info.get("doc"),
|
|
1198
1205
|
)
|
|
1199
1206
|
|
|
1200
|
-
|
|
1201
|
-
console.print(table)
|
|
1207
|
+
return table
|
|
1202
1208
|
|
|
1203
1209
|
|
|
1204
1210
|
class AdjustableMixin:
|
|
@@ -36,18 +36,13 @@ from bec_lib.device import (
|
|
|
36
36
|
)
|
|
37
37
|
from bec_lib.endpoints import MessageEndpoints
|
|
38
38
|
from bec_lib.logger import bec_logger
|
|
39
|
-
from bec_lib.messages import AvailableResourceMessage, DeviceConfigMessage
|
|
39
|
+
from bec_lib.messages import AvailableResourceMessage, DeviceConfigMessage, DeviceInfoMessage
|
|
40
40
|
from bec_lib.utils.import_utils import lazy_import_from
|
|
41
41
|
from bec_lib.utils.rpc_utils import rgetattr
|
|
42
42
|
|
|
43
43
|
if TYPE_CHECKING: # pragma: no cover
|
|
44
44
|
from bec_lib.bec_service import BECService
|
|
45
|
-
from bec_lib.messages import
|
|
46
|
-
BECStatus,
|
|
47
|
-
DeviceInfoMessage,
|
|
48
|
-
ScanStatusMessage,
|
|
49
|
-
ServiceResponseMessage,
|
|
50
|
-
)
|
|
45
|
+
from bec_lib.messages import BECStatus, ScanStatusMessage, ServiceResponseMessage
|
|
51
46
|
else:
|
|
52
47
|
# TODO: put back normal import when Pydantic gets faster
|
|
53
48
|
BECStatus, ServiceResponseMessage = lazy_import_from(
|
|
@@ -686,6 +681,55 @@ class DeviceManagerBase:
|
|
|
686
681
|
MessageEndpoints.device_config_update(), cb=self._device_config_update_callback
|
|
687
682
|
)
|
|
688
683
|
self.connector.register(MessageEndpoints.scan_status(), cb=self._update_scan_info)
|
|
684
|
+
if self._use_proxy_objects:
|
|
685
|
+
self.connector.register(
|
|
686
|
+
patterns=MessageEndpoints.device_info("*"), cb=self._device_info_update_callback
|
|
687
|
+
)
|
|
688
|
+
|
|
689
|
+
def _device_info_update_callback(self, msg) -> None:
|
|
690
|
+
"""Read current metadata after an invalidation without replacing proxy identities."""
|
|
691
|
+
if not self._use_proxy_objects:
|
|
692
|
+
return
|
|
693
|
+
update = msg.value
|
|
694
|
+
if not isinstance(update, DeviceInfoMessage):
|
|
695
|
+
return
|
|
696
|
+
with self._config_lock:
|
|
697
|
+
device = self.devices.get(update.device)
|
|
698
|
+
if device is not None:
|
|
699
|
+
# A notification queued during initialization may predate the initial Redis read.
|
|
700
|
+
current = self._get_device_info(update.device)
|
|
701
|
+
if isinstance(current, DeviceInfoMessage):
|
|
702
|
+
self._merge_device_info(
|
|
703
|
+
device._info, current.info # pylint: disable=protected-access
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
@classmethod
|
|
707
|
+
def _merge_device_info(cls, current: dict, updated: dict) -> None:
|
|
708
|
+
"""Refresh full descriptions while retaining nested proxy metadata references."""
|
|
709
|
+
for key in tuple(current):
|
|
710
|
+
if key not in updated:
|
|
711
|
+
del current[key]
|
|
712
|
+
for key, value in updated.items():
|
|
713
|
+
previous = current.get(key)
|
|
714
|
+
if isinstance(previous, dict) and isinstance(value, dict):
|
|
715
|
+
cls._merge_device_info(previous, value)
|
|
716
|
+
elif key == "sub_devices" and isinstance(previous, list) and isinstance(value, list):
|
|
717
|
+
by_name = {
|
|
718
|
+
item["device_attr_name"]: item
|
|
719
|
+
for item in previous
|
|
720
|
+
if isinstance(item, dict) and isinstance(item.get("device_attr_name"), str)
|
|
721
|
+
}
|
|
722
|
+
children = []
|
|
723
|
+
for child in value:
|
|
724
|
+
existing = by_name.get(child.get("device_attr_name"))
|
|
725
|
+
if existing is None:
|
|
726
|
+
children.append(copy.deepcopy(child))
|
|
727
|
+
else:
|
|
728
|
+
cls._merge_device_info(existing, child)
|
|
729
|
+
children.append(existing)
|
|
730
|
+
previous[:] = children
|
|
731
|
+
else:
|
|
732
|
+
current[key] = copy.deepcopy(value)
|
|
689
733
|
|
|
690
734
|
def _log_callback(self, msg, **kwargs) -> None:
|
|
691
735
|
"""
|
|
@@ -846,15 +890,17 @@ class DeviceManagerBase:
|
|
|
846
890
|
Supported types are "AsyncSignal", "AsyncMultiSignal", "DynamicSignal", "FileEventSignal", "PreviewSignal", and "ProgressSignal".
|
|
847
891
|
|
|
848
892
|
Returns:
|
|
849
|
-
list: List of tuples containing the device name, component name and
|
|
893
|
+
list: List of tuples containing the device name, component name and a copy of the
|
|
894
|
+
signal info.
|
|
850
895
|
"""
|
|
851
896
|
signals = []
|
|
852
897
|
if not isinstance(signal_type, list):
|
|
853
898
|
signal_type = [signal_type]
|
|
854
|
-
|
|
855
|
-
for
|
|
856
|
-
|
|
857
|
-
|
|
899
|
+
with self._config_lock:
|
|
900
|
+
for device in self.devices.values():
|
|
901
|
+
for comp, signal_info in device._info.get("signals", {}).items():
|
|
902
|
+
if signal_info.get("signal_class") in signal_type:
|
|
903
|
+
signals.append((device.name, comp, copy.deepcopy(signal_info)))
|
|
858
904
|
return signals
|
|
859
905
|
|
|
860
906
|
def get_device_config_cached(
|
|
@@ -911,19 +957,21 @@ class DeviceManagerBase:
|
|
|
911
957
|
device_obj = self.devices.get(dev_name)
|
|
912
958
|
if not device_obj:
|
|
913
959
|
continue
|
|
914
|
-
available_signal = device_obj._info.get("signals", {})
|
|
915
960
|
device_config = dev_conf.get("deviceConfig") or {}
|
|
916
961
|
|
|
917
962
|
for signal_name in device_config:
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
963
|
+
with self._config_lock:
|
|
964
|
+
available_signal = device_obj._info.get("signals", {})
|
|
965
|
+
signal_obj_name = available_signal.get(signal_name, {}).get("obj_name")
|
|
966
|
+
if not signal_obj_name:
|
|
967
|
+
continue
|
|
968
|
+
signal_obj = getattr(device_obj, signal_name, None)
|
|
969
|
+
if not signal_obj:
|
|
970
|
+
continue
|
|
971
|
+
sig_read = signal_obj.read(cached=True)
|
|
972
|
+
if sig_read is None:
|
|
973
|
+
continue
|
|
974
|
+
dev_conf["deviceConfig"][signal_name] = sig_read[signal_obj_name]["value"]
|
|
927
975
|
|
|
928
976
|
config[dev_name] = _DeviceModelCore(**dev_conf).model_dump(
|
|
929
977
|
exclude_defaults=exclude_defaults
|
|
@@ -346,7 +346,8 @@ class MessageEndpoints:
|
|
|
346
346
|
def device_info(device: str):
|
|
347
347
|
"""
|
|
348
348
|
Endpoint for device info. This endpoint is used by the device server to publish
|
|
349
|
-
the device info using a messages.DeviceInfoMessage message.
|
|
349
|
+
the device info using a messages.DeviceInfoMessage message. Updates are stored
|
|
350
|
+
and published so clients can refresh cached descriptions.
|
|
350
351
|
|
|
351
352
|
Args:
|
|
352
353
|
device (str): Device name, e.g. "samx".
|
|
@@ -358,7 +359,7 @@ class MessageEndpoints:
|
|
|
358
359
|
return EndpointInfo(
|
|
359
360
|
endpoint=endpoint,
|
|
360
361
|
message_type=messages.DeviceInfoMessage,
|
|
361
|
-
message_op=MessageOp.
|
|
362
|
+
message_op=MessageOp.SET_PUBLISH,
|
|
362
363
|
)
|
|
363
364
|
|
|
364
365
|
@staticmethod
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"""Receiver for ADZMQ's two-part array-1.0 wire format.
|
|
2
|
+
|
|
3
|
+
See https://github.com/paulscherrerinstitute/ADZMQ.
|
|
4
|
+
Raw arrays and JPEG are supported; other compression codecs are rejected.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import io
|
|
10
|
+
import json
|
|
11
|
+
from collections.abc import Mapping, Sequence
|
|
12
|
+
from math import isfinite, prod
|
|
13
|
+
from threading import Condition, Event, Lock, Thread
|
|
14
|
+
from typing import Literal
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
|
|
18
|
+
from bec_lib.messages import ZMQSignalTransport
|
|
19
|
+
|
|
20
|
+
from .protocol import DecodedFrame, EncodedFrame
|
|
21
|
+
|
|
22
|
+
# Matching protocol signatures and lazy optional dependency imports are intentional.
|
|
23
|
+
# pylint: disable=duplicate-code,import-outside-toplevel
|
|
24
|
+
|
|
25
|
+
_EPICS_TO_UNIX_SECONDS = 631_152_000
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _unix_timestamp(header: Mapping) -> float | None:
|
|
29
|
+
timestamp = header.get("timeStamp")
|
|
30
|
+
if not isinstance(timestamp, (int, float)) or isinstance(timestamp, bool):
|
|
31
|
+
return None
|
|
32
|
+
try:
|
|
33
|
+
timestamp = float(timestamp) + _EPICS_TO_UNIX_SECONDS
|
|
34
|
+
except OverflowError:
|
|
35
|
+
return None
|
|
36
|
+
return timestamp if isfinite(timestamp) else None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ADZMQStream:
|
|
40
|
+
"""Receive ADZMQ images; implements :class:`SignalStream`.
|
|
41
|
+
|
|
42
|
+
The proposed BEC encoder identifier is ``adzmq-array-1.0``. ADZMQ itself
|
|
43
|
+
identifies headers with ``htype=array-1.0``. There is no separate topic frame.
|
|
44
|
+
Missing byte order uses ``default_byte_order`` (little endian per array-1.0).
|
|
45
|
+
Configure this explicitly for publishers sending native big-endian memory.
|
|
46
|
+
Mailbox and lifecycle semantics follow :class:`SignalStream`.
|
|
47
|
+
The mailbox holds the latest message received by the worker; upstream ZMQ
|
|
48
|
+
and network buffers can still contain older frames. This is a preview API.
|
|
49
|
+
|
|
50
|
+
The original header's ``timeStamp`` is in seconds since the EPICS epoch
|
|
51
|
+
(1990-01-01 UTC). Frames expose it separately as ``timestamp`` in Unix seconds.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
default_byte_order (Literal["little", "big"]): Raw payload byte order
|
|
55
|
+
when neither header spelling supplies it.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(self, default_byte_order: Literal["little", "big"] = "little") -> None:
|
|
59
|
+
if default_byte_order not in ("little", "big"):
|
|
60
|
+
raise ValueError("Byte order must be little or big")
|
|
61
|
+
self._default_byte_order = default_byte_order
|
|
62
|
+
self._stop = Event()
|
|
63
|
+
self._lock = Lock()
|
|
64
|
+
self._condition = Condition()
|
|
65
|
+
self._thread: Thread | None = None
|
|
66
|
+
self._pending: tuple[bytes, ...] | None = None
|
|
67
|
+
self._failure: Exception | None = None
|
|
68
|
+
self._active = False
|
|
69
|
+
self._generation = 0
|
|
70
|
+
|
|
71
|
+
def start(self, transport: ZMQSignalTransport) -> None:
|
|
72
|
+
"""Start receiving into the latest-frame mailbox without decoding.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
transport (ZMQSignalTransport): ADZMQ connection settings.
|
|
76
|
+
"""
|
|
77
|
+
if transport.encoder != "adzmq-array-1.0":
|
|
78
|
+
raise ValueError(f"Unsupported encoder: {transport.encoder}")
|
|
79
|
+
if transport.topic:
|
|
80
|
+
raise ValueError("ADZMQ has no topic frame")
|
|
81
|
+
with self._lock:
|
|
82
|
+
if self._thread is not None and self._thread.is_alive():
|
|
83
|
+
raise RuntimeError("Stream is already running")
|
|
84
|
+
self._stop.clear()
|
|
85
|
+
with self._condition:
|
|
86
|
+
self._generation += 1
|
|
87
|
+
self._pending = None
|
|
88
|
+
self._failure = None
|
|
89
|
+
self._active = True
|
|
90
|
+
self._condition.notify_all()
|
|
91
|
+
self._thread = Thread(
|
|
92
|
+
target=self._run,
|
|
93
|
+
args=(transport.model_copy(deep=True),),
|
|
94
|
+
name="ADZMQStream",
|
|
95
|
+
daemon=True,
|
|
96
|
+
)
|
|
97
|
+
try:
|
|
98
|
+
self._thread.start()
|
|
99
|
+
except Exception:
|
|
100
|
+
with self._condition:
|
|
101
|
+
self._active = False
|
|
102
|
+
self._condition.notify_all()
|
|
103
|
+
self._thread = None
|
|
104
|
+
raise
|
|
105
|
+
|
|
106
|
+
def take_frame(self) -> EncodedFrame | None:
|
|
107
|
+
"""Wait for and take the newest encoded frame.
|
|
108
|
+
|
|
109
|
+
Header parsing occurs in the caller, after taking the frame. JPEG decoding
|
|
110
|
+
is left to the consumer; :meth:`decode` provides an optional NumPy decoder.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
EncodedFrame | None: Latest frame, or None when the stream is stopped.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
ValueError: Invalid multipart framing or JSON header.
|
|
117
|
+
Exception: Terminal socket/setup failure, retained until stop or restart.
|
|
118
|
+
"""
|
|
119
|
+
with self._condition:
|
|
120
|
+
generation = self._generation
|
|
121
|
+
self._condition.wait_for(
|
|
122
|
+
lambda: self._pending is not None
|
|
123
|
+
or self._failure is not None
|
|
124
|
+
or not self._active
|
|
125
|
+
or self._generation != generation
|
|
126
|
+
)
|
|
127
|
+
if self._generation != generation:
|
|
128
|
+
return None
|
|
129
|
+
if self._failure is not None:
|
|
130
|
+
raise self._failure.with_traceback(None)
|
|
131
|
+
parts, self._pending = self._pending, None
|
|
132
|
+
if parts is None:
|
|
133
|
+
return None
|
|
134
|
+
return self._unpack(parts)
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
def _unpack(parts: Sequence[bytes]) -> EncodedFrame:
|
|
138
|
+
if len(parts) != 2:
|
|
139
|
+
raise ValueError(f"Expected 2 ADZMQ frames, received {len(parts)}")
|
|
140
|
+
header = json.loads(parts[0])
|
|
141
|
+
if not isinstance(header, Mapping) or header.get("htype") != "array-1.0":
|
|
142
|
+
raise ValueError("Unsupported ADZMQ header type")
|
|
143
|
+
return EncodedFrame(data=parts[1], metadata=header, timestamp=_unix_timestamp(header))
|
|
144
|
+
|
|
145
|
+
def stop(self) -> None:
|
|
146
|
+
"""Clear the mailbox, wake consumers, and join the receiver."""
|
|
147
|
+
with self._lock:
|
|
148
|
+
self._stop.set()
|
|
149
|
+
with self._condition:
|
|
150
|
+
self._active = False
|
|
151
|
+
self._pending = None
|
|
152
|
+
self._failure = None
|
|
153
|
+
self._condition.notify_all()
|
|
154
|
+
if self._thread is not None:
|
|
155
|
+
self._thread.join()
|
|
156
|
+
self._thread = None
|
|
157
|
+
|
|
158
|
+
def _run(self, transport: ZMQSignalTransport) -> None:
|
|
159
|
+
try:
|
|
160
|
+
import zmq
|
|
161
|
+
|
|
162
|
+
with zmq.Context() as context:
|
|
163
|
+
socket_type = zmq.SUB if transport.socket_type == "sub" else zmq.PULL
|
|
164
|
+
with context.socket(socket_type) as socket:
|
|
165
|
+
socket.setsockopt(zmq.LINGER, 0)
|
|
166
|
+
socket.setsockopt(zmq.RCVHWM, 2)
|
|
167
|
+
if socket_type == zmq.SUB:
|
|
168
|
+
socket.setsockopt(zmq.SUBSCRIBE, b"")
|
|
169
|
+
if transport.connection_mode == "bind":
|
|
170
|
+
socket.bind(transport.address)
|
|
171
|
+
else:
|
|
172
|
+
socket.connect(transport.address)
|
|
173
|
+
while not self._stop.is_set():
|
|
174
|
+
if not socket.poll(timeout=100):
|
|
175
|
+
continue
|
|
176
|
+
parts = tuple(socket.recv_multipart())
|
|
177
|
+
with self._condition:
|
|
178
|
+
if self._stop.is_set():
|
|
179
|
+
break
|
|
180
|
+
self._pending = parts
|
|
181
|
+
self._condition.notify_all()
|
|
182
|
+
except Exception as exc: # pylint: disable=broad-exception-caught
|
|
183
|
+
with self._condition:
|
|
184
|
+
if not self._stop.is_set():
|
|
185
|
+
self._failure = exc.with_traceback(None)
|
|
186
|
+
finally:
|
|
187
|
+
with self._condition:
|
|
188
|
+
self._active = False
|
|
189
|
+
self._condition.notify_all()
|
|
190
|
+
|
|
191
|
+
def decode(self, parts: EncodedFrame | Sequence[bytes]) -> DecodedFrame:
|
|
192
|
+
"""Decode one complete ADZMQ message, retaining its original header.
|
|
193
|
+
|
|
194
|
+
Args:
|
|
195
|
+
parts (EncodedFrame | Sequence[bytes]): Frame from ``take_frame`` or
|
|
196
|
+
a complete multipart message (JSON header and binary image payload).
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
DecodedFrame: Image array and original publisher metadata. Raw RGB2
|
|
200
|
+
and RGB3 images (``ndattr.ColorMode`` 3 and 4) and color JPEG
|
|
201
|
+
images decode with channels last (height, width, 3).
|
|
202
|
+
The metadata retains the publisher's original shape.
|
|
203
|
+
|
|
204
|
+
Raises:
|
|
205
|
+
ValueError: Invalid header, payload size, or unsupported compression.
|
|
206
|
+
"""
|
|
207
|
+
frame = parts if isinstance(parts, EncodedFrame) else self._unpack(parts)
|
|
208
|
+
header, payload = frame.metadata, frame.data
|
|
209
|
+
if not isinstance(header, Mapping) or header.get("htype") != "array-1.0":
|
|
210
|
+
raise ValueError("Unsupported ADZMQ header type")
|
|
211
|
+
shape = header.get("shape")
|
|
212
|
+
if not isinstance(shape, list) or any(
|
|
213
|
+
not isinstance(dim, int) or isinstance(dim, bool) or dim < 0 for dim in shape
|
|
214
|
+
):
|
|
215
|
+
raise ValueError("Shape must be a list of non-negative integers")
|
|
216
|
+
encoding = header.get("encoding", "")
|
|
217
|
+
if encoding == "jpeg":
|
|
218
|
+
data = self._decode_jpeg(payload, shape, header.get("ndattr"))
|
|
219
|
+
elif encoding == "":
|
|
220
|
+
allowed_types = {
|
|
221
|
+
"int8",
|
|
222
|
+
"uint8",
|
|
223
|
+
"int16",
|
|
224
|
+
"uint16",
|
|
225
|
+
"int32",
|
|
226
|
+
"uint32",
|
|
227
|
+
"int64",
|
|
228
|
+
"uint64",
|
|
229
|
+
"float16",
|
|
230
|
+
"float32",
|
|
231
|
+
"float64",
|
|
232
|
+
}
|
|
233
|
+
dtype_name = header.get("type")
|
|
234
|
+
if not isinstance(dtype_name, str) or dtype_name not in allowed_types:
|
|
235
|
+
raise ValueError("Unsupported image dtype")
|
|
236
|
+
byte_order = header.get("endianness", header.get("endianess", self._default_byte_order))
|
|
237
|
+
if byte_order not in ("little", "big"):
|
|
238
|
+
raise ValueError(f"Unsupported byte order: {byte_order}")
|
|
239
|
+
dtype = np.dtype(header["type"]).newbyteorder("<" if byte_order == "little" else ">")
|
|
240
|
+
if len(payload) != prod(shape) * dtype.itemsize:
|
|
241
|
+
raise ValueError("Payload size does not match header shape and dtype")
|
|
242
|
+
data = np.frombuffer(payload, dtype=dtype).reshape(tuple(shape))
|
|
243
|
+
ndattrs = header.get("ndattr")
|
|
244
|
+
if data.ndim == 3 and isinstance(ndattrs, Mapping):
|
|
245
|
+
color_mode = ndattrs.get("ColorMode")
|
|
246
|
+
if color_mode == 3: # RGB2: height, channels, width
|
|
247
|
+
data = np.moveaxis(data, 1, -1)
|
|
248
|
+
elif color_mode == 4: # RGB3: channels, height, width
|
|
249
|
+
data = np.moveaxis(data, 0, -1)
|
|
250
|
+
else:
|
|
251
|
+
raise ValueError(f"Unsupported ADZMQ encoding: {encoding}")
|
|
252
|
+
data.setflags(write=False)
|
|
253
|
+
timestamp = frame.timestamp
|
|
254
|
+
if timestamp is None:
|
|
255
|
+
timestamp = _unix_timestamp(header)
|
|
256
|
+
return DecodedFrame(data=data, metadata=header, timestamp=timestamp)
|
|
257
|
+
|
|
258
|
+
@staticmethod
|
|
259
|
+
def _decode_jpeg(payload: bytes, shape: list[int], ndattrs: object) -> np.ndarray:
|
|
260
|
+
from PIL import Image
|
|
261
|
+
|
|
262
|
+
with Image.open(io.BytesIO(payload)) as image:
|
|
263
|
+
if image.format != "JPEG":
|
|
264
|
+
raise ValueError("Expected JPEG payload")
|
|
265
|
+
data = np.array(image)
|
|
266
|
+
|
|
267
|
+
# ADZMQ retains the original NDArray layout, while Pillow decodes RGB as HWC.
|
|
268
|
+
if len(shape) == 3 and isinstance(ndattrs, Mapping):
|
|
269
|
+
color_mode = ndattrs.get("ColorMode")
|
|
270
|
+
if color_mode == 3: # RGB2: height, channels, width
|
|
271
|
+
shape = [shape[0], shape[2], shape[1]]
|
|
272
|
+
elif color_mode == 4: # RGB3: channels, height, width
|
|
273
|
+
shape = [shape[1], shape[2], shape[0]]
|
|
274
|
+
if data.shape != tuple(shape):
|
|
275
|
+
raise ValueError("JPEG dimensions do not match header shape")
|
|
276
|
+
return data
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Pull-based plugin interface for latest-frame preview streams."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any, Protocol
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from bec_lib.messages import ZMQSignalTransport
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class EncodedFrame:
|
|
16
|
+
"""Publisher metadata and encoded image bytes, owned independently of the socket.
|
|
17
|
+
|
|
18
|
+
JPEG payloads remain compressed. Raw payloads retain their wire byte order.
|
|
19
|
+
Metadata must be treated as read-only by consumers.
|
|
20
|
+
The timestamp is in Unix seconds, or None if unavailable.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
data: bytes
|
|
24
|
+
metadata: Mapping[str, Any]
|
|
25
|
+
timestamp: float | None = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class DecodedFrame:
|
|
30
|
+
"""Read-only image and publisher metadata, with a timestamp in Unix seconds if available."""
|
|
31
|
+
|
|
32
|
+
data: np.ndarray
|
|
33
|
+
metadata: Mapping[str, Any]
|
|
34
|
+
timestamp: float | None = None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SignalStream(Protocol):
|
|
38
|
+
"""Single-consumer preview stream backed by a one-frame mailbox.
|
|
39
|
+
|
|
40
|
+
Use a concrete implementation such as ``ADZMQStream``; this protocol defines
|
|
41
|
+
the consumer-facing interface and is not instantiated directly. Choose the
|
|
42
|
+
implementation matching the transport descriptor's ``encoder``. Pass the
|
|
43
|
+
advertised descriptor to ``start``; its address, socket type, connection mode,
|
|
44
|
+
and topic describe the receiving side of the connection.
|
|
45
|
+
Install ``bec_lib[adzmq]`` for the ADZMQ receiver and JPEG decoder dependencies.
|
|
46
|
+
|
|
47
|
+
Receiving and consuming:
|
|
48
|
+
``start`` launches a background receiver and returns without waiting for a
|
|
49
|
+
publisher or first frame. The receiver replaces the pending message whenever
|
|
50
|
+
another arrives. ``take_frame`` waits without polling, atomically removes
|
|
51
|
+
the newest pending frame, and returns it. Call it again when ready for the
|
|
52
|
+
next frame. There is one consumer per stream instance; consumers sharing an
|
|
53
|
+
instance would compete for frames rather than each receive a copy.
|
|
54
|
+
|
|
55
|
+
This is a preview interface: intermediate frames may be discarded, and
|
|
56
|
+
there is no replay or lossless acquisition guarantee. The mailbox contains
|
|
57
|
+
the latest message received locally, not necessarily the publisher's latest
|
|
58
|
+
message: upstream socket and network buffers may still contain older data.
|
|
59
|
+
To limit processing to a desired refresh rate, pace the consumer before its
|
|
60
|
+
next read. A tight read/decode loop processes frames as fast as it can.
|
|
61
|
+
|
|
62
|
+
Decoding and GUI use:
|
|
63
|
+
No image decoding occurs in the receiver. ``EncodedFrame.data`` contains
|
|
64
|
+
the payload bytes and ``metadata`` describes their format. JPEG stays
|
|
65
|
+
compressed; raw arrays retain the advertised dtype, shape, and byte order.
|
|
66
|
+
Call ``decode`` after taking a frame to obtain a NumPy image. Consumers
|
|
67
|
+
that can use the encoded payload directly can skip decoding.
|
|
68
|
+
|
|
69
|
+
``take_frame`` can wait indefinitely, so call it in a consumer thread,
|
|
70
|
+
never in a GUI timer or the GUI thread. Process/decode there and deliver
|
|
71
|
+
results to the GUI through its thread-safe dispatch mechanism. Bound or
|
|
72
|
+
coalesce pending display updates as well; queuing every decoded image can
|
|
73
|
+
create a second backlog even though the transport mailbox is bounded.
|
|
74
|
+
|
|
75
|
+
Errors and shutdown:
|
|
76
|
+
Receiver setup and socket failures are raised by ``take_frame``, rather
|
|
77
|
+
than reported by a callback. Terminal failures remain observable until
|
|
78
|
+
``stop`` or restart; exit the consumer loop on these errors instead of
|
|
79
|
+
retrying in a tight loop. Malformed individual frames can also raise;
|
|
80
|
+
recovery policy belongs to the consumer.
|
|
81
|
+
|
|
82
|
+
Always call ``stop`` in a finally block. It clears the mailbox, releases
|
|
83
|
+
receiver resources, and wakes waiting consumers, whose read returns None.
|
|
84
|
+
Reads before start or after stop also return None. Lifecycle calls are
|
|
85
|
+
serialized, and restarting begins with an empty mailbox. A frame already
|
|
86
|
+
taken remains valid after stop; stop does not wait for consumer processing,
|
|
87
|
+
so join your consumer thread separately.
|
|
88
|
+
|
|
89
|
+
Examples:
|
|
90
|
+
Run an ADZMQ receiver and decode images in a consumer thread. The main
|
|
91
|
+
thread remains free to stop the stream even when no publisher is sending::
|
|
92
|
+
|
|
93
|
+
import logging
|
|
94
|
+
from threading import Thread
|
|
95
|
+
|
|
96
|
+
from bec_lib.messages import ZMQSignalTransport
|
|
97
|
+
from bec_lib.zmq_transports import ADZMQStream, SignalStream
|
|
98
|
+
|
|
99
|
+
stream: SignalStream = ADZMQStream()
|
|
100
|
+
transport = ZMQSignalTransport(
|
|
101
|
+
address="tcp://127.0.0.1:1234",
|
|
102
|
+
socket_type="sub",
|
|
103
|
+
connection_mode="connect",
|
|
104
|
+
encoder="adzmq-array-1.0",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def consume():
|
|
108
|
+
try:
|
|
109
|
+
while True:
|
|
110
|
+
frame = stream.take_frame()
|
|
111
|
+
if frame is None:
|
|
112
|
+
return
|
|
113
|
+
image = stream.decode(frame)
|
|
114
|
+
print(image.data.shape, image.metadata.get("frame"))
|
|
115
|
+
except Exception:
|
|
116
|
+
logging.exception("Image consumer stopped after a stream/decode error")
|
|
117
|
+
|
|
118
|
+
consumer = Thread(target=consume)
|
|
119
|
+
try:
|
|
120
|
+
stream.start(transport)
|
|
121
|
+
consumer.start()
|
|
122
|
+
input("Press Enter to stop receiving images: ")
|
|
123
|
+
finally:
|
|
124
|
+
stream.stop()
|
|
125
|
+
if consumer.ident is not None:
|
|
126
|
+
consumer.join()
|
|
127
|
+
|
|
128
|
+
If the consumer accepts JPEG bytes directly, use ``frame.data`` and
|
|
129
|
+
``frame.metadata`` instead of calling ``stream.decode``.
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
def start(self, transport: ZMQSignalTransport) -> None:
|
|
133
|
+
"""Start receiving asynchronously; an active stream raises RuntimeError.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
transport (ZMQSignalTransport): Receiver connection descriptor.
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
def take_frame(self) -> EncodedFrame | None:
|
|
140
|
+
"""Wait for, atomically take, and clear the newest pending frame.
|
|
141
|
+
|
|
142
|
+
This blocks until a frame, terminal failure, or stop notification arrives.
|
|
143
|
+
It does not decode the payload and must not run in a GUI event loop.
|
|
144
|
+
A subsequent call waits again if no newer frame has arrived.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
EncodedFrame | None: Latest frame, or None when the stream is stopped.
|
|
148
|
+
|
|
149
|
+
Raises:
|
|
150
|
+
Exception: Receiver failure or malformed frame. Terminal receiver
|
|
151
|
+
failures remain available until stop or restart.
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
def decode(self, frame: EncodedFrame, /) -> DecodedFrame:
|
|
155
|
+
"""Decode a frame into a NumPy image while retaining its publisher metadata.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
frame (EncodedFrame): Frame returned by ``take_frame``.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
DecodedFrame: Decoded image and publisher metadata.
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
def stop(self) -> None:
|
|
165
|
+
"""Stop receiving, clear the mailbox, and join the worker; safe to repeat."""
|