ServiceStationMng.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Sers.Core.Module.Env;
  6. using Sers.Gover.Base.Model;
  7. using Sers.Gover.Service.SersEvent;
  8. using Sers.Hardware.Usage;
  9. using Sers.ServiceCenter.Entity;
  10. using Vit.Extensions;
  11. namespace Sers.Gover.Base
  12. {
  13. public class ServiceStationMng
  14. {
  15. GoverManage goverManage;
  16. public ServiceStationMng Init(GoverManage goverManage)
  17. {
  18. this.goverManage = goverManage;
  19. return this;
  20. }
  21. public ICollection<ServiceStation> serviceStationCollection => serviceStation_ConnKey_Map.Values;
  22. /// <summary>
  23. /// connKey 和 服务站点 的映射
  24. /// </summary>
  25. readonly ConcurrentDictionary<int, ServiceStation> serviceStation_ConnKey_Map = new ConcurrentDictionary<int, ServiceStation>();
  26. /// <summary>
  27. /// serviceStationKey 和 服务站点 的映射
  28. /// </summary>
  29. ///
  30. readonly Reference<string, UsageStatus> serviceStationKey_UsageStatus_Map = new Reference<string, UsageStatus>();
  31. public void SaveUsageInfo(EnvUsageInfo item)
  32. {
  33. lock (this)
  34. {
  35. serviceStationKey_UsageStatus_Map.Get(item.serviceStationKey)?.CopyFrom(item.usageStatus);
  36. }
  37. }
  38. public List<ServiceStationData> ServiceStation_GetAll()
  39. {
  40. return serviceStation_ConnKey_Map.Values
  41. .Select(
  42. m => new ServiceStationData
  43. {
  44. connKey = "" + m.connection.GetHashCode(),
  45. startTime = m.startTime,
  46. deviceInfo = m.deviceInfo,
  47. serviceStationInfo = m.serviceStationInfo,
  48. status = "" + m.Status_Get(),
  49. usageStatus = m.usageStatus,
  50. counter = m.counter,
  51. qps = m.qps,
  52. apiNodeCount = m.apiNodes.Count,
  53. activeApiNodeCount = m.ActiveApiNodeCount_Get(),
  54. apiStationNames = m.ApiStationNames_Get()
  55. }
  56. ).OrderBy(m => m?.serviceStationInfo?.serviceStationName)
  57. .ThenBy(m => m.startTime)
  58. .ToList();
  59. }
  60. #region action
  61. /// <summary>
  62. /// 添加并启用站点
  63. /// </summary>
  64. /// <param name="serviceStation"></param>
  65. public void ServiceStation_Add(ServiceStation serviceStation)
  66. {
  67. lock (this)
  68. {
  69. serviceStation.startTime=DateTime.Now;
  70. serviceStation_ConnKey_Map[serviceStation.connection.GetHashCode()] = serviceStation;
  71. if (string.IsNullOrEmpty(serviceStation.serviceStationInfo.serviceStationKey))
  72. serviceStation.serviceStationInfo.serviceStationKey = "tmp" + serviceStation.GetHashCode();
  73. serviceStation.usageStatus = serviceStationKey_UsageStatus_Map.Add(serviceStation.serviceStationKey, serviceStation.usageStatus ?? new UsageStatus());
  74. serviceStation.Status_Set(EServiceStationStatus.正常);
  75. goverManage.apiStationMng.ServiceStation_Add(serviceStation);
  76. //发布 Sers Event
  77. SersEventService.Publish(SersEventService.Event_ServiceStation_Add, serviceStation);
  78. }
  79. }
  80. /// <summary>
  81. /// 更新服务站点设备硬件信息
  82. /// </summary>
  83. /// <param name="newServiceStation"></param>
  84. public bool ServiceStation_UpdateStationInfo(ServiceStation newServiceStation)
  85. {
  86. lock (this)
  87. {
  88. if (!serviceStation_ConnKey_Map.TryGetValue(newServiceStation.connection.GetHashCode(),out var serviceStation))
  89. {
  90. return false;
  91. }
  92. serviceStationKey_UsageStatus_Map.Remove(serviceStation.serviceStationKey);
  93. if (newServiceStation.serviceStationInfo != null)
  94. {
  95. serviceStation.serviceStationInfo = newServiceStation.serviceStationInfo;
  96. if (string.IsNullOrEmpty(serviceStation.serviceStationInfo.serviceStationKey))
  97. serviceStation.serviceStationInfo.serviceStationKey = "tmp" + serviceStation.GetHashCode();
  98. }
  99. if (newServiceStation.deviceInfo != null)
  100. serviceStation.deviceInfo = newServiceStation.deviceInfo;
  101. serviceStation.usageStatus = serviceStationKey_UsageStatus_Map.Add(serviceStation.serviceStationKey, serviceStation.usageStatus ?? new UsageStatus());
  102. return true;
  103. }
  104. }
  105. public ServiceStation ServiceStation_Remove(string connKey)
  106. {
  107. lock (this)
  108. {
  109. if (!serviceStation_ConnKey_Map.TryRemove(int.Parse(connKey), out var serviceStation))
  110. {
  111. return null;
  112. }
  113. serviceStationKey_UsageStatus_Map.Remove(serviceStation.serviceStationKey);
  114. goverManage.apiStationMng.ServiceStation_Remove(serviceStation);
  115. //发布 Sers Event
  116. SersEventService.Publish(SersEventService.Event_ServiceStation_Remove, serviceStation);
  117. return serviceStation;
  118. }
  119. }
  120. public ServiceStation ServiceStation_Pause(string connKey)
  121. {
  122. lock (this)
  123. {
  124. if (!serviceStation_ConnKey_Map.TryGetValue(int.Parse(connKey), out var serviceStation))
  125. {
  126. return null;
  127. }
  128. if (serviceStation.Status_Get() == EServiceStationStatus.暂停)
  129. {
  130. return serviceStation;
  131. }
  132. serviceStation.Status_Set(EServiceStationStatus.暂停);
  133. goverManage.apiStationMng.ServiceStation_Pause(serviceStation);
  134. //发布 Sers Event
  135. SersEventService.Publish(SersEventService.Event_ServiceStation_Pause, serviceStation);
  136. return serviceStation;
  137. }
  138. }
  139. public ServiceStation ServiceStation_Start(string connKey)
  140. {
  141. lock (this)
  142. {
  143. if (!serviceStation_ConnKey_Map.TryGetValue(int.Parse(connKey), out var serviceStation))
  144. {
  145. return null;
  146. }
  147. if (serviceStation.Status_Get() == EServiceStationStatus.正常)
  148. {
  149. return serviceStation;
  150. }
  151. serviceStation.Status_Set(EServiceStationStatus.正常);
  152. goverManage.apiStationMng.ServiceStation_Start(serviceStation);
  153. //发布 Sers Event
  154. SersEventService.Publish(SersEventService.Event_ServiceStation_Start, serviceStation);
  155. return serviceStation;
  156. }
  157. }
  158. #endregion
  159. #region Reference
  160. /// <summary>
  161. /// 线程不安全
  162. /// </summary>
  163. /// <typeparam name="KeyType"></typeparam>
  164. /// <typeparam name="ValueType"></typeparam>
  165. class Reference<KeyType, ValueType>
  166. {
  167. class Item
  168. {
  169. public int count = 1;
  170. public ValueType value;
  171. }
  172. SortedDictionary<KeyType, Item> map = new SortedDictionary<KeyType, Item>();
  173. public ValueType Get(KeyType key)
  174. {
  175. if (map.TryGetValue(key, out var item))
  176. {
  177. return item.value;
  178. }
  179. return default;
  180. }
  181. public ValueType Add(KeyType key, ValueType value)
  182. {
  183. if (map.TryGetValue(key, out var item))
  184. {
  185. item.count++;
  186. return item.value;
  187. }
  188. map.Add(key, new Item { value = value });
  189. return value;
  190. }
  191. public ValueType Remove(KeyType key)
  192. {
  193. if (map.TryGetValue(key, out var item))
  194. {
  195. if ((--item.count) <= 0)
  196. {
  197. map.Remove(key);
  198. }
  199. return item.value;
  200. }
  201. return default;
  202. }
  203. }
  204. #endregion
  205. }
  206. }