EventServiceClient.cs 6.3 KB

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