DetectCgController.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. using Newtonsoft.Json;
  15. namespace XdCxRhDW.WebApi.Controllers
  16. {
  17. /// <summary>
  18. /// 检测参估功能相关接口
  19. /// </summary>
  20. public class DetectCgController : BaseController
  21. {
  22. /// <summary>
  23. /// CPU参估计算(需要先上传文件)
  24. /// </summary>
  25. /// <param name="dto">CPU参估参数</param>
  26. /// <returns></returns>
  27. [HttpPost]
  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.Error("参估计算内部错误!");
  61. return Error<CpuCgResDto>("参估计算内部错误!");
  62. }
  63. else if (result.flag == -1)
  64. {
  65. Serilog.Log.Error("参估计算所需数据超出文件范围!");
  66. return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
  67. }
  68. resDto.TimeMs = result.tm;
  69. resDto.Smpstart = result.smpstart;
  70. resDto.Smplen = result.smplen;
  71. if (result.flag == 1)
  72. {
  73. resDto.Dt = result.dt.Value;
  74. resDto.Df = result.df.Value;
  75. resDto.Snr = result.snr.Value;
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. Serilog.Log.Error(ex, "执行CPU参估异常");
  81. return Error<CpuCgResDto>($"执行CPU参估异常");
  82. }
  83. finally
  84. {
  85. try
  86. {
  87. //删除计算得文件
  88. File.Delete(dto.file1);
  89. File.Delete(dto.file2);
  90. }
  91. catch
  92. {
  93. }
  94. }
  95. return Success(resDto);
  96. }
  97. /// <summary>
  98. /// GPU参估计算(需要先上传文件)
  99. /// </summary>
  100. /// <param name="dto">GPU参估参数</param>
  101. /// <returns></returns>
  102. [HttpPost]
  103. public async Task<AjaxResult<List<GpuCgResponseDto>>> GpuCgCalc(GpuCgRequestDto dto)
  104. {
  105. dto.file1 = GetLocalFile(dto.file1);
  106. dto.file2 = GetLocalFile(dto.file2);
  107. CpuCgResDto resDto = new CpuCgResDto();
  108. try
  109. {
  110. var result = await Task.Run(() =>
  111. {
  112. return GpuCgHelper.Calc(dto.file1, dto.file2, dto.samplingRate, dto.smpCount,
  113. dto.dtCenter, dto.dtRange, dto.dfRange, dto.snrThreshold, dto.TimeoutSeconds);
  114. });
  115. return Success(result);
  116. }
  117. catch (Exception ex)
  118. {
  119. Serilog.Log.Error(ex, "执行GPU参估异常");
  120. return Error<List<GpuCgResponseDto>>($"执行GPU参估异常");
  121. }
  122. finally
  123. {
  124. try
  125. {
  126. //删除计算得文件
  127. File.Delete(dto.file1);
  128. File.Delete(dto.file2);
  129. }
  130. catch
  131. {
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 信号检测(支持DAMA、IBS、能量检测)(需要先上传文件)
  137. /// </summary>
  138. /// <param name="dto">信号检测参数</param>
  139. /// <returns></returns>
  140. [HttpPost]
  141. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  142. {
  143. dto.file1 = GetLocalFile(dto.file1);
  144. List<DetectResDto> list = new List<DetectResDto>();
  145. try
  146. {
  147. XcorrUtils xcorr = new XcorrUtils();
  148. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, dto.dmcType,dto.band);
  149. foreach (var dmcItem in dmcResult)
  150. {
  151. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  152. detectRes.ModType = dmcItem.ModType;
  153. detectRes.File1 = dto.file1;
  154. detectRes.TimeMs = dmcItem.Times;
  155. list.Add(detectRes);
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  161. }
  162. finally
  163. {
  164. try
  165. {
  166. //删除检测的文件
  167. File.Delete(dto.file1);
  168. }
  169. catch
  170. {
  171. }
  172. }
  173. return Success<IEnumerable<DetectResDto>>(list);
  174. }
  175. /// <summary>
  176. /// 文件变采样(需要先上传文件)
  177. /// </summary>
  178. /// <param name="dto"></param>
  179. /// <returns></returns>
  180. public async Task<AjaxResult<ResampleResponseDto>> Resample(ResampleRequestDto dto)
  181. {
  182. var res = await Task.Run(() =>
  183. {
  184. string fileIn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", dto.File);
  185. string fileOut = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", Guid.NewGuid() + ".dat");
  186. var val = Gcd(dto.FsHz, dto.OutFsHz);//最大公约数
  187. var insertFactor = dto.OutFsHz / val;
  188. var extFactor = dto.FsHz / val;
  189. var outFile = ReSampleHelper.Resample(fileIn, fileOut, insertFactor, extFactor,dto.TimeoutSeconds);
  190. if (!string.IsNullOrWhiteSpace(outFile))
  191. {
  192. if (!File.Exists(outFile))
  193. return Error<ResampleResponseDto>("变采样后的文件未能生成");
  194. else
  195. return Success(new ResampleResponseDto() { File = Path.GetFileName(outFile), OutFsHz = dto.OutFsHz });
  196. }
  197. else
  198. {
  199. return Error<ResampleResponseDto>("变采样算法调用失败");
  200. }
  201. });
  202. return res;
  203. }
  204. //求最大公约数
  205. private int Gcd(int M, int N)
  206. {
  207. int Rem;
  208. while (N > 0)
  209. {
  210. Rem = M % N;
  211. M = N;
  212. N = Rem;
  213. }
  214. return M;
  215. }
  216. }
  217. }