XlController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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);
  69. xl.TimeUTC = dt;
  70. tmp.Add(xl);
  71. }
  72. using (RHDWContext db = new RHDWContext())
  73. {
  74. db.XlInfos.AddRange(tmp);
  75. await db.SaveChangesAsync();
  76. }
  77. return tmp.Count;
  78. });
  79. Serilog.Log.Information($"星历导入成功,共{count}条");
  80. return Success(new RecordRes(count));
  81. }
  82. catch (Exception ex)
  83. {
  84. Serilog.Log.Error(ex, $"{line}星历导入异常,不支持的格式");
  85. return Error<RecordRes>(ex.Message);
  86. }
  87. finally
  88. {
  89. try
  90. {
  91. File.Delete(GetLocalFile(dto.File));
  92. }
  93. catch
  94. { }
  95. }
  96. }
  97. /// <summary>
  98. /// 推算某个时间点XYZ星历
  99. /// </summary>
  100. /// <param name="dto">推算参数</param>
  101. /// <returns></returns>
  102. [HttpPost]
  103. public AjaxResult<SatEphResDto> Calc(XlCalcDto dto)
  104. {
  105. try
  106. {
  107. var p = EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc());
  108. return Success(new SatEphResDto()
  109. {
  110. SatId = p.SatId,
  111. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  112. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  113. Lon = p.Lon,
  114. X = p.X,
  115. Y = p.Y,
  116. Z = p.Z,
  117. VX = p.VX,
  118. VY = p.VY,
  119. VZ = p.VZ,
  120. });
  121. }
  122. catch (Exception ex)
  123. {
  124. return Error<SatEphResDto>(ex.Message);
  125. }
  126. }
  127. /// <summary>
  128. /// 推算某个时间段XYZ星星历
  129. /// </summary>
  130. /// <param name="dto">推算参数</param>
  131. /// <returns></returns>
  132. [HttpPost]
  133. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  134. {
  135. try
  136. {
  137. var eph = EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds);
  138. return Success(eph.Select(p => new EphResDto()
  139. {
  140. SatId = p.SatId,
  141. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  142. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  143. Lon = p.Lon,
  144. X = p.X,
  145. Y = p.Y,
  146. Z = p.Z,
  147. VX = p.VX,
  148. VY = p.VY,
  149. VZ = p.VZ,
  150. }).ToList());
  151. }
  152. catch (Exception ex)
  153. {
  154. return Error<List<EphResDto>>(ex.Message);
  155. }
  156. }
  157. }
  158. }