DetectCgController.cs 10 KB

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