BufferModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Device;
  8. using Aitex.Core.RT.Fsm;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.Routine;
  11. using Aitex.Core.Utilities;
  12. using Aitex.Sorter.Common;
  13. using MECF.Framework.Common.Equipment;
  14. using MECF.Framework.Common.Schedulers;
  15. using MECF.Framework.RT.ModuleLibrary.BufferModules;
  16. namespace JetEfemLib.Buffers
  17. {
  18. public class BufferModule : BufferModuleBase
  19. {
  20. public enum STATE
  21. {
  22. NotInstall,
  23. Init,
  24. Idle,
  25. Homing,
  26. Cooling,
  27. Error,
  28. }
  29. public enum MSG
  30. {
  31. Home,
  32. Reset,
  33. Abort,
  34. Error,
  35. Cooling,
  36. SetOnline,
  37. SetOffline,
  38. ToInit,
  39. };
  40. public override bool IsReady
  41. {
  42. get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
  43. }
  44. public override bool IsError
  45. {
  46. get { return FsmState == (int)STATE.Error; }
  47. }
  48. public override bool IsInit
  49. {
  50. get { return FsmState == (int)STATE.Init; }
  51. }
  52. public bool IsBusy
  53. {
  54. get { return !IsInit && !IsError && !IsReady; }
  55. }
  56. public bool IsIdle
  57. {
  58. get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
  59. }
  60. public int CurrentCoolingTime
  61. {
  62. get
  63. {
  64. if (FsmState == (int)STATE.Cooling)
  65. return _coolingRoutine.ElapsedTime;
  66. return 0;
  67. }
  68. }
  69. public int TotalCoolingTime
  70. {
  71. get
  72. {
  73. if (FsmState == (int)STATE.Cooling)
  74. return (int)_coolingRoutine._coolingValue;
  75. return 0;
  76. }
  77. }
  78. public bool CoolingTypeIsTime
  79. {
  80. get
  81. {
  82. if (FsmState == (int)STATE.Cooling)
  83. return _coolingRoutine._coolingTypeIsTime;
  84. return false;
  85. }
  86. }
  87. public event Action<string> OnEnterError;
  88. private bool _isInit;
  89. private BufferHomeRoutine _homeRoutine;
  90. private BufferCoolingRoutine _coolingRoutine;
  91. public BufferModule(ModuleName module) : base(3)
  92. {
  93. Module = module.ToString();
  94. Name = module.ToString();
  95. IsOnline = true;
  96. EnumLoop<STATE>.ForEach((item) =>
  97. {
  98. MapState((int)item, item.ToString());
  99. });
  100. EnumLoop<MSG>.ForEach((item) =>
  101. {
  102. MapMessage((int)item, item.ToString());
  103. });
  104. EnableFsm(50, IsInstalled ? STATE.Init : STATE.NotInstall);
  105. }
  106. public override bool Initialize()
  107. {
  108. InitRoutine();
  109. InitDevice();
  110. InitFsm();
  111. InitOp();
  112. InitData();
  113. return base.Initialize();
  114. }
  115. private void InitRoutine()
  116. {
  117. ModuleName module = ModuleHelper.Converter(Module);
  118. _homeRoutine = new BufferHomeRoutine(module);
  119. _coolingRoutine = new BufferCoolingRoutine(module);
  120. }
  121. private void InitDevice()
  122. {
  123. }
  124. private void InitFsm()
  125. {
  126. //Error
  127. AnyStateTransition(MSG.Error, FsmOnError, STATE.Error);
  128. AnyStateTransition(FSM_MSG.ALARM, FsmOnError, STATE.Error);
  129. Transition(STATE.Error, MSG.Reset, FsmReset, STATE.Idle);
  130. EnterExitTransition<STATE, FSM_MSG>(STATE.Error, FsmEnterError, FSM_MSG.NONE, FsmExitError);
  131. //Home
  132. Transition(STATE.Init, MSG.Home, FsmStartHome, STATE.Homing);
  133. Transition(STATE.Error, MSG.Home, FsmStartHome, STATE.Homing);
  134. Transition(STATE.Idle, MSG.Home, FsmStartHome, STATE.Homing);
  135. Transition(STATE.Homing, FSM_MSG.TIMER, FsmMonitorHomeTask, STATE.Idle);
  136. Transition(STATE.Homing, MSG.Error, null, STATE.Init);
  137. Transition(STATE.Homing, MSG.Abort, FsmAbortTask, STATE.Init);
  138. EnterExitTransition((int)STATE.Homing, FsmEnterIdle, (int)FSM_MSG.NONE, FsmExitIdle);
  139. AnyStateTransition(MSG.ToInit, FsmToInit, STATE.Init);
  140. //Online
  141. Transition(STATE.Idle, MSG.SetOnline, FsmStartSetOnline, STATE.Idle);
  142. Transition(STATE.Idle, MSG.SetOffline, FsmStartSetOffline, STATE.Idle);
  143. //cooling
  144. Transition(STATE.Idle, MSG.Cooling, FsmStartCooling, STATE.Cooling);
  145. Transition(STATE.Cooling, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  146. Transition(STATE.Cooling, MSG.Abort, FsmAbortTask, STATE.Idle);
  147. }
  148. private void InitOp()
  149. {
  150. OP.Subscribe($"{Name}.Home", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Home));
  151. OP.Subscribe($"{Name}.Reset", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Reset));
  152. OP.Subscribe($"{Name}.Abort", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Abort));
  153. OP.Subscribe($"{Module}.SetOnline", (string cmd, object[] args) => CheckToPostMessage((int)MSG.SetOnline));
  154. OP.Subscribe($"{Module}.SetOffline", (string cmd, object[] args) => CheckToPostMessage((int)MSG.SetOffline));
  155. OP.Subscribe($"{Name}.Cooling", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.Cooling, args); });
  156. }
  157. private void InitData()
  158. {
  159. DATA.Subscribe($"{Name}.Status", () => StringFsmStatus);
  160. DATA.Subscribe($"{Name}.IsOnline", () => IsOnline);
  161. DATA.Subscribe($"{Name}.IsBusy", () => IsBusy);
  162. DATA.Subscribe($"{Name}.ElapseCoolingTime", () => CurrentCoolingTime);
  163. DATA.Subscribe($"{Name}.TotalCoolingTime", () => TotalCoolingTime);
  164. DATA.Subscribe($"{Name}.CoolingTypeIsTime", () => CoolingTypeIsTime);
  165. }
  166. private bool FsmOnError(object[] param)
  167. {
  168. IsOnline = false;
  169. if (FsmState == (int)STATE.Error)
  170. {
  171. return false;
  172. }
  173. if (FsmState == (int)STATE.Init)
  174. return false;
  175. return true;
  176. }
  177. private bool FsmReset(object[] param)
  178. {
  179. if (!_isInit)
  180. {
  181. PostMsg(MSG.ToInit);
  182. return false;
  183. }
  184. return true;
  185. }
  186. private bool FsmExitError(object[] param)
  187. {
  188. return true;
  189. }
  190. private bool FsmEnterError(object[] param)
  191. {
  192. if (OnEnterError != null)
  193. OnEnterError(Module);
  194. return true;
  195. }
  196. private bool FsmStartHome(object[] param)
  197. {
  198. RState ret = StartRoutine(_homeRoutine);
  199. if (ret == RState.Failed || ret == RState.End)
  200. return false;
  201. _isInit = false;
  202. return ret == RState.Running;
  203. }
  204. private bool FsmMonitorHomeTask(object[] param)
  205. {
  206. RState ret = MonitorRoutine();
  207. if (ret == RState.Failed)
  208. {
  209. PostMsg(MSG.Error);
  210. return false;
  211. }
  212. if (ret == RState.End)
  213. {
  214. _isInit = true;
  215. return true;
  216. }
  217. return false;
  218. }
  219. private bool FsmAbortTask(object[] param)
  220. {
  221. AbortRoutine();
  222. return true;
  223. }
  224. private bool FsmExitIdle(object[] param)
  225. {
  226. return true;
  227. }
  228. private bool FsmEnterIdle(object[] param)
  229. {
  230. return true;
  231. }
  232. private bool FsmToInit(object[] param)
  233. {
  234. return true;
  235. }
  236. private bool FsmStartSetOffline(object[] param)
  237. {
  238. IsOnline = false;
  239. return true;
  240. }
  241. private bool FsmStartSetOnline(object[] param)
  242. {
  243. IsOnline = true;
  244. return true;
  245. }
  246. private bool FsmStartCooling(object[] param)
  247. {
  248. _coolingRoutine.Init((bool)param[0], (int)param[1]);
  249. RState ret = StartRoutine(_coolingRoutine);
  250. if (ret == RState.Failed || ret == RState.End)
  251. return false;
  252. return ret == RState.Running;
  253. }
  254. private bool FsmStartWarmUp(object[] param)
  255. {
  256. return true;
  257. }
  258. private bool FsmMonitorTask(object[] param)
  259. {
  260. RState ret = MonitorRoutine();
  261. if (ret == RState.Failed)
  262. {
  263. PostMsg(MSG.Error);
  264. return false;
  265. }
  266. return ret == RState.End;
  267. }
  268. public override bool Home(out string reason)
  269. {
  270. CheckToPostMessage((int)MSG.Home);
  271. reason = string.Empty;
  272. return true;
  273. }
  274. public bool CheckAcked(int entityTaskToken)
  275. {
  276. return FsmState == (int)STATE.Idle && CheckAllMessageProcessed();
  277. //return true;
  278. }
  279. public override void NoteTransferStart(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  280. {
  281. //CheckToPostMessage(MSG.InTransfer);
  282. }
  283. public override void NoteTransferStop(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  284. {
  285. //if (FsmState == (int)STATE.InTransfer)
  286. // CheckToPostMessage(MSG.TransferComplete);
  287. }
  288. public override bool PrepareTransfer(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
  289. {
  290. reason = string.Empty;
  291. return true;
  292. }
  293. public override bool TransferHandoff(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
  294. {
  295. reason = string.Empty;
  296. return true;
  297. }
  298. public override bool PostTransfer(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
  299. {
  300. reason = string.Empty;
  301. return true;
  302. }
  303. public override bool CheckReadyForTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType,
  304. out string reason)
  305. {
  306. reason = string.Empty;
  307. return true;
  308. }
  309. public void InvokeOffline()
  310. {
  311. PostMsg((int)MSG.SetOffline);
  312. }
  313. public void InvokeOnline()
  314. {
  315. PostMsg((int)MSG.SetOnline);
  316. }
  317. public int InvokeCooling(bool coolingTypeIsTime, int time)
  318. {
  319. if (CheckToPostMessage((int)MSG.Cooling, coolingTypeIsTime, time))
  320. return (int)MSG.Cooling;
  321. return (int)FSM_MSG.NONE;
  322. }
  323. }
  324. }