Search documentation of bb

# BB.Server.ParamResolution (bb v0.17.0)

Copy Markdown View Source

Shared parameter resolution logic for wrapper servers.

This module provides functions for resolving `ParamRef` values in options and subscribing to parameter change notifications. Used by:

- `BB.Actuator.Server`
- `BB.Sensor.Server`
- `BB.Controller.Server`
- `BB.Command.Server`

## Usage

In your server's `init/1`:

    {param_subscriptions, resolved_opts} =
      ParamResolution.resolve_and_subscribe(raw_opts, robot_module)

In `handle_info/2` for parameter changes:

    def handle_info({:bb, [:param | param_path], %{payload: %ParameterChanged{}}}, state) do
      ParamResolution.handle_param_change(
        param_path,
        state.param_subscriptions,
        state.raw_opts,
        robot_module,
        fn new_resolved ->
          # Call user's handle_options callback
        end
      )
    end

## Summary

### Types

subscriptions()

### Functions

handle_change(param_path, subscriptions, raw_opts, robot_module_or_state)

Handle a parameter change message.

resolve(opts, robot_module_or_state)

Resolve ParamRefs in options without subscribing.

resolve_and_subscribe(opts, robot_module_or_state)

Resolve ParamRefs in options and subscribe to parameter change topics.

subscribe(robot_module, subscriptions)

Subscribe to parameter change topics for tracked parameters.

## Types

# subscriptions()

    @type subscriptions() :: %{required([atom()]) => atom()}

## Functions

# handle_change(param_path, subscriptions, raw_opts, robot_module_or_state)

    @spec handle_change(
      [atom()],
      subscriptions(),
      keyword(),
      module() | BB.Robot.State.t()
    ) ::
      {:changed, keyword()} | :ignored

Handle a parameter change message.

Checks if the changed parameter is one we're subscribed to, re-resolves all options, and calls the provided callback with the new options.

Returns `{:changed, new_resolved}` if the parameter was tracked, or `:ignored` if not.

# resolve(opts, robot_module_or_state)

    @spec resolve(
      keyword(),
      module() | BB.Robot.State.t()
    ) :: {subscriptions(), keyword()}

Resolve ParamRefs in options without subscribing.

Takes either a robot module or a `BB.Robot.State` struct directly. Passing the state directly avoids a lookup and potential deadlock during init when the Runtime is still starting.

# resolve_and_subscribe(opts, robot_module_or_state)

    @spec resolve_and_subscribe(
      keyword(),
      module() | BB.Robot.State.t()
    ) :: {subscriptions(), keyword()}

Resolve ParamRefs in options and subscribe to parameter change topics.

Returns `{subscriptions_map, resolved_opts}` where:

- `subscriptions_map` maps parameter paths to option keys
- `resolved_opts` has ParamRefs replaced with their current values

This is a convenience function that calls `resolve/2` and `subscribe/2`.

# subscribe(robot_module, subscriptions)

    @spec subscribe(module(), subscriptions()) :: :ok

Subscribe to parameter change topics for tracked parameters.