123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DW5S.DTO;
- using DW5S.Entity;
- using DW5S.KxcApi;
- using DW5S.Repostory;
- using DW5S.WebApi;
- using Microsoft.AspNetCore.Mvc;
- using Serilog;
- namespace DW5S.Controllers
- {
- /// <summary>
- /// 星历推算功能相关接口
- /// </summary>
- public class XlController : BaseController
- {
- ILogger logger { get; set; }
- IUnitOfWork unitOfWork { get; set; }
- /// <summary>
- /// 导入Tle星历文件
- /// </summary>
- /// <param name="dto">星历导入参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<RecordRes>> ImportTleAsync(XlImportDto dto)
- {
- //https://www.space-track.org/documentation#tle网站上有星历文件格式说明
- string line = null;
- try
- {
- List<XlInfo> tmp = new List<XlInfo>();
- await Task.Run(() =>
- {
- var lines = System.IO.File.ReadAllLines(GetLocalFile(dto.File)).ToList();
- lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
- for (int i = 0; i < lines.Count; i += 3)
- {
- line = lines[i];
- var satName = lines[i].Trim();
- if (satName.StartsWith("0 "))
- satName = satName.Substring(2).Trim();
- if (satName.StartsWith("TBA"))//待发布的卫星
- continue;
- var line1 = lines[i + 1];
- var line2 = lines[i + 2];
- if (line1.Length != 69 || line2.Length != 69)
- {
- throw new Exception("星历文件内容错误格式");
- }
- XlInfo xl = new XlInfo()
- {
- SatName = satName,
- Line1 = line1,
- Line2 = line2,
- SatCode = Convert.ToInt32(line1.Substring(2, 5))
- };
- var timeStr = line1.Substring(18, 14).Replace(" ", "");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
- var yearStr = timeStr.Substring(0, 2);
- var dayStr = timeStr.Substring(2, timeStr.Length - 2);
- var day = Convert.ToDouble(dayStr);
- var year = 2000 + Convert.ToInt32(yearStr);
- DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
- dt = dt.AddDays(day - 1);
- xl.TimeUTC = dt;
- tmp.Add(xl);
- }
-
- });
- var repsXl = unitOfWork.Of<XlInfo>();
- await repsXl.AddOrUpdateAsync(tmp);
- await unitOfWork.SaveAsync(typeof(XlInfo));
- logger.Information($"星历导入成功,共{tmp.Count}条");
- return Success(new RecordRes(tmp.Count));
- }
- catch (Exception ex)
- {
- string msg = $"{line}星历导入异常";
- logger.Error(ex, msg);
- return Error<RecordRes>(msg);
- }
- }
- /// <summary>
- /// 使用指定的双行根数推算某个时间点XYZ星历
- /// </summary>
- /// <param name="dto">推算参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<SatEphResDto>> CalcByTleAsync(XlCalcDto dto)
- {
- try
- {
- var p =await Task.Run(() => EphHelper.Calc(dto.tleStr, dto.SigTime.ToUtc()));
- return Success(new SatEphResDto()
- {
- SatId = p.SatId,
- SatTime = p.SatTime.ToLocal(),
- TleTime = p.TleTime.ToLocal(),
- Lon = p.Lon,
- X = p.X,
- Y = p.Y,
- Z = p.Z,
- VX = p.VX,
- VY = p.VY,
- VZ = p.VZ,
- });
- }
- catch (Exception ex)
- {
- string msg = $"推算卫星ECEF星历出错,TwoLine={dto.tleStr}";
- return Error<SatEphResDto>(msg);
- }
- }
- /// <summary>
- /// 使用指定的双行根数推算一段时间的XYZ星历
- /// </summary>
- /// <param name="dto">推算参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<List<SatEphResDto>>> MultCalcByTleAsync(XlCalcMultDto dto)
- {
- try
- {
- var eph= await Task.Run(() => EphHelper.CalcMult(dto.tleStr, dto.startTime.ToUtc(), dto.endTime.ToUtc(), dto.spanSeconds));
- return Success(eph.Select(p => new SatEphResDto()
- {
- SatId = p.SatId,
- SatTime = p.SatTime.ToLocal(),
- TleTime = p.TleTime.ToLocal(),
- Lon = p.Lon,
- X = p.X,
- Y = p.Y,
- Z = p.Z,
- VX = p.VX,
- VY = p.VY,
- VZ = p.VZ,
- }).ToList());
- }
- catch (Exception ex)
- {
- return Error<List<SatEphResDto>>(ex.Message);
- }
- }
- /// <summary>
- /// 自动根据最佳匹配的时刻推算卫星某个时间点XYZ星历
- /// </summary>
- /// <param name="dto">推算参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<SatEphResDto>> CalcAsync(SatDto dto)
- {
- try
- {
- var repsXl = unitOfWork.Of<XlInfo>() as XlRepository;
- var xl1 = await repsXl.GetLatestAsync(dto.SatCode, dto.SigTime);
- var p = await Task.Run(() =>
- {
- return EphHelper.Calc(xl1.TwoLine, dto.SigTime.ToUtc());
- });
- return Success(new SatEphResDto()
- {
- SatId = p.SatId,
- SatTime = p.SatTime.ToLocal(),
- TleTime = p.TleTime.ToLocal(),
- Lon = p.Lon,
- X = p.X,
- Y = p.Y,
- Z = p.Z,
- VX = p.VX,
- VY = p.VY,
- VZ = p.VZ,
- }); ;
- }
- catch (Exception ex)
- {
- return Error<SatEphResDto>(ex.Message);
- }
- }
- }
- }
|