Class Switch
A two-input switch that forwards data from the currently active input to downstream blocks.
JSON configuration
Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.
JSON configuration:
{
"InputSwitch": {
"Type": "Switch",
"Inputs": [ "SourceA", "SourceB" ],
"Path": "C:/Data/session1"
}
}
Params: None. Active input is controlled at runtime via the UI or programmatically.
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.FlowControl
Assembly: MOSAIC.dll
Syntax
public class Switch : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
Overview: The Switch block accepts connections from exactly two upstream blocks. At any time, only one input is "active" — data from the active input is published downstream, while data from the inactive input is silently dropped.
Input identification: Senders are identified by reference on first arrival. The first distinct sender becomes input 0, the second becomes input 1. This assignment is stable for the lifetime of the block and depends on which upstream block publishes first.
Switching: Use Toggle() or SetActiveInput(int) to change the active input at runtime. The UseSecondInput property is observable for UI binding.
CSV logging: When a Path is specified, Dumper
automatically logs whatever the active input publishes via Publish(object).
Constructors
Switch(string, double)
Initializes a new Switch block.
Declaration
public Switch(string name, double desiredRate = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name of the block. |
| double | desiredRate | Desired processing rate in Hz (typically inherited). |
Fields
_input0Sender
Reference to the first sender (input 0), assigned on first data arrival.
Declaration
private object? _input0Sender
Field Value
| Type | Description |
|---|---|
| object |
_input1Sender
Reference to the second sender (input 1), assigned when a distinct sender arrives.
Declaration
private object? _input1Sender
Field Value
| Type | Description |
|---|---|
| object |
_useSecondInput
Declaration
[ObservableProperty]
private bool _useSecondInput
Field Value
| Type | Description |
|---|---|
| bool |
Properties
DumpFilePrefix
Recordings from this block are named <block>_switched.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
JsonTypeName
Block type identifier for JSON serialization. Override for a custom name.
Declaration
protected override string JsonTypeName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
MaxInputs
Gets the maximum number of input connections allowed for this block.
Declaration
public override int MaxInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
Remarks
Override in derived classes to specify input requirements. Default is 1 (most blocks accept exactly one input). Use MaxValue for blocks that accept unlimited inputs (e.g., Joiner).
MinInputs
Gets the minimum number of input connections required for this block.
Declaration
public override int MinInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
Remarks
Override in derived classes to specify input requirements. Default is 1 (most blocks require exactly one input). Source blocks (devices, generators) should return 0.
UseSecondInput
Declaration
[ExcludeFromCodeCoverage]
public bool UseSecondInput { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
ConfigureInput(IServiceProvider, JsonModel)
Creates a Switch from a JSON pipeline definition.
Declaration
public static Switch ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | Service provider for dependency injection. |
| JsonModel | m | JSON model containing configuration. |
Returns
| Type | Description |
|---|---|
| Switch | A configured Switch instance. |
OnReceive(object, object?)
Identifies which input the data came from and forwards it if from the active input.
Declaration
protected override void OnReceive(object sender, object? value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The upstream block. |
| object | value | The data payload (any type — forwarded transparently). |
Overrides
Remarks
Sender identification is by reference: the first distinct sender becomes input 0, the second becomes input 1. Data from the inactive input is silently dropped.
OnUseSecondInputChanged(bool)
Executes the logic for when UseSecondInput just changed.
Declaration
private void OnUseSecondInputChanged(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value that was set. |
Remarks
This method is invoked right after the value of UseSecondInput is changed.
OnUseSecondInputChanged(bool, bool)
Executes the logic for when UseSecondInput just changed.
Declaration
private void OnUseSecondInputChanged(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that was replaced. |
| bool | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of UseSecondInput is changed.
OnUseSecondInputChanging(bool)
Executes the logic for when UseSecondInput is changing.
Declaration
private void OnUseSecondInputChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of UseSecondInput is changed.
OnUseSecondInputChanging(bool, bool)
Executes the logic for when UseSecondInput is changing.
Declaration
private void OnUseSecondInputChanging(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that is being replaced. |
| bool | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of UseSecondInput is changed.
SetActiveInput(int)
Sets which input is active by index and raises InputSwitched.
Declaration
public void SetActiveInput(int inputIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | inputIndex | 0 for the first input, 1 for the second. |
Toggle()
Toggles between the two inputs and raises InputSwitched.
Declaration
public void Toggle()
Events
InputSwitched
Raised when the active input changes, with the new UseSecondInput value.
Declaration
public event Action<bool>? InputSwitched
Event Type
| Type | Description |
|---|---|
| Action<bool> |