123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Fsm;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.Routine;
- using Aitex.Core.RT.SCCore;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace FurnaceRT.Equipments.Systems
- {
- public class ModuleFsmDevice : FsmDevice
- {
- public bool IsInstalled
- {
- get
- {
- if (!SC.ContainsItem($"System.SetUp.Is{Module}Installed"))
- return true;
- return SC.GetValue<bool>($"System.SetUp.Is{Module}Installed");
- }
- }
- public virtual bool IsOnline
- {
- get;
- set;
- }
- protected Queue<IRoutine> QueueRoutine
- {
- get { return _routine; }
- }
- private Queue<IRoutine> _routine = new Queue<IRoutine>();
- public ModuleFsmDevice() : base()
- {
- }
- public override bool Initialize()
- {
- return base.Initialize();
- }
- public Result StartRoutine(IRoutine routine)
- {
- QueueRoutine.Clear();
- QueueRoutine.Enqueue(routine);
- return QueueRoutine.Peek().Start();
- }
- public Result StartRoutine()
- {
- if (_routine.Count == 0)
- return Result.DONE;
- Result ret = Result.DONE;
- var lst = _routine.ToList();
- for (int i = 0; i < lst.Count; i++)
- {
- ret = lst[i].Start();
- if (ret == Result.DONE)
- {
- _routine.Dequeue();
- continue;
- }
- else
- {
- break;
- }
- }
- return Result.RUN;
- }
- public Result MonitorRoutine()
- {
- if (_routine.Count == 0)
- return Result.DONE;
- IRoutine routine = _routine.Peek();
- Result ret = routine.Monitor();
- if (ret == Result.DONE)
- {
- _routine.Dequeue();
- var lst = _routine.ToList();
- for (int i = 0; i < lst.Count; i++)
- {
- ret = lst[i].Start();
- if (ret == Result.DONE)
- {
- _routine.Dequeue();
- continue;
- }
- else
- {
- break;
- }
- }
- }
- return ret;
- }
- public void AbortRoutine()
- {
- if (_routine != null && _routine.Any())
- {
- _routine.Peek().Abort();
- _routine.Clear();
- }
- }
- }
- public class FsmDevice : BaseDevice, IDevice
- {
- private Thread _thread = null;
- private IStateMachine _fsm = null;
- public int FsmState
- {
- get { return _fsm.State; }
- }
- public int FsmPreviousState
- {
- get { return _fsm.PrevState; }
- }
- public string StringFsmStatus
- {
- get
- {
- return _fsmStateMap.ContainsKey(FsmState) ? _fsmStateMap[FsmState] : FsmState.ToString();
- }
- }
- Dictionary<int, string> _fsmStateMap = new Dictionary<int, string>();
- Dictionary<int, string> _fsmMessageMap = new Dictionary<int, string>();
- public FsmDevice() : base()
- {
- }
- public void MapState(int state, string stringState)
- {
- _fsmStateMap[state] = stringState;
- }
- public void MapMessage(int msg, string stringMessage)
- {
- _fsmMessageMap[msg] = stringMessage;
- }
- public void EnableFsm(int fsmInterval, object initState)
- {
- EnableFsm(fsmInterval, (int)initState);
- }
- public void EnableFsm(int fsmInterval, int initState)
- {
- _fsm = new StateMachine($"{Module} {Name} FSM", initState, fsmInterval);
- _fsm.Start();
- _thread = new Thread(new ThreadStart(_fsm.Loop));
- _thread.Name = _fsm.Name;
- _thread.Start();
- while (!_thread.IsAlive)
- Thread.Sleep(1);
- }
- public virtual bool Initialize()
- {
- return true;
- }
- public virtual void Monitor()
- {
- }
- public virtual void Terminate()
- {
- if (_fsm != null)
- {
- _fsm.Stop();
- }
- if (_thread != null)
- {
- if (_thread.IsAlive)
- {
- Thread.Sleep(100);
- if (_thread.IsAlive)
- {
- try
- {
- _thread.Abort();
- }
- catch (Exception ex)
- {
- LOG.Error(String.Format("Entity terminate has exception."), ex);
- }
- }
- }
- }
- //Term();
- }
- public virtual void Reset()
- {
- }
- protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
- {
- Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
- int _state = Convert.ToInt32(state);
- int _next = Convert.ToInt32(next);
- int _msg = Convert.ToInt32(msg);
- Transition(_state, _msg, func, _next);
- }
- protected void Transition(int state, int msg, FsmFunc func, int next)
- {
- if (_fsm != null)
- _fsm.Transition(state, msg, func, next);
- }
- protected void AnyStateTransition(int msg, FsmFunc func, int next)
- {
- if (_fsm != null)
- _fsm.AnyStateTransition(msg, func, next);
- }
- protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
- {
- Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
- int _next = Convert.ToInt32(next);
- int _msg = Convert.ToInt32(msg);
- AnyStateTransition(_msg, func, _next);
- }
- protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
- {
- Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
- int _state = Convert.ToInt32(state);
- int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
- EnterExitTransition(_state, enter, _msg, exit);
- }
- protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
- {
- if (_fsm != null)
- _fsm.EnterExitTransition(state, enter, msg, exit);
- }
- public void PostMsg<T>(T msg, params object[] args) where T : struct
- {
- Debug.Assert(typeof(T).IsEnum);
- int id = Convert.ToInt32(msg);
- PostMsg(id, args);
- }
- public void PostMsg(int msg, params object[] args)
- {
- if (_fsm == null)
- {
- LOG.Error($"fsm is null, post msg {msg}");
- return;
- }
- _fsm.PostMsgWithoutLock(msg, args);
- }
- public bool CheckAllMessageProcessed()
- {
- return _fsm.CheckExecuted();
- }
- public bool CheckToPostMessage<T>(T msg, params object[] args)
- {
- return CheckToPostMessage(Convert.ToInt32(msg));
- }
- public bool CheckToPostMessage(int msg, params object[] args)
- {
- int state = _fsm.State;
- string status = _fsmStateMap[state];
- if (!_fsm.FindTransition(_fsm.State, msg))
- {
- string message = string.Empty;
- if (_fsmMessageMap.ContainsKey(msg))
- message = _fsmMessageMap[msg];
- else
- {
- message = msg.ToString();
- }
- EV.PostWarningLog(Module, $"{Name} is in {status} state,can not do {message}");
- return false;
- }
- _fsm.PostMsg(msg, args);
- return true;
- }
- }
- }
|