RorzeEfemConnection.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using MECF.Framework.Common.Equipment;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Efems.Rorzes
  8. {
  9. public class RorzeEfemMessage : AsciiMessage
  10. {
  11. public ModuleName TargetModule { get; set; }
  12. public RorzeEfemMessageType MessageType { get; set; }
  13. public RorzeEfemBasicMessage BasicMessage { get; set; }
  14. public string Parameter { get; set; }
  15. public string NakFactor { get; set; }
  16. public bool WaitAck { get; set; }
  17. public bool WaitInf { get; set; }
  18. //ABS : Message* | ERROR / Parameter1 / Parameter2 ;
  19. //ERROR / Parameter1 / Parameter2
  20. public string AbsError { get; set; }
  21. //CAN : Message* | Factor / Place ;
  22. //Factor / Place
  23. public string CanError { get; set; }
  24. }
  25. public class RorzeEfemConnection : FsmConnection
  26. {
  27. private string _cachedBuffer = string.Empty;
  28. private object _lockerHandler = new object();
  29. private IConnectionContext _config;
  30. List<HandlerBase> _handlers = new List<HandlerBase>();
  31. private Dictionary<RorzeEfemBasicMessage, HandlerBase> _eventHandler = new Dictionary<RorzeEfemBasicMessage, HandlerBase>();
  32. public RorzeEfemConnection(IConnectionContext config)
  33. {
  34. _config = config;
  35. }
  36. public bool IsBusy(int mutexId)
  37. {
  38. lock (_lockerHandler)
  39. {
  40. foreach (var handlerBase in _handlers)
  41. {
  42. if (handlerBase.MutexId == mutexId)
  43. {
  44. return true;
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. public void AddEventHandler(RorzeEfemBasicMessage type, RorzeEfemHandler handler)
  51. {
  52. System.Diagnostics.Debug.Assert(!_eventHandler.ContainsKey(type));
  53. _eventHandler[type] = handler;
  54. }
  55. public void Initialize()
  56. {
  57. base.Initialize(100, new SocketClient(_config), _config);
  58. }
  59. protected override void OnConnect()
  60. {
  61. }
  62. protected override void OnDisconnect()
  63. {
  64. lock (_lockerHandler)
  65. {
  66. _cachedBuffer = string.Empty;
  67. _handlers.Clear();
  68. }
  69. }
  70. protected override void OnConnectMonitor()
  71. {
  72. MonitorTimeout();
  73. base.OnConnectMonitor();
  74. }
  75. private HandlerBase MonitorTimeout()
  76. {
  77. HandlerBase result = null;
  78. lock (_lockerHandler)
  79. {
  80. foreach (var handlerBase in _handlers)
  81. {
  82. if (handlerBase.CheckTimeout())
  83. {
  84. EV.PostWarningLog("System", $"execute {handlerBase.Name} timeout");
  85. result = handlerBase;
  86. break;
  87. }
  88. }
  89. if (result != null)
  90. {
  91. _handlers.Remove(result);
  92. }
  93. }
  94. return result;
  95. }
  96. public bool Execute(HandlerBase handler, out string reason)
  97. {
  98. lock (_lockerHandler)
  99. {
  100. foreach (var handlerBase in _handlers)
  101. {
  102. if (handlerBase.MutexId == handler.MutexId && handler.MutexId!=-1)
  103. {
  104. reason = $"Can not execute {handler.Name} while execute {handlerBase.Name}";
  105. return false;
  106. }
  107. }
  108. _handlers.Add(handler);
  109. handler.SetState(EnumHandlerState.Sent);
  110. if (_config.IsAscii)
  111. {
  112. InvokeSendData(handler.SendText);
  113. }
  114. else
  115. {
  116. InvokeSendData(handler.SendBinary);
  117. }
  118. }
  119. reason = string.Empty;
  120. return true;
  121. }
  122. public void Reset()
  123. {
  124. InvokeReset();
  125. }
  126. protected override void OnReceiveData(string message)
  127. {
  128. _cachedBuffer += message;
  129. int enderIndex = _cachedBuffer.IndexOf(";\r");
  130. while (enderIndex != -1)
  131. {
  132. if (enderIndex > 0)
  133. {
  134. ProceedResponse(_cachedBuffer.Substring(0, enderIndex));
  135. }
  136. _cachedBuffer = _cachedBuffer.Remove(0, enderIndex + 2);
  137. enderIndex = _cachedBuffer.IndexOf(";\r");
  138. }
  139. }
  140. private void AckInfo(string message)
  141. {
  142. if (message.StartsWith("INF:"))
  143. {
  144. InvokeSendData(message.Replace("INF:", "ACK:") + ";\r");
  145. }
  146. else if (message.StartsWith("ABS:"))
  147. {
  148. InvokeSendData(message.Replace("ABS:", "ACK:") + ";\r");
  149. }
  150. else if (message.StartsWith("NAK:"))
  151. {
  152. InvokeSendData(message.Replace("NAK:", "ACK:") + ";\r");
  153. }
  154. else if (message.StartsWith("CAN:"))
  155. {
  156. InvokeSendData(message.Replace("CAN:", "ACK:") + ";\r");
  157. }
  158. }
  159. protected void ProceedResponse(string rawMessage)
  160. {
  161. RorzeEfemMessage msg = new RorzeEfemMessage();
  162. msg.RawMessage = rawMessage;
  163. if (rawMessage == "INF:READY/COMM")
  164. {
  165. _eventHandler[RorzeEfemBasicMessage.READY].HandleMessage(msg, out _);
  166. AckInfo(rawMessage);
  167. return;
  168. }
  169. string[] words = rawMessage.Split(new char[5] { ':', '/', '>', '|', ';' }, StringSplitOptions.RemoveEmptyEntries);
  170. msg.MessagePart = words;
  171. if (!Enum.TryParse<RorzeEfemMessageType>(words[0], out RorzeEfemMessageType type))
  172. {
  173. LOG.Write($"{rawMessage} is not a valid EFEM message format");
  174. return;
  175. }
  176. if (type == RorzeEfemMessageType.MOV || type == RorzeEfemMessageType.GET ||
  177. type == RorzeEfemMessageType.SET)
  178. {
  179. LOG.Write($"{rawMessage} is not a valid EFEM message format, {type} is TO EFEM");
  180. return;
  181. }
  182. msg.MessageType = type;
  183. msg.IsEvent = msg.MessageType == RorzeEfemMessageType.EVT;
  184. int messageIndex = msg.MessageType == RorzeEfemMessageType.NAK ? 2 : 1;
  185. if (words.Length <= messageIndex || !Enum.TryParse<RorzeEfemBasicMessage>(words[messageIndex], out RorzeEfemBasicMessage basicMessage))
  186. {
  187. LOG.Write($"{rawMessage} is not a valid EFEM message format");
  188. return;
  189. }
  190. msg.BasicMessage = basicMessage;
  191. if (msg.MessageType == RorzeEfemMessageType.NAK)
  192. {
  193. msg.NakFactor = words[1];
  194. msg.Parameter = rawMessage.Substring(rawMessage.IndexOf('/') + 1);
  195. }
  196. else if (msg.MessageType == RorzeEfemMessageType.CAN)
  197. {
  198. msg.CanError = rawMessage.Substring(rawMessage.IndexOf('|'));
  199. msg.Parameter = rawMessage.Substring(rawMessage.IndexOf('/') + 1, rawMessage.IndexOf('|') - rawMessage.IndexOf('/'));
  200. }
  201. else if (msg.MessageType == RorzeEfemMessageType.ABS)
  202. {
  203. msg.AbsError = rawMessage.Substring(rawMessage.IndexOf('|'));
  204. msg.Parameter = rawMessage.Substring(rawMessage.IndexOf('/') + 1, rawMessage.IndexOf('|') - rawMessage.IndexOf('/'));
  205. }
  206. else
  207. {
  208. msg.Parameter = rawMessage.Substring(rawMessage.IndexOf('/') + 1);
  209. }
  210. if (msg.IsEvent)
  211. {
  212. ProceedEvent(msg);
  213. return;
  214. }
  215. //Report of ERROR
  216. if (msg.MessageType == RorzeEfemMessageType.INF && words.Length>=2 && words[1].ToUpper() == "ERROR")
  217. {
  218. AckInfo(rawMessage);
  219. return;
  220. }
  221. ProceedMessage(msg);
  222. }
  223. private void ProceedMessage(RorzeEfemMessage msg)
  224. {
  225. lock (_lockerHandler)
  226. {
  227. HandlerBase handlerDone = null;
  228. bool matchMessage = false;
  229. foreach (var handler in _handlers)
  230. {
  231. matchMessage = handler.HandleMessage(msg, out bool completed);
  232. if (completed)
  233. {
  234. handlerDone = handler;
  235. AckInfo(msg.RawMessage);
  236. break;
  237. }
  238. if (matchMessage)
  239. break;
  240. }
  241. if (!matchMessage)
  242. {
  243. LOG.Write($"No handler for message, {msg.RawMessage}");
  244. return;
  245. }
  246. _handlers.Remove(handlerDone);
  247. }
  248. }
  249. private void ProceedEvent(RorzeEfemMessage msg)
  250. {
  251. if (_eventHandler.ContainsKey(msg.BasicMessage))
  252. {
  253. _eventHandler[msg.BasicMessage].HandleMessage(msg, out _);
  254. }
  255. else
  256. {
  257. LOG.Write($"No handler for event {msg.RawMessage}");
  258. }
  259. }
  260. }
  261. }