DetectCgController.cs 9.9 KB

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