init
This commit is contained in:
109
DataPRO/IService/StateMachine/States.cs
Normal file
109
DataPRO/IService/StateMachine/States.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using DTS.Common.Interface.DASFactory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DTS.DASLib.Service.StateMachine
|
||||
{
|
||||
public enum State
|
||||
{
|
||||
Prepare,
|
||||
HardwareDiscovery,
|
||||
HardwareDiscoveryStart,
|
||||
Configure,
|
||||
ConfigureStart,
|
||||
Diagnose,
|
||||
Realtime,
|
||||
Arming,
|
||||
Arm,
|
||||
Download,
|
||||
RealtimeStart,
|
||||
DownloadStart
|
||||
}
|
||||
|
||||
public class States
|
||||
{
|
||||
private static readonly States instance = new States();
|
||||
private Dictionary<State, IDASState> _statesDict = new Dictionary<State, IDASState>();
|
||||
private Dictionary<State, IDASStateWithSelector> _statesSelectableDict = new Dictionary<State, IDASStateWithSelector>();
|
||||
|
||||
// Explicit static constructor to tell C# compiler
|
||||
// not to mark type as beforefieldinit
|
||||
// https://csharpindepth.com/articles/singleton
|
||||
static States()
|
||||
{
|
||||
}
|
||||
|
||||
public IDASState Prepare { get => _statesDict[State.Prepare]; }
|
||||
public IDASState HardwareDiscovery { get => _statesDict[State.HardwareDiscovery]; }
|
||||
public IDASStateWithSelector HardwareDiscoveryStart { get => _statesSelectableDict[State.HardwareDiscoveryStart]; }
|
||||
public IDASState Download { get => _statesDict[State.Download]; }
|
||||
public IDASStateWithSelector DownloadStart { get => _statesSelectableDict[State.DownloadStart]; }
|
||||
public IDASStateWithSelector Diagnose { get => _statesSelectableDict[State.Diagnose]; }
|
||||
public IDASStateWithSelector Configure { get => _statesSelectableDict[State.Configure]; }
|
||||
public IDASState Arming { get => _statesDict[State.Arming]; }
|
||||
public IDASState Arm { get => _statesDict[State.Arm]; }
|
||||
public IDASState Realtime { get => _statesDict[State.Realtime]; }
|
||||
public IDASStateWithSelector RealtimeStart { get => _statesSelectableDict[State.RealtimeStart]; }
|
||||
public IDASStateWithSelector ConfigureStart
|
||||
{
|
||||
get => _statesSelectableDict[State.ConfigureStart];
|
||||
}
|
||||
|
||||
public static void SetDASFactory(IDASFactory dasFactory)
|
||||
{
|
||||
foreach (var enumState in Instance._statesDict)
|
||||
{
|
||||
enumState.Value.DASFactory = dasFactory;
|
||||
}
|
||||
|
||||
foreach (var enumState in Instance._statesSelectableDict)
|
||||
{
|
||||
enumState.Value.DASFactory = dasFactory;
|
||||
}
|
||||
}
|
||||
private States()
|
||||
{
|
||||
AddState(State.Prepare, new Prepare());
|
||||
AddState(State.HardwareDiscovery, new HardwareDiscovery());
|
||||
AddState(State.HardwareDiscoveryStart, new HardwareDiscoveryStart());
|
||||
AddState(State.Configure, new Configure());
|
||||
AddState(State.Download, new Download());
|
||||
AddState(State.Diagnose, new Diagnose());
|
||||
AddState(State.Arming, new Arming());
|
||||
AddState(State.Arm, new Arm());
|
||||
AddState(State.Realtime, new Realtime());
|
||||
AddState(State.ConfigureStart, new ConfigureStart());
|
||||
AddState(State.RealtimeStart, new RealtimeStart());
|
||||
AddState(State.DownloadStart, new DownloadStart());
|
||||
}
|
||||
private void AddState<T>(State state, T dasState)
|
||||
{
|
||||
if (dasState is IDASStateWithSelector)
|
||||
{
|
||||
_statesSelectableDict.Add(state, (IDASStateWithSelector)dasState);
|
||||
return;
|
||||
}
|
||||
_statesDict.Add(state, (IDASState)dasState);
|
||||
}
|
||||
public IDASState GetIDASState(State state)
|
||||
{
|
||||
if (_statesDict.ContainsKey(state))
|
||||
{
|
||||
return _statesDict[state];
|
||||
}
|
||||
if (_statesSelectableDict.ContainsKey(state))
|
||||
{
|
||||
return _statesSelectableDict[state];
|
||||
}
|
||||
throw new InvalidOperationException("Undefined State");
|
||||
}
|
||||
|
||||
public static States Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user