DetectCgController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web.Http;
  10. using XdCxRhDw.Dto;
  11. using XdCxRhDW.Core.Api;
  12. using XdCxRhDW.WebApi;
  13. using System.ComponentModel.DataAnnotations;
  14. namespace XdCxRhDW.WebApi.Controllers
  15. {
  16. /// <summary>
  17. /// 检测参估功能相关接口
  18. /// </summary>
  19. public class DetectCgController : BaseController
  20. {
  21. /// <summary>
  22. /// CPU参估计算
  23. /// </summary>
  24. /// <param name="dto">CPU参估参数</param>
  25. /// <returns></returns>
  26. [HttpPost]
  27. [CustomValidation(typeof(CpuCgDto), "Validate")]
  28. public async Task<AjaxResult<CpuCgResDto>> CpuCgCalc(CpuCgDto dto)
  29. {
  30. dto.file1 = GetLocalFile(dto.file1);
  31. dto.file2 = GetLocalFile(dto.file2);
  32. XcorrStruct xItem = new XcorrStruct();
  33. xItem.file1 = dto.file1;
  34. xItem.file2 = dto.file2;
  35. xItem.samplingRate = dto.samplingRate;
  36. xItem.dtCenter = dto.dtCenter;
  37. xItem.dtRange = dto.dtRange;
  38. xItem.dfRange = dto.dfRange;
  39. //样点数为0时计算所有样本
  40. if (dto.smpCount == 0)
  41. {
  42. FileInfo file = new FileInfo(dto.file1);
  43. long totalsamp = file.Length / 4;
  44. xItem.smpCount = (int)totalsamp - dto.smpStart;
  45. }
  46. else
  47. {
  48. xItem.smpCount = dto.smpCount;
  49. }
  50. xItem.smpStart = dto.smpStart;
  51. xItem.snrThreshold = dto.snrThreshold;
  52. CpuCgResDto resDto = new CpuCgResDto();
  53. try
  54. {
  55. XcorrUtils xcorr = new XcorrUtils();
  56. var result = await xcorr.Calc(xItem);
  57. //开始计算
  58. if (result.flag == -2)
  59. {
  60. Serilog.Log.Warning("参估计算内部错误!");
  61. return Error<CpuCgResDto>("参估计算内部错误!");
  62. }
  63. else if (result.flag == -1)
  64. {
  65. Serilog.Log.Warning("参估计算所需数据超出文件范围!");
  66. return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
  67. }
  68. resDto.TimeMs = result.tm;
  69. resDto.Smpstart = result.smpstart;
  70. resDto.Smplen = result.smplen;
  71. resDto.File1 = result.file1;
  72. resDto.File2 = result.file2;
  73. if (result.flag == 1)
  74. {
  75. resDto.Dt = result.dt.Value;
  76. resDto.Df = result.df.Value;
  77. resDto.Snr = result.snr.Value;
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. return Error<CpuCgResDto>($"执行参估计算异常,{ex.Message}");
  83. }
  84. finally
  85. {
  86. try
  87. {
  88. //删除计算得文件
  89. File.Delete(dto.file1);
  90. File.Delete(dto.file2);
  91. }
  92. catch
  93. {
  94. }
  95. }
  96. return Success(resDto);
  97. }
  98. /// <summary>
  99. /// 信号检测(支持DAMA、IBS、能量检测)
  100. /// </summary>
  101. /// <param name="dto">信号检测参数</param>
  102. /// <returns></returns>
  103. [HttpPost]
  104. [CustomValidation(typeof(DetectDto), "Validate")]
  105. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  106. {
  107. dto.file1 = GetLocalFile(dto.file1);
  108. List<DetectResDto> list = new List<DetectResDto>();
  109. try
  110. {
  111. XcorrUtils xcorr = new XcorrUtils();
  112. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, dto.dmcType);
  113. foreach (var dmcItem in dmcResult)
  114. {
  115. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  116. detectRes.File1 = dto.file1;
  117. detectRes.TimeMs = dmcItem.Times;
  118. list.Add(detectRes);
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  124. }
  125. finally
  126. {
  127. try
  128. {
  129. //删除检测的文件
  130. File.Delete(dto.file1);
  131. }
  132. catch
  133. {
  134. }
  135. }
  136. return Success<IEnumerable<DetectResDto>>(list);
  137. }
  138. }
  139. }