DetectCgController.cs 7.7 KB

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