123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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
- {
- /// <summary>
- /// 检测参估功能相关接口
- /// </summary>
- public class DetectCgController : BaseController
- {
- private readonly string uploadFolder;
- /// <summary>
- ///
- /// </summary>
- public DetectCgController()
- {
- this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
- }
- /// <summary>
- /// CPU参估计算
- /// </summary>
- /// <param name="dto">CPU参估参数</param>
- /// <returns></returns>
- [HttpPost]
- [CustomValidation(typeof(CpuCgDto), "Validate")]
- public async Task<AjaxResult<CpuCgResDto>> 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<CpuCgResDto>(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<CpuCgResDto>("参估计算内部错误!");
- }
- else if (result.flag == -1)
- {
- Serilog.Log.Warning("参估计算所需数据超出文件范围!");
- return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
- }
- 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<CpuCgResDto>($"执行参估计算异常,{ex.Message}");
- }
- finally
- {
- try
- {
- //删除计算得文件
- File.Delete(dto.file1);
- File.Delete(dto.file2);
- }
- catch
- {
- }
- }
- return Success(resDto);
- }
- /// <summary>
- /// 信号检测(支持DAMA、IBS、能量检测)
- /// </summary>
- /// <param name="dto">信号检测参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<IEnumerable<DetectResDto>>> 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<IEnumerable<DetectResDto>>(vpres.Item2);
- }
- List<DetectResDto> list = new List<DetectResDto>();
- 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<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
- }
- finally
- {
- try
- {
- //删除检测的文件
- File.Delete(dto.file1);
- }
- catch
- {
- }
- }
- return Success<IEnumerable<DetectResDto>>(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);
- }
- }
- }
|