XlController.cs 7.0 KB

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