using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using PosResAnalysis;
using Serilog;
using XdCxRhDw.Dto;
using XdCxRhDW.App.Api;
using XdCxRhDW.App.EFContext;
using XdCxRhDW.App.Model;
using XdCxRhDW.App.WebAPI.DTO;
using XdCxRhDW.Dto;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using static XdCxRhDw.CpuCgTools.FormCpuCg;
namespace XdCxRhDW.App.WebAPI
{
///
/// 定位功能相关接口
///
public class PosController : BaseController
{
///
/// 执行两星一地定位
///
///
[HttpPost]
public async Task PosX2D1Async(X2D1PosDto dto)
{
using (RHDWContext db = new RHDWContext())
{
var runTask = await db.TaskInfos.Where(p => p.TaskState == EnumTaskState.Running && p.PosType == EnumPosType.X2D1).FirstOrDefaultAsync();
if (runTask == null)
{
Serilog.Log.Warning($"接收到两星一地定位参数,由于任务没有运行中忽略本次定位!");
return Error($"多模式融合定位平台没有启动两星一地定位任务");
}
var listTx = await db.TxInfos.ToListAsync();
var parameter1 = new SQLiteParameter("@sigTime", dto.SigTime);
var parameter2 = new SQLiteParameter("@satcode", dto.MainSatID);
var parameter3 = new SQLiteParameter("@satcode", dto.AdjaSatID);
var xlInfo1 = await db.XlInfos.SqlQuery("select * from XlInfo where ABS(JULIANDAY(TimeBJ)-JULIANDAY(@sigTime))=(select min(ABS(JULIANDAY(TimeBJ)-JULIANDAY(@sigTime))) from XlInfo where satcode=@satcode)", parameter1, parameter2).FirstOrDefaultAsync();
if (xlInfo1 == null)
{
Serilog.Log.Error($"未找到卫星[{dto.MainSatID}]的双行根数星历");
return Error($"未找到卫星[{dto.MainSatID}]的双行根数星历");
}
var xlInfo2 = await db.XlInfos.SqlQuery("select * from XlInfo where ABS(JULIANDAY(TimeBJ)-JULIANDAY(@sigTime))=(select min(ABS(JULIANDAY(TimeBJ)-JULIANDAY(@sigTime))) from XlInfo where satcode=@satcode)", parameter1, parameter3).FirstOrDefaultAsync();
if (xlInfo2 == null)
{
Serilog.Log.Error($"未找到卫星[{dto.AdjaSatID}]的双行根数星历");
return Error($"未找到卫星[{dto.AdjaSatID}]的双行根数星历");
}
Serilog.Log.Information($"卫星{dto.MainSatID}使用{xlInfo1.TwoLine}进行星历推算");
Serilog.Log.Information($"卫星{dto.AdjaSatID}使用{xlInfo2.TwoLine}进行星历推算");
var ephMain = EphHelper.Calc(xlInfo1.TwoLine, dto.SigTime);
var ephAdja = EphHelper.Calc(xlInfo2.TwoLine, dto.SigTime);
var cgRes = db.CgRes.Add(new Model.CgRes()
{
SigTime = dto.SigTime,
DtoSx = dto.SxDto,
DfoSx = dto.SxDfo,
SnrSx = dto.SxSnr,
DtoCdb = dto.XdDto,
DfoCdb = dto.XdDfo,
SnrCdb = dto.XdSnr,
YbMain = dto.MainYbDto,
YbAdja = dto.AdjaYbDto,
MainX = ephMain.X,
MainY = ephMain.Y,
MainZ = ephMain.Z,
AdjaX = ephAdja.X,
AdjaY = ephAdja.Y,
AdjaZ = ephAdja.Z,
});
var satTx = listTx.Find(p => p.TxType == EnumTxType.MainSat);
var cdbTx = listTx.Find(p => p.TxType == EnumTxType.Cdb);
var cxTx = listTx.Find(p => p.TxType == EnumTxType.Cx);
var refTx = listTx.Find(p => p.TxType == EnumTxType.Ref);
double[] mainSat = new double[3] { ephMain.X, ephMain.Y, ephMain.Z };
double[] adjaSat = new double[3] { ephAdja.X, ephAdja.Y, ephAdja.Z };
double[] satStation = new double[3] { satTx.Lon, satTx.Lat, 0 };
double[] cdbStation = new double[3] { cdbTx.Lon, cdbTx.Lat, 0 };
double[] refStation = new double[3] { refTx.Lon, refTx.Lat, 0 };
double[] zone = new double[] { -85, 85, -180, 180 }; //定位区域
double[] res = new double[6];
PosApi.X2D1_POS_Core(mainSat, adjaSat, cdbStation, satStation, satStation, satStation, satStation, satStation, refStation, zone, dto.SxDto / 1e6, dto.XdDto / 1e6, dto.MainYbDto / 1e6, dto.AdjaYbDto / 1e6, res);
PosRes posRes = new PosRes()
{
SigTime = dto.SigTime,
TaskID = runTask.ID,
CgResID = cgRes.ID,
TarName = string.IsNullOrWhiteSpace(dto.TarName) ? "未知目标" : dto.TarName,
TsName = dto.TsName,
PosLon = res[0],
PosLat = res[1],
MirrLon = res[3],
MirrLat = res[4],
};
db.PosRes.Add(posRes);
await db.SaveChangesAsync();
}
return Success();
}
///
/// 查询定位结果(暂未实现)
///
///
///
///
[HttpGet]
public async Task> GetPosRes(PosResQueryDto dto)
{
await Task.Delay(100);
return Error("该功能暂未实现!");
}
}
}