DetectCgController.cs 11 KB

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