DetectCgController.cs 11 KB

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