using DevExpress.Utils.About;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using XdCxRhDW.Dto;
using XdCxRhDW.Entity;
using XdCxRhDW.Repostory;
namespace XdCxRhDW.App
{
///
/// 两星一地定位取参考
///
public class X2D1PosHandle
{
public int TaskId;
//十分钟时间内的数据
private int TimeSeconds = 30 * 60;//十分钟
private bool isRuning = true;
BlockingCollection posItems = new BlockingCollection();
public X2D1PosHandle(int taskId)
{
TaskId = taskId;
StartMonitor();
}
public void StartMonitor()
{
Task.Run(async () =>
{
while (isRuning)
{
try
{
var dto = posItems.Take();
FixedStation fixedStation;
int fixedStationId = dto.FixedStationId.HasValue ? dto.FixedStationId.Value : 0;
using (RHDWContext db = new RHDWContext())
{
fixedStation = await db.FixedStation.FirstOrDefaultAsync(f => f.ID == dto.FixedStationId.Value);
}
if (fixedStation == null)//没有固定站
{
SendX2D1NoXlNoParAsync(dto);
continue;
}
using (RHDWPartContext db = RHDWPartContext.GetContext(dto.SigTime, true))
{
DateTime min = dto.SigTime.AddSeconds(-TimeSeconds);
DateTime max = dto.SigTime.AddSeconds(TimeSeconds);
//获取设定分钟之内的固定站数据
var cgResRefs = await db.CgRes.Where(m => m.TaskID == dto.TaskID && m.FixedStationID == dto.FixedStationId && m.SigTime >= min && m.SigTime <= max).OrderBy(d => d.SigTime).ToListAsync();
List matchCgList = new List();
cgResRefs.ForEach(c =>
{
if ((dto.SigTime - c.SigTime).TotalSeconds <= TimeSeconds)
{
matchCgList.Add(c);
}
});
var cgOrderList = matchCgList.OrderBy(d => d.SigTime).ToList();
var point1 = cgOrderList.Where(c => c.SigTime <= dto.SigTime).FirstOrDefault();
var point2 = cgOrderList.Where(c => c.SigTime >= dto.SigTime).FirstOrDefault();
if (point1 == null || point2 == null)//若参考频点未出结果,查找目标信号出
{
bool isTarAny = await db.CgRes.AnyAsync(m => m.TaskID == dto.TaskID && m.TarFreqUp.HasValue&&m.TarFreqUp.Value== fixedStation.FreqUpHz && m.SigTime >= dto.SigTime);
if (!isTarAny)//目标未出结果
{
Append(dto);
continue;
}
}
bool isSucces = false;
if (point1 != null && point2 != null)
{
//通过双星时差及超短波时差怎么计算参考信息的时差
double refDto = LinearInterpolation.CalSigTimeDto(dto.SigTime, point1.SigTime, point2.SigTime, point1.Dto1.Value, point2.Dto1.Value);
double refDtoCdb = LinearInterpolation.CalSigTimeDto(dto.SigTime, point1.SigTime, point2.SigTime, point1.DtoCdb.Value, point2.DtoCdb.Value);
//执行两星一地有参定位
X2D1NoXlPosDto X2D1NoXlPosDto = new X2D1NoXlPosDto()
{
SigTime = dto.SigTime,
MainCode = dto.MainCode,
AdjaCode = dto.AdjaCode,
SxDto = dto.SxDto,
XdDto = dto.XdDto,
MainYbDto = refDto,
AdjaYbDto = refDtoCdb,
SatTxLon = dto.SatTxLon,
SatTxLat = dto.SatTxLat,
CdbTxLon = dto.CdbTxLon,
CdbTxLat = dto.CdbTxLat,
RefLon = fixedStation.Lon,
RefLat = fixedStation.Lat,
FreqDown = dto.FreqDown,
FreqUp = dto.FreqUp,
XdDfo = dto.XdDfo,
XdSnr = dto.XdSnr,
SxDfo = dto.SxDfo,
SxSnr = dto.SxSnr,
CalcConfidence = true,
TheoryDfoCalc = true,
};
var result = await HttpHelper.PostRequestAsync(SysConfig.GetUrl("Pos/PosX2D1NoXlAsync"), X2D1NoXlPosDto);
if (result.code != 200)
{
isSucces = false;
Serilog.Log.Error($"{dto.SigTime:yyyy-MM-dd HH:mm:ss}两星一地有参定位失败{result.msg}");
}
else
{
isSucces = true;
Serilog.Log.Error($"{dto.SigTime:yyyy-MM-dd HH:mm:ss}两星一地有参定位完成{result.msg}");
}
if (isSucces && result.data.PosLon == 999)
{
isSucces = false;
}
}
if (!isSucces)
{
SendX2D1NoXlNoParAsync(dto);
}
}
}
catch (Exception ex)
{
Serilog.Log.Error(ex, $"【取参考信号流程异常{ex.Message}");
}
}
});
}
///
/// 发送两星一地无参定位
///
///
private async void SendX2D1NoXlNoParAsync(X2D1NoXlNoParlPosDto dto)
{
dto.BeFindRef = false;
dto.FixedStationId = null;
var result = await HttpHelper.PostRequestAsync(SysConfig.GetUrl("Pos/PosX2D1NoXlNoParAsync"), dto);
if (result.code != 200)
{
Serilog.Log.Error($"{dto.SigTime:yyyy-MM-dd HH:mm:ss}两星一地无参定位失败{result.msg}");
}
else
{
Serilog.Log.Error($"{dto.SigTime:yyyy-MM-dd HH:mm:ss}两星一地无参定位完成{result.msg}");
}
}
public void Append(X2D1NoXlNoParlPosDto dto)
{
bool ret = posItems.TryAdd(dto);
if (!ret)
{
Serilog.Log.Error($"【任务{dto.TaskID}】添加取参考Dto");
}
}
public void Stop()
{
//当前任务停止处理
isRuning = false;
}
}
}