EventService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 Func<IEnumerable<string>,bool?> 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 bool? UpDateWafers(IEnumerable<string> modules)
  120. {
  121. var tasks = new List<Task<bool?>>();
  122. foreach (var client in _callbackClientList)
  123. {
  124. tasks.Add(Task.Run(() =>
  125. {
  126. try
  127. {
  128. return client.Value.SendChangedWaferModule(modules);//客户端通过回调接受事件
  129. }
  130. catch (Exception ex)
  131. {
  132. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", client.Key), ex);
  133. _callbackClientList.TryRemove(client.Key, out IEventServiceCallback service);
  134. _isLockState.TryRemove(client.Key, out bool lockState);
  135. return null;
  136. }
  137. }));
  138. }
  139. Task.WhenAll(tasks).Wait(TimeSpan.FromSeconds(400));
  140. if (tasks.Any(r => r.Result == false)) return false;
  141. return true;
  142. }
  143. public bool Register(Guid id)
  144. {
  145. try
  146. {
  147. if (!_callbackClientList.ContainsKey(id))
  148. {
  149. LOG.Info(string.Format("客户端{0}已连接", id.ToString()), true);
  150. }
  151. _callbackClientList[id] = OperationContext.Current.GetCallbackChannel<IEventServiceCallback>();
  152. _isLockState[id] = false;
  153. }
  154. catch (Exception ex)
  155. {
  156. LOG.Error("监听事件发生错误", ex);
  157. }
  158. return true;
  159. }
  160. public void UnRegister(Guid id)
  161. {
  162. if (!_callbackClientList.ContainsKey(id))
  163. {
  164. LOG.Info(string.Format("客户端{0}断开连接", id.ToString()), true);
  165. }
  166. IEventServiceCallback service;
  167. _callbackClientList.TryRemove(id, out service);
  168. bool lockState = false;
  169. _isLockState.TryRemove(id,out lockState);
  170. }
  171. public int Heartbeat()
  172. {
  173. return _counter++ % 100000;
  174. }
  175. }
  176. }