XlController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using DW5S.DTO;
  9. using DW5S.Entity;
  10. using DW5S.KxcApi;
  11. using DW5S.Repostory;
  12. using DW5S.WebApi;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Serilog;
  15. namespace DW5S.Controllers
  16. {
  17. /// <summary>
  18. /// 星历推算功能相关接口
  19. /// </summary>
  20. public class XlController : BaseController
  21. {
  22. ILogger logger { get; set; }
  23. IUnitOfWork unitOfWork { get; set; }
  24. /// <summary>
  25. /// 导入Tle星历文件
  26. /// </summary>
  27. /// <param name="dto">星历导入参数</param>
  28. /// <returns></returns>
  29. [HttpPost]
  30. public async Task<AjaxResult<RecordRes>> ImportTleAsync(XlImportDto dto)
  31. {
  32. //https://www.space-track.org/documentation#tle网站上有星历文件格式说明
  33. string line = null;
  34. try
  35. {
  36. List<XlInfo> tmp = new List<XlInfo>();
  37. await Task.Run(() =>
  38. {
  39. var lines = System.IO.File.ReadAllLines(GetLocalFile(dto.File)).ToList();
  40. lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
  41. for (int i = 0; i < lines.Count; i += 3)
  42. {
  43. line = lines[i];
  44. var satName = lines[i].Trim();
  45. if (satName.StartsWith("0 "))
  46. satName = satName.Substring(2).Trim();
  47. if (satName.StartsWith("TBA"))//待发布的卫星
  48. continue;
  49. var line1 = lines[i + 1];
  50. var line2 = lines[i + 2];
  51. if (line1.Length != 69 || line2.Length != 69)
  52. {
  53. throw new Exception("星历文件内容错误格式");
  54. }
  55. XlInfo xl = new XlInfo()
  56. {
  57. SatName = satName,
  58. Line1 = line1,
  59. Line2 = line2,
  60. SatCode = Convert.ToInt32(line1.Substring(2, 5))
  61. };
  62. var timeStr = line1.Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  63. var yearStr = timeStr.Substring(0, 2);
  64. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  65. var day = Convert.ToDouble(dayStr);
  66. var year = 2000 + Convert.ToInt32(yearStr);
  67. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  68. dt = dt.AddDays(day - 1);
  69. xl.TimeUTC = dt;
  70. tmp.Add(xl);
  71. }
  72. });
  73. var repsXl = unitOfWork.Of<XlInfo>();
  74. await repsXl.AddOrUpdateAsync(tmp);
  75. await unitOfWork.SaveAsync(typeof(XlInfo));
  76. logger.Information($"星历导入成功,共{tmp.Count}条");
  77. return Success(new RecordRes(tmp.Count));
  78. }
  79. catch (Exception ex)
  80. {
  81. string msg = $"{line}星历导入异常";
  82. logger.Error(ex, msg);
  83. return Error<RecordRes>(msg);
  84. }
  85. }
  86. /// <summary>
  87. /// 使用指定的双行根数推算某个时间点XYZ星历
  88. /// </summary>
  89. /// <param name="dto">推算参数</param>
  90. /// <returns></returns>
  91. [HttpPost]
  92. public async Task<AjaxResult<SatEphResDto>> CalcByTleAsync(XlCalcDto dto)
  93. {
  94. try
  95. {
  96. var p =await Task.Run(() => EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc()));
  97. return Success(new SatEphResDto()
  98. {
  99. SatId = p.SatId,
  100. SatTime = p.SatTime.ToLocal(),
  101. TleTime = p.TleTime.ToLocal(),
  102. Lon = p.Lon,
  103. X = p.X,
  104. Y = p.Y,
  105. Z = p.Z,
  106. VX = p.VX,
  107. VY = p.VY,
  108. VZ = p.VZ,
  109. });
  110. }
  111. catch (Exception ex)
  112. {
  113. string msg = $"推算卫星ECEF星历出错,TwoLine={dto.tleStr}";
  114. return Error<SatEphResDto>(msg);
  115. }
  116. }
  117. /// <summary>
  118. /// 使用指定的双行根数推算一段时间的XYZ星历
  119. /// </summary>
  120. /// <param name="dto">推算参数</param>
  121. /// <returns></returns>
  122. [HttpPost]
  123. public async Task<AjaxResult<List<SatEphResDto>>> MultCalcByTleAsync(XlCalcMultDto dto)
  124. {
  125. try
  126. {
  127. var eph= await Task.Run(() => EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds));
  128. return Success(eph.Select(p => new SatEphResDto()
  129. {
  130. SatId = p.SatId,
  131. SatTime = p.SatTime.ToLocal(),
  132. TleTime = p.TleTime.ToLocal(),
  133. Lon = p.Lon,
  134. X = p.X,
  135. Y = p.Y,
  136. Z = p.Z,
  137. VX = p.VX,
  138. VY = p.VY,
  139. VZ = p.VZ,
  140. }).ToList());
  141. }
  142. catch (Exception ex)
  143. {
  144. return Error<List<SatEphResDto>>(ex.Message);
  145. }
  146. }
  147. /// <summary>
  148. /// 自动根据最佳匹配的时刻推算卫星某个时间点XYZ星历
  149. /// </summary>
  150. /// <param name="dto">推算参数</param>
  151. /// <returns></returns>
  152. [HttpPost]
  153. public async Task<AjaxResult<SatEphResDto>> CalcAsync(SatDto dto)
  154. {
  155. try
  156. {
  157. var repsXl = unitOfWork.Of<XlInfo>() as XlRepository;
  158. var xl1 = await repsXl.GetLatestAsync(dto.SatCode, dto.SigTime);
  159. var p = await Task.Run(() =>
  160. {
  161. return EphHelper.Calc(xl1.TwoLine, dto.SigTime.ToUtc());
  162. });
  163. return Success(new SatEphResDto()
  164. {
  165. SatId = p.SatId,
  166. SatTime = p.SatTime.ToLocal(),
  167. TleTime = p.TleTime.ToLocal(),
  168. Lon = p.Lon,
  169. X = p.X,
  170. Y = p.Y,
  171. Z = p.Z,
  172. VX = p.VX,
  173. VY = p.VY,
  174. VZ = p.VZ,
  175. }); ;
  176. }
  177. catch (Exception ex)
  178. {
  179. return Error<SatEphResDto>(ex.Message);
  180. }
  181. }
  182. }
  183. }