123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Collections.Generic;
- using System.Collections.Concurrent;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- using Aitex.Core.Utilities;
- using System.Threading.Tasks;
- using Aitex.Core.MES;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.MES.YZQZ;
- namespace Aitex.Core.RT.Fsm
- {
- /// <summary>
- /// FSM transition function
- /// 返回值: true 需要转换状态
- /// false 保持状态不变
- /// </summary>
- /// <param name="param"></param>
- /// <returns></returns>
- public enum FSM_MSG
- {
- TIMER = 0x0FFFFFF0,
- NONE,
- WARNING,
- ALARM,
-
- }
- public enum FSM_STATE
- {
- UNDEFINE = 0x0FFFFFF0,
- SAME,
- ANY,
- }
- public class StateMachine<T> : IStateMachine
- {
- public string Name { get; set; }
- public int ElapsedTime
- {
- get
- {
- int ticket = Environment.TickCount;
- return ticket > enterTime ? ticket - enterTime : 0x7FFFFFFF + ticket - enterTime;
- }
- }
- public int EnterTime { set { enterTime = value; } }
- public int State { get; set; }
- public int PrevState { get; set; }
- private bool _enableRepeatedMsg;
- public int MaxMsgInQueue { get; set; }
- private int enterTime;
- private int interval;
- private CancellationTokenSource cancelToken;
- private BlockingCollection<KeyValuePair<int, object[]>> msgQuene;
- private KeyValuePair<int, object[]> timeoutMsg = new KeyValuePair<int, object[]>((int)FSM_MSG.TIMER, null);
- /// <summary>
- /// 状态迁移表 当前状态, 消息, 函数, 迁移状态
- /// AnyState迁移表 消息, 函数, 迁移状态
- /// 状态进入/退出迁移表 状态,进入函数,进入消息,退出函数,
- /// </summary>
- private Dictionary<int, Dictionary<int, Tuple<FsmFunc, int>>> transition;
- private Dictionary<int, Tuple<FsmFunc, int>> anyStateTransition;
- private Dictionary<int, Tuple<FsmFunc, int, FsmFunc>> enterExitTransition;
- private Dictionary<int, string> _stringState = new Dictionary<int, string>();
- private Dictionary<int, string> _stringMessage = new Dictionary<int, string>();
- DeviceTimer _loopTimer = new DeviceTimer();
- public StateMachine(string name, int initState = (int)FSM_STATE.UNDEFINE, int interval = 100)
- {
- Name = name + " FSM";
- MaxMsgInQueue = 500;
- this.State = initState;
- this.PrevState = initState;
- this.interval = interval;
- cancelToken = new CancellationTokenSource();
- msgQuene = new BlockingCollection<KeyValuePair<int, object[]>>();
- transition = new Dictionary<int, Dictionary<int, Tuple<FsmFunc, int>>>();
- anyStateTransition = new Dictionary<int, Tuple<FsmFunc, int>>();
- enterExitTransition = new Dictionary<int, Tuple<FsmFunc, int, FsmFunc>>();
- EnumLoop<FSM_STATE>.ForEach((item) =>
- {
- MapState((int)item, item.ToString());
- });
- EnumLoop<FSM_MSG>.ForEach((item) =>
- {
- MapMessage((int)item, item.ToString());
- });
- }
- #region Interface
- public void Start()
- {
- OnEnterState(this.State);
- }
- public void Loop()
- {
- while (!cancelToken.IsCancellationRequested)
- {
- KeyValuePair<int, object[]> msg = timeoutMsg;
- try
- {
- int timeout = interval - (int)_loopTimer.GetElapseTime();
- if (timeout < 50)
- timeout = 50;
- if (!msgQuene.TryTake(out msg, timeout, cancelToken.Token))
- msg = timeoutMsg;
- _loopTimer.Start(0);
- //Loop AnyState迁移表
- if (anyStateTransition.ContainsKey(msg.Key))
- {
- if (anyStateTransition[msg.Key].Item1 != null)
- {
- if (anyStateTransition[msg.Key].Item1(msg.Value)) //need ChangeState
- {
- if (anyStateTransition[msg.Key].Item2 != State && anyStateTransition[msg.Key].Item2 != (int)FSM_STATE.SAME)
- {
- OnExitState((int)FSM_STATE.ANY);
- OnExitState(State);
- enterTime = Environment.TickCount;
- PrevState = State;
- State = anyStateTransition[msg.Key].Item2;
-
- OnEnterState((int)FSM_STATE.ANY);
- OnEnterState((int)State);
-
- }
- }
- }
- continue;
- }
- if (!transition.ContainsKey(State))
- {
- if (msg.Key != (int)FSM_MSG.TIMER)
- {
- LOG.Warning("状态机{0}未定义在状态{1}时处理方法。", Name, GetStringState(State));
- }
- continue;
- }
- if (!transition[State].ContainsKey(msg.Key))
- {
- if (msg.Key != (int)FSM_MSG.TIMER)
- {
- LOG.Warning("状态机{0}未定义在状态{1}时收到消息{2}的处理方法。", Name, GetStringState(State), GetStringMessage(msg.Key));
- }
- continue;
- }
- //Loop Transition
- //if fun is null or fun return true need change
- if (transition[State][msg.Key].Item1 == null || transition[State][msg.Key].Item1(msg.Value))
- {
- if (transition[State][msg.Key].Item2 != State && transition[State][msg.Key].Item2 != (int)FSM_STATE.SAME)
- {
- OnExitState((int)FSM_STATE.ANY);
- OnExitState(State);
- enterTime = Environment.TickCount;
- PrevState = State;
- State = transition[State][msg.Key].Item2;
- OnEnterState((int)FSM_STATE.ANY);
- OnEnterState((int)State);
- postYZQZMachineStatus((int)State);
- }
- }
- }
- catch (OperationCanceledException ex)
- {
- LOG.Error("FSM cancel", ex);
- }
- catch (Exception ex)
- {
- LOG.Error(String.Format("{0}: State {1},Msg{2} Loop has exception ", Name, GetStringState(State), GetStringMessage(msg.Key)), ex);
- try
- {
- StackFrame sf = new StackTrace(true).GetFrame(3);
- LOG.Error(string.Format("State Machine running error in {0}, {1}",
- sf.GetFileLineNumber() ,
- sf.GetMethod().Name));
- }
- catch (Exception)
- {
-
- }
-
-
- }
- }
- }
- private async void postYZQZMachineStatus(int state)
- {
- await Task.Run(() => {
-
- Fab fb = (Fab)Enum.Parse(typeof(Fab), SC.GetValue<int>(SCName.System_Company).ToString());
- if (fb == Fab.YZQZ)
- {
-
- FabAutoComm com = new YZQZFabAuto();
-
-
- com.SaveMachineStatusRecord(state);
- }
- });
- }
- public void Stop()
- {
- cancelToken.Cancel();
- }
- string GetStringState(int state)
- {
- if (_stringState.ContainsKey(state))
- return _stringState[state];
- return state.ToString();
- }
- string GetStringMessage(int message)
- {
- if (_stringMessage.ContainsKey(message))
- return _stringMessage[message];
- return message.ToString();
- }
- public void MapState(int state, string stringState)
- {
- System.Diagnostics.Debug.Assert(!_stringState.ContainsKey(state));
- _stringState[state] = stringState;
- }
- public void MapMessage(int message, string stringMessage)
- {
- System.Diagnostics.Debug.Assert(!_stringMessage.ContainsKey(message));
- _stringMessage[message] = stringMessage;
- }
- public bool FindTransition(int state, int msg)
- {
- if (anyStateTransition.ContainsKey(msg))
- return true;
- if (transition.ContainsKey(state) && transition[state] != null)
- {
- Dictionary<int, Tuple<FsmFunc, int>> table = transition[state];
- if (table.ContainsKey(msg))
- return true;
- }
- return false;
- }
- public void Transition(int state, int msg, FsmFunc func, int next)
- {
- if (!transition.ContainsKey(state) || transition[state] == null)
- {
- transition[state] = new Dictionary<int, Tuple<FsmFunc, int>>();
- }
- transition[state][msg] = new Tuple<FsmFunc, int>(func, next);
- }
- public void AnyStateTransition(int msg, FsmFunc func, int next)
- {
- anyStateTransition[msg] = new Tuple<FsmFunc, int>(func, next);
- }
- public void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
- {
- enterExitTransition[state] = new Tuple<FsmFunc, int, FsmFunc>(enter, msg, exit);
- }
- public void EnableRepeatedMsg(bool enable)
- {
- _enableRepeatedMsg = enable;
- }
- public void PostMsg(int msg, params object[] args)
- {
- foreach (var item in msgQuene)
- {
- if (item.Key == msg && !_enableRepeatedMsg)
- {
- if (msg != (int)FSM_MSG.WARNING && msg != (int)FSM_MSG.ALARM)
- LOG.Warning("状态机【{0}】收到重复发送的消息{1},该条消息被忽略。",Name, msg);
- return;
- }
- }
- if (msgQuene.Count > MaxMsgInQueue)
- {
- LOG.Warning("状态机【{0}】收到 的消息超过{1}条,消息{2}被忽略。",Name, MaxMsgInQueue, msg);
- }
- msgQuene.Add(new KeyValuePair<int, object[]>(msg, args));
- }
- #endregion
- private void OnEnterState(int state)
- {
- if (enterExitTransition.ContainsKey(state))
- {
- if (enterExitTransition[state].Item1 != null)
- {
- enterExitTransition[state].Item1(null);
- }
- if (enterExitTransition[state].Item2 != (int)FSM_MSG.NONE)
- {
- PostMsg(enterExitTransition[state].Item2, null);
- }
- }
- }
- private void OnExitState(int state)
- {
- if (enterExitTransition.ContainsKey(state))
- {
- if (enterExitTransition[state].Item3 != null)
- {
- enterExitTransition[state].Item3(null);
- }
- }
- }
- }
- }
|