Skip to main content

sample_with_config

Function sample_with_config 

Source
pub(crate) async fn sample_with_config(
    run_id: usize,
    cfg: SampleConfig,
    config_rx: &mut Receiver<SampleConfig>,
    shutdown_rx: &mut Receiver<bool>,
    js: &Context,
) -> Result<(), LJMError>
Expand description

Runs one LabJack streaming session with a fixed configuration.

In order, the session:

  1. Creates or updates the JetStream stream (ensure_stream_exists).
  2. Opens and checks the LabJack (labjack::open_labjack_from_env) and wraps the handle in a LabJackGuard.
  3. Configures all analog inputs as single-ended (T7 only), +/-10 V range, resolution index 0 and STREAM_SETTLING_US 0.
  4. Starts the hardware stream and derives the sample interval from the rate the device actually runs at.
  5. Reads batches on a blocking thread, replaces LJM dummy values with NaN, splits each batch by channel, encodes one FlatBuffer Scan per channel, and publishes it to the channel’s subject.

It runs until the configuration changes, shutdown is signaled, or an error occurs. A publish that fails is logged and that channel’s message for the batch is dropped; it does not end the session.

§Arguments

  • run_id - Run counter from run_sampler, used in log lines.
  • cfg - Configuration for this session.
  • config_rx - Configuration channel; any change ends the session.
  • shutdown_rx - Shutdown flag; true ends the session.
  • js - JetStream context of the local NATS server.

§Returns

Ok(()) when the session stopped because of a configuration change or shutdown.

§Errors

Returns an error if stream setup fails, the LabJack cannot be opened or configured, a channel number has no AIN register, stream_start fails or reports an invalid rate, the reader stops (for example on an LJM read error), a batch is empty or not a multiple of the channel count, or a timestamp cannot be computed.