using DevExpress.Utils.Extensions;
using DevExpress.XtraPrinting.Native.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
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;
public virtual void Start(HistoryTaskProcessingDto dto)
{
}
///
/// 根据下行频点获取卫星Id
///
///
///
public int GetSatId(double freqdown)
{
// (洋区固定大写字母aì,不是数字1,查不到结果给出日志方便排查
string sql = $"select 卫星ID from freguencysatid where 下行 = '{freqdown}'and 洋区 = 'I' LIMIT 1";
sql = "SELECT id FROM role where id=1 LIMIT 1";
var res = MySqlTools.ExecuteScalar(System.Data.CommandType.Text, sql);
int satId = 0;
bool isInt = int.TryParse($"{res}", out satId);
if (!isInt)
{
LogHelper.Error($"下行频点{freqdown}未找到卫星编号");
}
return satId;
}
public virtual void Stop()
{
IsRuning = true;
}
//检测
public async Task> DAMAAsync(DmcTypeDto 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);
#warning 信号带宽怎么获取
//dto.band = double.Parse(txtBand.Text);
var dmcResult = await HttpHelper.PostRequestAsync>(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 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(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 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 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 CPUCalcAsync(string file1, string file2, double fsHz, DetectResDto detect)
{
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 = 0;
dto.dtRange = 40000;
dto.smpStart = detect.Start;
dto.snrThreshold = 14;
try
{
var result = await HttpHelper.PostRequestAsync(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}");
}
}
//GPU计算
public async Task GPUCalcAsync(string file1, string file2, double fsHz)
{
string step = "GPU计算";
GpuCgRequestDto dto = new GpuCgRequestDto();
dto.file1 = await UploadFileAsync(step, file1);
dto.file2 = await UploadFileAsync(step, file2);
dto.smpCount = 0;
dto.samplingRate = fsHz;
dto.dtCenter = 0;
dto.dtRange = 10000;
dto.snrThreshold = 14;
try
{
var result = await HttpHelper.PostRequestAsync>(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}");
}
}
}
}