EphManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using DevExpress.Xpo;
  2. using Ips.Eph.ResolveUtil;
  3. using Ips.EphAlgorithm;
  4. using Ips.Library.Entity;
  5. using Ips.Sps.Sats;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Ips.Sps.Ephs
  13. {
  14. public sealed class EphManager
  15. {
  16. public static readonly EphManager Default = new EphManager();
  17. private EphManager() { }
  18. Session session = new Session();
  19. ConcurrentDictionary<string, EphResult> _ephCache = new ConcurrentDictionary<string, EphResult>();
  20. public EphResult GetEph(int satNum, DateTime time, CancellationToken token = default)
  21. {
  22. string key = $"{satNum}_{time:yyyyMMddHHmmss}";
  23. EphResult result;
  24. if (_ephCache.ContainsKey(key))
  25. {
  26. result = _ephCache[key];
  27. }
  28. else
  29. {
  30. result = GetEphHigh(satNum, time, token);
  31. if (result == null)
  32. {
  33. result = GetEphTle(satNum, time, token);
  34. }
  35. if (result != null)
  36. _ephCache.TryAdd(key, result);
  37. }
  38. while (_ephCache.Count > 2000)
  39. {
  40. _ephCache.TryRemove(_ephCache.First());
  41. }
  42. return result;
  43. }
  44. public EphResult GetEphHigh(int satNum, DateTime time, CancellationToken token = default)
  45. {
  46. EphResult result = null;
  47. var eph = session.Query<Eph>()
  48. .Where(m => m.SatNum == satNum && m.EphType == EphType.High && m.StartTime <= time && m.EndTime >= time)
  49. .OrderByDescending(m => m.Id)
  50. .FirstOrDefault();
  51. if (eph != null)
  52. {
  53. var ephHigh = session.Query<EphHigh>()
  54. .Where(m => m.EphId == eph.Id && m.EphTime == time)
  55. .FirstOrDefault();
  56. if (ephHigh != null)
  57. {
  58. result = new EphResult(satNum, EphType.High, time, ephHigh.X, ephHigh.Y, ephHigh.Z);
  59. }
  60. }
  61. return result;
  62. }
  63. public EphResult GetEphTle(int satNum, DateTime time, CancellationToken token = default)
  64. {
  65. var ephModel = session.Query<Eph>()
  66. .Where(m => m.SatNum == satNum && m.EphType == EphType.Tle && m.StartTime <= time)
  67. .OrderByDescending(m => m.StartTime)
  68. .FirstOrDefault();
  69. if (ephModel == null)
  70. {
  71. ephModel = session.Query<Eph>()
  72. .Where(m => m.SatNum == satNum && m.StartTime > time)
  73. .OrderBy(m => m.StartTime)
  74. .FirstOrDefault();
  75. }
  76. if (ephModel == null) return null;
  77. var lineArr = ephModel.Content.Split(';');
  78. if (lineArr.Length != 2) return null;
  79. ExeResult<EphResult> ephResult;
  80. if (SpsConst.EnablEphTle)
  81. {
  82. ephResult = Task.Run(() => EphTleUtil2.Get(lineArr[0], lineArr[1], time, token)).Result;
  83. }
  84. else
  85. {
  86. ephResult = Task.Run(() => EphTleUtil.Get(lineArr[0], lineArr[1], time, token)).Result;
  87. }
  88. if (ephResult != null && ephResult.Result != null && ephResult.Result.SatNum == 0)
  89. {
  90. ephResult.Result.SatNum = satNum;
  91. ephResult.Result.EphType = EphType.Tle;
  92. }
  93. return ephResult.Result;
  94. }
  95. public void ClearCache()
  96. {
  97. _ephCache.Clear();
  98. }
  99. public void DeleteEph(int[] ephIds)
  100. {
  101. using var uow = new UnitOfWork();
  102. var ephList = uow.Query<Eph>().Where(m => ephIds.Contains(m.Id)).ToList();
  103. foreach (var ephItem in ephList)
  104. {
  105. if (ephItem.EphType == EphType.High)
  106. {
  107. uow.ExecuteNonQuery($"delete from ephhigh where ephid={ephItem.Id}");
  108. }
  109. ephItem.Delete();
  110. }
  111. uow.CommitChanges();
  112. }
  113. }
  114. }