XlController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.Dto;
  15. using XdCxRhDW.Entity;
  16. using XdCxRhDW.Repostory;
  17. using XdCxRhDW.WebApi;
  18. namespace XdCxRhDW.App.Controllers
  19. {
  20. /// <summary>
  21. /// 星历推算功能相关接口
  22. /// </summary>
  23. public class XlController : BaseController
  24. {
  25. /// <summary>
  26. /// 导入Tle星历文件
  27. /// </summary>
  28. /// <param name="dto">星历导入参数</param>
  29. /// <returns></returns>
  30. [HttpPost]
  31. public async Task<AjaxResult<RecordRes>> ImportTleAsync(XlImportDto dto)
  32. {
  33. //https://www.space-track.org/documentation#tle网站上有星历文件格式说明
  34. string line = null;
  35. try
  36. {
  37. var count = await Task.Run(async () =>
  38. {
  39. var lines = File.ReadAllLines(GetLocalFile(dto.File)).ToList();
  40. lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
  41. List<XlInfo> tmp = new List<XlInfo>();
  42. for (int i = 0; i < lines.Count; i += 3)
  43. {
  44. line = lines[i];
  45. var satName = lines[i].Trim();
  46. if (satName.StartsWith("0 "))
  47. satName = satName.Substring(2).Trim();
  48. if (satName.StartsWith("TBA"))//待发布的卫星
  49. continue;
  50. XlInfo xl = new XlInfo()
  51. {
  52. SatName = satName,
  53. Line1 = lines[i + 1],
  54. Line2 = lines[i + 2],
  55. SatCode = Convert.ToInt32(lines[i + 1].Substring(2, 5))
  56. };
  57. var timeStr = lines[i + 1].Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  58. var yearStr = timeStr.Substring(0, 2);
  59. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  60. var day = Convert.ToDouble(dayStr);
  61. var year = 2000 + Convert.ToInt32(yearStr);
  62. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  63. dt = dt.AddDays(day);
  64. xl.TimeBJ = dt.AddHours(8);
  65. tmp.Add(xl);
  66. }
  67. using (RHDWContext db = new RHDWContext())
  68. {
  69. db.XlInfos.AddRange(tmp);
  70. await db.SaveChangesAsync();
  71. }
  72. return tmp.Count;
  73. });
  74. Serilog.Log.Information($"星历导入成功,共{count}条");
  75. return Success(new RecordRes(count));
  76. }
  77. catch (Exception ex)
  78. {
  79. Serilog.Log.Error(ex, $"{line}星历导入异常");
  80. return Error<RecordRes>(ex.Message);
  81. }
  82. finally
  83. {
  84. try
  85. {
  86. File.Delete(GetLocalFile(dto.File));
  87. }
  88. catch
  89. { }
  90. }
  91. }
  92. /// <summary>
  93. /// 推算某个时间点XYZ星历
  94. /// </summary>
  95. /// <param name="dto">推算参数</param>
  96. /// <returns></returns>
  97. [HttpPost]
  98. public AjaxResult<SatEphResDto> Calc(XlCalcDto dto)
  99. {
  100. try
  101. {
  102. var p = EphHelper.Calc(dto.tleStr, dto.dt);
  103. return Success(new SatEphResDto()
  104. {
  105. SatId = p.SatId,
  106. SatTime = p.SatTime,
  107. TleTime = p.TleTime,
  108. Lon=p.Lon,
  109. X = p.X,
  110. Y = p.Y,
  111. Z = p.Z,
  112. VX = p.VX,
  113. VY = p.VY,
  114. VZ = p.VZ,
  115. });
  116. }
  117. catch (Exception ex)
  118. {
  119. return Error<SatEphResDto>(ex.Message);
  120. }
  121. }
  122. /// <summary>
  123. /// 推算某个时间段XYZ星星历
  124. /// </summary>
  125. /// <param name="dto">推算参数</param>
  126. /// <returns></returns>
  127. [HttpPost]
  128. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  129. {
  130. try
  131. {
  132. var eph = EphHelper.CalcMult(dto.tleStr, dto.start, dto.end, dto.spanSeconds);
  133. return Success(eph.Select(p => new EphResDto()
  134. {
  135. SatId = p.SatId,
  136. SatTime = p.SatTime,
  137. TleTime = p.TleTime,
  138. Lon = p.Lon,
  139. X = p.X,
  140. Y = p.Y,
  141. Z = p.Z,
  142. VX = p.VX,
  143. VY = p.VY,
  144. VZ = p.VZ,
  145. }).ToList());
  146. }
  147. catch (Exception ex)
  148. {
  149. return Error<List<EphResDto>>(ex.Message);
  150. }
  151. }
  152. }
  153. }