11 KiB
11 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||||
|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T03:56:17.184753+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 86e0fafc151b8d16 |
State Machine Module Documentation
1. Purpose
This module implements a state machine for managing the operational lifecycle of a data acquisition system (DAS), using the Stateless library. It defines a finite state machine over a set of discrete states (e.g., Prepare, HardwareDiscovery, Configure, Realtime) and triggers (e.g., PingAndConnect, ApplyConfiguration, StartRealtime) that govern transitions between those states. The module centralizes system state management and provides high-level methods (e.g., PingAndConnect, UpdateConfig, StartRealtime) that configure and fire state transitions with associated parameters and callbacks. It acts as the orchestrator for hardware initialization, configuration, diagnostics, and real-time data acquisition workflows.
2. Public Interface
enum Trigger
- Definition:
enum Trigger { PingAndConnect, Reset, ResolveChannelsAuto, ResolveChannelsManual, ApplyConfiguration, TurnOffExcitation, Cancel, Finish, Arm, StartRealtime, RequeryDevice, Download } - Behavior: Defines the set of events that can cause state transitions in the state machine. Each trigger corresponds to a specific user or system-initiated action.
interface IDASState
- Properties:
Status Status { get; }— Returns the sharedStatusobject (seeDASState).IDASFactory DASFactory { get; set; }— Factory used to create DAS components.Action OnEntry { get; }— Action to execute on state entry (defaults toOnEnterState).Action OnExit { get; }— Action to execute on state exit (defaults toOnExitState).State State { get; }— The enum value representing this state.
- Methods:
void OnEnterState()— Virtual method called on state entry (logs state name).void OnExitState()— Virtual method called on state exit (logs state name).
interface IDASStateWithSelector : IDASState
- Methods:
IDASState StateSelector()— Returns the next state to transition to dynamically (used for conditional transitions).
abstract class DASState : IDASState
- Properties:
IDASFactory DASFactory { get; set; }abstract State State { get; }virtual Action OnEntry { get => OnEnterState; }virtual Action OnExit { get => OnExitState; }
- Properties:
Status Status { get; }— Returns the shared static_statusinstance (see Gotchas).
- Methods:
virtual void OnEnterState()— Logs"Enter {State} State".virtual void OnExitState()— Logs"Exit {State} State".
abstract class DASStateSelector : DASState, IDASStateWithSelector
- Methods:
public abstract IDASState StateSelector();— Must be implemented to determine dynamic next state.
enum State
- Definition:
enum State { Prepare, HardwareDiscovery, HardwareDiscoveryStart, Configure, ConfigureStart, Diagnose, Realtime, Arming, Arm, Download, RealtimeStart, DownloadStart } - Behavior: Defines the set of states in the state machine. Some states (
HardwareDiscoveryStart,ConfigureStart,RealtimeStart,DownloadStart,Diagnose,Configure) are selector states (implementIDASStateWithSelector), others are simple states.
class States
- Properties (singleton access to state instances):
IDASState Prepare,HardwareDiscovery,Download,Arming,Arm,RealtimeIDASStateWithSelector HardwareDiscoveryStart,DownloadStart,Diagnose,Configure,ConfigureStart,RealtimeStart
- Methods:
public static void SetDASFactory(IDASFactory dasFactory)— Assigns the factory to all state instances.public IDASState GetIDASState(State state)— Returns the state object for a givenStateenum.public static States Instance { get; }— Singleton instance.
class StateMachineBootstrap
- Constructor:
StateMachineBootstrap(IDASFactory dasFactory = null)— Initializes the state machine withPrepareas the initial state, sets up logging, and callsConfigure().
- Public Methods:
void TurnOffExcitation(...)— ConfiguresConfigureStartparameters to skip power and setTurnOffExcitation = true, then firesTrigger.TurnOffExcitation.void PrepareForDiagnostics(...)— ConfiguresConfigureStartparameters for diagnostics (PrepareForDiagnostics = true,SetConfiguration = false, etc.), then firesTrigger.ApplyConfiguration.void UpdateConfig(...)— ConfiguresConfigureStartparameters for full configuration, then firesTrigger.ApplyConfiguration.void PingAndConnectAndCheckHardware(...)— ConfiguresHardwareDiscoveryparameters with full hardware checks, then firesTrigger.PingAndConnect.void PingAndConnect(...)— Same as above, but setsDoHardwareChecks = false.void RequeryDevice(IDASCommunication device, ...)— ConfiguresHardwareDiscoveryto requery a specific device, then firesTrigger.RequeryDevice.void Ping(bool UseUDPDiscovery, string[] ipAddresses, ...)— FiresTrigger.PingAndConnectwithConnect = false.void StartRealtime(...)— ConfiguresRealtimeparameters and firesTrigger.StartRealtime.void StopRealtime()— CallsStopRealtime()on theRealtimestate’s status object.bool IsInRealtime { get; }— ReturnsIsInRealtimefromRealtimestate’s status.void Reset()— FiresTrigger.Resetand resets the sharedStatusobject.
- Private Helper Methods (used internally by public methods):
GetConfigureStatus(),GetConfigureParameters()GetDiagnoseParameters()GetPingAndConnectParameters(),GetPingAndConnectStatus()GetRealtimeParameters(),GetRealtimeStatus()
- Private Methods (internal testing/simulation):
void Configure()— Defines all state machine transitions (see Dependencies).void GenerateStateMachineGraph()— Outputs DOT graph for visualization.void Start(),SimulatePingAndConnectComplete(),SimulatePingAndConnectWithCancel(),BasicInfoToDiagnostics(),BasicInfoToRealtime(),StartAndStopRealtime(),ResolveChannelsAuto(),ResolveChannelsManual(),ApplyConfiguration()— Internal simulation/test methods.
3. Invariants
- Shared Status Object: All
DASStateinstances share the same staticStatusinstance (_status). This is explicitly noted in the source as a temporary design decision to prevent accidental duplication. - State Hierarchy: States like
HardwareDiscoveryStart,ConfigureStart,RealtimeStart,DownloadStartare substates of their parent states (HardwareDiscovery,Configure,Realtime,Downloadrespectively). - Dynamic Transitions: Transitions using
PermitDynamicIf(e.g.,Trigger.FinishfromHardwareDiscoveryStart,RealtimeStart,Diagnose) rely on theStateSelector()method of the current state to determine the next state. - Trigger Semantics:
Trigger.Finishalways transitions to the parent state (viaStateSelector()).Trigger.Canceltransitions back to the parent state (e.g.,ConfigureStart→Configure).Trigger.Resettransitions toPrepare.
- State Entry/Exit Logging: Every state logs entry/exit via
Console.WriteLine, unless overridden.
4. Dependencies
Imports/Usings (from source):
Stateless— Core state machine library.DTS.Common.Interface.DASFactory— DefinesIDASFactory.DTS.Common.Interface.StatusAndProgressBar— DefinesStatus,StatusIntDelegate,StatusExIntDelegate,SetProgressValueDelegate,ActionCompleteDelegate.DTS.Common.Interface.DataRecorders— DefinesIDASCommunication,IDASHardware,IDiscoveredDevice.DTS.Common.Utilities.Logging— DefinesTextLogger,APILogger.DTS.Common.Classes.DSP— DefinesDSPFilterType.System,System.Linq,System.Collections.Generic,System.Text,System.Windows.Forms,System— Standard .NET types.
Inferred Dependencies:
- External Libraries:
Stateless(NuGet package for state machine).DTS.Common.*— Internal libraries for factory, status, data recorders, utilities, DSP, and logging.
- Consumers:
StateMachineBootstrapis the primary public-facing class; it is instantiated and used by higher-level application logic (e.g., UI or service layer).States.Instanceis used byStateMachineBootstrapto access state objects and byConfigure()to set up transitions.
5. Gotchas
- Shared Static Status: All states share the same
Statusinstance (_status). Modifying status in one state affects all others. This is explicitly flagged inDASState.csas a short-term measure pending refactoring. Trigger.PingAndConnectReuse: ThePing,PingAndConnect,PingAndConnectAndCheckHardware, andRequeryDevicemethods all fire the same trigger (Trigger.PingAndConnect) but configure different parameters inHardwareDiscovery’s status/params. The behavior is determined entirely by parameter state, not the trigger itself.Trigger.ResolveChannelsAuto/ResolveChannelsManual: These triggers arePermitReentry, meaning they do not change state but re-trigger the current state (Configure). Their implementation is empty inStateMachineBootstrap(no-op stubs), suggesting logic is elsewhere or incomplete.DownloadState Disabled: TheDownloadandDownloadStartstate configurations are commented out inConfigure(), indicating this functionality is not currently active.- Console Logging Only:
OnEnterState/OnExitStatelog toConsole.WriteLine, not theTextLogger. Real logging is done manually in public methods viaAPILogger.StateLog. - Hardcoded Simulation Logic: Methods like
SimulatePingAndConnectComplete()andBasicInfoToRealtime()contain hardcoded IP ranges and test sequences, suggesting they are for unit testing or demos and should not be used in production. StateSelector()Implementation Not Visible: TheStateSelector()implementations for selector states (e.g.,HardwareDiscoveryStart,ConfigureStart) are not included in the provided source, so their transition logic is unknown.Trigger.DownloadNot Used: TheDownloadstate exists in theStateenum but is not wired into the state machine configuration (Configure()), and no public method usesTrigger.Download.