EventServiceClient.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.WCF;
  6. using Aitex.Core.RT.Event;
  7. using System.ServiceModel;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.Util;
  10. using Aitex.Core.Utilities;
  11. using Aitex.Core.WCF.Interface;
  12. using MECF.Framework.Common.Properties;
  13. namespace Aitex.Core.WCF
  14. {
  15. public class EventClient : Singleton<EventClient>
  16. {
  17. public event Action<EventItem> OnEvent;
  18. public event Action OnDisconnectedWithRT;
  19. public event Action<bool> LockAndUnlockEvent;
  20. public event Func<IEnumerable<string>,bool?> OnUpdateWaferEvent;
  21. public bool InProcess { get; set; }
  22. private IEventService _service;
  23. public IEventService Service
  24. {
  25. get
  26. {
  27. if (_service == null)
  28. {
  29. if (InProcess)
  30. {
  31. _service = Singleton<EventManager>.Instance.Service;
  32. }
  33. else
  34. {
  35. _service = new EventServiceClient(new EventServiceCallback());
  36. }
  37. _service.OnEvent += _service_OnEvent;
  38. _service.OnLockAndUnlockEvent += _service_OnLockAndUnlockEvent;
  39. _service.OnUpdateWaferEvent += _sevice_OnUpdateWaferEvent;
  40. }
  41. return _service;
  42. }
  43. }
  44. private void _service_OnLockAndUnlockEvent(bool obj)
  45. {
  46. if (LockAndUnlockEvent != null)
  47. {
  48. LockAndUnlockEvent(obj);
  49. }
  50. }
  51. private bool? _sevice_OnUpdateWaferEvent(IEnumerable<string> modules)=> OnUpdateWaferEvent?.Invoke(modules);
  52. public bool IsConnectedWithRT
  53. {
  54. get { return _retryConnectRT.Result; }
  55. }
  56. public Guid ClientGuid
  57. {
  58. get { return _retryConnectRT.ClientGuid; }
  59. }
  60. Retry _retryConnectRT = new Retry();
  61. FixSizeQueue<EventItem> _queue = new FixSizeQueue<EventItem>(500);
  62. PeriodicJob _fireJob;
  63. Guid _guid = Guid.NewGuid();
  64. private R_TRIG _trigDisconnect = new R_TRIG();
  65. private DeviceTimer _timer = new DeviceTimer();
  66. public bool ConnectRT()
  67. {
  68. _retryConnectRT.Result = InProcess || Service.Register(_guid);
  69. _retryConnectRT.ClientGuid = _guid;
  70. return _retryConnectRT.IsSucceeded;
  71. }
  72. public void Start()
  73. {
  74. if (_fireJob != null)
  75. return;
  76. _fireJob = new PeriodicJob(500, this.FireEvent, "UIEvent", true);
  77. }
  78. public void Stop()
  79. {
  80. if (!InProcess)
  81. {
  82. if (_retryConnectRT.IsSucceeded)
  83. {
  84. Service.UnRegister(_guid);
  85. }
  86. }
  87. if (_fireJob != null)
  88. {
  89. _fireJob.Stop();
  90. }
  91. }
  92. bool FireEvent()
  93. {
  94. _retryConnectRT.Result = InProcess || Service.Register(_guid);
  95. if (_retryConnectRT.IsSucceeded)
  96. {
  97. _service_OnEvent(new EventItem()
  98. {
  99. OccuringTime = DateTime.Now,
  100. Description = "Connected with RT",
  101. Id = 0,
  102. Level = EventLevel.Information,
  103. Type = EventType.EventUI_Notify,
  104. });
  105. }
  106. if (_retryConnectRT.IsErrored)
  107. {
  108. _service_OnEvent(new EventItem()
  109. {
  110. OccuringTime = DateTime.Now,
  111. Description = "Disconnect from RT",
  112. Id = 0,
  113. Level = EventLevel.Information,
  114. Type = EventType.EventUI_Notify,
  115. });
  116. _timer.Start(0);
  117. }
  118. if (_timer.GetElapseTime() > 2000 && !_retryConnectRT.Result && OnDisconnectedWithRT!=null)
  119. {
  120. OnDisconnectedWithRT();
  121. }
  122. if (OnEvent != null)
  123. {
  124. EventItem ev;
  125. while (_queue.TryDequeue(out ev))
  126. {
  127. //if (ev.Description.Equals("PM1 Process Abort") || ev.Description.Equals("PM2 Process Abort") || ev.Description.Equals("PM3 Process Abort"))
  128. //{
  129. // Console.WriteLine("EventServiceClient(QueeeD):" + ev.Description);
  130. //}
  131. OnEvent(ev);
  132. }
  133. }
  134. return true;
  135. }
  136. private void _service_OnEvent(EventItem ev)
  137. {
  138. _queue.Enqueue(ev);
  139. //if (ev.Description.Equals("PM1 Process Abort") || ev.Description.Equals("PM2 Process Abort") || ev.Description.Equals("PM3 Process Abort"))
  140. //{
  141. // Console.WriteLine("EventServiceClient(QueeeE):" + ev.Description);
  142. //}
  143. }
  144. }
  145. public class EventServiceClient : DuplexChannelServiceClientWrapper<IEventService>, IEventService
  146. {
  147. public event Action<EventItem> OnEvent;
  148. public event Action<bool> OnLockAndUnlockEvent;
  149. public event Func<IEnumerable<string>,bool?> OnUpdateWaferEvent;
  150. public EventServiceClient(EventServiceCallback callbackInstance)
  151. : base(new InstanceContext(callbackInstance), "Client_IEventService", "EventService")
  152. {
  153. callbackInstance.FireEvent += new Action<EventItem>(callbackInstance_FireEvent);
  154. callbackInstance.OnLockAndUnlockEvent += CallbackInstance_OnLockAndUnlockEvent;
  155. callbackInstance.OnUpdateWaferEvent += CallbackInstance_OnUpdateWaferEvent;
  156. }
  157. void callbackInstance_FireEvent(EventItem obj)
  158. {
  159. if (OnEvent != null)
  160. {
  161. OnEvent(obj);
  162. }
  163. }
  164. void CallbackInstance_OnLockAndUnlockEvent(bool isLock)
  165. {
  166. if (OnLockAndUnlockEvent != null)
  167. {
  168. OnLockAndUnlockEvent(isLock);
  169. }
  170. }
  171. bool? CallbackInstance_OnUpdateWaferEvent(IEnumerable<string> modules) => OnUpdateWaferEvent?.Invoke(modules);
  172. public bool Register(Guid id)
  173. {
  174. bool ret = false;
  175. Invoke(svc => { ret = svc.Register(id); });
  176. return ret;
  177. }
  178. public void UnRegister(Guid id)
  179. {
  180. Invoke(svc => { svc.UnRegister(id); });
  181. }
  182. public int Heartbeat()
  183. {
  184. int result = -1;
  185. Invoke(svc => { result = svc.Heartbeat(); });
  186. return result;
  187. }
  188. }
  189. }