DetectCgController.cs 11 KB

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