Ver código fonte

支持并行调用检测计算

wyq 1 ano atrás
pai
commit
da7fed51d4

+ 1 - 5
XdCxRhDW.App/Api/PosApi.cs

@@ -54,11 +54,7 @@ namespace XdCxRhDW.App.Api
         private extern static void X2_Pos20240305_Core(double[] mainSat, double[] adjaSat, double[] mainSatTarStation, double[] adjaSatTarStation, double[] refStation, double[] zone
             , double tarDto, double tarDfo, double refDto, double refDfo, double fu1, double fd1, double fu2, double fd2, double[] res);
 
-        private const string XdtsDll = @"Api\时差线\Positioning.dll";
-
-        [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOA", CallingConvention = CallingConvention.Cdecl)]//星地时差线
-        public extern static void CurveByTwoTDOA(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] ref_pos, double[] Zone,
-      double target_dto, double ref_dto, out IntPtr LOP_Value, ref int LOP_Len);
+      
 
         #endregion
 

+ 7 - 2
XdCxRhDW.App/Api/时差线/DrawDtoLineHelper.cs

@@ -10,7 +10,12 @@ namespace XdCxRhDW.App.Api.时差线
 {
     public class DrawDtoLineHelper
     {
-     
+        private const string XdtsDll = @"Api\时差线\Positioning.dll";
+
+        [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOA", CallingConvention = CallingConvention.Cdecl)]//星地时差线
+        public extern static void CurveByTwoTDOA(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] ref_pos, double[] Zone,
+      double target_dto, double ref_dto, out IntPtr LOP_Value, ref int LOP_Len);
+
         private const string exeName = "XingDiSCX.exe";
         private static double[] zone = new double[] { -85, 85, -180, 180 }; //定位区域
         /// <summary>
@@ -99,7 +104,7 @@ namespace XdCxRhDW.App.Api.时差线
             int LOP_Len = 0;
 
 
-            PosApi.CurveByTwoTDOA(
+            CurveByTwoTDOA(
                 opt.MsEph,
                  opt.NsEph,
                  opt.MsAnt,

+ 3 - 3
XdCxRhDW.App/CorTools/CorToolForm.cs

@@ -115,7 +115,7 @@ namespace XdCxRhDW.App.CorTools
         {
             if (btnOK.Text == "停止")
             {
-                XcorrUtils.Stop();
+                //XcorrUtils.Stop();
                 return;
             }
             //开始计算
@@ -184,8 +184,8 @@ namespace XdCxRhDW.App.CorTools
 
         private async Task<CafResult> ExecuteCorAsync(XcorrStruct xItem)
         {
-
-            var result = await XcorrUtils.Calc(xItem);
+            XcorrUtils xcorr = new XcorrUtils();
+            var result = await xcorr.Calc(xItem);
             //开始计算
             if (result.flag == -2)
             {

+ 23 - 2
XdCxRhDW.App/CorTools/DetectToolForm.cs

@@ -25,6 +25,7 @@ using XdCxRhDW.App.Basic;
 using XdCxRhDW.App.CpuCgTools;
 using XdCxRhDW.App.EFContext;
 using XdCxRhDW.App.WebAPI;
+using static DevExpress.Data.Helpers.FindSearchRichParser;
 using static XdCxRhDW.App.WebAPI.BaseController;
 
 namespace XdCxRhDW.App.CorTools
@@ -34,6 +35,7 @@ namespace XdCxRhDW.App.CorTools
         static readonly string inifile = Path.Combine(Application.StartupPath, "par.ini");
         volatile bool beRunning = false;
         private string uploadUrL;
+        private string token;
         BindingList<CafResult> gridSource = new BindingList<CafResult>();
 
         public DetectToolForm()
@@ -241,6 +243,7 @@ namespace XdCxRhDW.App.CorTools
                     gridSource.Clear();
                     foreach (var dmcItem in dmcResult)
                     {
+                        token = dmcItem.Token;
                         gridSource.Add(new CafResult()
                         {
                             file1 = btnFile1.Text,
@@ -299,7 +302,7 @@ namespace XdCxRhDW.App.CorTools
         {
             if (btnCalc.Text == "停止")
             {
-                XcorrUtils.Stop();
+                StopCalc();
                 return;
             }
             //开始计算
@@ -371,6 +374,7 @@ namespace XdCxRhDW.App.CorTools
                     var result = PostRequest<EstimationResDto>(uploadUri, content);
                     if (result != null)
                     {
+                        token = result.Token;
                         gridSource.Add(new CafResult()
                         {
                             file1 = btnFile1.Text,
@@ -397,6 +401,22 @@ namespace XdCxRhDW.App.CorTools
             beRunning = false;
 
         }
+        private void StopCalc()
+        {   // 构建上传请求的 Uri
+            string uploadUri = $"{uploadUrL}/StopEstimationCalc?token={token}";
+            var content = new StringContent("", System.Text.Encoding.UTF8, "application/json");
+            try
+            {
+                var result = PostRequest<bool>(uploadUri, content);
+                if (result)
+                {
+                    DxHelper.MsgBoxHelper.ShowInfo($"停止成功");
+                }
+            }
+            catch (Exception)
+            {
+            }
+        }
         private string UpLoadFile(string filePath)
         {
             string file = string.Empty;
@@ -429,7 +449,8 @@ namespace XdCxRhDW.App.CorTools
         private async Task<CafResult> ExecuteCorAsync(XcorrStruct xItem)
         {
 
-            var result = await XcorrUtils.Calc(xItem);
+            XcorrUtils xcorr = new XcorrUtils();
+            var result = await xcorr.Calc(xItem);
             //开始计算
             if (result.flag == -2)
             {

+ 45 - 26
XdCxRhDW.App/CorTools/XcorrUtils.cs

@@ -46,26 +46,28 @@ namespace XdCxRhDW.App.CorTools
     }
     class XcorrUtils
     {
-        static Process p = new Process();
+        Process cafp;
+        Process dmp;
         // D:/data/test/r1.dat  D:/data/test/r4.dat 1562500 -250000 20 16384 100000 1048576 11
-        public static async Task<CafResult> Calc(XcorrStruct xs)
+        public async Task<CafResult> Calc(XcorrStruct xs)
         {
             CafResult res = new CafResult();
+            cafp = new Process();
             await Task.Run(() =>
             {
-                p.StartInfo.FileName = Path.Combine(p.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
-                p.StartInfo.Arguments = $"\"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
-                p.StartInfo.CreateNoWindow = true;
-                p.StartInfo.RedirectStandardError = true;
-                p.StartInfo.RedirectStandardOutput = true;
-                p.StartInfo.UseShellExecute = false;
-                p.Start();
+                cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
+                cafp.StartInfo.Arguments = $"\"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
+                cafp.StartInfo.CreateNoWindow = true;
+                cafp.StartInfo.RedirectStandardError = true;
+                cafp.StartInfo.RedirectStandardOutput = true;
+                cafp.StartInfo.UseShellExecute = false;
+                cafp.Start();
                 Stopwatch stopWatch = new Stopwatch();
                 stopWatch.Start();
-                p.WaitForExit();
+                cafp.WaitForExit();
                 stopWatch.Stop();
                 TimeSpan ts = stopWatch.Elapsed;
-                var str = p.StandardOutput.ReadToEnd();
+                var str = cafp.StandardOutput.ReadToEnd();
                 res.FromLine(str);
                 res.file1 = xs.file1;
                 res.file2 = xs.file2;
@@ -76,10 +78,11 @@ namespace XdCxRhDW.App.CorTools
             return res;
         }
 
-        public static async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType)
+        public async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType)
         {
             string dmcCmd = "all";
-            string pFileName = Path.Combine(p.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
+            dmp = new Process();
+            string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
             string pArguments = string.Empty;
             switch (dmcType)
             {
@@ -92,7 +95,7 @@ namespace XdCxRhDW.App.CorTools
                     pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
                     break;
                 case DmcType.Ky5758:
-                    pFileName = Path.Combine(p.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
+                    pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
                     //enc.exe enc-test.dat 0.096 5 0全部文件
                     pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
                     break;
@@ -101,19 +104,19 @@ namespace XdCxRhDW.App.CorTools
             }
             return await Task.Run(() =>
              {
-                 p.StartInfo.FileName = pFileName;
-                 p.StartInfo.Arguments = pArguments;
-                 p.StartInfo.CreateNoWindow = true;
-                 p.StartInfo.RedirectStandardError = true;
-                 p.StartInfo.RedirectStandardOutput = true;
-                 p.StartInfo.UseShellExecute = false;
-                 p.Start();
+                 dmp.StartInfo.FileName = pFileName;
+                 dmp.StartInfo.Arguments = pArguments;
+                 dmp.StartInfo.CreateNoWindow = true;
+                 dmp.StartInfo.RedirectStandardError = true;
+                 dmp.StartInfo.RedirectStandardOutput = true;
+                 dmp.StartInfo.UseShellExecute = false;
+                 dmp.Start();
                  Stopwatch stopWatch = new Stopwatch();
                  stopWatch.Start();
-                 p.WaitForExit();
+                 dmp.WaitForExit();
                  stopWatch.Stop();
                  TimeSpan ts = stopWatch.Elapsed;
-                 var str = p.StandardOutput.ReadToEnd();
+                 var str = dmp.StandardOutput.ReadToEnd();
                  return ConvertDmcResult(str, ts.TotalMilliseconds);
              });
         }
@@ -130,15 +133,31 @@ namespace XdCxRhDW.App.CorTools
                 string userName = "";
                 if (items.Length >= 3)
                     userName = items[2];
-                yield return new DmcResult(start, length, userName, Math.Round(tm,4));
+                yield return new DmcResult(start, length, userName, Math.Round(tm, 4));
             }
         }
 
-        public static void Stop()
+        public void StopDm()
         {
             try
             {
-                p.Kill();
+                if (dmp != null)
+                {
+                    dmp.Kill();
+                }
+            }
+            catch (Exception)
+            {
+            }
+        }
+        public void StopCalc()
+        {
+            try
+            {
+                if (cafp != null)
+                {
+                    cafp.Kill();
+                }
             }
             catch (Exception)
             {

+ 1 - 0
XdCxRhDW.App/WebAPI/Controllers/BaseController.cs

@@ -64,6 +64,7 @@ namespace XdCxRhDW.App.WebAPI
                 throw new Exception(AjaxResult.msg);
             }
         }
+       
 
         /// <summary>
         /// Http返回结果

+ 118 - 28
XdCxRhDW.App/WebAPI/Controllers/DetectCgController.cs

@@ -14,6 +14,7 @@ using System.Web;
 using Newtonsoft.Json.Linq;
 using XdCxRhDW.App.CpuCgTools;
 using static XdCxRhDW.App.WebAPI.Startup;
+using System.Diagnostics;
 namespace XdCxRhDW.App.WebAPI
 {
     /// <summary>
@@ -22,6 +23,9 @@ namespace XdCxRhDW.App.WebAPI
     public class DetectCgController : BaseController
     {
 
+        private static readonly object thisLock = new object();
+        Dictionary<string, XcorrUtils> keyValues = new Dictionary<string, XcorrUtils>();
+
         private string UploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UploadFolder");
         private (bool, string) ValidateCalcParam(CalcDto dto)
         {
@@ -67,6 +71,7 @@ namespace XdCxRhDW.App.WebAPI
         [HttpPost]
         public async Task<AjaxResult<EstimationResDto>> EstimationCalc(CalcDto dto)
         {
+            var token = Request.GetCorrelationId().ToString();
             dto.file1 = Path.Combine(UploadFolder, dto.file1);
             dto.file2 = Path.Combine(UploadFolder, dto.file2);
             var vpres = ValidateCalcParam(dto);
@@ -97,43 +102,92 @@ namespace XdCxRhDW.App.WebAPI
             xItem.smpStart = dto.smpStart;
             xItem.snrThreshold = dto.snrThreshold;
 
-            var result = await XcorrUtils.Calc(xItem);
-            //开始计算
-            if (result.flag == -2)
-            {
-                Serilog.Log.Warning("参估计算内部错误!");
-                return Error<EstimationResDto>("参估计算内部错误!");
-            }
-            else if (result.flag == -1)
-            {
-                Serilog.Log.Warning("参估计算所需数据超出文件范围!");
-                return Error<EstimationResDto>("参估计算所需数据超出文件范围!");
-            }
             EstimationResDto resDto = new EstimationResDto();
-            resDto.TimeMs = result.tm;
-            resDto.Smpstart = result.smpstart;
-            resDto.Smplen = result.smplen;
-            resDto.File1 = result.file1;
-            resDto.File2 = result.file2;
-            if (result.flag == 1)
+            try
             {
-                resDto.Dt = result.dt.Value;
-                resDto.Df = result.df.Value;
-                resDto.Snr = result.snr.Value;
+                XcorrUtils xcorr = new XcorrUtils();
+                lock (thisLock)
+                {
+                    keyValues.Add(token, xcorr);
+                }
+                var result = await xcorr.Calc(xItem);
+                //开始计算
+                if (result.flag == -2)
+                {
+                    Serilog.Log.Warning("参估计算内部错误!");
+                    return Error<EstimationResDto>("参估计算内部错误!");
+                }
+                else if (result.flag == -1)
+                {
+                    Serilog.Log.Warning("参估计算所需数据超出文件范围!");
+                    return Error<EstimationResDto>("参估计算所需数据超出文件范围!");
+                }
+                resDto.Token = token;
+                resDto.TimeMs = result.tm;
+                resDto.Smpstart = result.smpstart;
+                resDto.Smplen = result.smplen;
+                resDto.File1 = result.file1;
+                resDto.File2 = result.file2;
+                if (result.flag == 1)
+                {
+                    resDto.Dt = result.dt.Value;
+                    resDto.Df = result.df.Value;
+                    resDto.Snr = result.snr.Value;
+                }
+
+
             }
-            try
+            catch (Exception ex)
             {
-                //删除计算得文件
-                File.Delete(dto.file1);
-                File.Delete(dto.file2);
+                return Error<EstimationResDto>($"执行参估计算异常,{ex.Message}");
             }
-            catch
+            finally
             {
+                lock (thisLock)
+                {
+                    keyValues.Remove(token);
+                }
+                try
+                {
+                    //删除计算得文件
+                    File.Delete(dto.file1);
+                    File.Delete(dto.file2);
+                }
+                catch
+                {
+                }
             }
             return Success(resDto);
 
         }
 
+      /// <summary>
+      /// 参估计算停止
+      /// </summary>
+      /// <param name="token"></param>
+      /// <returns></returns>
+        [HttpPost]
+        public async Task<AjaxResult<bool>> StopEstimationCalc(string token)
+        {
+            return await Task.Run(() =>
+            {
+                lock (thisLock)
+                {
+                    if (keyValues.ContainsKey(token))
+                    {
+                        keyValues[token].StopCalc();
+                        return Success(true);
+                    }
+                    else
+                    {
+                        return Success(false);
+                    }
+                }
+            });
+
+        }
+
+
 
         private (bool, string) ValidateDetectParam(DetectDto dto)
         {
@@ -166,6 +220,7 @@ namespace XdCxRhDW.App.WebAPI
         [HttpPost]
         public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
         {
+            var token = Request.GetCorrelationId().ToString();
             dto.file1 = Path.Combine(UploadFolder, dto.file1);
             var vpres = ValidateDetectParam(dto);
             if (!vpres.Item1)
@@ -176,12 +231,18 @@ namespace XdCxRhDW.App.WebAPI
             List<DetectResDto> list = new List<DetectResDto>();
             try
             {
-                var dmcResult = await XcorrUtils.DmcCheckAsync(dto.file1,dto.fsHz, dto.dmcType);
+                XcorrUtils xcorr = new XcorrUtils();
+                lock (thisLock)
+                {
+                    keyValues.Add(token, xcorr);
+                }
+                var dmcResult = await xcorr.DmcCheckAsync(dto.file1, dto.fsHz, dto.dmcType);
                 foreach (var dmcItem in dmcResult)
                 {
                     DetectResDto detectRes = new DetectResDto(dmcItem.Start, dmcItem.Length, dmcItem.UserName);
                     detectRes.File1 = dto.file1;
                     detectRes.TimeMs = dmcItem.Times;
+                    detectRes.Token = token;
                     list.Add(detectRes);
 
                 }
@@ -192,6 +253,10 @@ namespace XdCxRhDW.App.WebAPI
             }
             finally
             {
+                lock (thisLock)
+                {
+                    //keyValues.Remove(token);
+                }
                 try
                 {
                     //删除检测的文件
@@ -206,6 +271,31 @@ namespace XdCxRhDW.App.WebAPI
 
         }
 
+        /// <summary>
+        /// 信号检测停止
+        /// </summary>
+        /// <param name="token"></param>
+        /// <returns></returns>
+        [HttpPost]
+        public async Task<AjaxResult<bool>> StopDetectCalc(string token)
+        {
+            return await Task.Run(() =>
+            {
+                lock (thisLock)
+                {
+                    if (keyValues.ContainsKey(token))
+                    {
+                        keyValues[token].StopDm();
+                        return Success(true);
+                    }
+                    else
+                    {
+                        return Success(false);
+                    }
+                }
+            });
+
+        }
 
         /// <summary>
         /// 上传文件
@@ -222,7 +312,7 @@ namespace XdCxRhDW.App.WebAPI
             await Request.Content.ReadAsMultipartAsync(provider);
             Directory.CreateDirectory(UploadFolder);
             var content = provider.Contents.First();
-            var fileName = Guid.NewGuid().ToString()+".dat";
+            var fileName = Guid.NewGuid().ToString() + ".dat";
             var fileData = await content.ReadAsByteArrayAsync();
             FileDto fileDto = new FileDto();
             fileDto.FileName = fileName;

+ 4 - 2
XdCxRhDw.Dto/DetectResDto.cs

@@ -53,7 +53,9 @@ namespace XdCxRhDw.Dto
         /// </summary>
         public double TimeMs { get; set; }
 
-
-
+        /// <summary>
+        /// 标识
+        /// </summary>
+        public string Token { get; set; }
     }
 }

+ 1 - 0
XdCxRhDw.Dto/EstimationResDto.cs

@@ -41,6 +41,7 @@ namespace XdCxRhDw.Dto
         /// </summary>
         public double TimeMs { get; set; }
 
+        public string Token { get; set; }
         /// <summary>
         /// 开始样点
         /// </summary>