DetectCgController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. namespace XdCxRhDW.App.WebAPI
  17. {
  18. /// <summary>
  19. /// 检测参估功能相关接口
  20. /// </summary>
  21. public class DetectCgController : BaseController
  22. {
  23. private string UploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UploadFolder");
  24. private (bool, string) ValidateCalcParam(CalcDto dto)
  25. {
  26. if (dto == null)
  27. {
  28. return (false, "参估计算参数格式错误!");
  29. }
  30. if (string.IsNullOrEmpty(dto.file1))
  31. {
  32. return (false, "参估计算参数数据文件[file1]不能为空!");
  33. }
  34. if (string.IsNullOrEmpty(dto.file2))
  35. {
  36. return (false, "参估计算参数数据文件[file2]不能为空!");
  37. }
  38. if (!File.Exists(dto.file1))
  39. {
  40. return (false, $"参估计算参数数据文件[{dto.file1}]不存在!");
  41. }
  42. if (!File.Exists(dto.file2))
  43. {
  44. return (false, $"参估计算参数数据文件[{dto.file2}]不存在!");
  45. }
  46. if (dto.dtRange <= 0)
  47. {
  48. return (false, "参估计算参数时差范围[dtRange]不能小于等于0!");
  49. }
  50. if (dto.smpStart < 0)
  51. {
  52. return (false, "参估计算参数开始样点[smpStart]不能小于0!");
  53. }
  54. if (dto.smpCount < 0)
  55. {
  56. return (false, "参估计算参数样点数[smpCount]不能小于0!");
  57. }
  58. return (true, string.Empty);
  59. }
  60. /// <summary>
  61. /// 参估计算
  62. /// </summary>
  63. /// <param name="dto"></param>
  64. /// <returns></returns>
  65. [HttpPost]
  66. public async Task<AjaxResult<EstimationResDto>> EstimationCalc(CalcDto dto)
  67. {
  68. var vpres = ValidateCalcParam(dto);
  69. if (!vpres.Item1)
  70. {
  71. Serilog.Log.Warning(vpres.Item2);
  72. return Error<EstimationResDto>(vpres.Item2);
  73. }
  74. XcorrStruct xItem = new XcorrStruct();
  75. xItem.file1 = dto.file1;
  76. xItem.file2 = dto.file2;
  77. xItem.samplingRate = dto.samplingRate;
  78. xItem.dtCenter = dto.dtCenter;
  79. xItem.dtRange = dto.dtRange;
  80. xItem.dfRange = dto.dfRange;
  81. //样点数为0时计算所有样本
  82. if (dto.smpCount == 0)
  83. {
  84. FileInfo file = new FileInfo(dto.file1);
  85. long totalsamp = file.Length / 4;
  86. xItem.smpCount = (int)totalsamp - dto.smpStart;
  87. }
  88. else
  89. {
  90. xItem.smpCount = dto.smpCount;
  91. }
  92. xItem.smpStart = dto.smpStart;
  93. xItem.snrThreshold = dto.snrThreshold;
  94. var result = await XcorrUtils.Calc(xItem);
  95. //开始计算
  96. if (result.flag == -2)
  97. {
  98. Serilog.Log.Warning("参估计算内部错误!");
  99. return Error<EstimationResDto>("参估计算内部错误!");
  100. }
  101. else if (result.flag == -1)
  102. {
  103. Serilog.Log.Warning("参估计算所需数据超出文件范围!");
  104. return Error<EstimationResDto>("参估计算所需数据超出文件范围!");
  105. }
  106. EstimationResDto resDto = new EstimationResDto();
  107. resDto.Tm = result.tm;
  108. resDto.Smpstart = result.smpstart;
  109. resDto.Smplen = result.smplen;
  110. resDto.File1 = result.file1;
  111. resDto.File2 = result.file2;
  112. if (result.flag == 1)
  113. {
  114. resDto.Dt = result.dt.Value;
  115. resDto.Df = result.df.Value;
  116. resDto.Snr = result.snr.Value;
  117. }
  118. try
  119. {
  120. //删除计算得文件
  121. File.Delete(dto.file1);
  122. File.Delete(dto.file2);
  123. }
  124. catch
  125. {
  126. }
  127. return Success(resDto);
  128. }
  129. private (bool, string) ValidateDetectParam(DetectDto dto)
  130. {
  131. if (dto == null)
  132. {
  133. return (false, "检测计算参数格式错误!");
  134. }
  135. if (string.IsNullOrEmpty(dto.file1))
  136. {
  137. return (false, "检测计算参数数据文件[file1]不能为空!");
  138. }
  139. if (!File.Exists(dto.file1))
  140. {
  141. return (false, $"检测计算参数数据文件[{dto.file1}]不存在!");
  142. }
  143. bool containsValue = Enum.IsDefined(typeof(DmcType), dto.dmcType);
  144. if (!containsValue)
  145. {
  146. return (false, $"检测计算参数[dmcType]检测类型值{dto.dmcType}不存在!");
  147. }
  148. return (true, string.Empty);
  149. }
  150. /// <summary>
  151. /// 检测计算
  152. /// </summary>
  153. /// <param name="dto"></param>
  154. /// <returns></returns>
  155. [HttpPost]
  156. public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
  157. {
  158. var vpres = ValidateDetectParam(dto);
  159. if (!vpres.Item1)
  160. {
  161. Serilog.Log.Warning(vpres.Item2);
  162. return Error<IEnumerable<DetectResDto>>(vpres.Item2);
  163. }
  164. List<DetectResDto> list = new List<DetectResDto>();
  165. try
  166. {
  167. var dmcResult = await XcorrUtils.DmcCheckAsync(dto.file1, dto.dmcType);
  168. foreach (var dmcItem in dmcResult)
  169. {
  170. DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
  171. detectRes.File1 = dto.file1;
  172. list.Add(detectRes);
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. return Error<IEnumerable<DetectResDto>>($"执行检测计算异常,{ex.Message}");
  178. }
  179. finally
  180. {
  181. try
  182. {
  183. //删除检测的文件
  184. File.Delete(dto.file1);
  185. }
  186. catch
  187. {
  188. }
  189. }
  190. return Success<IEnumerable<DetectResDto>>(list);
  191. }
  192. /// <summary>
  193. /// 上传文件
  194. /// </summary>
  195. /// <returns></returns>
  196. [HttpPost]
  197. public async Task<AjaxResult<string>> UploadFile()
  198. {
  199. if (!Request.Content.IsMimeMultipartContent("form-data"))
  200. {
  201. return Error<string>("请求数据不是multipart/form-data类型");
  202. }
  203. var provider = new MultipartMemoryStreamProvider();
  204. await Request.Content.ReadAsMultipartAsync(provider);
  205. // 创建文件夹(如果尚未存在)
  206. if (!Directory.Exists(UploadFolder))
  207. {
  208. Directory.CreateDirectory(UploadFolder);
  209. }
  210. var content = provider.Contents.First();
  211. var fileName = GetFileName(content.Headers);
  212. var fileData = await content.ReadAsByteArrayAsync();
  213. // 将文件保存到本地文件夹中
  214. var filePath = Path.Combine(UploadFolder, fileName);
  215. using (var fileStream = new FileStream(filePath, FileMode.Create))
  216. {
  217. await fileStream.WriteAsync(fileData, 0, fileData.Length);
  218. }
  219. return Success<string>(filePath);
  220. }
  221. private string GetFileName(System.Net.Http.Headers.HttpContentHeaders headers)
  222. {
  223. var disposition = headers.ContentDisposition;
  224. var fileName = disposition.FileName.Trim('"');
  225. // 如果 fileName 为空,则可以根据需要生成唯一的文件名
  226. if (string.IsNullOrEmpty(fileName))
  227. {
  228. fileName = Guid.NewGuid().ToString();
  229. }
  230. return fileName;
  231. }
  232. }
  233. }