XlController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-1);
  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. await LogHelper.Info($"星历导入成功,共{count}条");
  74. return Success(new RecordRes(count));
  75. }
  76. catch (Exception ex)
  77. {
  78. await LogHelper.Error($"{line}星历导入异常", ex);
  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<SatEphResDto> XLCalc(SatDto dto)
  128. {
  129. try
  130. {
  131. var xl1 = XlRepository.GetLatestAsync(dto.SatCode, dto.SigTime).GetAwaiter().GetResult();
  132. if (xl1 == null)
  133. {
  134. Serilog.Log.Error($"系统缺少编号{dto.SatCode}卫星的星历!");
  135. return Error<SatEphResDto>($"系统缺少编号{dto.SatCode}卫星的星历!");
  136. }
  137. Serilog.Log.Information($"卫星{dto.SatCode}使用发布时刻为{xl1.TimeUTC:yyyyMMddHHmmss}的星历进行推算");
  138. var p = EphHelper.Calc(xl1.TwoLine, dto.SigTime.ToUtc());
  139. return Success(new SatEphResDto()
  140. {
  141. SatId = p.SatId,
  142. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  143. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  144. Lon = p.Lon,
  145. X = p.X,
  146. Y = p.Y,
  147. Z = p.Z,
  148. VX = p.VX,
  149. VY = p.VY,
  150. VZ = p.VZ,
  151. });
  152. }
  153. catch (Exception ex)
  154. {
  155. return Error<SatEphResDto>(ex.Message);
  156. }
  157. }
  158. /// <summary>
  159. /// 推算某个时间段XYZ星星历
  160. /// </summary>
  161. /// <param name="dto">推算参数</param>
  162. /// <returns></returns>
  163. [HttpPost]
  164. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  165. {
  166. try
  167. {
  168. var eph = EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds);
  169. return Success(eph.Select(p => new EphResDto()
  170. {
  171. SatId = p.SatId,
  172. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  173. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  174. Lon = p.Lon,
  175. X = p.X,
  176. Y = p.Y,
  177. Z = p.Z,
  178. VX = p.VX,
  179. VY = p.VY,
  180. VZ = p.VZ,
  181. }).ToList());
  182. }
  183. catch (Exception ex)
  184. {
  185. return Error<List<EphResDto>>(ex.Message);
  186. }
  187. }
  188. }
  189. }