EventService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.WCF.Interface;
  10. using System.Collections.Concurrent;
  11. using MECF.Framework.Common.Event;
  12. using System.Threading.Tasks;
  13. namespace Aitex.Core.WCF
  14. {
  15. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  16. public class EventService : IEventService
  17. {
  18. ConcurrentDictionary<Guid, IEventServiceCallback> _callbackClientList = new ConcurrentDictionary<Guid, IEventServiceCallback>();
  19. ConcurrentDictionary<Guid, bool> _isLockState = new ConcurrentDictionary<Guid, bool>();
  20. public event Action<EventItem> OnEvent;
  21. public event Action<bool> OnLockAndUnlockEvent;
  22. public event Action<IEnumerable<string>> OnUpdateWaferEvent;
  23. private int _counter;
  24. public EventService()
  25. {
  26. }
  27. public void FireEvent(EventItem obj)
  28. {
  29. //DeviceTimer _timer = new DeviceTimer();
  30. //_timer.Start(0);
  31. //double t1 = 0;
  32. //double t2 = 0;
  33. //double t3 = 0;
  34. //t1 = _timer.GetElapseTime();
  35. Guid[] ids = _callbackClientList.Keys.ToArray();
  36. for (int i = 0; i < ids.Length; i++)
  37. {
  38. try
  39. {
  40. //t2 = _timer.GetElapseTime() - t1;
  41. _callbackClientList[ids[i]].SendEvent(obj);
  42. //t3 = _timer.GetElapseTime() - t2;
  43. }
  44. catch (Exception ex)
  45. {
  46. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);
  47. IEventServiceCallback service;
  48. _callbackClientList.TryRemove(ids[i], out service);
  49. bool lockState = false;
  50. _isLockState.TryRemove(ids[i], out lockState);
  51. }
  52. }
  53. if (OnEvent != null)
  54. {
  55. OnEvent(obj);
  56. }
  57. //System.Diagnostics.Trace.WriteLine(string.Format("[{0}] ----- [{1}] --------- [{2}]", t1, t2, t3));
  58. }
  59. public void SetLockAndUnLock(string cmd, Guid guid)
  60. {
  61. Guid[] ids = _callbackClientList.Keys.ToArray();
  62. if (cmd == "Unlock")
  63. {
  64. try
  65. {
  66. _callbackClientList.Where(x => x.Key == guid).FirstOrDefault().Value.SendLockEvent(false);
  67. _isLockState[guid] = false;
  68. if (OnLockAndUnlockEvent != null)
  69. {
  70. OnLockAndUnlockEvent(false);
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", guid), ex);
  76. IEventServiceCallback service;
  77. _callbackClientList.TryRemove(guid, out service);
  78. bool lockState = false;
  79. _isLockState.TryRemove(guid, out lockState);
  80. }
  81. }
  82. if (cmd == "Lock")
  83. {
  84. for (int i = 0; i < ids.Length; i++)
  85. {
  86. try
  87. {
  88. if (_isLockState[ids[i]] == true)
  89. {
  90. _callbackClientList[ids[i]].SendLockEvent(_isLockState[ids[i]]);
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);
  96. IEventServiceCallback service;
  97. _callbackClientList.TryRemove(ids[i], out service);
  98. bool lockState = false;
  99. _isLockState.TryRemove(guid, out lockState);
  100. }
  101. }
  102. if (_isLockState.Where(x => x.Value == true).Count() == 0)
  103. {
  104. try
  105. {
  106. _callbackClientList[guid].SendLockEvent(true);
  107. _isLockState[guid] = true;
  108. if (OnLockAndUnlockEvent != null)
  109. {
  110. OnLockAndUnlockEvent(true);
  111. }
  112. }
  113. catch (Exception)
  114. {
  115. }
  116. }
  117. }
  118. }
  119. public void UpDateWafers(IEnumerable<string> modules)
  120. {
  121. foreach (var client in _callbackClientList)
  122. {
  123. Task.Run(() =>
  124. {
  125. try
  126. {
  127. client.Value.SendChangedWaferModule(modules);//客户端通过回调接受事件
  128. }
  129. catch (Exception ex)
  130. {
  131. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", client.Key), ex);
  132. _callbackClientList.TryRemove(client.Key, out IEventServiceCallback service);
  133. _isLockState.TryRemove(client.Key, out bool lockState);
  134. }
  135. });
  136. }
  137. }
  138. public bool Register(Guid id)
  139. {
  140. try
  141. {
  142. if (!_callbackClientList.ContainsKey(id))
  143. {
  144. LOG.Info(string.Format("客户端{0}已连接", id.ToString()), true);
  145. }
  146. _callbackClientList[id] = OperationContext.Current.GetCallbackChannel<IEventServiceCallback>();
  147. _isLockState[id] = false;
  148. }
  149. catch (Exception ex)
  150. {
  151. LOG.Error("监听事件发生错误", ex);
  152. }
  153. return true;
  154. }
  155. public void UnRegister(Guid id)
  156. {
  157. if (!_callbackClientList.ContainsKey(id))
  158. {
  159. LOG.Info(string.Format("客户端{0}断开连接", id.ToString()), true);
  160. }
  161. IEventServiceCallback service;
  162. _callbackClientList.TryRemove(id, out service);
  163. bool lockState = false;
  164. _isLockState.TryRemove(id,out lockState);
  165. }
  166. public int Heartbeat()
  167. {
  168. return _counter++ % 100000;
  169. }
  170. }
  171. }