EventServiceClient.cs 5.3 KB

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