123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using DevExpress.Utils.Extensions;
- using MySqlX.XDevAPI.Common;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using XdCxRhDW.Dto;
- namespace XdCxRhDW.X2D1TaskServer.Service
- {
- public class HistoryTaskService
- {
- private readonly string baseUrl;
- CancellationTokenSource cts;
- public HistoryTaskService()
- {
- var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();//like http://127.0.0.1:8091 or http://127.0.0.1:8091/
- if (posPlatformAddr.EndsWith("/"))
- this.baseUrl = posPlatformAddr + "api/";
- else
- this.baseUrl = posPlatformAddr + "/api/";
- }
- public void StartAsync(X2D1HistoryTaskHandleDto dto)
- {
- cts = new CancellationTokenSource();
- LogHelper.Info($"接收到开始执行任务[{dto.TaskName}],ID={dto.ID}");
- //点击定位平台右上角查看接口可以在浏览器中查看平台提供的所有接口详细信息
- Task.Run(async () =>
- {
- DateTime preTime = dto.StartTime;
- int formatFlag;
- if (string.IsNullOrWhiteSpace(dto.DateDirFormat))
- {
- //没有日期目录,处理完目录中的数据后停止任务
- formatFlag = 0;
- }
- else if (dto.DateDirFormat.ToLower().EndsWith("dd"))
- {
- //处理完一个目录后跳转到第二天的目录
- formatFlag = 1;
- }
- else if (dto.DateDirFormat.ToUpper().EndsWith("HH"))
- {
- //处理完一个目录后跳转到下一个小时的目录
- formatFlag = 2;
- }
- else
- {
- LogHelper.Error($"【任务{dto.ID}】执行异常,不支持的日期目录格式");
- return;
- }
- while (!cts.IsCancellationRequested && preTime <= dto.EndTime)
- {
- string filesDir = null;
- try
- {
- filesDir = Path.Combine(dto.CapDir, $"{preTime.ToString(dto.DateDirFormat)}");//yyyyMMdd
- LogHelper.Info($"【任务{dto.ID}】正在处理[{filesDir}]目录中的数据...");
- IEnumerable<string> files;
- if (!Directory.Exists(filesDir))
- {
- LogHelper.Info($"【任务{dto.ID}】目录[{filesDir}]不存在,跳过此目录");
- ResetTime(formatFlag, ref preTime);
- continue;
- }
- files = Directory.EnumerateFiles(filesDir, "*.dat");
- if (!files.Any())
- {
- LogHelper.Info($"【任务{dto.ID}】目录[{filesDir}]中没有文件,跳过此目录");
- ResetTime(formatFlag, ref preTime);
- continue;
- }
- IOrderedEnumerable<IGrouping<DateTime, HistoryFile>> groups = null;
- groups = files.Select(f => FileToHistoryFile(dto, f)).GroupBy(m => m.CapTime).OrderBy(m => m.Key);
- foreach (var item in groups)
- {
- if (cts.IsCancellationRequested) break;
- var finfos = item.ToList();
- var capTime = finfos.First().CapTime;
- if (capTime < dto.StartTime) continue;
- if (finfos.Count < 3)
- {
- LogHelper.Warning($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻文件数量只有{finfos.Count}个,跳过此组数据");
- continue;
- }
- //超短波信号
- var dinfo = finfos.FirstOrDefault(m => m.Ch == 1);
- if (dinfo == null)
- {
- LogHelper.Warning($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻未找到超短波信号ch1文件,跳过此组数据");
- continue;
- }
- //主星
- var minfo = finfos.FirstOrDefault(m => m.Ch == 2);
- if (minfo == null)
- {
- LogHelper.Warning($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻未找到主星信号ch2文件,跳过此组数据");
- continue;
- }
- //邻1
- var ninfo = finfos.FirstOrDefault(m => m.Ch == 3);
- if (ninfo == null)
- {
- LogHelper.Info($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻未找到邻星信号ch3文件,跳过此组数据");
- continue;
- }
- try
- {
- string mainFile = await HttpHelper.UploadFileAsync(minfo.FilePath, baseUrl + "File/UploadFileAsync", token: cts.Token);//主星文件
- string adjaFile = await HttpHelper.UploadFileAsync(ninfo.FilePath, baseUrl + "File/UploadFileAsync", token: cts.Token);//邻星文件
- string cdbFile = await HttpHelper.UploadFileAsync(dinfo.FilePath, baseUrl + "File/UploadFileAsync", token: cts.Token);//超短文件
- DetectDto detectDto = new DetectDto()
- {
- file1 = cdbFile,//11局使用上行泄露信号进行检测
- dmcType = DmcType.Ky5758,//上行信号目前算法只能使用基于能量的Ky或IBS检测
- fsHz = minfo.FsHz,
- };
- var deteResp = await HttpHelper.PostRequestAsync<List<DetectResDto>>(baseUrl + "DetectCg/DetectCalc", detectDto, token: cts.Token);
- if (deteResp.code != 200)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻信号检测出错.{deteResp.msg}");
- continue;
- }
- LogHelper.Info($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻信号检测完成,共{deteResp.data.Count}个时隙");
- var smps = deteResp.data.Select(m => new SmpPosition(m.Start, m.Length)).ToList();//怎么补0?
- var cgDto = new CpuCgMultiDto()
- {
- dtCenter = 260000,
- dtRange = 60000,
- file1 = mainFile,
- file2 = cdbFile,
- samplingRate = minfo.FsHz,
- smpPositions = smps,
- snrThreshold = 15,
- };
- var result1 = await HttpHelper.PostRequestAsync<List<CpuCgResDto>>(baseUrl + "DetectCg/CpuCgMultiCalc", cgDto, token: cts.Token);
- if (result1.code != 200)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻主星超短CPU参估出错.{deteResp.msg}");
- continue;
- }
- LogHelper.Info($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻主星超短CPU参估完成.");
- cgDto = new CpuCgMultiDto()
- {
- dtCenter = 260000,
- dtRange = 60000,
- file1 = adjaFile,
- file2 = cdbFile,
- samplingRate = minfo.FsHz,
- smpPositions = smps,
- snrThreshold = 15,
- };
- var result2 = await HttpHelper.PostRequestAsync<List<CpuCgResDto>>(baseUrl + "DetectCg/CpuCgMultiCalc", cgDto, token: cts.Token);
- if (result2.code != 200)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻邻星超短CPU参估出错.{deteResp.msg}");
- continue;
- }
- LogHelper.Info($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻邻星超短CPU参估完成");
- var data1 = result1.data;
- var data2 = result2.data;
- if (data1.Count != data2.Count || data1.Count != deteResp.data.Count)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻参估结果个数和检测结果个数不匹配");
- continue;
- }
- for (int i = 0; i < data1.Count; i++)
- {
- try
- {
- if (cts.IsCancellationRequested) break;
- X2D1NoXlNoParlPosDto x2D1 = new X2D1NoXlNoParlPosDto()
- {
- TaskID = dto.ID,
- SigTime = minfo.CapTime.AddSeconds(data1[i].Smpstart / minfo.FsHz),
- MainCode = minfo.SatId,
- AdjaCode = ninfo.SatId,
- SxDto = data1[i].Dt,
- SxDfo = data1[i].Df,
- SxSnr = data1[i].Snr,
- XdDto = data2[i].Dt,
- XdDfo = data2[i].Df,
- XdSnr = data2[i].Snr,
- SatTxLon = minfo.CapLon,
- SatTxLat = minfo.CapLat,
- CdbTxLon = minfo.CapLon,
- CdbTxLat = minfo.CapLat,
- FreqDown = minfo.FreqHz,
- FreqUp = dinfo.FreqHz,
- CheckRes = new CheckResDto()
- {
- FileName = Path.GetFileName(dinfo.FilePath),
- ModRate = deteResp.data[i].ModRate,
- ModType = deteResp.data[i].ModType,
- SmpCount = deteResp.data[i].Length,
- SmpStart = deteResp.data[i].Start,
- UserName = deteResp.data[i].UserName,
- PosCheckType = deteResp.data[i].DmcType.GetEnumByDisplayName<EnumPosCheckTypeDto>(),
- }
- };
- var result = await HttpHelper.PostRequestAsync<PosResDto>(baseUrl + "Pos/PosX2D1NoXlNoParAsync", x2D1);
- if (result.code != 200)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻时隙位置{data1[i].Smpstart}定位异常.{result.msg}");
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻时隙位置{data1[i].Smpstart}定位异常", ex);
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error($"【任务{dto.ID}】{capTime:yyyyMMddHHmmss}时刻文件处理异常", ex);
- continue;
- }
- }
- ResetTime(formatFlag, ref preTime);
- LogHelper.Info($"【任务{dto.ID}】目录[{filesDir}]中的数据处理完成");
- }
- catch (Exception ex)
- {
- LogHelper.Error($"【任务{dto.ID}】目录[{filesDir}]中的数据处理出错,跳过此目录", ex);
- ResetTime(formatFlag, ref preTime);
- }
- }
- LogHelper.Info($"【任务{dto.ID}】数据处理完成,任务结束");
- });
- }
- private void ResetTime(int formatFlag, ref DateTime time)
- {
- if (formatFlag == 0)
- {
- time = DateTime.MaxValue;
- }
- else if (formatFlag == 1)
- {
- time = time.AddHours(24);
- }
- else
- {
- time = time.AddHours(1);
- }
- }
- private HistoryFile FileToHistoryFile(X2D1HistoryTaskHandleDto dto, string filePath)
- { //读取采集文件
- //2024_01_31_10_01_51_000000000_ch11_-1__Nxx.xxxxxx_Exx.xxxxxx_xxxxx.xxxHz_xxx.xxxMHz_ch1_xd1.dat
- HistoryFile historyFile = new HistoryFile();
- historyFile.FilePath = filePath;
- var fileName = Path.GetFileNameWithoutExtension(filePath);
- var strs = fileName.Split(new string[] { "_", "MHz", "Hz", "ch" }, StringSplitOptions.RemoveEmptyEntries);
- //采集时间
- var datestr = string.Join("_", strs.Take(6));
- DateTime dateTime;
- bool istime = DateTime.TryParseExact(datestr, "yyyy_MM_dd_HH_mm_ss", null, DateTimeStyles.None, out dateTime);
- if (!istime) return historyFile;
- historyFile.CapTime = dateTime;
- //采集通道
- var chstr = strs.Skip(strs.Length - 2).Take(1).First();
- int ch;
- int.TryParse(chstr, out ch);
- //采集频点
- var freqstr = strs.Skip(strs.Length - 3).Take(1).First();
- double freqMHz;
- double.TryParse(freqstr, out freqMHz);
- //采样率
- var fsstr = strs.Skip(strs.Length - 4).Take(1).First();
- double fsHz;
- double.TryParse(fsstr, out fsHz);
- historyFile.FreqHz = freqMHz * 1e6;
- historyFile.Ch = ch;
- historyFile.FsHz = fsHz;
- if (ch == 2)
- {
- historyFile.SatId = dto.MainSatCode;
- }
- else if (ch == 3)
- {
- historyFile.SatId = dto.AdjaSatCode;
- }
- return historyFile;
- }
- public void Stop()
- {
- cts.Cancel();
- }
- }
- }
|