XlController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. for (int i = 0; i < 8; i++)
  41. {
  42. lines.AddRange(lines);
  43. }
  44. List<XlInfo> tmp = new List<XlInfo>();
  45. for (int i = 0; i < lines.Count; i += 3)
  46. {
  47. line = lines[i];
  48. var satName = lines[i].Trim();
  49. if (satName.StartsWith("0 "))
  50. satName = satName.Substring(2).Trim();
  51. if (satName.StartsWith("TBA"))//待发布的卫星
  52. continue;
  53. var line1 = lines[i + 1];
  54. var line2 = lines[i + 2];
  55. if (line1.Length != 69 || line2.Length != 69)
  56. {
  57. throw new Exception("星历文件内容错误格式");
  58. }
  59. XlInfo xl = new XlInfo()
  60. {
  61. SatName = satName,
  62. Line1 = line1,
  63. Line2 = line2,
  64. SatCode = Convert.ToInt32(line1.Substring(2, 5))
  65. };
  66. var timeStr = line1.Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  67. var yearStr = timeStr.Substring(0, 2);
  68. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  69. var day = Convert.ToDouble(dayStr);
  70. var year = 2000 + Convert.ToInt32(yearStr);
  71. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  72. dt = dt.AddDays(day);
  73. xl.TimeUTC = dt;
  74. tmp.Add(xl);
  75. }
  76. using (RHDWContext db = new RHDWContext())
  77. {
  78. db.XlInfos.AddRange(tmp);
  79. await db.SaveChangesAsync();
  80. }
  81. return tmp.Count;
  82. });
  83. Serilog.Log.Information($"星历导入成功,共{count}条");
  84. return Success(new RecordRes(count));
  85. }
  86. catch (Exception ex)
  87. {
  88. Serilog.Log.Error(ex, $"{line}星历导入异常,不支持的格式");
  89. return Error<RecordRes>(ex.Message);
  90. }
  91. finally
  92. {
  93. try
  94. {
  95. File.Delete(GetLocalFile(dto.File));
  96. }
  97. catch
  98. { }
  99. }
  100. }
  101. /// <summary>
  102. /// 推算某个时间点XYZ星历
  103. /// </summary>
  104. /// <param name="dto">推算参数</param>
  105. /// <returns></returns>
  106. [HttpPost]
  107. public AjaxResult<SatEphResDto> Calc(XlCalcDto dto)
  108. {
  109. try
  110. {
  111. var p = EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc());
  112. return Success(new SatEphResDto()
  113. {
  114. SatId = p.SatId,
  115. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  116. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  117. Lon = p.Lon,
  118. X = p.X,
  119. Y = p.Y,
  120. Z = p.Z,
  121. VX = p.VX,
  122. VY = p.VY,
  123. VZ = p.VZ,
  124. });
  125. }
  126. catch (Exception ex)
  127. {
  128. return Error<SatEphResDto>(ex.Message);
  129. }
  130. }
  131. /// <summary>
  132. /// 推算某个时间段XYZ星星历
  133. /// </summary>
  134. /// <param name="dto">推算参数</param>
  135. /// <returns></returns>
  136. [HttpPost]
  137. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  138. {
  139. try
  140. {
  141. var eph = EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds);
  142. return Success(eph.Select(p => new EphResDto()
  143. {
  144. SatId = p.SatId,
  145. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  146. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  147. Lon = p.Lon,
  148. X = p.X,
  149. Y = p.Y,
  150. Z = p.Z,
  151. VX = p.VX,
  152. VY = p.VY,
  153. VZ = p.VZ,
  154. }).ToList());
  155. }
  156. catch (Exception ex)
  157. {
  158. return Error<List<EphResDto>>(ex.Message);
  159. }
  160. }
  161. }
  162. }