DetectCgController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using System.Web.Http;
  6. using XdCxRhDW.Api;
  7. using XdCxRhDW.Dto;
  8. using XdCxRhDW.WebApi;
  9. namespace XdCxRhDW.App.Controllers
  10. {
  11. /// <summary>
  12. /// 检测参估功能相关接口
  13. /// </summary>
  14. public class DetectCgController : BaseController
  15. {
  16. /// <summary>
  17. /// CPU参估计算(需要先上传文件)
  18. /// </summary>
  19. /// <param name="dto">CPU参估参数</param>
  20. /// <returns></returns>
  21. [HttpPost]
  22. public async Task<AjaxResult<CpuCgResDto>> CpuCgCalc(CpuCgDto dto)
  23. {
  24. dto.file1 = GetLocalFile(dto.file1);
  25. dto.file2 = GetLocalFile(dto.file2);
  26. XcorrStruct xItem = new XcorrStruct();
  27. xItem.file1 = dto.file1;
  28. xItem.file2 = dto.file2;
  29. xItem.samplingRate = dto.samplingRate;
  30. xItem.dtCenter = dto.dtCenter;
  31. xItem.dtRange = dto.dtRange;
  32. xItem.dfRange = dto.dfRange;
  33. FileInfo file1 = new FileInfo(dto.file1);
  34. FileInfo file2 = new FileInfo(dto.file2);
  35. long totalsamp = file1.Length < file2.Length ? file1.Length / 4 : file2.Length / 4;
  36. //样点数为0时计算所有样本
  37. if (dto.smpCount == 0)
  38. {
  39. xItem.smpCount = (int)totalsamp - dto.smpStart;
  40. }
  41. else if (dto.smpCount > 0 && dto.smpCount < 1)
  42. {
  43. xItem.smpCount = (int)(totalsamp * dto.smpCount);
  44. }
  45. else
  46. {
  47. xItem.smpCount = dto.smpCount;
  48. }
  49. xItem.smpStart = dto.smpStart;
  50. xItem.snrThreshold = dto.snrThreshold;
  51. CpuCgResDto resDto = new CpuCgResDto();
  52. try
  53. {
  54. XcorrUtils xcorr = new XcorrUtils();
  55. var result = await xcorr.Calc(xItem);
  56. //开始计算
  57. if (result.flag == -2)
  58. {
  59. Serilog.Log.Error("参估计算内部错误!");
  60. return Error<CpuCgResDto>("参估计算内部错误!");
  61. }
  62. else if (result.flag == -1)
  63. {
  64. Serilog.Log.Error("参估计算所需数据超出文件范围!");
  65. return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
  66. }
  67. resDto.TimeMs = result.tm;
  68. resDto.Smpstart = result.smpstart;
  69. resDto.Smplen = result.smplen;
  70. if (result.flag == 1)
  71. {
  72. resDto.Dt = result.dt.Value;
  73. resDto.Df = result.df.Value;
  74. resDto.Snr = result.snr.Value;
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. Serilog.Log.Error(ex, "执行CPU参估异常");
  80. return Error<CpuCgResDto>($"执行CPU参估异常");
  81. }
  82. finally
  83. {
  84. try
  85. {
  86. //删除计算得文件
  87. File.Delete(dto.file1);
  88. File.Delete(dto.file2);
  89. }
  90. catch
  91. {
  92. }
  93. }
  94. return Success(resDto);
  95. }
  96. /// <summary>
  97. /// CPU参估多样点位置计算(需要先上传文件)
  98. /// </summary>
  99. /// <param name="dto">CPU参估多样点位置参数</param>
  100. /// <returns></returns>
  101. [HttpPost]
  102. public async Task<AjaxResult<List<CpuCgResDto>>> CpuCgMultiCalc(CpuCgMultiDto dto)
  103. {
  104. dto.file1 = GetLocalFile(dto.file1);
  105. dto.file2 = GetLocalFile(dto.file2);
  106. XcorrStruct xItem = new XcorrStruct();
  107. xItem.file1 = dto.file1;
  108. xItem.file2 = dto.file2;
  109. xItem.samplingRate = dto.samplingRate;
  110. xItem.dtCenter = dto.dtCenter;
  111. xItem.dtRange = dto.dtRange;
  112. xItem.dfRange = dto.dfRange;
  113. xItem.snrThreshold = dto.snrThreshold;
  114. List<CpuCgResDto> resDtos = new List<CpuCgResDto>();
  115. foreach (var smpItem in dto.smpPositions)
  116. {
  117. try
  118. {
  119. XcorrUtils xcorr = new XcorrUtils();
  120. xItem.smpStart = smpItem.smpStart;
  121. xItem.smpCount = smpItem.smpCount;
  122. var result = await xcorr.Calc(xItem);
  123. //开始计算
  124. if (result.flag == -2)
  125. {
  126. Serilog.Log.Error("参估计算内部错误!");
  127. continue;
  128. }
  129. else if (result.flag == -1)
  130. {
  131. Serilog.Log.Error("参估计算所需数据超出文件范围!");
  132. continue;
  133. }
  134. CpuCgResDto resDto = new CpuCgResDto();
  135. resDto.TimeMs = result.tm;
  136. resDto.Smpstart = result.smpstart;
  137. resDto.Smplen = result.smplen;
  138. if (result.flag == 1)
  139. {
  140. resDto.Dt = result.dt.Value;
  141. resDto.Df = result.df.Value;
  142. resDto.Snr = result.snr.Value;
  143. resDtos.Add(resDto);
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Serilog.Log.Error(ex, "执行CPU参估异常");
  149. continue;
  150. }
  151. }
  152. try
  153. {
  154. //删除计算得文件
  155. File.Delete(dto.file1);
  156. File.Delete(dto.file2);
  157. }
  158. catch
  159. {
  160. }
  161. return Success(resDtos);
  162. }
  163. /// <summary>
  164. /// GPU参估计算(需要先上传文件)
  165. /// </summary>
  166. /// <param name="dto">GPU参估参数</param>
  167. /// <returns></returns>
  168. [HttpPost]
  169. public async Task<AjaxResult<List<GpuCgResponseDto>>> GpuCgCalc(GpuCgRequestDto dto)
  170. {
  171. dto.file1 = GetLocalFile(dto.file1);
  172. dto.file2 = GetLocalFile(dto.file2);
  173. FileInfo file1 = new FileInfo(dto.file1);
  174. FileInfo file2 = new FileInfo(dto.file2);
  175. long totalsamp = file1.Length < file2.Length ? file1.Length / 4 : file2.Length / 4;
  176. //样点数为0时计算所有样本
  177. if (dto.smpCount == 0)
  178. {
  179. dto.smpCount = totalsamp;
  180. }
  181. else if (dto.smpCount > 0 && dto.smpCount < 1)
  182. {
  183. dto.smpCount = (long)(totalsamp * dto.smpCount);
  184. }
  185. CpuCgResDto resDto = new CpuCgResDto();
  186. try
  187. {
  188. var result = await Task.Run(() =>
  189. {
  190. return GpuCgHelper.Calc(dto.file1, dto.file2, dto.samplingRate, dto.smpCount,
  191. dto.dtCenter, dto.dtRange, dto.dfRange, dto.snrThreshold, dto.TimeoutSeconds);
  192. });
  193. return Success(result);
  194. }
  195. catch (Exception ex)
  196. {
  197. Serilog.Log.Error(ex, "执行GPU参估异常");
  198. return Error<List<GpuCgResponseDto>>($"执行GPU参估异常");
  199. }
  200. finally
  201. {
  202. try
  203. {
  204. //删除计算得文件
  205. File.Delete(dto.file1);
  206. File.Delete(dto.file2);
  207. }
  208. catch
  209. {
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 信号检测(支持DAMA、IBS、能量检测)(需要先上传文件)
  215. /// </summary>
  216. /// <param name="dto">信号检测参数</param>
  217. /// <returns></returns>
  218. [HttpPost]
  219. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  220. {
  221. dto.file1 = GetLocalFile(dto.file1);
  222. List<DetectResDto> list = new List<DetectResDto>();
  223. try
  224. {
  225. List<DmcResult> dmcResults = new List<DmcResult>();
  226. XcorrUtils xcorr = new XcorrUtils();
  227. if (dto.dmcType.HasFlag(DmcType.DAMA))
  228. {
  229. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.DAMA, dto.band);
  230. dmcResults.AddRange(dmcResult);
  231. }
  232. if (dto.dmcType.HasFlag(DmcType.IBS))
  233. {
  234. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.IBS, dto.band);
  235. dmcResults.AddRange(dmcResult);
  236. }
  237. if (dto.dmcType.HasFlag(DmcType.Ky5758))
  238. {
  239. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, DmcType.Ky5758, dto.band);
  240. dmcResults.AddRange(dmcResult);
  241. }
  242. foreach (var dmcItem in dmcResults)
  243. {
  244. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  245. detectRes.ModType = dmcItem.ModType;
  246. detectRes.DmcType = dmcItem.DmcType;
  247. detectRes.File1 = dto.file1;
  248. detectRes.TimeMs = dmcItem.Times;
  249. list.Add(detectRes);
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  255. }
  256. finally
  257. {
  258. try
  259. {
  260. //删除检测的文件
  261. File.Delete(dto.file1);
  262. }
  263. catch
  264. {
  265. }
  266. }
  267. return Success<IEnumerable<DetectResDto>>(list);
  268. }
  269. /// <summary>
  270. /// 文件变采样(需要先上传文件)
  271. /// </summary>
  272. /// <param name="dto"></param>
  273. /// <returns></returns>
  274. public async Task<AjaxResult<ResampleResponseDto>> Resample(ResampleRequestDto dto)
  275. {
  276. var res = await Task.Run(() =>
  277. {
  278. string fileIn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", dto.File);
  279. string fileOut = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", Guid.NewGuid() + ".dat");
  280. var val = Gcd(dto.FsHz, dto.OutFsHz);//最大公约数
  281. var insertFactor = dto.OutFsHz / val;
  282. var extFactor = dto.FsHz / val;
  283. var outFile = ReSampleHelper.Resample(fileIn, fileOut, insertFactor, extFactor, dto.TimeoutSeconds);
  284. if (!string.IsNullOrWhiteSpace(outFile))
  285. {
  286. if (!File.Exists(outFile))
  287. return Error<ResampleResponseDto>("变采样后的文件未能生成");
  288. else
  289. return Success(new ResampleResponseDto() { File = Path.GetFileName(outFile), OutFsHz = dto.OutFsHz });
  290. }
  291. else
  292. {
  293. return Error<ResampleResponseDto>("变采样算法调用失败");
  294. }
  295. });
  296. return res;
  297. }
  298. //求最大公约数
  299. private int Gcd(int M, int N)
  300. {
  301. int Rem;
  302. while (N > 0)
  303. {
  304. Rem = M % N;
  305. M = N;
  306. N = Rem;
  307. }
  308. return M;
  309. }
  310. }
  311. }