using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using XdCxRhDw.Dto;
using XdCxRhDW.Core.Api;
using XdCxRhDW.WebApi;
using System.ComponentModel.DataAnnotations;
namespace XdCxRhDW.WebApi.Controllers
{
///
/// 检测参估功能相关接口
///
public class DetectCgController : BaseController
{
private readonly string uploadFolder;
///
///
///
public DetectCgController()
{
this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
}
///
/// CPU参估计算
///
/// CPU参估参数
///
[HttpPost]
[CustomValidation(typeof(CpuCgDto), "Validate")]
public async Task> CpuCgCalc(CpuCgDto dto)
{
dto.file1 = Path.Combine(uploadFolder, dto.file1);
dto.file2 = Path.Combine(uploadFolder, dto.file2);
var vpres = ValidateCalcParam(dto);
if (!vpres.Item1)
{
Serilog.Log.Warning(vpres.Item2);
return Error(vpres.Item2);
}
XcorrStruct xItem = new XcorrStruct();
xItem.file1 = dto.file1;
xItem.file2 = dto.file2;
xItem.samplingRate = dto.samplingRate;
xItem.dtCenter = dto.dtCenter;
xItem.dtRange = dto.dtRange;
xItem.dfRange = dto.dfRange;
//样点数为0时计算所有样本
if (dto.smpCount == 0)
{
FileInfo file = new FileInfo(dto.file1);
long totalsamp = file.Length / 4;
xItem.smpCount = (int)totalsamp - dto.smpStart;
}
else
{
xItem.smpCount = dto.smpCount;
}
xItem.smpStart = dto.smpStart;
xItem.snrThreshold = dto.snrThreshold;
CpuCgResDto resDto = new CpuCgResDto();
try
{
XcorrUtils xcorr = new XcorrUtils();
var result = await xcorr.Calc(xItem);
//开始计算
if (result.flag == -2)
{
Serilog.Log.Warning("参估计算内部错误!");
return Error("参估计算内部错误!");
}
else if (result.flag == -1)
{
Serilog.Log.Warning("参估计算所需数据超出文件范围!");
return Error("参估计算所需数据超出文件范围!");
}
resDto.TimeMs = result.tm;
resDto.Smpstart = result.smpstart;
resDto.Smplen = result.smplen;
resDto.File1 = result.file1;
resDto.File2 = result.file2;
if (result.flag == 1)
{
resDto.Dt = result.dt.Value;
resDto.Df = result.df.Value;
resDto.Snr = result.snr.Value;
}
}
catch (Exception ex)
{
return Error($"执行参估计算异常,{ex.Message}");
}
finally
{
try
{
//删除计算得文件
File.Delete(dto.file1);
File.Delete(dto.file2);
}
catch
{
}
}
return Success(resDto);
}
///
/// 信号检测(支持DAMA、IBS、能量检测)
///
/// 信号检测参数
///
[HttpPost]
public async Task>> DetectCalc(DetectDto dto)
{
var token = Request.GetCorrelationId().ToString();
dto.file1 = Path.Combine(uploadFolder, dto.file1);
var vpres = ValidateDetectParam(dto);
if (!vpres.Item1)
{
Serilog.Log.Warning(vpres.Item2);
return Error>(vpres.Item2);
}
List list = new List();
try
{
XcorrUtils xcorr = new XcorrUtils();
var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, dto.dmcType);
foreach (var dmcItem in dmcResult)
{
DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
detectRes.File1 = dto.file1;
detectRes.TimeMs = dmcItem.Times;
detectRes.Token = token;
list.Add(detectRes);
}
}
catch (Exception ex)
{
return Error>($"执行检测计算异常,{ex.Message}");
}
finally
{
try
{
//删除检测的文件
File.Delete(dto.file1);
}
catch
{
}
}
return Success>(list);
}
private (bool, string) ValidateCalcParam(CpuCgDto dto)
{
if (dto == null)
{
return (false, "参估计算参数格式错误!");
}
if (string.IsNullOrEmpty(dto.file1))
{
return (false, "参估计算参数数据文件[file1]不能为空!");
}
if (string.IsNullOrEmpty(dto.file2))
{
return (false, "参估计算参数数据文件[file2]不能为空!");
}
if (!File.Exists(dto.file1))
{
return (false, $"参估计算参数数据文件[{Path.GetFileName(dto.file1)}]不存在!");
}
if (!File.Exists(dto.file2))
{
return (false, $"参估计算参数数据文件[{Path.GetFileName(dto.file2)}]不存在!");
}
if (dto.dtRange <= 0)
{
return (false, "参估计算参数时差范围[dtRange]不能小于等于0!");
}
if (dto.smpStart < 0)
{
return (false, "参估计算参数开始样点[smpStart]不能小于0!");
}
if (dto.smpCount < 0)
{
return (false, "参估计算参数样点数[smpCount]不能小于0!");
}
return (true, string.Empty);
}
private (bool, string) ValidateDetectParam(DetectDto dto)
{
if (dto == null)
{
return (false, "检测计算参数格式错误!");
}
if (string.IsNullOrEmpty(dto.file1))
{
return (false, "检测计算参数数据文件[file1]不能为空!");
}
if (!File.Exists(dto.file1))
{
return (false, $"检测计算参数数据文件[{dto.file1}]不存在!");
}
bool containsValue = Enum.IsDefined(typeof(DmcType), dto.dmcType);
if (!containsValue)
{
return (false, $"检测计算参数[dmcType]检测类型值{dto.dmcType}不存在!");
}
return (true, string.Empty);
}
}
}