XlController.cs 5.4 KB

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