DetectCgController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. namespace XdCxRhDW.WebApi.Controllers
  14. {
  15. /// <summary>
  16. /// 检测参估功能相关接口
  17. /// </summary>
  18. public class DetectCgController : BaseController
  19. {
  20. private readonly string uploadFolder;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public DetectCgController()
  25. {
  26. this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  27. }
  28. /// <summary>
  29. /// CPU参估计算
  30. /// </summary>
  31. /// <param name="dto">CPU参估参数</param>
  32. /// <returns></returns>
  33. [HttpPost]
  34. public async Task<AjaxResult<CpuCgResDto>> CpuCgCalc(CpuCgDto dto)
  35. {
  36. dto.file1 = Path.Combine(uploadFolder, dto.file1);
  37. dto.file2 = Path.Combine(uploadFolder, dto.file2);
  38. var vpres = ValidateCalcParam(dto);
  39. if (!vpres.Item1)
  40. {
  41. Serilog.Log.Warning(vpres.Item2);
  42. return Error<CpuCgResDto>(vpres.Item2);
  43. }
  44. XcorrStruct xItem = new XcorrStruct();
  45. xItem.file1 = dto.file1;
  46. xItem.file2 = dto.file2;
  47. xItem.samplingRate = dto.samplingRate;
  48. xItem.dtCenter = dto.dtCenter;
  49. xItem.dtRange = dto.dtRange;
  50. xItem.dfRange = dto.dfRange;
  51. //样点数为0时计算所有样本
  52. if (dto.smpCount == 0)
  53. {
  54. FileInfo file = new FileInfo(dto.file1);
  55. long totalsamp = file.Length / 4;
  56. xItem.smpCount = (int)totalsamp - dto.smpStart;
  57. }
  58. else
  59. {
  60. xItem.smpCount = dto.smpCount;
  61. }
  62. xItem.smpStart = dto.smpStart;
  63. xItem.snrThreshold = dto.snrThreshold;
  64. CpuCgResDto resDto = new CpuCgResDto();
  65. try
  66. {
  67. XcorrUtils xcorr = new XcorrUtils();
  68. var result = await xcorr.Calc(xItem);
  69. //开始计算
  70. if (result.flag == -2)
  71. {
  72. Serilog.Log.Warning("参估计算内部错误!");
  73. return Error<CpuCgResDto>("参估计算内部错误!");
  74. }
  75. else if (result.flag == -1)
  76. {
  77. Serilog.Log.Warning("参估计算所需数据超出文件范围!");
  78. return Error<CpuCgResDto>("参估计算所需数据超出文件范围!");
  79. }
  80. resDto.TimeMs = result.tm;
  81. resDto.Smpstart = result.smpstart;
  82. resDto.Smplen = result.smplen;
  83. resDto.File1 = result.file1;
  84. resDto.File2 = result.file2;
  85. if (result.flag == 1)
  86. {
  87. resDto.Dt = result.dt.Value;
  88. resDto.Df = result.df.Value;
  89. resDto.Snr = result.snr.Value;
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. return Error<CpuCgResDto>($"执行参估计算异常,{ex.Message}");
  95. }
  96. finally
  97. {
  98. try
  99. {
  100. //删除计算得文件
  101. File.Delete(dto.file1);
  102. File.Delete(dto.file2);
  103. }
  104. catch
  105. {
  106. }
  107. }
  108. return Success(resDto);
  109. }
  110. /// <summary>
  111. /// 信号检测(支持DAMA、IBS、能量检测)
  112. /// </summary>
  113. /// <param name="dto">信号检测参数</param>
  114. /// <returns></returns>
  115. [HttpPost]
  116. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  117. {
  118. var token = Request.GetCorrelationId().ToString();
  119. dto.file1 = Path.Combine(uploadFolder, dto.file1);
  120. var vpres = ValidateDetectParam(dto);
  121. if (!vpres.Item1)
  122. {
  123. Serilog.Log.Warning(vpres.Item2);
  124. return Error<IEnumerable<DetectResDto>>(vpres.Item2);
  125. }
  126. List<DetectResDto> list = new List<DetectResDto>();
  127. try
  128. {
  129. XcorrUtils xcorr = new XcorrUtils();
  130. var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, dto.dmcType);
  131. foreach (var dmcItem in dmcResult)
  132. {
  133. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  134. detectRes.File1 = dto.file1;
  135. detectRes.TimeMs = dmcItem.Times;
  136. detectRes.Token = token;
  137. list.Add(detectRes);
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  143. }
  144. finally
  145. {
  146. try
  147. {
  148. //删除检测的文件
  149. File.Delete(dto.file1);
  150. }
  151. catch
  152. {
  153. }
  154. }
  155. return Success<IEnumerable<DetectResDto>>(list);
  156. }
  157. private (bool, string) ValidateCalcParam(CpuCgDto dto)
  158. {
  159. if (dto == null)
  160. {
  161. return (false, "参估计算参数格式错误!");
  162. }
  163. if (string.IsNullOrEmpty(dto.file1))
  164. {
  165. return (false, "参估计算参数数据文件[file1]不能为空!");
  166. }
  167. if (string.IsNullOrEmpty(dto.file2))
  168. {
  169. return (false, "参估计算参数数据文件[file2]不能为空!");
  170. }
  171. if (!File.Exists(dto.file1))
  172. {
  173. return (false, $"参估计算参数数据文件[{Path.GetFileName(dto.file1)}]不存在!");
  174. }
  175. if (!File.Exists(dto.file2))
  176. {
  177. return (false, $"参估计算参数数据文件[{Path.GetFileName(dto.file2)}]不存在!");
  178. }
  179. if (dto.dtRange <= 0)
  180. {
  181. return (false, "参估计算参数时差范围[dtRange]不能小于等于0!");
  182. }
  183. if (dto.smpStart < 0)
  184. {
  185. return (false, "参估计算参数开始样点[smpStart]不能小于0!");
  186. }
  187. if (dto.smpCount < 0)
  188. {
  189. return (false, "参估计算参数样点数[smpCount]不能小于0!");
  190. }
  191. return (true, string.Empty);
  192. }
  193. private (bool, string) ValidateDetectParam(DetectDto dto)
  194. {
  195. if (dto == null)
  196. {
  197. return (false, "检测计算参数格式错误!");
  198. }
  199. if (string.IsNullOrEmpty(dto.file1))
  200. {
  201. return (false, "检测计算参数数据文件[file1]不能为空!");
  202. }
  203. if (!File.Exists(dto.file1))
  204. {
  205. return (false, $"检测计算参数数据文件[{dto.file1}]不存在!");
  206. }
  207. bool containsValue = Enum.IsDefined(typeof(DmcType), dto.dmcType);
  208. if (!containsValue)
  209. {
  210. return (false, $"检测计算参数[dmcType]检测类型值{dto.dmcType}不存在!");
  211. }
  212. return (true, string.Empty);
  213. }
  214. }
  215. }