XlController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Migrations;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Http;
  12. using XdCxRhDW.Dto;
  13. using XdCxRhDW.Api;
  14. using XdCxRhDW.Entity;
  15. using XdCxRhDW.Repostory;
  16. using XdCxRhDW.WebApi;
  17. namespace XdCxRhDW.App.Controllers
  18. {
  19. /// <summary>
  20. /// 星历推算功能相关接口
  21. /// </summary>
  22. public class XlController : BaseController
  23. {
  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. var count = await Task.Run(async () =>
  37. {
  38. var lines = File.ReadAllLines(GetLocalFile(dto.File)).ToList();
  39. lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
  40. List<XlInfo> tmp = new List<XlInfo>();
  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. XlInfo xl = new XlInfo()
  50. {
  51. SatName = satName,
  52. Line1 = lines[i + 1],
  53. Line2 = lines[i + 2],
  54. SatCode = Convert.ToInt32(lines[i + 1].Substring(2, 5))
  55. };
  56. var timeStr = lines[i + 1].Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  57. var yearStr = timeStr.Substring(0, 2);
  58. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  59. var day = Convert.ToDouble(dayStr);
  60. var year = 2000 + Convert.ToInt32(yearStr);
  61. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  62. dt = dt.AddDays(day);
  63. xl.TimeUTC = dt;
  64. tmp.Add(xl);
  65. }
  66. using (RHDWContext db = new RHDWContext())
  67. {
  68. db.XlInfos.AddRange(tmp);
  69. await db.SaveChangesAsync();
  70. }
  71. return tmp.Count;
  72. });
  73. Serilog.Log.Information($"星历导入成功,共{count}条");
  74. return Success(new RecordRes(count));
  75. }
  76. catch (Exception ex)
  77. {
  78. Serilog.Log.Error(ex, $"{line}星历导入异常");
  79. return Error<RecordRes>(ex.Message);
  80. }
  81. finally
  82. {
  83. try
  84. {
  85. File.Delete(GetLocalFile(dto.File));
  86. }
  87. catch
  88. { }
  89. }
  90. }
  91. /// <summary>
  92. /// 推算某个时间点XYZ星历
  93. /// </summary>
  94. /// <param name="dto">推算参数</param>
  95. /// <returns></returns>
  96. [HttpPost]
  97. public AjaxResult<SatEphResDto> Calc(XlCalcDto dto)
  98. {
  99. try
  100. {
  101. var p = EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc());
  102. return Success(new SatEphResDto()
  103. {
  104. SatId = p.SatId,
  105. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  106. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  107. Lon = p.Lon,
  108. X = p.X,
  109. Y = p.Y,
  110. Z = p.Z,
  111. VX = p.VX,
  112. VY = p.VY,
  113. VZ = p.VZ,
  114. });
  115. }
  116. catch (Exception ex)
  117. {
  118. return Error<SatEphResDto>(ex.Message);
  119. }
  120. }
  121. /// <summary>
  122. /// 推算某个时间段XYZ星星历
  123. /// </summary>
  124. /// <param name="dto">推算参数</param>
  125. /// <returns></returns>
  126. [HttpPost]
  127. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  128. {
  129. try
  130. {
  131. var eph = EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds);
  132. return Success(eph.Select(p => new EphResDto()
  133. {
  134. SatId = p.SatId,
  135. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  136. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  137. Lon = p.Lon,
  138. X = p.X,
  139. Y = p.Y,
  140. Z = p.Z,
  141. VX = p.VX,
  142. VY = p.VY,
  143. VZ = p.VZ,
  144. }).ToList());
  145. }
  146. catch (Exception ex)
  147. {
  148. return Error<List<EphResDto>>(ex.Message);
  149. }
  150. }
  151. }
  152. }