XlController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. try
  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. var line1 = lines[i + 1];
  52. var line2 = lines[i + 2];
  53. if (line1.Length != 69 || line2.Length != 69)
  54. {
  55. throw new Exception("星历文件内容错误格式");
  56. }
  57. XlInfo xl = new XlInfo()
  58. {
  59. SatName = satName,
  60. Line1 = line1,
  61. Line2 = line2,
  62. SatCode = Convert.ToInt32(line1.Substring(2, 5))
  63. };
  64. var timeStr = line1.Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  65. var yearStr = timeStr.Substring(0, 2);
  66. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  67. var day = Convert.ToDouble(dayStr);
  68. var year = 2000 + Convert.ToInt32(yearStr);
  69. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  70. dt = dt.AddDays(day - 1);
  71. xl.TimeUTC = dt;
  72. tmp.Add(xl);
  73. }
  74. catch (Exception ex)
  75. {
  76. XdCxRhDW.Framework.LogHelper.Error($"星历导入第{i + 1}行格式错误,跳过此行数据", ex);
  77. }
  78. }
  79. Parallel.For(0, 50, i =>
  80. {
  81. using (MySqlContext db = new MySqlContext())
  82. {
  83. db.XlInfos.AddRange(tmp);
  84. db.SaveChanges();
  85. }
  86. });
  87. return 1;
  88. });
  89. XdCxRhDW.Framework.LogHelper.Info($"星历导入成功,共{count}条");
  90. return Success(new RecordRes(count));
  91. }
  92. catch (Exception ex)
  93. {
  94. XdCxRhDW.Framework.LogHelper.Error($"星历导入异常", ex);
  95. return Error<RecordRes>(ex.Message);
  96. }
  97. finally
  98. {
  99. try
  100. {
  101. File.Delete(GetLocalFile(dto.File));
  102. }
  103. catch
  104. { }
  105. }
  106. }
  107. /// <summary>
  108. /// 推算某个时间点XYZ星历
  109. /// </summary>
  110. /// <param name="dto">推算参数</param>
  111. /// <returns></returns>
  112. [HttpPost]
  113. public AjaxResult<SatEphResDto> Calc(XlCalcDto dto)
  114. {
  115. try
  116. {
  117. var p = EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc());
  118. return Success(new SatEphResDto()
  119. {
  120. SatId = p.SatId,
  121. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  122. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  123. Lon = p.Lon,
  124. X = p.X,
  125. Y = p.Y,
  126. Z = p.Z,
  127. VX = p.VX,
  128. VY = p.VY,
  129. VZ = p.VZ,
  130. });
  131. }
  132. catch (Exception ex)
  133. {
  134. return Error<SatEphResDto>(ex.Message);
  135. }
  136. }
  137. /// <summary>
  138. /// 推算某个时间段XYZ星星历
  139. /// </summary>
  140. /// <param name="dto">推算参数</param>
  141. /// <returns></returns>
  142. [HttpPost]
  143. public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
  144. {
  145. try
  146. {
  147. var eph = EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds);
  148. return Success(eph.Select(p => new EphResDto()
  149. {
  150. SatId = p.SatId,
  151. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  152. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  153. Lon = p.Lon,
  154. X = p.X,
  155. Y = p.Y,
  156. Z = p.Z,
  157. VX = p.VX,
  158. VY = p.VY,
  159. VZ = p.VZ,
  160. }).ToList());
  161. }
  162. catch (Exception ex)
  163. {
  164. return Error<List<EphResDto>>(ex.Message);
  165. }
  166. }
  167. /// <summary>
  168. /// 推算卫星某个时间点XYZ星历
  169. /// </summary>
  170. /// <param name="dto">推算参数</param>
  171. /// <returns></returns>
  172. [HttpPost]
  173. public AjaxResult<SatEphResDto> XLCalc(SatDto dto)
  174. {
  175. try
  176. {
  177. var xl1 = XlRepository.GetLatestAsync(dto.SatCode, dto.SigTime).GetAwaiter().GetResult();
  178. if (xl1 == null)
  179. {
  180. XdCxRhDW.Framework.LogHelper.Error($"系统缺少编号{dto.SatCode}卫星的星历!");
  181. return Error<SatEphResDto>($"系统缺少编号{dto.SatCode}卫星的星历!");
  182. }
  183. XdCxRhDW.Framework.LogHelper.Info($"卫星{dto.SatCode}使用发布时刻为{xl1.TimeUTC:yyyyMMddHHmmss}的星历进行推算");
  184. var p = EphHelper.Calc(xl1.TwoLine, dto.SigTime.ToUtc());
  185. return Success(new SatEphResDto()
  186. {
  187. SatId = p.SatId,
  188. SatTime = p.SatTime.AddHours(SysConfig.Config.ZoneHours),
  189. TleTime = p.TleTime.AddHours(SysConfig.Config.ZoneHours),
  190. Lon = p.Lon,
  191. X = p.X,
  192. Y = p.Y,
  193. Z = p.Z,
  194. VX = p.VX,
  195. VY = p.VY,
  196. VZ = p.VZ,
  197. });
  198. }
  199. catch (Exception ex)
  200. {
  201. return Error<SatEphResDto>(ex.Message);
  202. }
  203. }
  204. }
  205. }