123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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 XdCxRhDW.WebApi.Service;
- namespace XdCxRhDW.WebApi.Controllers
- {
- /// <summary>
- /// 检测参估功能相关接口
- /// </summary>
- public class DetectCgController : BaseController
- {
- private readonly string uploadFolder;
- private readonly DetectService service;
- /// <summary>
- ///
- /// </summary>
- /// <param name="service"></param>
- public DetectCgController(DetectService service)
- {
- this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
- this.service = service;
- }
- private static readonly object thisLock = new object();
- Dictionary<string, XcorrUtils> keyValues = new Dictionary<string, XcorrUtils>();
- private (bool, string) ValidateCalcParam(CalcDto 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, $"参估计算参数数据文件[{dto.file1}]不存在!");
- }
- if (!File.Exists(dto.file2))
- {
- return (false, $"参估计算参数数据文件[{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);
- }
- /// <summary>
- /// CPU参估计算
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<EstimationResDto>> EstimationCalc(CalcDto dto)
- {
- var token = Request.GetCorrelationId().ToString();
- 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<EstimationResDto>(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;
- EstimationResDto resDto = new EstimationResDto();
- try
- {
- XcorrUtils xcorr = new XcorrUtils();
- lock (thisLock)
- {
- keyValues.Add(token, xcorr);
- }
- var result = await xcorr.Calc(xItem);
- //开始计算
- if (result.flag == -2)
- {
- Serilog.Log.Warning("参估计算内部错误!");
- return Error<EstimationResDto>("参估计算内部错误!");
- }
- else if (result.flag == -1)
- {
- Serilog.Log.Warning("参估计算所需数据超出文件范围!");
- return Error<EstimationResDto>("参估计算所需数据超出文件范围!");
- }
- resDto.Token = token;
- 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<EstimationResDto>($"执行参估计算异常,{ex.Message}");
- }
- finally
- {
- lock (thisLock)
- {
- keyValues.Remove(token);
- }
- try
- {
- //删除计算得文件
- File.Delete(dto.file1);
- File.Delete(dto.file2);
- }
- catch
- {
- }
- }
- return Success(resDto);
- }
- /// <summary>
- /// 参估计算停止
- /// </summary>
- /// <param name="token"></param>
- /// <returns></returns>
- //[HttpPost]
- //public async Task<AjaxResult<bool>> StopEstimationCalc(string token)
- //{
- // return await Task.Run(() =>
- // {
- // lock (thisLock)
- // {
- // if (keyValues.ContainsKey(token))
- // {
- // keyValues[token].StopCalc();
- // return Success(true);
- // }
- // else
- // {
- // return Success(false);
- // }
- // }
- // });
- //}
- 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);
- }
- /// <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();
- lock (thisLock)
- {
- keyValues.Add(token, xcorr);
- }
- 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
- {
- lock (thisLock)
- {
- //keyValues.Remove(token);
- }
- try
- {
- //删除检测的文件
- File.Delete(dto.file1);
- }
- catch
- {
- }
- }
- return Success<IEnumerable<DetectResDto>>(list);
- }
- /// <summary>
- /// 信号检测停止
- /// </summary>
- /// <param name="token"></param>
- /// <returns></returns>
- //[HttpPost]
- //public async Task<AjaxResult<bool>> StopDetectCalc(string token)
- //{
- // return await Task.Run(() =>
- // {
- // lock (thisLock)
- // {
- // if (keyValues.ContainsKey(token))
- // {
- // keyValues[token].StopDm();
- // return Success(true);
- // }
- // else
- // {
- // return Success(false);
- // }
- // }
- // });
- //}
- }
- }
|