123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using DevExpress.Utils.Extensions;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using XdCxRhDW;
- using XdCxRhDW.Dto;
- namespace XdCxRhDW.TaskServer.Task
- {
- public class HistoryTaskI
- {
- protected internal virtual string baseUrl => $"{ConfigurationManager.AppSettings["PosPlatformAddr"].Trim()}/api/";
- protected internal virtual bool IsRuning { get; set; } = false;
- //变采样
- protected internal virtual int OutFsHz { get; set; } = 96000;
- protected internal virtual HistoryTaskProcessingDto TaskDto { get; set; }
- public virtual void Start(HistoryTaskProcessingDto dto)
- {
- }
- /// <summary>
- /// 根据下行频点获取卫星Id
- /// </summary>
- /// <param name="freqdown"></param>
- /// <returns></returns>
- public int GetSatId(double freqdown)
- {
- int satId = 0;
- try
- {
- // (洋区固定大写字母aì,不是数字1,查不到结果给出日志方便排查
- string sql = $"select 卫星ID from freguencysatid where 下行 = '{freqdown}'and 洋区 = 'I' LIMIT 1";
- var res = MySqlTools.ExecuteScalar(System.Data.CommandType.Text, sql);
- bool isInt = int.TryParse($"{res}", out satId);
- if (!isInt)
- {
- LogHelper.Error($"下行频点{freqdown * 1e-6}未找到卫星编号");
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error($"下行频点{freqdown}找卫星编号异常:{ex.Message}");
- }
- return satId;
- }
- public virtual void Stop()
- {
- IsRuning = false;
- }
- //检测
- public async Task<IEnumerable<DetectResDto>> DAMAAsync(EnumTaskCheckTypeDto dmc, double fsHz, string mFile)
- {
- try
- {
- //主星变采样
- var resampleRes = await ToResampleAsync((int)fsHz, mFile);
- DetectDto dto = new DetectDto();
- dto.dmcType = (DmcType)dmc;
- dto.fsHz = resampleRes.OutFsHz;
- dto.file1 = await UploadFileAsync("DAMA检测", resampleRes.File);
- var dmcResult = await HttpHelper.PostRequestAsync<IEnumerable<DetectResDto>>(baseUrl + "DetectCg/DetectCalc", dto);
- if (dmcResult.code != 200)
- {
- throw new Exception($"执行DAMA检测异常:{dmcResult.msg}");
- }
- else
- {
- dmcResult.data.ForEach(m => m.File1 = resampleRes.File);
- return dmcResult.data;
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"信号检测出错:{ex.Message}");
- }
- }
- public async Task<ResampleResponseDto> ToResampleAsync(int fsHz, string file)
- {
- ResampleResponseDto dtores = new ResampleResponseDto();
- dtores.File = file;
- dtores.OutFsHz = fsHz;
- string step = "变采样";
- //不需要变采样
- if (fsHz == OutFsHz)
- {
- return dtores;
- }
- string file1 = await UploadFileAsync(step, file);
- ResampleRequestDto dto = new ResampleRequestDto()
- {
- File = file1,
- FsHz = fsHz,
- };
- var response = await HttpHelper.PostRequestAsync<ResampleResponseDto>(baseUrl + "/DetectCg/Resample", dto, dto.TimeoutSeconds);
- if (response.code == 200)
- {
- string downloadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
- string outFile = Path.Combine(downloadFolder, Path.GetFileNameWithoutExtension(file1) + $"_Resample{response.data.OutFsHz}K.dat");
- await DownloadFileAsync(step, response.data.File, outFile);
- dtores.OutFsHz = response.data.OutFsHz;
- dtores.File = outFile;
- return dtores;
- }
- else
- {
- throw new Exception($"采样率:{fsHz}变采样{file}异常:{response.msg}");
- }
- }
- public async Task<string> UploadFileAsync(string step, string file)
- {
- try
- {
- string file1 = await HttpHelper.UploadFileAsync(file, baseUrl + "File/UploadFileAsync");
- return file1;
- }
- catch (Exception ex)
- {
- throw new Exception($"执行{step}上传文件异常:{ex.Message}");
- }
- }
- public async Task<bool> DownloadFileAsync(string step, string Infile, string outFile)
- {
- try
- {
- return await HttpHelper.DownloadFileAsync(baseUrl, Infile, outFile);
- }
- catch (Exception ex)
- {
- throw new Exception($"执行{step}下载文件{Infile}异常:{ex.Message}");
- }
- }
- //CPU计算
- public async Task<CpuCgResDto> CPUCalcAsync(string file1, string file2, double fsHz, DetectResDto detect, double dtCenter, double dtRange)
- {
- string step = "CPU计算";
- CpuCgDto dto = new CpuCgDto();
- dto.file1 = await UploadFileAsync(step, file1);
- dto.file2 = await UploadFileAsync(step, file2);
- dto.smpCount = detect.Length;
- dto.samplingRate = fsHz;
- dto.dtCenter = dtCenter;
- dto.dtRange = dtRange;
- dto.smpStart = detect.Start;
- dto.snrThreshold = 14;
- try
- {
- var result = await HttpHelper.PostRequestAsync<CpuCgResDto>(baseUrl + "DetectCg/CpuCgCalc", dto);
- if (result.code != 200)
- {
- throw new Exception($"CPU文件参估出错,{result.msg}");
- }
- return result.data;
- }
- catch (Exception ex)
- {
- throw new Exception($"CPU文件参估出错,{ex.Message}");
- }
- }
- //CPU多检测计算
- public async Task<List<CpuCgResDto>> CPUCalcAsync(string file1, string file2, double fsHz, List<SmpPosition> smps, double dtCenter, double dtRange)
- {
- string step = "CPU计算";
- CpuCgMultiDto dto = new CpuCgMultiDto();
- dto.file1 = await UploadFileAsync(step, file1);
- dto.file2 = await UploadFileAsync(step, file2);
- dto.smpPositions = smps;
- dto.samplingRate = fsHz;
- dto.dtCenter = dtCenter;
- dto.dtRange = dtRange;
- dto.snrThreshold = 14;
- try
- {
- var result = await HttpHelper.PostRequestAsync<List<CpuCgResDto>>(baseUrl + "DetectCg/CpuCgMultiCalc", dto);
- if (result.code != 200)
- {
- throw new Exception($"CPU文件参估出错,{result.msg}");
- }
- return result.data;
- }
- catch (Exception ex)
- {
- throw new Exception($"CPU文件参估出错,{ex.Message}");
- }
- }
- //GPU计算
- public async Task<GpuCgResponseDto> GPUCalcAsync(string file1, string file2, double fsHz, double dtCenter, double dtRange)
- {
- string step = "GPU计算";
- GpuCgRequestDto dto = new GpuCgRequestDto();
- dto.file1 = await UploadFileAsync(step, file1);
- dto.file2 = await UploadFileAsync(step, file2);
- dto.samplingRate = fsHz;
- dto.dtCenter = dtCenter;
- dto.dtRange = dtRange;
- dto.snrThreshold = 14;
- try
- {
- var result = await HttpHelper.PostRequestAsync<List<GpuCgResponseDto>>(baseUrl + "DetectCg/GpuCgCalc", dto, dto.TimeoutSeconds);
- if (result.code != 200)
- {
- throw new Exception($"GPU文件参估出错,{result.msg}");
- }
- return result.data.Count > 0 ? result.data.First() : new GpuCgResponseDto();
- }
- catch (TaskCanceledException)
- {
- throw new Exception("GPU文件参估Http接口调用超时");
- }
- catch (Exception ex)
- {
- throw new Exception($"GPU文件参估出错{ex.Message}");
- }
- }
- /// <summary>
- /// 提取检测时隙数据
- /// </summary>
- /// <param name="file"></param>
- /// <param name="fsHz"></param>
- /// <param name="smps"></param>
- /// <param name="dtCenter"></param>
- /// <param name="dtRange"></param>
- /// <param name="Ch"></param>
- /// <returns></returns>
- public async Task<ExtractRes> ExtractMergeAsync(string file, double fsHz, List<SmpPosition> smps, double dtCenter, double dtRange, int Ch)
- {
- long offset = 0;
- long zero = 0;
- if (Ch == 2)//主星
- {
- zero = 0;
- }
- else if (Ch != 2 && dtCenter == 0)//三星信号文件
- {
- zero = Convert.ToInt64(dtRange * 1e-6 * fsHz) / 2;
- }
- else//地信号文件
- {
- zero = Convert.ToInt64(dtRange * 1e-6 * fsHz) / 2;
- offset = Convert.ToInt64(0.26 * fsHz);
- }
- string detectFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MultiDetect");
- Directory.CreateDirectory(detectFolder);
- string outfile = Path.Combine(detectFolder, Path.GetFileNameWithoutExtension(file) + "_de" + Path.GetExtension(file));
- ExtractRes res = new ExtractRes();
- res.file = outfile;
- List<SmpPosition> smpp = new List<SmpPosition>();
- // 从指定位置开始读取数据
- using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
- {
- byte[] bytes = new byte[fileStream.Length];
- foreach (var smp in smps)
- {
-
- long start = (smp.smpStart - zero - offset) * 4;
- int length = Convert.ToInt32(smp.smpCount + zero * 2 - offset) * 4;
- if (start < 0 || length < 0 || length > fileStream.Length)
- {
- continue;
- }
- // 移动到文件的指定位置
- fileStream.Seek(start, SeekOrigin.Begin);
- byte[] buffer = new byte[length];
- // 读取指定长度的数据
- int bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length);
- Array.ConstrainedCopy(buffer, 0, bytes, (int)start, bytesRead);
- smpp.Add(smp);
-
- }
- using (FileStream wrStream = new FileStream(outfile, FileMode.Create, FileAccess.ReadWrite))
- {
- await wrStream.WriteAsync(bytes, 0, bytes.Length);
- }
- }
- res.positions = smpp;
- return res;
- }
- }
- }
|