From ad30ab9b73e3f1cfb2d6ab7ad737188a6bce238a Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 18:09:20 +0530 Subject: [PATCH 01/22] update ni fgen example --- examples/nifgen_standard_function/_helpers.py | 26 +++++++++- .../_nifgen_helpers.py | 38 ++++++++++++++ .../nifgen_standard_function/measurement.py | 50 +++---------------- 3 files changed, 70 insertions(+), 44 deletions(-) create mode 100644 examples/nifgen_standard_function/_nifgen_helpers.py diff --git a/examples/nifgen_standard_function/_helpers.py b/examples/nifgen_standard_function/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/nifgen_standard_function/_helpers.py +++ b/examples/nifgen_standard_function/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/nifgen_standard_function/_nifgen_helpers.py b/examples/nifgen_standard_function/_nifgen_helpers.py new file mode 100644 index 000000000..d11aa2f65 --- /dev/null +++ b/examples/nifgen_standard_function/_nifgen_helpers.py @@ -0,0 +1,38 @@ +"""nifgen Helper classes and functions for MeasurementLink examples.""" + +from typing import Any, Dict + +import grpc +import nifgen + +import ni_measurementlink_service as nims + +# To use a physical NI-FGen instrument, set this to False or specify +# --no-use-simulation on the command line. +USE_SIMULATION = True + + +def _create_nifgen_session( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel = None, + initialization_behavior=nifgen.SessionInitializationBehavior.AUTO, +) -> nifgen.Session: + options: Dict[str, Any] = {} + if USE_SIMULATION: + options["simulate"] = True + options["driver_setup"] = {"Model": "5423 (2CH)"} + + session_kwargs: Dict[str, Any] = {} + if session_grpc_channel is not None: + # Assumption: the pin map specifies one NI-FGEN session per instrument. If the pin map + # specified an NI-FGEN session per channel, the session name would need to include the + # channel name(s). + session_kwargs["grpc_options"] = nifgen.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) + + return nifgen.Session( + session_info.resource_name, session_info.channel_list, options=options, **session_kwargs + ) diff --git a/examples/nifgen_standard_function/measurement.py b/examples/nifgen_standard_function/measurement.py index f62096608..69b750533 100644 --- a/examples/nifgen_standard_function/measurement.py +++ b/examples/nifgen_standard_function/measurement.py @@ -4,7 +4,7 @@ import logging import pathlib import time -from typing import Any, Dict, Tuple +from typing import Tuple import click import grpc @@ -13,12 +13,15 @@ from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, str_to_enum, use_simulation_option, verbosity_option, ) +from _nifgen_helpers import _create_nifgen_session import ni_measurementlink_service as nims @@ -85,12 +88,7 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( @@ -105,8 +103,9 @@ def cancel_callback(): ) ) + grpc_device_channel = get_grpc_device_channel(measurement_service, nifgen) sessions = [ - stack.enter_context(_create_nifgen_session(session_info)) + stack.enter_context(_create_nifgen_session(session_info, grpc_device_channel)) for session_info in reservation.session_info ] @@ -165,41 +164,6 @@ def cancel_callback(): return () -def _create_nifgen_session( - session_info: nims.session_management.SessionInformation, -) -> nifgen.Session: - options: Dict[str, Any] = {} - if service_options.use_simulation: - options["simulate"] = True - options["driver_setup"] = {"Model": "5423 (2CH)"} - - session_kwargs: Dict[str, Any] = {} - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=nifgen.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - # Assumption: the pin map specifies one NI-FGEN session per instrument. If the pin map - # specified an NI-FGEN session per channel, the session name would need to include the - # channel name(s). - session_kwargs["grpc_options"] = nifgen.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=nifgen.SessionInitializationBehavior.AUTO, - ) - - return nifgen.Session( - session_info.resource_name, session_info.channel_list, options=options, **session_kwargs - ) - - @click.command @verbosity_option @grpc_device_options From d2a4449299d1c978520cf6b18d670821a83d5ba9 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 18:23:29 +0530 Subject: [PATCH 02/22] update the scope example --- examples/niscope_acquire_waveform/_helpers.py | 26 ++++++++++- .../_niscope_helpers.py | 34 ++++++++++++++ .../niscope_acquire_waveform/measurement.py | 45 +++---------------- 3 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 examples/niscope_acquire_waveform/_niscope_helpers.py diff --git a/examples/niscope_acquire_waveform/_helpers.py b/examples/niscope_acquire_waveform/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/niscope_acquire_waveform/_helpers.py +++ b/examples/niscope_acquire_waveform/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/niscope_acquire_waveform/_niscope_helpers.py b/examples/niscope_acquire_waveform/_niscope_helpers.py new file mode 100644 index 000000000..498ec2632 --- /dev/null +++ b/examples/niscope_acquire_waveform/_niscope_helpers.py @@ -0,0 +1,34 @@ +"""niscope Helper classes and functions for MeasurementLink examples.""" + +from typing import Any, Dict + +import grpc +import niscope + +import ni_measurementlink_service as nims + +# To use a physical NI-SCOPE instrument, set this to False or specify +# --no-use-simulation on the command line. +USE_SIMULATION = True + + +def _create_niscope_session( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel = None, + initialization_behavior=niscope.SessionInitializationBehavior.AUTO, +) -> niscope.Session: + options: Dict[str, Any] = {} + if USE_SIMULATION: + options["simulate"] = True + options["driver_setup"] = {"Model": "5162 (4CH)"} + + session_kwargs: Dict[str, Any] = {} + + if session_grpc_channel is not None: + session_kwargs["grpc_options"] = niscope.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=niscope.SessionInitializationBehavior.AUTO, + ) + + return niscope.Session(session_info.resource_name, options=options, **session_kwargs) diff --git a/examples/niscope_acquire_waveform/measurement.py b/examples/niscope_acquire_waveform/measurement.py index 77e5876c4..958997bbd 100644 --- a/examples/niscope_acquire_waveform/measurement.py +++ b/examples/niscope_acquire_waveform/measurement.py @@ -4,7 +4,7 @@ import logging import pathlib import time -from typing import Any, Dict, Tuple +from typing import Tuple import click import grpc @@ -12,12 +12,15 @@ from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, str_to_enum, use_simulation_option, verbosity_option, ) +from _niscope_helpers import _create_niscope_session import ni_measurementlink_service as nims @@ -112,12 +115,7 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( @@ -146,7 +144,8 @@ def cancel_callback(): if trigger_source in pin_to_channel: trigger_source = pin_to_channel[trigger_source] - session = stack.enter_context(_create_niscope_session(session_info)) + grpc_device_channel = get_grpc_device_channel(measurement_service, niscope) + session = stack.enter_context(_create_niscope_session(session_info, grpc_device_channel)) session.channels[""].channel_enabled = False session.channels[channel_names].configure_vertical( vertical_range, @@ -196,36 +195,6 @@ def cancel_callback(): return () -def _create_niscope_session( - session_info: nims.session_management.SessionInformation, -) -> niscope.Session: - options: Dict[str, Any] = {} - if service_options.use_simulation: - options["simulate"] = True - options["driver_setup"] = {"Model": "5162 (4CH)"} - - session_kwargs: Dict[str, Any] = {} - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=niscope.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - session_kwargs["grpc_options"] = niscope.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=niscope.SessionInitializationBehavior.AUTO, - ) - - return niscope.Session(session_info.resource_name, options=options, **session_kwargs) - - @click.command @verbosity_option @grpc_device_options From 774f0068a487a59b88b9e2565d0d6c72676bf642 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 18:46:03 +0530 Subject: [PATCH 03/22] nidigital example updates --- examples/nidigital_spi/_helpers.py | 26 ++++++++++- examples/nidigital_spi/_nidigital_helpers.py | 33 ++++++++++++++ examples/nidigital_spi/measurement.py | 47 ++++---------------- 3 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 examples/nidigital_spi/_nidigital_helpers.py diff --git a/examples/nidigital_spi/_helpers.py b/examples/nidigital_spi/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/nidigital_spi/_helpers.py +++ b/examples/nidigital_spi/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/nidigital_spi/_nidigital_helpers.py b/examples/nidigital_spi/_nidigital_helpers.py new file mode 100644 index 000000000..e764c01b4 --- /dev/null +++ b/examples/nidigital_spi/_nidigital_helpers.py @@ -0,0 +1,33 @@ +"""nidigital Helper classes and functions for MeasurementLink examples.""" + +from typing import Any, Dict + +import grpc +import nidigital + +import ni_measurementlink_service as nims + +# To use a physical NI-DIGITAL instrument, set this to False or specify +# --no-use-simulation on the command line. +USE_SIMULATION = True + + +def _create_nidigital_session( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel=None, + initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, +) -> nidigital.Session: + options: Dict[str, Any] = {} + if USE_SIMULATION: + options["simulate"] = True + options["driver_setup"] = {"Model": "6570"} + + session_kwargs: Dict[str, Any] = {} + if session_grpc_channel is not None: + session_kwargs["grpc_options"] = nidigital.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) + + return nidigital.Session(session_info.resource_name, options=options, **session_kwargs) diff --git a/examples/nidigital_spi/measurement.py b/examples/nidigital_spi/measurement.py index 20a419ea8..4f9eb907c 100644 --- a/examples/nidigital_spi/measurement.py +++ b/examples/nidigital_spi/measurement.py @@ -3,7 +3,7 @@ import contextlib import logging import pathlib -from typing import Any, Dict, Iterable, Tuple, Union +from typing import Iterable, Tuple, Union import click import grpc @@ -11,11 +11,14 @@ from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, - verbosity_option, use_simulation_option, + verbosity_option, ) +from _nidigital_helpers import _create_nidigital_session import ni_measurementlink_service as nims @@ -58,12 +61,7 @@ def measure( """Test a SPI device using an NI Digital Pattern instrument.""" logging.info("Starting test: pin_names=%s", pin_names) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( @@ -85,7 +83,8 @@ def measure( ) session_info = reservation.session_info[0] - session = stack.enter_context(_create_nidigital_session(session_info)) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital) + session = stack.enter_context(_create_nidigital_session(session_info, grpc_device_channel)) pin_map_context = measurement_service.context.pin_map_context selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) @@ -118,36 +117,6 @@ def measure( return (passing_sites, failing_sites) -def _create_nidigital_session( - session_info: nims.session_management.SessionInformation, -) -> nidigital.Session: - options: Dict[str, Any] = {} - if service_options.use_simulation: - options["simulate"] = True - options["driver_setup"] = {"Model": "6570"} - - session_kwargs: Dict[str, Any] = {} - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=nidigital.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - session_kwargs["grpc_options"] = nidigital.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, - ) - - return nidigital.Session(session_info.resource_name, options=options, **session_kwargs) - - def _resolve_relative_path( directory_path: pathlib.Path, file_path: Union[str, pathlib.Path] ) -> pathlib.Path: From 575dd04bf7c0156320edafac9c23c4237a6bb43b Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 18:57:51 +0530 Subject: [PATCH 04/22] update nidigital examples --- examples/nidmm_measurement/_helpers.py | 26 ++++++++++- examples/nidmm_measurement/_nidmm_helpers.py | 33 ++++++++++++++ examples/nidmm_measurement/measurement.py | 47 ++++---------------- 3 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 examples/nidmm_measurement/_nidmm_helpers.py diff --git a/examples/nidmm_measurement/_helpers.py b/examples/nidmm_measurement/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/nidmm_measurement/_helpers.py +++ b/examples/nidmm_measurement/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/nidmm_measurement/_nidmm_helpers.py b/examples/nidmm_measurement/_nidmm_helpers.py new file mode 100644 index 000000000..a3d5dcaef --- /dev/null +++ b/examples/nidmm_measurement/_nidmm_helpers.py @@ -0,0 +1,33 @@ +"""nidmm Helper classes and functions for MeasurementLink examples.""" + +from typing import Any, Dict + +import grpc +import nidmm + +import ni_measurementlink_service as nims + +# To use a physical NI-DMM instrument, set this to False or specify +# --no-use-simulation on the command line. +USE_SIMULATION = True + + +def _create_nidmm_session( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel, + initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, +) -> nidmm.Session: + options: Dict[str, Any] = {} + if USE_SIMULATION: + options["simulate"] = True + options["driver_setup"] = {"Model": "4081"} + + session_kwargs: Dict[str, Any] = {} + + session_kwargs["grpc_options"] = nidmm.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) + + return nidmm.Session(session_info.resource_name, options=options, **session_kwargs) diff --git a/examples/nidmm_measurement/measurement.py b/examples/nidmm_measurement/measurement.py index 7f9fdcfcb..baf4d3cb4 100644 --- a/examples/nidmm_measurement/measurement.py +++ b/examples/nidmm_measurement/measurement.py @@ -4,7 +4,7 @@ import logging import math import pathlib -from typing import Any, Dict, Tuple +from typing import Tuple import click import grpc @@ -12,12 +12,15 @@ from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, str_to_enum, - verbosity_option, use_simulation_option, + verbosity_option, ) +from _nidmm_helpers import _create_nidmm_session import ni_measurementlink_service as nims @@ -79,12 +82,7 @@ def measure( resolution_digits, ) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( @@ -106,7 +104,8 @@ def measure( ) session_info = reservation.session_info[0] - session = stack.enter_context(_create_nidmm_session(session_info)) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidmm) + session = stack.enter_context(_create_nidmm_session(session_info, grpc_device_channel)) session.configure_measurement_digits( str_to_enum(FUNCTION_TO_ENUM, measurement_type), range, @@ -125,36 +124,6 @@ def measure( return (measured_value, signal_out_of_range, absolute_resolution) -def _create_nidmm_session( - session_info: nims.session_management.SessionInformation, -) -> nidmm.Session: - options: Dict[str, Any] = {} - if service_options.use_simulation: - options["simulate"] = True - options["driver_setup"] = {"Model": "4081"} - - session_kwargs: Dict[str, Any] = {} - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=nidmm.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - session_kwargs["grpc_options"] = nidmm.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, - ) - - return nidmm.Session(session_info.resource_name, options=options, **session_kwargs) - - @click.command @verbosity_option @grpc_device_options From fc37dd002b3f789105e99fd8d982a438d0d2648c Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 19:07:16 +0530 Subject: [PATCH 05/22] update niswitch helpers --- examples/nidigital_spi/_nidigital_helpers.py | 2 +- examples/niswitch_control_relays/_helpers.py | 26 +++++++++- .../_niswitch_helpers.py | 34 ++++++++++++++ .../niswitch_control_relays/measurement.py | 47 +++---------------- 4 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 examples/niswitch_control_relays/_niswitch_helpers.py diff --git a/examples/nidigital_spi/_nidigital_helpers.py b/examples/nidigital_spi/_nidigital_helpers.py index e764c01b4..db9db7360 100644 --- a/examples/nidigital_spi/_nidigital_helpers.py +++ b/examples/nidigital_spi/_nidigital_helpers.py @@ -14,7 +14,7 @@ def _create_nidigital_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel=None, + session_grpc_channel: grpc.Channel = None, initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, ) -> nidigital.Session: options: Dict[str, Any] = {} diff --git a/examples/niswitch_control_relays/_helpers.py b/examples/niswitch_control_relays/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/niswitch_control_relays/_helpers.py +++ b/examples/niswitch_control_relays/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/niswitch_control_relays/_niswitch_helpers.py b/examples/niswitch_control_relays/_niswitch_helpers.py new file mode 100644 index 000000000..4aa9752eb --- /dev/null +++ b/examples/niswitch_control_relays/_niswitch_helpers.py @@ -0,0 +1,34 @@ +"""niswitch Helper classes and functions for MeasurementLink examples.""" + +from typing import Any, Dict + +import grpc +import niswitch + +import ni_measurementlink_service as nims + +# To use a physical NI-SWITCH instrument, set this to False or specify +# --no-use-simulation on the command line. +USE_SIMULATION = True + + +def _create_niswitch_session( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel = None, + initialization_behavior=niswitch.SessionInitializationBehavior.AUTO, +) -> niswitch.Session: + resource_name = session_info.resource_name + session_kwargs: Dict[str, Any] = {} + if USE_SIMULATION: + resource_name = "" + session_kwargs["simulate"] = True + session_kwargs["topology"] = "2567/Independent" + if session_grpc_channel is None: + session_kwargs["grpc_options"] = niswitch.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=niswitch.SessionInitializationBehavior.AUTO, + ) + + # This uses the topology configured in MAX. + return niswitch.Session(resource_name, **session_kwargs) diff --git a/examples/niswitch_control_relays/measurement.py b/examples/niswitch_control_relays/measurement.py index e5b543d48..2ef771efb 100644 --- a/examples/niswitch_control_relays/measurement.py +++ b/examples/niswitch_control_relays/measurement.py @@ -3,18 +3,21 @@ import contextlib import logging import pathlib -from typing import Any, Dict, Tuple +from typing import Tuple import click import niswitch from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, use_simulation_option, verbosity_option, ) +from _niswitch_helpers import _create_niswitch_session import ni_measurementlink_service as nims @@ -45,12 +48,7 @@ def measure( close_relays, ) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: relay_list = [r.strip() for r in relay_names.split(",")] @@ -66,8 +64,9 @@ def measure( ) ) + grpc_device_channel = get_grpc_device_channel(measurement_service, niswitch) sessions = [ - stack.enter_context(_create_niswitch_session(session_info)) + stack.enter_context(_create_niswitch_session(session_info, grpc_device_channel)) for session_info in reservation.session_info ] @@ -83,38 +82,6 @@ def measure( return () -def _create_niswitch_session( - session_info: nims.session_management.SessionInformation, -) -> niswitch.Session: - resource_name = session_info.resource_name - session_kwargs: Dict[str, Any] = {} - if service_options.use_simulation: - resource_name = "" - session_kwargs["simulate"] = True - session_kwargs["topology"] = "2567/Independent" - - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=niswitch.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - session_kwargs["grpc_options"] = niswitch.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=niswitch.SessionInitializationBehavior.AUTO, - ) - - # This uses the topology configured in MAX. - return niswitch.Session(resource_name, **session_kwargs) - - @click.command @verbosity_option @grpc_device_options From 70afe18974fc9890edb5fd46f64225d50c1a2e20 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 19:25:27 +0530 Subject: [PATCH 06/22] nidaqmx example update --- examples/nidaqmx_analog_input/_helpers.py | 26 ++++++++++++- .../nidaqmx_analog_input/_nidaqmx_helpers.py | 23 +++++++++++ examples/nidaqmx_analog_input/measurement.py | 38 +++---------------- 3 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 examples/nidaqmx_analog_input/_nidaqmx_helpers.py diff --git a/examples/nidaqmx_analog_input/_helpers.py b/examples/nidaqmx_analog_input/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/nidaqmx_analog_input/_helpers.py +++ b/examples/nidaqmx_analog_input/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py new file mode 100644 index 000000000..9ed4991cf --- /dev/null +++ b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py @@ -0,0 +1,23 @@ +"""nidaqmx Helper classes and functions for MeasurementLink examples.""" + +import grpc +import nidaqmx + +import ni_measurementlink_service as nims + + +def _create_nidaqmx_task( + session_info: nims.session_management.SessionInformation, + session_grpc_channel: grpc.Channel=None, + initialization_behavior=nidaqmx.SessionInitializationBehavior.AUTO, +) -> nidaqmx.Task: + session_kwargs = {} + + if session_grpc_channel is not None: + session_kwargs["grpc_options"] = nidaqmx.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) + + return nidaqmx.Task(new_task_name=session_info.session_name, **session_kwargs) diff --git a/examples/nidaqmx_analog_input/measurement.py b/examples/nidaqmx_analog_input/measurement.py index 5727d69fc..c04676acb 100644 --- a/examples/nidaqmx_analog_input/measurement.py +++ b/examples/nidaqmx_analog_input/measurement.py @@ -11,10 +11,13 @@ from _helpers import ( ServiceOptions, configure_logging, + create_session_management_client, + get_grpc_device_channel, get_service_options, grpc_device_options, verbosity_option, ) +from _nidaqmx_helpers import _create_nidaqmx_task from nidaqmx.constants import TaskMode import ni_measurementlink_service as nims @@ -46,12 +49,7 @@ def measure(pin_name, sample_rate, number_of_samples): sample_rate, number_of_samples, ) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( session_management_client.reserve_sessions( @@ -80,7 +78,8 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) - task = stack.enter_context(_create_nidaqmx_task(session_info)) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidaqmx) + task = stack.enter_context(_create_nidaqmx_task(session_info, grpc_device_channel)) if not session_info.session_exists: task.ai_channels.add_ai_voltage_chan(session_info.channel_list) @@ -98,31 +97,6 @@ def cancel_callback(): return (voltage_values,) -def _create_nidaqmx_task( - session_info: nims.session_management.SessionInformation, -) -> nidaqmx.Task: - session_kwargs = {} - if service_options.use_grpc_device: - session_grpc_address = service_options.grpc_device_address - - if not session_grpc_address: - session_grpc_channel = measurement_service.get_channel( - provided_interface=nidaqmx.GRPC_SERVICE_INTERFACE_NAME, - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) - else: - session_grpc_channel = measurement_service.channel_pool.get_channel( - target=session_grpc_address - ) - session_kwargs["grpc_options"] = nidaqmx.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=nidaqmx.SessionInitializationBehavior.AUTO, - ) - - return nidaqmx.Task(new_task_name=session_info.session_name, **session_kwargs) - - def _log_measured_values(samples, max_samples_to_display=5): """Log the measured values.""" if len(samples) > max_samples_to_display: From 3ed4370ec0bc7dc1e4de9d677d95a1834bc58ccb Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 7 Jun 2023 19:33:51 +0530 Subject: [PATCH 07/22] udpate the visa dmm example --- examples/nidmm_measurement/_nidmm_helpers.py | 13 +++++----- examples/nivisa_dmm_measurement/_helpers.py | 26 ++++++++++++++++++- .../nivisa_dmm_measurement/measurement.py | 8 ++---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/examples/nidmm_measurement/_nidmm_helpers.py b/examples/nidmm_measurement/_nidmm_helpers.py index a3d5dcaef..9fed2edee 100644 --- a/examples/nidmm_measurement/_nidmm_helpers.py +++ b/examples/nidmm_measurement/_nidmm_helpers.py @@ -14,7 +14,7 @@ def _create_nidmm_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel, + session_grpc_channel: grpc.Channel=None, initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, ) -> nidmm.Session: options: Dict[str, Any] = {} @@ -24,10 +24,11 @@ def _create_nidmm_session( session_kwargs: Dict[str, Any] = {} - session_kwargs["grpc_options"] = nidmm.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=initialization_behavior, - ) + if session_grpc_channel is not None: + session_kwargs["grpc_options"] = nidmm.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) return nidmm.Session(session_info.resource_name, options=options, **session_kwargs) diff --git a/examples/nivisa_dmm_measurement/_helpers.py b/examples/nivisa_dmm_measurement/_helpers.py index 5d87916b2..d0bdc94b1 100644 --- a/examples/nivisa_dmm_measurement/_helpers.py +++ b/examples/nivisa_dmm_measurement/_helpers.py @@ -2,18 +2,20 @@ import logging import pathlib +import types from typing import Any, Callable, Dict, NamedTuple, TypeVar import click import grpc +import ni_measurementlink_service as nims from ni_measurementlink_service import session_management from ni_measurementlink_service._internal.discovery_client import DiscoveryClient from ni_measurementlink_service._internal.stubs.ni.measurementlink.pinmap.v1 import ( pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool +from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService class ServiceOptions(NamedTuple): @@ -231,3 +233,25 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: is_flag=True, help="Use simulated instruments.", ) + + +def get_grpc_device_channel( + measurement_service: MeasurementService, driver_module: types.ModuleType +) -> grpc.Channel: + """Returns driver specific grpc device channel.""" + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + + +def create_session_management_client( + measurement_service: MeasurementService, +) -> nims.session_management.Client: + """Return created session management client.""" + return nims.session_management.Client( + grpc_channel=measurement_service.get_channel( + provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, + service_class=nims.session_management.GRPC_SERVICE_CLASS, + ) + ) diff --git a/examples/nivisa_dmm_measurement/measurement.py b/examples/nivisa_dmm_measurement/measurement.py index cb8b0bda8..becebe047 100644 --- a/examples/nivisa_dmm_measurement/measurement.py +++ b/examples/nivisa_dmm_measurement/measurement.py @@ -12,6 +12,7 @@ ServiceOptions, configure_logging, get_service_options, + create_session_management_client, str_to_enum, use_simulation_option, verbosity_option, @@ -77,12 +78,7 @@ def measure( resolution_digits, ) - session_management_client = nims.session_management.Client( - grpc_channel=measurement_service.get_channel( - provided_interface=nims.session_management.GRPC_SERVICE_INTERFACE_NAME, - service_class=nims.session_management.GRPC_SERVICE_CLASS, - ) - ) + session_management_client = create_session_management_client(measurement_service) with contextlib.ExitStack() as stack: reservation = stack.enter_context( From a68d7250ca9a2bc2dc33b2c9e10327b839ff6de9 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Thu, 8 Jun 2023 22:28:01 +0530 Subject: [PATCH 08/22] fix lint erros --- examples/nidaqmx_analog_input/_nidaqmx_helpers.py | 2 +- examples/nidmm_measurement/_nidmm_helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py index 9ed4991cf..523562f2d 100644 --- a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py +++ b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py @@ -8,7 +8,7 @@ def _create_nidaqmx_task( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel=None, + session_grpc_channel: grpc.Channel = None, initialization_behavior=nidaqmx.SessionInitializationBehavior.AUTO, ) -> nidaqmx.Task: session_kwargs = {} diff --git a/examples/nidmm_measurement/_nidmm_helpers.py b/examples/nidmm_measurement/_nidmm_helpers.py index 9fed2edee..7a623f33a 100644 --- a/examples/nidmm_measurement/_nidmm_helpers.py +++ b/examples/nidmm_measurement/_nidmm_helpers.py @@ -14,7 +14,7 @@ def _create_nidmm_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel=None, + session_grpc_channel: grpc.Channel = None, initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, ) -> nidmm.Session: options: Dict[str, Any] = {} From 8fd0bcf68103d2cada87505fd17713cec1325207 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Fri, 9 Jun 2023 12:09:33 +0530 Subject: [PATCH 09/22] rename session creation helpers --- examples/nidaqmx_analog_input/_nidaqmx_helpers.py | 3 ++- examples/nidaqmx_analog_input/measurement.py | 4 ++-- examples/nidigital_spi/_nidigital_helpers.py | 5 +++-- examples/nidigital_spi/measurement.py | 8 ++------ examples/nidmm_measurement/_nidmm_helpers.py | 5 +++-- examples/nidmm_measurement/measurement.py | 8 ++------ examples/nifgen_standard_function/_nifgen_helpers.py | 5 +++-- examples/nifgen_standard_function/measurement.py | 8 ++------ examples/niscope_acquire_waveform/_niscope_helpers.py | 5 +++-- examples/niscope_acquire_waveform/measurement.py | 8 ++------ examples/niswitch_control_relays/_niswitch_helpers.py | 5 +++-- examples/niswitch_control_relays/measurement.py | 8 ++------ 12 files changed, 29 insertions(+), 43 deletions(-) diff --git a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py index 523562f2d..e8b545c32 100644 --- a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py +++ b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py @@ -6,11 +6,12 @@ import ni_measurementlink_service as nims -def _create_nidaqmx_task( +def create_task( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=nidaqmx.SessionInitializationBehavior.AUTO, ) -> nidaqmx.Task: + """Create daqmx task based on reserved session and grpc channel.""" session_kwargs = {} if session_grpc_channel is not None: diff --git a/examples/nidaqmx_analog_input/measurement.py b/examples/nidaqmx_analog_input/measurement.py index c04676acb..85cab70c3 100644 --- a/examples/nidaqmx_analog_input/measurement.py +++ b/examples/nidaqmx_analog_input/measurement.py @@ -17,7 +17,7 @@ grpc_device_options, verbosity_option, ) -from _nidaqmx_helpers import _create_nidaqmx_task +from _nidaqmx_helpers import create_task from nidaqmx.constants import TaskMode import ni_measurementlink_service as nims @@ -79,7 +79,7 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) grpc_device_channel = get_grpc_device_channel(measurement_service, nidaqmx) - task = stack.enter_context(_create_nidaqmx_task(session_info, grpc_device_channel)) + task = stack.enter_context(create_task(session_info, grpc_device_channel)) if not session_info.session_exists: task.ai_channels.add_ai_voltage_chan(session_info.channel_list) diff --git a/examples/nidigital_spi/_nidigital_helpers.py b/examples/nidigital_spi/_nidigital_helpers.py index db9db7360..5d9ad8969 100644 --- a/examples/nidigital_spi/_nidigital_helpers.py +++ b/examples/nidigital_spi/_nidigital_helpers.py @@ -7,16 +7,17 @@ import ni_measurementlink_service as nims -# To use a physical NI-DIGITAL instrument, set this to False or specify +# To use a physical NI Digital Pattern instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True -def _create_nidigital_session( +def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, ) -> nidigital.Session: + """Create driver session based on reserved session and grpc channel.""" options: Dict[str, Any] = {} if USE_SIMULATION: options["simulate"] = True diff --git a/examples/nidigital_spi/measurement.py b/examples/nidigital_spi/measurement.py index 4f9eb907c..f1e3ea0b8 100644 --- a/examples/nidigital_spi/measurement.py +++ b/examples/nidigital_spi/measurement.py @@ -18,14 +18,10 @@ use_simulation_option, verbosity_option, ) -from _nidigital_helpers import _create_nidigital_session +from _nidigital_helpers import create_session, USE_SIMULATION import ni_measurementlink_service as nims -# To use a physical NI Digital Pattern instrument, set this to False or specify -# --no-use-simulation on the command line. -USE_SIMULATION = True - service_directory = pathlib.Path(__file__).resolve().parent measurement_service = nims.MeasurementService( service_config_path=service_directory / "NIDigitalSPI.serviceconfig", @@ -84,7 +80,7 @@ def measure( session_info = reservation.session_info[0] grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital) - session = stack.enter_context(_create_nidigital_session(session_info, grpc_device_channel)) + session = stack.enter_context(create_session(session_info, grpc_device_channel)) pin_map_context = measurement_service.context.pin_map_context selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) diff --git a/examples/nidmm_measurement/_nidmm_helpers.py b/examples/nidmm_measurement/_nidmm_helpers.py index 7a623f33a..36f8629e4 100644 --- a/examples/nidmm_measurement/_nidmm_helpers.py +++ b/examples/nidmm_measurement/_nidmm_helpers.py @@ -7,16 +7,17 @@ import ni_measurementlink_service as nims -# To use a physical NI-DMM instrument, set this to False or specify +# To use a physical NI DMM instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True -def _create_nidmm_session( +def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, ) -> nidmm.Session: + """Create driver session based on reserved session and grpc channel.""" options: Dict[str, Any] = {} if USE_SIMULATION: options["simulate"] = True diff --git a/examples/nidmm_measurement/measurement.py b/examples/nidmm_measurement/measurement.py index baf4d3cb4..ff179e5bf 100644 --- a/examples/nidmm_measurement/measurement.py +++ b/examples/nidmm_measurement/measurement.py @@ -20,14 +20,10 @@ use_simulation_option, verbosity_option, ) -from _nidmm_helpers import _create_nidmm_session +from _nidmm_helpers import create_session, USE_SIMULATION import ni_measurementlink_service as nims -# To use a physical NI DMM instrument, set this to False or specify -# --no-use-simulation on the command line. -USE_SIMULATION = True - service_directory = pathlib.Path(__file__).resolve().parent measurement_service = nims.MeasurementService( service_config_path=service_directory / "NIDmmMeasurement.serviceconfig", @@ -105,7 +101,7 @@ def measure( session_info = reservation.session_info[0] grpc_device_channel = get_grpc_device_channel(measurement_service, nidmm) - session = stack.enter_context(_create_nidmm_session(session_info, grpc_device_channel)) + session = stack.enter_context(create_session(session_info, grpc_device_channel)) session.configure_measurement_digits( str_to_enum(FUNCTION_TO_ENUM, measurement_type), range, diff --git a/examples/nifgen_standard_function/_nifgen_helpers.py b/examples/nifgen_standard_function/_nifgen_helpers.py index d11aa2f65..780a354dc 100644 --- a/examples/nifgen_standard_function/_nifgen_helpers.py +++ b/examples/nifgen_standard_function/_nifgen_helpers.py @@ -7,16 +7,17 @@ import ni_measurementlink_service as nims -# To use a physical NI-FGen instrument, set this to False or specify +# To use a physical NI waveform generator instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True -def _create_nifgen_session( +def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=nifgen.SessionInitializationBehavior.AUTO, ) -> nifgen.Session: + """Create driver session based on reserved session and grpc channel.""" options: Dict[str, Any] = {} if USE_SIMULATION: options["simulate"] = True diff --git a/examples/nifgen_standard_function/measurement.py b/examples/nifgen_standard_function/measurement.py index 69b750533..e4f196c87 100644 --- a/examples/nifgen_standard_function/measurement.py +++ b/examples/nifgen_standard_function/measurement.py @@ -21,14 +21,10 @@ use_simulation_option, verbosity_option, ) -from _nifgen_helpers import _create_nifgen_session +from _nifgen_helpers import create_session, USE_SIMULATION import ni_measurementlink_service as nims -# To use a physical NI waveform generator instrument, set this to False or specify -# --no-use-simulation on the command line. -USE_SIMULATION = True - NIFGEN_OPERATION_TIMED_OUT_ERROR_CODE = -1074098044 NIFGEN_MAX_TIME_EXCEEDED_ERROR_CODE = -1074118637 @@ -105,7 +101,7 @@ def cancel_callback(): grpc_device_channel = get_grpc_device_channel(measurement_service, nifgen) sessions = [ - stack.enter_context(_create_nifgen_session(session_info, grpc_device_channel)) + stack.enter_context(create_session(session_info, grpc_device_channel)) for session_info in reservation.session_info ] diff --git a/examples/niscope_acquire_waveform/_niscope_helpers.py b/examples/niscope_acquire_waveform/_niscope_helpers.py index 498ec2632..b3c15de09 100644 --- a/examples/niscope_acquire_waveform/_niscope_helpers.py +++ b/examples/niscope_acquire_waveform/_niscope_helpers.py @@ -7,16 +7,17 @@ import ni_measurementlink_service as nims -# To use a physical NI-SCOPE instrument, set this to False or specify +# To use a physical NI oscilloscope instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True -def _create_niscope_session( +def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=niscope.SessionInitializationBehavior.AUTO, ) -> niscope.Session: + """Create driver session based on reserved session and grpc channel.""" options: Dict[str, Any] = {} if USE_SIMULATION: options["simulate"] = True diff --git a/examples/niscope_acquire_waveform/measurement.py b/examples/niscope_acquire_waveform/measurement.py index 958997bbd..a3bb26250 100644 --- a/examples/niscope_acquire_waveform/measurement.py +++ b/examples/niscope_acquire_waveform/measurement.py @@ -20,14 +20,10 @@ use_simulation_option, verbosity_option, ) -from _niscope_helpers import _create_niscope_session +from _niscope_helpers import create_session, USE_SIMULATION import ni_measurementlink_service as nims -# To use a physical NI oscilloscope instrument, set this to False or specify -# --no-use-simulation on the command line. -USE_SIMULATION = True - service_directory = pathlib.Path(__file__).resolve().parent measurement_service = nims.MeasurementService( service_config_path=service_directory / "NIScopeAcquireWaveform.serviceconfig", @@ -145,7 +141,7 @@ def cancel_callback(): trigger_source = pin_to_channel[trigger_source] grpc_device_channel = get_grpc_device_channel(measurement_service, niscope) - session = stack.enter_context(_create_niscope_session(session_info, grpc_device_channel)) + session = stack.enter_context(create_session(session_info, grpc_device_channel)) session.channels[""].channel_enabled = False session.channels[channel_names].configure_vertical( vertical_range, diff --git a/examples/niswitch_control_relays/_niswitch_helpers.py b/examples/niswitch_control_relays/_niswitch_helpers.py index 4aa9752eb..e2346e89b 100644 --- a/examples/niswitch_control_relays/_niswitch_helpers.py +++ b/examples/niswitch_control_relays/_niswitch_helpers.py @@ -7,16 +7,17 @@ import ni_measurementlink_service as nims -# To use a physical NI-SWITCH instrument, set this to False or specify +# To use a physical NI relay driver instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True -def _create_niswitch_session( +def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel = None, initialization_behavior=niswitch.SessionInitializationBehavior.AUTO, ) -> niswitch.Session: + """Create driver session based on reserved session and grpc channel.""" resource_name = session_info.resource_name session_kwargs: Dict[str, Any] = {} if USE_SIMULATION: diff --git a/examples/niswitch_control_relays/measurement.py b/examples/niswitch_control_relays/measurement.py index 2ef771efb..b000b4849 100644 --- a/examples/niswitch_control_relays/measurement.py +++ b/examples/niswitch_control_relays/measurement.py @@ -17,14 +17,10 @@ use_simulation_option, verbosity_option, ) -from _niswitch_helpers import _create_niswitch_session +from _niswitch_helpers import USE_SIMULATION, create_session import ni_measurementlink_service as nims -# To use a physical NI relay driver instrument, set this to False or specify -# --no-use-simulation on the command line. -USE_SIMULATION = True - service_directory = pathlib.Path(__file__).resolve().parent measurement_service = nims.MeasurementService( service_config_path=service_directory / "NISwitchControlRelays.serviceconfig", @@ -66,7 +62,7 @@ def measure( grpc_device_channel = get_grpc_device_channel(measurement_service, niswitch) sessions = [ - stack.enter_context(_create_niswitch_session(session_info, grpc_device_channel)) + stack.enter_context(create_session(session_info, grpc_device_channel)) for session_info in reservation.session_info ] From 46336e9b510913622bee6a655386f20cfad2652a Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Fri, 9 Jun 2023 12:18:06 +0530 Subject: [PATCH 10/22] update ni dcpower examples --- .../_nidcpower_helpers.py | 25 ++----------------- .../measurement.py | 13 +++++----- .../teststand_fixture.py | 14 ++++++----- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py index d922a519e..4c85b0df1 100644 --- a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py @@ -1,6 +1,6 @@ """nidcpower Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict, Iterable, Optional +from typing import Any, Dict import grpc import nidcpower @@ -15,9 +15,7 @@ def create_session( session_info: nims.session_management.SessionInformation, session_grpc_channel: grpc.Channel, - initialization_behavior: Optional[ - nidcpower.SessionInitializationBehavior - ] = nidcpower.SessionInitializationBehavior.AUTO, + initialization_behavior=nidcpower.SessionInitializationBehavior.AUTO, ) -> nidcpower.Session: """Create driver session based on reserved session and grpc channel.""" options: Dict[str, Any] = {} @@ -36,22 +34,3 @@ def create_session( return nidcpower.Session( resource_name=session_info.resource_name, options=options, **session_kwargs ) - - -def reserve_session( - session_management_client: nims.session_management.Client, - pin_map_context: nims.session_management.PinMapContext, - pin_names: Optional[Iterable[str]] = None, - timeout: Optional[float] = None, -) -> nims.session_management.Reservation: - """Reserve session(s). - - Reserve session(s) for the given pins and returns the - information needed to create or access the session. - """ - return session_management_client.reserve_sessions( - context=pin_map_context, - pin_or_relay_names=pin_names, - instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DCPOWER, - timeout=timeout, - ) diff --git a/examples/nidcpower_source_dc_voltage/measurement.py b/examples/nidcpower_source_dc_voltage/measurement.py index 0a0519bae..13313a605 100644 --- a/examples/nidcpower_source_dc_voltage/measurement.py +++ b/examples/nidcpower_source_dc_voltage/measurement.py @@ -13,17 +13,16 @@ ServiceOptions, configure_logging, create_session_management_client, - get_service_options, get_grpc_device_channel, + get_service_options, grpc_device_options, use_simulation_option, verbosity_option, ) -from _nidcpower_helpers import create_session, reserve_session, USE_SIMULATION +from _nidcpower_helpers import USE_SIMULATION, create_session import ni_measurementlink_service as nims - NIDCPOWER_WAIT_FOR_EVENT_TIMEOUT_ERROR_CODE = -1074116059 NIDCPOWER_TIMEOUT_EXCEEDED_ERROR_CODE = -1074097933 @@ -69,10 +68,10 @@ def measure( session_management_client = create_session_management_client(measurement_service) - with reserve_session( - session_management_client, - measurement_service.context.pin_map_context, - pin_names=pin_names, + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=pin_names, + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DCPOWER, # If another measurement is using the session, wait for it to complete. # Specify a timeout to aid in debugging missed unreserve calls. # Long measurements may require a longer timeout. diff --git a/examples/nidcpower_source_dc_voltage/teststand_fixture.py b/examples/nidcpower_source_dc_voltage/teststand_fixture.py index 3947709c5..4a80ce3fe 100644 --- a/examples/nidcpower_source_dc_voltage/teststand_fixture.py +++ b/examples/nidcpower_source_dc_voltage/teststand_fixture.py @@ -3,7 +3,7 @@ import nidcpower from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport -from _nidcpower_helpers import create_session, reserve_session +from _nidcpower_helpers import create_session import ni_measurementlink_service as nims @@ -48,11 +48,13 @@ def create_nidcpower_sessions(sequence_context: Any) -> None: pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) - with reserve_session( - session_management_client, - pin_map_context, - # This code module sets up the sessions, so error immediately if they are in use. - timeout=0, + with session_management_client.reserve_sessions( + context=pin_map_context, + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DCPOWER, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, ) as reservation: for session_info in reservation.session_info: grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( From a2859470b8bb7abd11616ac5d33bcad7bf616972 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Fri, 9 Jun 2023 12:40:37 +0530 Subject: [PATCH 11/22] update single session examples to use 'with' --- examples/nidaqmx_analog_input/measurement.py | 48 ++++--- examples/nidigital_spi/measurement.py | 74 +++++------ examples/nidmm_measurement/measurement.py | 43 +++---- .../niscope_acquire_waveform/measurement.py | 119 +++++++++--------- 4 files changed, 134 insertions(+), 150 deletions(-) diff --git a/examples/nidaqmx_analog_input/measurement.py b/examples/nidaqmx_analog_input/measurement.py index 85cab70c3..101bf84f8 100644 --- a/examples/nidaqmx_analog_input/measurement.py +++ b/examples/nidaqmx_analog_input/measurement.py @@ -1,6 +1,5 @@ """Perform a finite analog input measurement with NI-DAQmx.""" -import contextlib import logging import pathlib from typing import Optional @@ -50,18 +49,15 @@ def measure(pin_name, sample_rate, number_of_samples): number_of_samples, ) session_management_client = create_session_management_client(measurement_service) - with contextlib.ExitStack() as stack: - reservation = stack.enter_context( - session_management_client.reserve_sessions( - context=measurement_service.context.pin_map_context, - pin_or_relay_names=[pin_name], - instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DAQMX, - # If another measurement is using the session, wait for it to complete. - # Specify a timeout to aid in debugging missed unreserve calls. - # Long measurements may require a longer timeout. - timeout=60, - ) - ) + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=[pin_name], + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DAQMX, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, + ) as reservation: if len(reservation.session_info) != 1: measurement_service.context.abort( grpc.StatusCode.INVALID_ARGUMENT, @@ -79,18 +75,20 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) grpc_device_channel = get_grpc_device_channel(measurement_service, nidaqmx) - task = stack.enter_context(create_task(session_info, grpc_device_channel)) - if not session_info.session_exists: - task.ai_channels.add_ai_voltage_chan(session_info.channel_list) - - task.timing.cfg_samp_clk_timing( - rate=sample_rate, - samps_per_chan=number_of_samples, - ) - - timeout = min(measurement_service.context.time_remaining, 10.0) - voltage_values = task.read(number_of_samples_per_channel=number_of_samples, timeout=timeout) - task = None # Don't abort after this point + with create_task(session_info, grpc_device_channel) as task: + if not session_info.session_exists: + task.ai_channels.add_ai_voltage_chan(session_info.channel_list) + + task.timing.cfg_samp_clk_timing( + rate=sample_rate, + samps_per_chan=number_of_samples, + ) + + timeout = min(measurement_service.context.time_remaining, 10.0) + voltage_values = task.read( + number_of_samples_per_channel=number_of_samples, timeout=timeout + ) + task = None # Don't abort after this point _log_measured_values(voltage_values) logging.info("Completed measurement") diff --git a/examples/nidigital_spi/measurement.py b/examples/nidigital_spi/measurement.py index f1e3ea0b8..54b4ddc9c 100644 --- a/examples/nidigital_spi/measurement.py +++ b/examples/nidigital_spi/measurement.py @@ -1,6 +1,5 @@ """Test a SPI device using an NI Digital Pattern instrument.""" -import contextlib import logging import pathlib from typing import Iterable, Tuple, Union @@ -18,7 +17,7 @@ use_simulation_option, verbosity_option, ) -from _nidigital_helpers import create_session, USE_SIMULATION +from _nidigital_helpers import USE_SIMULATION, create_session import ni_measurementlink_service as nims @@ -59,19 +58,15 @@ def measure( session_management_client = create_session_management_client(measurement_service) - with contextlib.ExitStack() as stack: - reservation = stack.enter_context( - session_management_client.reserve_sessions( - context=measurement_service.context.pin_map_context, - pin_or_relay_names=pin_names, - instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DIGITAL_PATTERN, - # If another measurement is using the session, wait for it to complete. - # Specify a timeout to aid in debugging missed unreserve calls. - # Long measurements may require a longer timeout. - timeout=60, - ) - ) - + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=pin_names, + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DIGITAL_PATTERN, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, + ) as reservation: if len(reservation.session_info) != 1: measurement_service.context.abort( grpc.StatusCode.INVALID_ARGUMENT, @@ -80,34 +75,33 @@ def measure( session_info = reservation.session_info[0] grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital) - session = stack.enter_context(create_session(session_info, grpc_device_channel)) - - pin_map_context = measurement_service.context.pin_map_context - selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) - selected_sites = session.sites[selected_sites_string] - - if not session_info.session_exists: - # When running the measurement from TestStand, teststand_fixture.py should have - # already loaded the pin map, specifications, levels, timing, and patterns. - session.load_pin_map(pin_map_context.pin_map_id) - session.load_specifications_levels_and_timing( - str(_resolve_relative_path(service_directory, specifications_file_path)), + with create_session(session_info, grpc_device_channel) as session: + pin_map_context = measurement_service.context.pin_map_context + selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) + selected_sites = session.sites[selected_sites_string] + + if not session_info.session_exists: + # When running the measurement from TestStand, teststand_fixture.py should have + # already loaded the pin map, specifications, levels, timing, and patterns. + session.load_pin_map(pin_map_context.pin_map_id) + session.load_specifications_levels_and_timing( + str(_resolve_relative_path(service_directory, specifications_file_path)), + str(_resolve_relative_path(service_directory, levels_file_path)), + str(_resolve_relative_path(service_directory, timing_file_path)), + ) + session.load_pattern( + str(_resolve_relative_path(service_directory, pattern_file_path)), + ) + + selected_sites.apply_levels_and_timing( str(_resolve_relative_path(service_directory, levels_file_path)), str(_resolve_relative_path(service_directory, timing_file_path)), ) - session.load_pattern( - str(_resolve_relative_path(service_directory, pattern_file_path)), - ) - - selected_sites.apply_levels_and_timing( - str(_resolve_relative_path(service_directory, levels_file_path)), - str(_resolve_relative_path(service_directory, timing_file_path)), - ) - selected_sites.burst_pattern(start_label="SPI_Pattern") - site_pass_fail = selected_sites.get_site_pass_fail() - passing_sites = [site for site, pass_fail in site_pass_fail.items() if pass_fail] - failing_sites = [site for site, pass_fail in site_pass_fail.items() if not pass_fail] - session.selected_function = nidigital.SelectedFunction.DISCONNECT + selected_sites.burst_pattern(start_label="SPI_Pattern") + site_pass_fail = selected_sites.get_site_pass_fail() + passing_sites = [site for site, pass_fail in site_pass_fail.items() if pass_fail] + failing_sites = [site for site, pass_fail in site_pass_fail.items() if not pass_fail] + session.selected_function = nidigital.SelectedFunction.DISCONNECT logging.info("Completed test: passing_sites=%s failing_sites=%s", passing_sites, failing_sites) return (passing_sites, failing_sites) diff --git a/examples/nidmm_measurement/measurement.py b/examples/nidmm_measurement/measurement.py index ff179e5bf..37fb56133 100644 --- a/examples/nidmm_measurement/measurement.py +++ b/examples/nidmm_measurement/measurement.py @@ -1,6 +1,5 @@ """Perform a measurement using an NI DMM.""" -import contextlib import logging import math import pathlib @@ -20,7 +19,7 @@ use_simulation_option, verbosity_option, ) -from _nidmm_helpers import create_session, USE_SIMULATION +from _nidmm_helpers import USE_SIMULATION, create_session import ni_measurementlink_service as nims @@ -80,19 +79,15 @@ def measure( session_management_client = create_session_management_client(measurement_service) - with contextlib.ExitStack() as stack: - reservation = stack.enter_context( - session_management_client.reserve_sessions( - context=measurement_service.context.pin_map_context, - pin_or_relay_names=[pin_name], - instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DMM, - # If another measurement is using the session, wait for it to complete. - # Specify a timeout to aid in debugging missed unreserve calls. - # Long measurements may require a longer timeout. - timeout=60, - ) - ) - + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=[pin_name], + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DMM, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, + ) as reservation: if len(reservation.session_info) != 1: measurement_service.context.abort( grpc.StatusCode.INVALID_ARGUMENT, @@ -101,15 +96,15 @@ def measure( session_info = reservation.session_info[0] grpc_device_channel = get_grpc_device_channel(measurement_service, nidmm) - session = stack.enter_context(create_session(session_info, grpc_device_channel)) - session.configure_measurement_digits( - str_to_enum(FUNCTION_TO_ENUM, measurement_type), - range, - resolution_digits, - ) - measured_value = session.read() - signal_out_of_range = math.isnan(measured_value) or math.isinf(measured_value) - absolute_resolution = session.resolution_absolute + with create_session(session_info, grpc_device_channel) as session: + session.configure_measurement_digits( + str_to_enum(FUNCTION_TO_ENUM, measurement_type), + range, + resolution_digits, + ) + measured_value = session.read() + signal_out_of_range = math.isnan(measured_value) or math.isinf(measured_value) + absolute_resolution = session.resolution_absolute logging.info( "Completed measurement: measured_value=%g signal_out_of_range=%s absolute_resolution=%g", diff --git a/examples/niscope_acquire_waveform/measurement.py b/examples/niscope_acquire_waveform/measurement.py index a3bb26250..5eca65404 100644 --- a/examples/niscope_acquire_waveform/measurement.py +++ b/examples/niscope_acquire_waveform/measurement.py @@ -1,6 +1,5 @@ """Acquire a waveform using an NI oscilloscope.""" -import contextlib import logging import pathlib import time @@ -20,7 +19,7 @@ use_simulation_option, verbosity_option, ) -from _niscope_helpers import create_session, USE_SIMULATION +from _niscope_helpers import USE_SIMULATION, create_session import ni_measurementlink_service as nims @@ -113,19 +112,15 @@ def cancel_callback(): session_management_client = create_session_management_client(measurement_service) - with contextlib.ExitStack() as stack: - reservation = stack.enter_context( - session_management_client.reserve_sessions( - context=measurement_service.context.pin_map_context, - pin_or_relay_names=pin_names, - instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_SCOPE, - # If another measurement is using the session, wait for it to complete. - # Specify a timeout to aid in debugging missed unreserve calls. - # Long measurements may require a longer timeout. - timeout=60, - ) - ) - + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=pin_names, + instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_SCOPE, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, + ) as reservation: if len(reservation.session_info) != 1: measurement_service.context.abort( grpc.StatusCode.INVALID_ARGUMENT, @@ -141,52 +136,54 @@ def cancel_callback(): trigger_source = pin_to_channel[trigger_source] grpc_device_channel = get_grpc_device_channel(measurement_service, niscope) - session = stack.enter_context(create_session(session_info, grpc_device_channel)) - session.channels[""].channel_enabled = False - session.channels[channel_names].configure_vertical( - vertical_range, - str_to_enum(VERTICAL_COUPLING_TO_ENUM, vertical_coupling), - ) - session.channels[channel_names].configure_chan_characteristics( - input_impedance, max_input_frequency=0.0 - ) - session.configure_horizontal_timing( - min_sample_rate, - min_record_length, - ref_position=50.0, - num_records=1, - enforce_realtime=True, - ) - session.configure_trigger_edge( - trigger_source, - trigger_level, - str_to_enum(TRIGGER_COUPLING_TO_ENUM, trigger_coupling), - str_to_enum(TRIGGER_SLOPE_TO_ENUM, trigger_slope), - ) - session.trigger_modifier = ( - niscope.TriggerModifier.AUTO if auto_trigger else niscope.TriggerModifier.NO_TRIGGER_MOD - ) - - with session.initiate(): - deadline = time.time() + min(measurement_service.context.time_remaining, timeout) - while True: - if time.time() > deadline: - measurement_service.context.abort( - grpc.StatusCode.DEADLINE_EXCEEDED, "Deadline exceeded." - ) - if pending_cancellation: - measurement_service.context.abort( - grpc.StatusCode.CANCELLED, "Client requested cancellation." - ) - status = session.acquisition_status() - if status == niscope.AcquisitionStatus.COMPLETE: - break - remaining_time = max(deadline - time.time(), 0.0) - sleep_time = min(remaining_time, 10e-3) - time.sleep(sleep_time) - - waveform_infos = session.channels[channel_names].fetch() - return tuple(w.samples for w in waveform_infos) + with create_session(session_info, grpc_device_channel) as session: + session.channels[""].channel_enabled = False + session.channels[channel_names].configure_vertical( + vertical_range, + str_to_enum(VERTICAL_COUPLING_TO_ENUM, vertical_coupling), + ) + session.channels[channel_names].configure_chan_characteristics( + input_impedance, max_input_frequency=0.0 + ) + session.configure_horizontal_timing( + min_sample_rate, + min_record_length, + ref_position=50.0, + num_records=1, + enforce_realtime=True, + ) + session.configure_trigger_edge( + trigger_source, + trigger_level, + str_to_enum(TRIGGER_COUPLING_TO_ENUM, trigger_coupling), + str_to_enum(TRIGGER_SLOPE_TO_ENUM, trigger_slope), + ) + session.trigger_modifier = ( + niscope.TriggerModifier.AUTO + if auto_trigger + else niscope.TriggerModifier.NO_TRIGGER_MOD + ) + + with session.initiate(): + deadline = time.time() + min(measurement_service.context.time_remaining, timeout) + while True: + if time.time() > deadline: + measurement_service.context.abort( + grpc.StatusCode.DEADLINE_EXCEEDED, "Deadline exceeded." + ) + if pending_cancellation: + measurement_service.context.abort( + grpc.StatusCode.CANCELLED, "Client requested cancellation." + ) + status = session.acquisition_status() + if status == niscope.AcquisitionStatus.COMPLETE: + break + remaining_time = max(deadline - time.time(), 0.0) + sleep_time = min(remaining_time, 10e-3) + time.sleep(sleep_time) + + waveform_infos = session.channels[channel_names].fetch() + return tuple(w.samples for w in waveform_infos) return () From 5b42fd10791e788c77ad95ee13964ef57f298335 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Sun, 11 Jun 2023 23:09:28 +0530 Subject: [PATCH 12/22] update measurements to make use of service options --- examples/nidaqmx_analog_input/measurement.py | 2 +- examples/nidcpower_source_dc_voltage/_helpers.py | 8 ++++++-- .../_nidcpower_helpers.py | 13 +++++++------ examples/nidcpower_source_dc_voltage/measurement.py | 4 ++-- examples/nidigital_spi/measurement.py | 2 +- examples/nidmm_measurement/measurement.py | 2 +- examples/nifgen_standard_function/measurement.py | 2 +- examples/niscope_acquire_waveform/measurement.py | 2 +- examples/niswitch_control_relays/measurement.py | 2 +- 9 files changed, 21 insertions(+), 16 deletions(-) diff --git a/examples/nidaqmx_analog_input/measurement.py b/examples/nidaqmx_analog_input/measurement.py index 101bf84f8..3d4a3a4f5 100644 --- a/examples/nidaqmx_analog_input/measurement.py +++ b/examples/nidaqmx_analog_input/measurement.py @@ -74,7 +74,7 @@ def cancel_callback(): measurement_service.context.add_cancel_callback(cancel_callback) - grpc_device_channel = get_grpc_device_channel(measurement_service, nidaqmx) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidaqmx, service_options) with create_task(session_info, grpc_device_channel) as task: if not session_info.session_exists: task.ai_channels.add_ai_voltage_chan(session_info.channel_list) diff --git a/examples/nidcpower_source_dc_voltage/_helpers.py b/examples/nidcpower_source_dc_voltage/_helpers.py index d0bdc94b1..a8275c173 100644 --- a/examples/nidcpower_source_dc_voltage/_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_helpers.py @@ -234,11 +234,15 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: help="Use simulated instruments.", ) - def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py index 4c85b0df1..227ac7d22 100644 --- a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel, + session_grpc_channel: grpc.Channel=None, initialization_behavior=nidcpower.SessionInitializationBehavior.AUTO, ) -> nidcpower.Session: """Create driver session based on reserved session and grpc channel.""" @@ -25,11 +25,12 @@ def create_session( session_kwargs: Dict[str, Any] = {} - session_kwargs["grpc_options"] = nidcpower.GrpcSessionOptions( - session_grpc_channel, - session_name=session_info.session_name, - initialization_behavior=initialization_behavior, - ) + if session_grpc_channel is not None: + session_kwargs["grpc_options"] = nidcpower.GrpcSessionOptions( + session_grpc_channel, + session_name=session_info.session_name, + initialization_behavior=initialization_behavior, + ) return nidcpower.Session( resource_name=session_info.resource_name, options=options, **session_kwargs diff --git a/examples/nidcpower_source_dc_voltage/measurement.py b/examples/nidcpower_source_dc_voltage/measurement.py index 13313a605..6a75cb518 100644 --- a/examples/nidcpower_source_dc_voltage/measurement.py +++ b/examples/nidcpower_source_dc_voltage/measurement.py @@ -84,10 +84,10 @@ def measure( ) session_info = reservation.session_info[0] - + grpc_device_channel = get_grpc_device_channel(measurement_service, nidcpower, service_options) with create_session( session_info, - get_grpc_device_channel(measurement_service, nidcpower), + grpc_device_channel, ) as session: channels = session.channels[session_info.channel_list] channel_mappings = session_info.channel_mappings diff --git a/examples/nidigital_spi/measurement.py b/examples/nidigital_spi/measurement.py index 54b4ddc9c..5c80082dc 100644 --- a/examples/nidigital_spi/measurement.py +++ b/examples/nidigital_spi/measurement.py @@ -74,7 +74,7 @@ def measure( ) session_info = reservation.session_info[0] - grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital, service_options) with create_session(session_info, grpc_device_channel) as session: pin_map_context = measurement_service.context.pin_map_context selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) diff --git a/examples/nidmm_measurement/measurement.py b/examples/nidmm_measurement/measurement.py index 37fb56133..45ac4d2e7 100644 --- a/examples/nidmm_measurement/measurement.py +++ b/examples/nidmm_measurement/measurement.py @@ -95,7 +95,7 @@ def measure( ) session_info = reservation.session_info[0] - grpc_device_channel = get_grpc_device_channel(measurement_service, nidmm) + grpc_device_channel = get_grpc_device_channel(measurement_service, nidmm, service_options) with create_session(session_info, grpc_device_channel) as session: session.configure_measurement_digits( str_to_enum(FUNCTION_TO_ENUM, measurement_type), diff --git a/examples/nifgen_standard_function/measurement.py b/examples/nifgen_standard_function/measurement.py index e4f196c87..61308d8d6 100644 --- a/examples/nifgen_standard_function/measurement.py +++ b/examples/nifgen_standard_function/measurement.py @@ -99,7 +99,7 @@ def cancel_callback(): ) ) - grpc_device_channel = get_grpc_device_channel(measurement_service, nifgen) + grpc_device_channel = get_grpc_device_channel(measurement_service, nifgen, service_options) sessions = [ stack.enter_context(create_session(session_info, grpc_device_channel)) for session_info in reservation.session_info diff --git a/examples/niscope_acquire_waveform/measurement.py b/examples/niscope_acquire_waveform/measurement.py index 5eca65404..90c44088b 100644 --- a/examples/niscope_acquire_waveform/measurement.py +++ b/examples/niscope_acquire_waveform/measurement.py @@ -135,7 +135,7 @@ def cancel_callback(): if trigger_source in pin_to_channel: trigger_source = pin_to_channel[trigger_source] - grpc_device_channel = get_grpc_device_channel(measurement_service, niscope) + grpc_device_channel = get_grpc_device_channel(measurement_service, niscope, service_options) with create_session(session_info, grpc_device_channel) as session: session.channels[""].channel_enabled = False session.channels[channel_names].configure_vertical( diff --git a/examples/niswitch_control_relays/measurement.py b/examples/niswitch_control_relays/measurement.py index b000b4849..0be179cd5 100644 --- a/examples/niswitch_control_relays/measurement.py +++ b/examples/niswitch_control_relays/measurement.py @@ -60,7 +60,7 @@ def measure( ) ) - grpc_device_channel = get_grpc_device_channel(measurement_service, niswitch) + grpc_device_channel = get_grpc_device_channel(measurement_service, niswitch, service_options) sessions = [ stack.enter_context(create_session(session_info, grpc_device_channel)) for session_info in reservation.session_info From 6550262fb61851e01a964a31e0dc68782568b244 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Sun, 11 Jun 2023 23:44:03 +0530 Subject: [PATCH 13/22] fix lint errors --- examples/nidcpower_source_dc_voltage/_helpers.py | 12 ++++++++---- .../_nidcpower_helpers.py | 2 +- examples/nidcpower_source_dc_voltage/measurement.py | 4 +++- examples/nidigital_spi/measurement.py | 4 +++- examples/niswitch_control_relays/measurement.py | 4 +++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/nidcpower_source_dc_voltage/_helpers.py b/examples/nidcpower_source_dc_voltage/_helpers.py index a8275c173..b97c8a78c 100644 --- a/examples/nidcpower_source_dc_voltage/_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -234,15 +237,16 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: help="Use simulated instruments.", ) + def get_grpc_device_channel( - measurement_service: MeasurementService, + measurement_service: MeasurementService, driver_module: types.ModuleType, - service_options: ServiceOptions + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device and service_options.grpc_device_address: return measurement_service.get_channel(service_options.grpc_device_address) - + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py index 227ac7d22..e26204772 100644 --- a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel=None, + session_grpc_channel: grpc.Channel = None, initialization_behavior=nidcpower.SessionInitializationBehavior.AUTO, ) -> nidcpower.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/nidcpower_source_dc_voltage/measurement.py b/examples/nidcpower_source_dc_voltage/measurement.py index 6a75cb518..0ae105d58 100644 --- a/examples/nidcpower_source_dc_voltage/measurement.py +++ b/examples/nidcpower_source_dc_voltage/measurement.py @@ -84,7 +84,9 @@ def measure( ) session_info = reservation.session_info[0] - grpc_device_channel = get_grpc_device_channel(measurement_service, nidcpower, service_options) + grpc_device_channel = get_grpc_device_channel( + measurement_service, nidcpower, service_options + ) with create_session( session_info, grpc_device_channel, diff --git a/examples/nidigital_spi/measurement.py b/examples/nidigital_spi/measurement.py index 5c80082dc..460892240 100644 --- a/examples/nidigital_spi/measurement.py +++ b/examples/nidigital_spi/measurement.py @@ -74,7 +74,9 @@ def measure( ) session_info = reservation.session_info[0] - grpc_device_channel = get_grpc_device_channel(measurement_service, nidigital, service_options) + grpc_device_channel = get_grpc_device_channel( + measurement_service, nidigital, service_options + ) with create_session(session_info, grpc_device_channel) as session: pin_map_context = measurement_service.context.pin_map_context selected_sites_string = ",".join(f"site{i}" for i in pin_map_context.sites) diff --git a/examples/niswitch_control_relays/measurement.py b/examples/niswitch_control_relays/measurement.py index 0be179cd5..46a7754ab 100644 --- a/examples/niswitch_control_relays/measurement.py +++ b/examples/niswitch_control_relays/measurement.py @@ -60,7 +60,9 @@ def measure( ) ) - grpc_device_channel = get_grpc_device_channel(measurement_service, niswitch, service_options) + grpc_device_channel = get_grpc_device_channel( + measurement_service, niswitch, service_options + ) sessions = [ stack.enter_context(create_session(session_info, grpc_device_channel)) for session_info in reservation.session_info From d056f93e928f7d425eea6b620f9024e86f076409 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Sun, 11 Jun 2023 23:52:42 +0530 Subject: [PATCH 14/22] mark grpc channel args as optional --- examples/nidaqmx_analog_input/_nidaqmx_helpers.py | 4 +++- examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py | 4 ++-- examples/nidigital_spi/_nidigital_helpers.py | 4 ++-- examples/nidmm_measurement/_nidmm_helpers.py | 4 ++-- examples/nifgen_standard_function/_nifgen_helpers.py | 4 ++-- examples/niscope_acquire_waveform/_niscope_helpers.py | 4 ++-- examples/niswitch_control_relays/_niswitch_helpers.py | 4 ++-- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py index e8b545c32..d7617c8bc 100644 --- a/examples/nidaqmx_analog_input/_nidaqmx_helpers.py +++ b/examples/nidaqmx_analog_input/_nidaqmx_helpers.py @@ -1,5 +1,7 @@ """nidaqmx Helper classes and functions for MeasurementLink examples.""" +from typing import Optional + import grpc import nidaqmx @@ -8,7 +10,7 @@ def create_task( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=nidaqmx.SessionInitializationBehavior.AUTO, ) -> nidaqmx.Task: """Create daqmx task based on reserved session and grpc channel.""" diff --git a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py index e26204772..6e114827d 100644 --- a/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_nidcpower_helpers.py @@ -1,6 +1,6 @@ """nidcpower Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import nidcpower @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=nidcpower.SessionInitializationBehavior.AUTO, ) -> nidcpower.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/nidigital_spi/_nidigital_helpers.py b/examples/nidigital_spi/_nidigital_helpers.py index 5d9ad8969..ac45b0f97 100644 --- a/examples/nidigital_spi/_nidigital_helpers.py +++ b/examples/nidigital_spi/_nidigital_helpers.py @@ -1,6 +1,6 @@ """nidigital Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import nidigital @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, ) -> nidigital.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/nidmm_measurement/_nidmm_helpers.py b/examples/nidmm_measurement/_nidmm_helpers.py index 36f8629e4..a094b7c5c 100644 --- a/examples/nidmm_measurement/_nidmm_helpers.py +++ b/examples/nidmm_measurement/_nidmm_helpers.py @@ -1,6 +1,6 @@ """nidmm Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import nidmm @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=nidmm.SessionInitializationBehavior.AUTO, ) -> nidmm.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/nifgen_standard_function/_nifgen_helpers.py b/examples/nifgen_standard_function/_nifgen_helpers.py index 780a354dc..e50a510b1 100644 --- a/examples/nifgen_standard_function/_nifgen_helpers.py +++ b/examples/nifgen_standard_function/_nifgen_helpers.py @@ -1,6 +1,6 @@ """nifgen Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import nifgen @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=nifgen.SessionInitializationBehavior.AUTO, ) -> nifgen.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/niscope_acquire_waveform/_niscope_helpers.py b/examples/niscope_acquire_waveform/_niscope_helpers.py index b3c15de09..fe6e33dee 100644 --- a/examples/niscope_acquire_waveform/_niscope_helpers.py +++ b/examples/niscope_acquire_waveform/_niscope_helpers.py @@ -1,6 +1,6 @@ """niscope Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import niscope @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=niscope.SessionInitializationBehavior.AUTO, ) -> niscope.Session: """Create driver session based on reserved session and grpc channel.""" diff --git a/examples/niswitch_control_relays/_niswitch_helpers.py b/examples/niswitch_control_relays/_niswitch_helpers.py index e2346e89b..71d937631 100644 --- a/examples/niswitch_control_relays/_niswitch_helpers.py +++ b/examples/niswitch_control_relays/_niswitch_helpers.py @@ -1,6 +1,6 @@ """niswitch Helper classes and functions for MeasurementLink examples.""" -from typing import Any, Dict +from typing import Any, Dict, Optional import grpc import niswitch @@ -14,7 +14,7 @@ def create_session( session_info: nims.session_management.SessionInformation, - session_grpc_channel: grpc.Channel = None, + session_grpc_channel: Optional[grpc.Channel] = None, initialization_behavior=niswitch.SessionInitializationBehavior.AUTO, ) -> niswitch.Session: """Create driver session based on reserved session and grpc channel.""" From c44e1cd674221820a20cbcd65193b9cd4dd42f44 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Mon, 12 Jun 2023 15:11:40 +0530 Subject: [PATCH 15/22] update helpers --- examples/nidaqmx_analog_input/_helpers.py | 12 ++++++++++-- examples/nidigital_spi/_helpers.py | 12 ++++++++++-- examples/nidmm_measurement/_helpers.py | 12 ++++++++++-- examples/nifgen_standard_function/_helpers.py | 12 ++++++++++-- examples/niscope_acquire_waveform/_helpers.py | 12 ++++++++++-- examples/niswitch_control_relays/_helpers.py | 12 ++++++++++-- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/examples/nidaqmx_analog_input/_helpers.py b/examples/nidaqmx_analog_input/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/nidaqmx_analog_input/_helpers.py +++ b/examples/nidaqmx_analog_input/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/nidigital_spi/_helpers.py b/examples/nidigital_spi/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/nidigital_spi/_helpers.py +++ b/examples/nidigital_spi/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/nidmm_measurement/_helpers.py b/examples/nidmm_measurement/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/nidmm_measurement/_helpers.py +++ b/examples/nidmm_measurement/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/nifgen_standard_function/_helpers.py b/examples/nifgen_standard_function/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/nifgen_standard_function/_helpers.py +++ b/examples/nifgen_standard_function/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/niscope_acquire_waveform/_helpers.py b/examples/niscope_acquire_waveform/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/niscope_acquire_waveform/_helpers.py +++ b/examples/niscope_acquire_waveform/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", diff --git a/examples/niswitch_control_relays/_helpers.py b/examples/niswitch_control_relays/_helpers.py index d0bdc94b1..b97c8a78c 100644 --- a/examples/niswitch_control_relays/_helpers.py +++ b/examples/niswitch_control_relays/_helpers.py @@ -15,7 +15,10 @@ pin_map_service_pb2, pin_map_service_pb2_grpc, ) -from ni_measurementlink_service.measurement.service import GrpcChannelPool, MeasurementService +from ni_measurementlink_service.measurement.service import ( + GrpcChannelPool, + MeasurementService, +) class ServiceOptions(NamedTuple): @@ -236,9 +239,14 @@ def use_simulation_option(default: bool) -> Callable[[F], F]: def get_grpc_device_channel( - measurement_service: MeasurementService, driver_module: types.ModuleType + measurement_service: MeasurementService, + driver_module: types.ModuleType, + service_options: ServiceOptions, ) -> grpc.Channel: """Returns driver specific grpc device channel.""" + if service_options.use_grpc_device and service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), service_class="ni.measurementlink.v1.grpcdeviceserver", From 8eb2f94d57523cce2d54dbda0b153250be96fba8 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 12:25:25 +0530 Subject: [PATCH 16/22] udpate test stand fixtures --- .../nidaqmx_analog_input/teststand_fixture.py | 28 ++++++------- examples/nidigital_spi/teststand_fixture.py | 41 ++++++------------- .../nidmm_measurement/teststand_fixture.py | 35 ++++++---------- .../teststand_fixture.py | 37 ++++++----------- .../teststand_fixture.py | 34 ++++++--------- .../teststand_fixture.py | 32 ++++++--------- 6 files changed, 79 insertions(+), 128 deletions(-) diff --git a/examples/nidaqmx_analog_input/teststand_fixture.py b/examples/nidaqmx_analog_input/teststand_fixture.py index 523e17163..0fc65c33f 100644 --- a/examples/nidaqmx_analog_input/teststand_fixture.py +++ b/examples/nidaqmx_analog_input/teststand_fixture.py @@ -3,6 +3,7 @@ import nidaqmx from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _nidaqmx_helpers import create_task from nidaqmx.grpc_session_options import ( GRPC_SERVICE_INTERFACE_NAME, GrpcSessionOptions, @@ -60,14 +61,14 @@ def create_nidaqmx_tasks(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - session_kwargs["grpc_options"] = GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidaqmx.GRPC_SERVICE_INTERFACE_NAME + ) + task = create_task( + session_info, + grpc_device_channel, + initialization_behavior=nidaqmx.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) - - # Leave session open - task = nidaqmx.Task(new_task_name=session_info.session_name, **session_kwargs) task.ai_channels.add_ai_voltage_chan(session_info.channel_list) session_management_client.register_sessions(reservation.session_info) @@ -88,13 +89,12 @@ def destroy_nidaqmx_tasks() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_options = GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidaqmx.GRPC_SERVICE_INTERFACE_NAME ) - - task = nidaqmx.Task( - new_task_name=session_info.session_name, grpc_options=grpc_options + task = create_task( + session_info, + grpc_device_channel, + initialization_behavior=nidaqmx.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) task.close() diff --git a/examples/nidigital_spi/teststand_fixture.py b/examples/nidigital_spi/teststand_fixture.py index 3e88f63ac..9e89bb8ff 100644 --- a/examples/nidigital_spi/teststand_fixture.py +++ b/examples/nidigital_spi/teststand_fixture.py @@ -3,6 +3,7 @@ import nidigital from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _nidigital_helpers import create_session import ni_measurementlink_service as nims from ni_measurementlink_service.session_management import ( @@ -191,36 +192,17 @@ def _reserve_sessions( ) -def _create_nidigital_session( - grpc_channel_pool: GrpcChannelPoolHelper, - session_info: nims.session_management.SessionInformation, - initialization_behavior=nidigital.SessionInitializationBehavior.AUTO, -) -> nidigital.Session: - options: Dict[str, Any] = {} - if USE_SIMULATION: - options["simulate"] = True - options["driver_setup"] = {"Model": "6570"} - - grpc_channel = grpc_channel_pool.get_grpc_device_channel(nidigital.GRPC_SERVICE_INTERFACE_NAME) - grpc_options = nidigital.GrpcSessionOptions( - grpc_channel, - session_info.session_name, - initialization_behavior=initialization_behavior, - ) - - return nidigital.Session( - resource_name=session_info.resource_name, options=options, grpc_options=grpc_options - ) - - def _create_new_nidigital_session( grpc_channel_pool: GrpcChannelPoolHelper, session_info: nims.session_management.SessionInformation, ) -> nidigital.Session: - return _create_nidigital_session( - grpc_channel_pool, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidigital.GRPC_SERVICE_INTERFACE_NAME + ) + return create_session( session_info, - nidigital.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, + grpc_device_channel, + initialization_behavior=nidigital.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) @@ -228,8 +210,11 @@ def _attach_nidigital_session( grpc_channel_pool: GrpcChannelPoolHelper, session_info: nims.session_management.SessionInformation, ) -> nidigital.Session: - return _create_nidigital_session( - grpc_channel_pool, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidigital.GRPC_SERVICE_INTERFACE_NAME + ) + return create_session( session_info, - nidigital.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, + grpc_device_channel, + initialization_behavior=nidigital.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) diff --git a/examples/nidmm_measurement/teststand_fixture.py b/examples/nidmm_measurement/teststand_fixture.py index ea9f61dcc..39b613c98 100644 --- a/examples/nidmm_measurement/teststand_fixture.py +++ b/examples/nidmm_measurement/teststand_fixture.py @@ -3,6 +3,7 @@ import nidmm from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _nidmm_helpers import create_session import ni_measurementlink_service as nims @@ -55,22 +56,13 @@ def create_nidmm_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - options: Dict[str, Any] = {} - if USE_SIMULATION: - options["simulate"] = True - options["driver_setup"] = {"Model": "4081"} - - grpc_options = nidmm.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(nidmm.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=nidmm.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidmm.GRPC_SERVICE_INTERFACE_NAME ) - - # Leave session open - nidmm.Session( - resource_name=session_info.resource_name, - options=options, - grpc_options=grpc_options, + create_session( + session_info, + grpc_device_channel, + initialization_behavior=nidmm.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) session_management_client.register_sessions(reservation.session_info) @@ -91,13 +83,12 @@ def destroy_nidmm_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_options = nidmm.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(nidmm.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=nidmm.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidmm.GRPC_SERVICE_INTERFACE_NAME ) - - session = nidmm.Session( - resource_name=session_info.resource_name, grpc_options=grpc_options + session = create_session( + session_info, + grpc_device_channel, + initialization_behavior=nidmm.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) session.close() diff --git a/examples/nifgen_standard_function/teststand_fixture.py b/examples/nifgen_standard_function/teststand_fixture.py index 95abd42bd..7e678fb20 100644 --- a/examples/nifgen_standard_function/teststand_fixture.py +++ b/examples/nifgen_standard_function/teststand_fixture.py @@ -3,6 +3,7 @@ import nifgen from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _nifgen_helpers import create_session import ni_measurementlink_service as nims @@ -55,23 +56,13 @@ def create_nifgen_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - options: Dict[str, Any] = {} - if USE_SIMULATION: - options["simulate"] = True - options["driver_setup"] = {"Model": "5423 (2CH)"} - - grpc_options = nifgen.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(nifgen.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=nifgen.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nifgen.GRPC_SERVICE_INTERFACE_NAME ) - - # Leave session open - nifgen.Session( - resource_name=session_info.resource_name, - channel_name=session_info.channel_list, - options=options, - grpc_options=grpc_options, + create_session( + session_info, + grpc_device_channel, + initialization_behavior=nifgen.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) session_management_client.register_sessions(reservation.session_info) @@ -92,15 +83,13 @@ def destroy_nifgen_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_options = nifgen.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(nifgen.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nifgen.GRPC_SERVICE_INTERFACE_NAME + ) + session = create_session( + session_info, + grpc_device_channel, initialization_behavior=nifgen.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) - session = nifgen.Session( - resource_name=session_info.resource_name, - channel_name=session_info.channel_list, - grpc_options=grpc_options, - ) session.close() diff --git a/examples/niscope_acquire_waveform/teststand_fixture.py b/examples/niscope_acquire_waveform/teststand_fixture.py index a9c8bb9d3..b738fa4fd 100644 --- a/examples/niscope_acquire_waveform/teststand_fixture.py +++ b/examples/niscope_acquire_waveform/teststand_fixture.py @@ -3,6 +3,7 @@ import niscope from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _niscope_helpers import create_session import ni_measurementlink_service as nims @@ -56,22 +57,13 @@ def create_niscope_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - options: Dict[str, Any] = {} - if USE_SIMULATION: - options["simulate"] = True - options["driver_setup"] = {"Model": "5162 (4CH)"} - - grpc_options = niscope.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(niscope.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, - initialization_behavior=niscope.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niscope.GRPC_SERVICE_INTERFACE_NAME ) - - # Leave session open - niscope.Session( - resource_name=session_info.resource_name, - options=options, - grpc_options=grpc_options, + create_session( + session_info, + grpc_device_channel, + initialization_behavior=niscope.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) session_management_client.register_sessions(reservation.session_info) @@ -92,13 +84,13 @@ def destroy_niscope_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_options = niscope.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(niscope.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niscope.GRPC_SERVICE_INTERFACE_NAME + ) + session = create_session( + session_info, + grpc_device_channel, initialization_behavior=niscope.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) - session = niscope.Session( - resource_name=session_info.resource_name, grpc_options=grpc_options - ) session.close() diff --git a/examples/niswitch_control_relays/teststand_fixture.py b/examples/niswitch_control_relays/teststand_fixture.py index 78003327f..c85dc2edb 100644 --- a/examples/niswitch_control_relays/teststand_fixture.py +++ b/examples/niswitch_control_relays/teststand_fixture.py @@ -3,6 +3,7 @@ import niswitch from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport +from _niswitch_helpers import create_session import ni_measurementlink_service as nims @@ -56,22 +57,15 @@ def create_niswitch_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - resource_name = session_info.resource_name - session_kwargs: Dict[str, Any] = {} - if USE_SIMULATION: - resource_name = "" - session_kwargs["simulate"] = True - session_kwargs["topology"] = "2567/Independent" - - session_kwargs["grpc_options"] = niswitch.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(niswitch.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niswitch.GRPC_SERVICE_INTERFACE_NAME + ) + create_session( + session_info, + grpc_device_channel, initialization_behavior=niswitch.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) - # Leave session open - niswitch.Session(resource_name, **session_kwargs) - session_management_client.register_sessions(reservation.session_info) @@ -90,13 +84,13 @@ def destroy_niswitch_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_options = niswitch.GrpcSessionOptions( - grpc_channel_pool.get_grpc_device_channel(niswitch.GRPC_SERVICE_INTERFACE_NAME), - session_name=session_info.session_name, + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niswitch.GRPC_SERVICE_INTERFACE_NAME + ) + session = create_session( + session_info, + grpc_device_channel, initialization_behavior=niswitch.SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION, ) - session = niswitch.Session( - resource_name=session_info.resource_name, grpc_options=grpc_options - ) session.close() From f7e62d27b3cd96f0123affea3be9053eb4259df8 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 12:40:17 +0530 Subject: [PATCH 17/22] Update the get grpc channel method --- examples/nidaqmx_analog_input/_helpers.py | 18 ++++++++++-------- .../nidcpower_source_dc_voltage/_helpers.py | 18 ++++++++++-------- examples/nidigital_spi/_helpers.py | 18 ++++++++++-------- examples/nidmm_measurement/_helpers.py | 18 ++++++++++-------- examples/nifgen_standard_function/_helpers.py | 18 ++++++++++-------- examples/niscope_acquire_waveform/_helpers.py | 18 ++++++++++-------- examples/niswitch_control_relays/_helpers.py | 18 ++++++++++-------- 7 files changed, 70 insertions(+), 56 deletions(-) diff --git a/examples/nidaqmx_analog_input/_helpers.py b/examples/nidaqmx_analog_input/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/nidaqmx_analog_input/_helpers.py +++ b/examples/nidaqmx_analog_input/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/nidcpower_source_dc_voltage/_helpers.py b/examples/nidcpower_source_dc_voltage/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/nidcpower_source_dc_voltage/_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/nidigital_spi/_helpers.py b/examples/nidigital_spi/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/nidigital_spi/_helpers.py +++ b/examples/nidigital_spi/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/nidmm_measurement/_helpers.py b/examples/nidmm_measurement/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/nidmm_measurement/_helpers.py +++ b/examples/nidmm_measurement/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/nifgen_standard_function/_helpers.py b/examples/nifgen_standard_function/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/nifgen_standard_function/_helpers.py +++ b/examples/nifgen_standard_function/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/niscope_acquire_waveform/_helpers.py b/examples/niscope_acquire_waveform/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/niscope_acquire_waveform/_helpers.py +++ b/examples/niscope_acquire_waveform/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( diff --git a/examples/niswitch_control_relays/_helpers.py b/examples/niswitch_control_relays/_helpers.py index b97c8a78c..fb16d3d81 100644 --- a/examples/niswitch_control_relays/_helpers.py +++ b/examples/niswitch_control_relays/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union import click import grpc @@ -242,15 +242,17 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> grpc.Channel: +) -> Union[grpc.Channel, None]: """Returns driver specific grpc device channel.""" - if service_options.use_grpc_device and service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + if service_options.use_grpc_device: + if service_options.grpc_device_address: + return measurement_service.get_channel(service_options.grpc_device_address) - return measurement_service.get_channel( - provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), - service_class="ni.measurementlink.v1.grpcdeviceserver", - ) + return measurement_service.get_channel( + provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), + service_class="ni.measurementlink.v1.grpcdeviceserver", + ) + return None def create_session_management_client( From 186b25777778480df6e1b6da73d8448468ed2248 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 12:43:31 +0530 Subject: [PATCH 18/22] remove exit stack from visa dmm example --- examples/nidigital_spi/_nidigital_helpers.py | 2 +- .../nivisa_dmm_measurement/measurement.py | 67 +++++++++---------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/examples/nidigital_spi/_nidigital_helpers.py b/examples/nidigital_spi/_nidigital_helpers.py index ac45b0f97..908efdbab 100644 --- a/examples/nidigital_spi/_nidigital_helpers.py +++ b/examples/nidigital_spi/_nidigital_helpers.py @@ -7,7 +7,7 @@ import ni_measurementlink_service as nims -# To use a physical NI Digital Pattern instrument, set this to False or specify +# To use a physical NI digital pattern instrument, set this to False or specify # --no-use-simulation on the command line. USE_SIMULATION = True diff --git a/examples/nivisa_dmm_measurement/measurement.py b/examples/nivisa_dmm_measurement/measurement.py index becebe047..0231a5a79 100644 --- a/examples/nivisa_dmm_measurement/measurement.py +++ b/examples/nivisa_dmm_measurement/measurement.py @@ -11,8 +11,8 @@ from _helpers import ( ServiceOptions, configure_logging, - get_service_options, create_session_management_client, + get_service_options, str_to_enum, use_simulation_option, verbosity_option, @@ -80,19 +80,15 @@ def measure( session_management_client = create_session_management_client(measurement_service) - with contextlib.ExitStack() as stack: - reservation = stack.enter_context( - session_management_client.reserve_sessions( - context=measurement_service.context.pin_map_context, - pin_or_relay_names=[pin_name], - instrument_type_id=INSTRUMENT_TYPE_DMM_SIMULATOR, - # If another measurement is using the session, wait for it to complete. - # Specify a timeout to aid in debugging missed unreserve calls. - # Long measurements may require a longer timeout. - timeout=60, - ) - ) - + with session_management_client.reserve_sessions( + context=measurement_service.context.pin_map_context, + pin_or_relay_names=[pin_name], + instrument_type_id=INSTRUMENT_TYPE_DMM_SIMULATOR, + # If another measurement is using the session, wait for it to complete. + # Specify a timeout to aid in debugging missed unreserve calls. + # Long measurements may require a longer timeout. + timeout=60, + ) as reservation: if len(reservation.session_info) != 1: measurement_service.context.abort( grpc.StatusCode.INVALID_ARGUMENT, @@ -101,29 +97,26 @@ def measure( resource_manager = create_visa_resource_manager(service_options.use_simulation) session_info = reservation.session_info[0] - session = stack.enter_context( - create_visa_session(resource_manager, session_info.resource_name) - ) - - # Work around https://github.com/pyvisa/pyvisa/issues/739 - Type annotation for Resource - # context manager implicitly upcasts derived class to base class - assert isinstance(session, pyvisa.resources.MessageBasedResource) - - log_instrument_id(session) - - # When this measurement is called from outside of TestStand (session_exists == False), - # reset the instrument to a known state. In TestStand, ProcessSetup resets the instrument. - if not session_info.session_exists: - reset_instrument(session) - - function_enum = str_to_enum(FUNCTION_TO_ENUM, measurement_type) - resolution_value = str_to_enum(RESOLUTION_DIGITS_TO_VALUE, str(resolution_digits)) - session.write("CONF:%s %.g,%.g" % (function_enum, range, resolution_value)) - check_instrument_error(session) - - response = session.query("READ?") - check_instrument_error(session) - measured_value = float(response) + with create_visa_session(resource_manager, session_info.resource_name) as session: + # Work around https://github.com/pyvisa/pyvisa/issues/739 - Type annotation for Resource + # context manager implicitly upcasts derived class to base class + assert isinstance(session, pyvisa.resources.MessageBasedResource) + + log_instrument_id(session) + + # When this measurement is called from outside of TestStand (session_exists == False), + # reset the instrument to a known state. In TestStand, ProcessSetup resets the instrument. + if not session_info.session_exists: + reset_instrument(session) + + function_enum = str_to_enum(FUNCTION_TO_ENUM, measurement_type) + resolution_value = str_to_enum(RESOLUTION_DIGITS_TO_VALUE, str(resolution_digits)) + session.write("CONF:%s %.g,%.g" % (function_enum, range, resolution_value)) + check_instrument_error(session) + + response = session.query("READ?") + check_instrument_error(session) + measured_value = float(response) logging.info("Completed measurement: measured_value=%g", measured_value) return (measured_value,) From 9ebceefdf26a279b3bd44fd7626a1ea5e0b15c5e Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 17:21:44 +0530 Subject: [PATCH 19/22] fix lint errors --- examples/nidaqmx_analog_input/teststand_fixture.py | 7 ------- examples/nidigital_spi/teststand_fixture.py | 2 +- examples/nidmm_measurement/teststand_fixture.py | 2 +- examples/nifgen_standard_function/teststand_fixture.py | 2 +- examples/niscope_acquire_waveform/teststand_fixture.py | 2 +- examples/niswitch_control_relays/teststand_fixture.py | 2 +- examples/nivisa_dmm_measurement/measurement.py | 4 ++-- 7 files changed, 7 insertions(+), 14 deletions(-) diff --git a/examples/nidaqmx_analog_input/teststand_fixture.py b/examples/nidaqmx_analog_input/teststand_fixture.py index 0fc65c33f..77e2b5b53 100644 --- a/examples/nidaqmx_analog_input/teststand_fixture.py +++ b/examples/nidaqmx_analog_input/teststand_fixture.py @@ -4,11 +4,6 @@ import nidaqmx from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport from _nidaqmx_helpers import create_task -from nidaqmx.grpc_session_options import ( - GRPC_SERVICE_INTERFACE_NAME, - GrpcSessionOptions, - SessionInitializationBehavior, -) import ni_measurementlink_service as nims @@ -48,8 +43,6 @@ def create_nidaqmx_tasks(sequence_context: Any) -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - session_kwargs = {} - teststand_support = TestStandSupport(sequence_context) pin_map_id = teststand_support.get_active_pin_map_id() diff --git a/examples/nidigital_spi/teststand_fixture.py b/examples/nidigital_spi/teststand_fixture.py index 9e89bb8ff..13dec8759 100644 --- a/examples/nidigital_spi/teststand_fixture.py +++ b/examples/nidigital_spi/teststand_fixture.py @@ -1,5 +1,5 @@ """Functions to set up and tear down sessions of NI Digital Pattern instruments in NI TestStand.""" -from typing import Any, Dict, Iterable +from typing import Any, Iterable import nidigital from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport diff --git a/examples/nidmm_measurement/teststand_fixture.py b/examples/nidmm_measurement/teststand_fixture.py index 39b613c98..22f8644db 100644 --- a/examples/nidmm_measurement/teststand_fixture.py +++ b/examples/nidmm_measurement/teststand_fixture.py @@ -1,5 +1,5 @@ """Functions to set up and tear down sessions of NI-DMM devices in NI TestStand.""" -from typing import Any, Dict +from typing import Any import nidmm from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport diff --git a/examples/nifgen_standard_function/teststand_fixture.py b/examples/nifgen_standard_function/teststand_fixture.py index 7e678fb20..c936c26ab 100644 --- a/examples/nifgen_standard_function/teststand_fixture.py +++ b/examples/nifgen_standard_function/teststand_fixture.py @@ -1,5 +1,5 @@ """Functions to set up and tear down sessions of NI-FGEN devices in NI TestStand.""" -from typing import Any, Dict +from typing import Any import nifgen from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport diff --git a/examples/niscope_acquire_waveform/teststand_fixture.py b/examples/niscope_acquire_waveform/teststand_fixture.py index b738fa4fd..df58739cf 100644 --- a/examples/niscope_acquire_waveform/teststand_fixture.py +++ b/examples/niscope_acquire_waveform/teststand_fixture.py @@ -1,5 +1,5 @@ """Functions to set up and tear down sessions of NI-Scope devices in NI TestStand.""" -from typing import Any, Dict +from typing import Any import niscope from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport diff --git a/examples/niswitch_control_relays/teststand_fixture.py b/examples/niswitch_control_relays/teststand_fixture.py index c85dc2edb..b32141aa9 100644 --- a/examples/niswitch_control_relays/teststand_fixture.py +++ b/examples/niswitch_control_relays/teststand_fixture.py @@ -1,5 +1,5 @@ """Functions to set up and tear down sessions of NI-Switch devices in NI TestStand.""" -from typing import Any, Dict +from typing import Any import niswitch from _helpers import GrpcChannelPoolHelper, PinMapClient, TestStandSupport diff --git a/examples/nivisa_dmm_measurement/measurement.py b/examples/nivisa_dmm_measurement/measurement.py index 0231a5a79..94d1c2f8a 100644 --- a/examples/nivisa_dmm_measurement/measurement.py +++ b/examples/nivisa_dmm_measurement/measurement.py @@ -1,6 +1,5 @@ """Perform a DMM measurement using NI-VISA and an NI Instrument Simulator v2.0.""" -import contextlib import logging import pathlib from typing import Tuple @@ -105,7 +104,8 @@ def measure( log_instrument_id(session) # When this measurement is called from outside of TestStand (session_exists == False), - # reset the instrument to a known state. In TestStand, ProcessSetup resets the instrument. + # reset the instrument to a known state. + # In TestStand, ProcessSetup resets the instrument. if not session_info.session_exists: reset_instrument(session) From 5c2ecbb1b01706ddd11e86c56cb6257a0e6077a9 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 20:58:07 +0530 Subject: [PATCH 20/22] minor fixes to the helpers --- examples/nidaqmx_analog_input/_helpers.py | 6 +++--- examples/nidcpower_source_dc_voltage/_helpers.py | 6 +++--- .../nidcpower_source_dc_voltage/teststand_fixture.py | 11 +++++------ examples/nidigital_spi/_helpers.py | 6 +++--- examples/nidmm_measurement/_helpers.py | 6 +++--- examples/nidmm_measurement/teststand_fixture.py | 9 +++++---- examples/nifgen_standard_function/_helpers.py | 6 +++--- .../nifgen_standard_function/teststand_fixture.py | 9 +++++---- examples/niscope_acquire_waveform/_helpers.py | 6 +++--- .../niscope_acquire_waveform/teststand_fixture.py | 9 +++++---- examples/niswitch_control_relays/_helpers.py | 6 +++--- examples/niswitch_control_relays/teststand_fixture.py | 9 +++++---- 12 files changed, 46 insertions(+), 43 deletions(-) diff --git a/examples/nidaqmx_analog_input/_helpers.py b/examples/nidaqmx_analog_input/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/nidaqmx_analog_input/_helpers.py +++ b/examples/nidaqmx_analog_input/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/nidcpower_source_dc_voltage/_helpers.py b/examples/nidcpower_source_dc_voltage/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/nidcpower_source_dc_voltage/_helpers.py +++ b/examples/nidcpower_source_dc_voltage/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/nidcpower_source_dc_voltage/teststand_fixture.py b/examples/nidcpower_source_dc_voltage/teststand_fixture.py index 4a80ce3fe..8b409890a 100644 --- a/examples/nidcpower_source_dc_voltage/teststand_fixture.py +++ b/examples/nidcpower_source_dc_voltage/teststand_fixture.py @@ -46,8 +46,10 @@ def create_nidcpower_sessions(sequence_context: Any) -> None: teststand_support = TestStandSupport(sequence_context) pin_map_id = teststand_support.get_active_pin_map_id() - pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidcpower.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DCPOWER, @@ -57,15 +59,12 @@ def create_nidcpower_sessions(sequence_context: Any) -> None: timeout=60, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidcpower.GRPC_SERVICE_INTERFACE_NAME - ) - create_session( + # Leave session open + _ = create_session( session_info, grpc_device_channel, initialization_behavior=nidcpower.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, ) - session_management_client.register_sessions(reservation.session_info) diff --git a/examples/nidigital_spi/_helpers.py b/examples/nidigital_spi/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/nidigital_spi/_helpers.py +++ b/examples/nidigital_spi/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/nidmm_measurement/_helpers.py b/examples/nidmm_measurement/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/nidmm_measurement/_helpers.py +++ b/examples/nidmm_measurement/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/nidmm_measurement/teststand_fixture.py b/examples/nidmm_measurement/teststand_fixture.py index 22f8644db..fc4d93a46 100644 --- a/examples/nidmm_measurement/teststand_fixture.py +++ b/examples/nidmm_measurement/teststand_fixture.py @@ -49,6 +49,9 @@ def create_nidmm_sessions(sequence_context: Any) -> None: teststand_support = TestStandSupport(sequence_context) pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidmm.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DMM, @@ -56,10 +59,8 @@ def create_nidmm_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidmm.GRPC_SERVICE_INTERFACE_NAME - ) - create_session( + # Leave session open + _ = create_session( session_info, grpc_device_channel, initialization_behavior=nidmm.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, diff --git a/examples/nifgen_standard_function/_helpers.py b/examples/nifgen_standard_function/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/nifgen_standard_function/_helpers.py +++ b/examples/nifgen_standard_function/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/nifgen_standard_function/teststand_fixture.py b/examples/nifgen_standard_function/teststand_fixture.py index c936c26ab..c3ac8b3a3 100644 --- a/examples/nifgen_standard_function/teststand_fixture.py +++ b/examples/nifgen_standard_function/teststand_fixture.py @@ -49,6 +49,9 @@ def create_nifgen_sessions(sequence_context: Any) -> None: teststand_support = TestStandSupport(sequence_context) pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nifgen.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_FGEN, @@ -56,10 +59,8 @@ def create_nifgen_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nifgen.GRPC_SERVICE_INTERFACE_NAME - ) - create_session( + # Leave session open + _ = create_session( session_info, grpc_device_channel, initialization_behavior=nifgen.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, diff --git a/examples/niscope_acquire_waveform/_helpers.py b/examples/niscope_acquire_waveform/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/niscope_acquire_waveform/_helpers.py +++ b/examples/niscope_acquire_waveform/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/niscope_acquire_waveform/teststand_fixture.py b/examples/niscope_acquire_waveform/teststand_fixture.py index df58739cf..40dd6ce4d 100644 --- a/examples/niscope_acquire_waveform/teststand_fixture.py +++ b/examples/niscope_acquire_waveform/teststand_fixture.py @@ -50,6 +50,9 @@ def create_niscope_sessions(sequence_context: Any) -> None: pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niscope.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_SCOPE, @@ -57,10 +60,8 @@ def create_niscope_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - niscope.GRPC_SERVICE_INTERFACE_NAME - ) - create_session( + # Leave session open + _ = create_session( session_info, grpc_device_channel, initialization_behavior=niscope.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, diff --git a/examples/niswitch_control_relays/_helpers.py b/examples/niswitch_control_relays/_helpers.py index fb16d3d81..58df78911 100644 --- a/examples/niswitch_control_relays/_helpers.py +++ b/examples/niswitch_control_relays/_helpers.py @@ -3,7 +3,7 @@ import logging import pathlib import types -from typing import Any, Callable, Dict, NamedTuple, TypeVar, Union +from typing import Any, Callable, Dict, NamedTuple, TypeVar, Optional import click import grpc @@ -242,11 +242,11 @@ def get_grpc_device_channel( measurement_service: MeasurementService, driver_module: types.ModuleType, service_options: ServiceOptions, -) -> Union[grpc.Channel, None]: +) -> Optional[grpc.Channel]: """Returns driver specific grpc device channel.""" if service_options.use_grpc_device: if service_options.grpc_device_address: - return measurement_service.get_channel(service_options.grpc_device_address) + return measurement_service.channel_pool.get_channel(service_options.grpc_device_address) return measurement_service.get_channel( provided_interface=getattr(driver_module, "GRPC_SERVICE_INTERFACE_NAME"), diff --git a/examples/niswitch_control_relays/teststand_fixture.py b/examples/niswitch_control_relays/teststand_fixture.py index b32141aa9..49f327246 100644 --- a/examples/niswitch_control_relays/teststand_fixture.py +++ b/examples/niswitch_control_relays/teststand_fixture.py @@ -50,6 +50,9 @@ def create_niswitch_sessions(sequence_context: Any) -> None: pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niswitch.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_RELAY_DRIVER, @@ -57,10 +60,8 @@ def create_niswitch_sessions(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - niswitch.GRPC_SERVICE_INTERFACE_NAME - ) - create_session( + # Leave session open + _ = create_session( session_info, grpc_device_channel, initialization_behavior=niswitch.SessionInitializationBehavior.INITIALIZE_SERVER_SESSION, From 31fed3bdfd7cdd0e71cf798273810dcf0e0ec336 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Tue, 13 Jun 2023 21:17:32 +0530 Subject: [PATCH 21/22] wrap the comments to a 100 columns --- examples/nivisa_dmm_measurement/measurement.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/nivisa_dmm_measurement/measurement.py b/examples/nivisa_dmm_measurement/measurement.py index 94d1c2f8a..edd5a5ac7 100644 --- a/examples/nivisa_dmm_measurement/measurement.py +++ b/examples/nivisa_dmm_measurement/measurement.py @@ -104,8 +104,8 @@ def measure( log_instrument_id(session) # When this measurement is called from outside of TestStand (session_exists == False), - # reset the instrument to a known state. - # In TestStand, ProcessSetup resets the instrument. + # reset the instrument to a known state. In TestStand, ProcessSetup resets the + # instrument. if not session_info.session_exists: reset_instrument(session) From 10038ef91732d64828f57b304f33e5b847988673 Mon Sep 17 00:00:00 2001 From: Subash Suresh Date: Wed, 14 Jun 2023 14:41:24 +0530 Subject: [PATCH 22/22] update test fixtures --- examples/nidaqmx_analog_input/teststand_fixture.py | 13 ++++++------- .../teststand_fixture.py | 7 +++---- examples/nidmm_measurement/teststand_fixture.py | 8 +++----- .../nifgen_standard_function/teststand_fixture.py | 8 +++----- .../niscope_acquire_waveform/teststand_fixture.py | 7 +++---- .../niswitch_control_relays/teststand_fixture.py | 7 +++---- 6 files changed, 21 insertions(+), 29 deletions(-) diff --git a/examples/nidaqmx_analog_input/teststand_fixture.py b/examples/nidaqmx_analog_input/teststand_fixture.py index 77e2b5b53..db8c6aa4a 100644 --- a/examples/nidaqmx_analog_input/teststand_fixture.py +++ b/examples/nidaqmx_analog_input/teststand_fixture.py @@ -47,6 +47,9 @@ def create_nidaqmx_tasks(sequence_context: Any) -> None: pin_map_id = teststand_support.get_active_pin_map_id() pin_map_context = nims.session_management.PinMapContext(pin_map_id=pin_map_id, sites=None) + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidaqmx.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_sessions( context=pin_map_context, instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DAQMX, @@ -54,9 +57,6 @@ def create_nidaqmx_tasks(sequence_context: Any) -> None: timeout=0, ) as reservation: for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidaqmx.GRPC_SERVICE_INTERFACE_NAME - ) task = create_task( session_info, grpc_device_channel, @@ -73,7 +73,9 @@ def destroy_nidaqmx_tasks() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidaqmx.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DAQMX, # This code module sets up the sessions, so error immediately if they are in use. @@ -82,9 +84,6 @@ def destroy_nidaqmx_tasks() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidaqmx.GRPC_SERVICE_INTERFACE_NAME - ) task = create_task( session_info, grpc_device_channel, diff --git a/examples/nidcpower_source_dc_voltage/teststand_fixture.py b/examples/nidcpower_source_dc_voltage/teststand_fixture.py index 8b409890a..d4cd9c13e 100644 --- a/examples/nidcpower_source_dc_voltage/teststand_fixture.py +++ b/examples/nidcpower_source_dc_voltage/teststand_fixture.py @@ -74,7 +74,9 @@ def destroy_nidcpower_sessions() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidcpower.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DCPOWER, # This code module sets up the sessions, so error immediately if they are in use. @@ -83,9 +85,6 @@ def destroy_nidcpower_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidcpower.GRPC_SERVICE_INTERFACE_NAME - ) session = create_session( session_info, grpc_device_channel, diff --git a/examples/nidmm_measurement/teststand_fixture.py b/examples/nidmm_measurement/teststand_fixture.py index fc4d93a46..6361f7478 100644 --- a/examples/nidmm_measurement/teststand_fixture.py +++ b/examples/nidmm_measurement/teststand_fixture.py @@ -75,18 +75,16 @@ def destroy_nidmm_sessions() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nidmm.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_DMM, # This code module sets up the sessions, so error immediately if they are in use. timeout=0, ) as reservation: session_management_client.unregister_sessions(reservation.session_info) - for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nidmm.GRPC_SERVICE_INTERFACE_NAME - ) session = create_session( session_info, grpc_device_channel, diff --git a/examples/nifgen_standard_function/teststand_fixture.py b/examples/nifgen_standard_function/teststand_fixture.py index c3ac8b3a3..2fa579856 100644 --- a/examples/nifgen_standard_function/teststand_fixture.py +++ b/examples/nifgen_standard_function/teststand_fixture.py @@ -75,18 +75,16 @@ def destroy_nifgen_sessions() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + nifgen.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_FGEN, # This code module sets up the sessions, so error immediately if they are in use. timeout=0, ) as reservation: session_management_client.unregister_sessions(reservation.session_info) - for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - nifgen.GRPC_SERVICE_INTERFACE_NAME - ) session = create_session( session_info, grpc_device_channel, diff --git a/examples/niscope_acquire_waveform/teststand_fixture.py b/examples/niscope_acquire_waveform/teststand_fixture.py index 40dd6ce4d..48b971e7a 100644 --- a/examples/niscope_acquire_waveform/teststand_fixture.py +++ b/examples/niscope_acquire_waveform/teststand_fixture.py @@ -76,7 +76,9 @@ def destroy_niscope_sessions() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niscope.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_SCOPE, # This code module sets up the sessions, so error immediately if they are in use. @@ -85,9 +87,6 @@ def destroy_niscope_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - niscope.GRPC_SERVICE_INTERFACE_NAME - ) session = create_session( session_info, grpc_device_channel, diff --git a/examples/niswitch_control_relays/teststand_fixture.py b/examples/niswitch_control_relays/teststand_fixture.py index 49f327246..22e5ee30d 100644 --- a/examples/niswitch_control_relays/teststand_fixture.py +++ b/examples/niswitch_control_relays/teststand_fixture.py @@ -76,7 +76,9 @@ def destroy_niswitch_sessions() -> None: session_management_client = nims.session_management.Client( grpc_channel=grpc_channel_pool.session_management_channel ) - + grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( + niswitch.GRPC_SERVICE_INTERFACE_NAME + ) with session_management_client.reserve_all_registered_sessions( instrument_type_id=nims.session_management.INSTRUMENT_TYPE_NI_RELAY_DRIVER, # This code module sets up the sessions, so error immediately if they are in use. @@ -85,9 +87,6 @@ def destroy_niswitch_sessions() -> None: session_management_client.unregister_sessions(reservation.session_info) for session_info in reservation.session_info: - grpc_device_channel = grpc_channel_pool.get_grpc_device_channel( - niswitch.GRPC_SERVICE_INTERFACE_NAME - ) session = create_session( session_info, grpc_device_channel,