AdcController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using CliWrap;
  2. using Ips.Library.Basic;
  3. using Ips.Library.Entity;
  4. using Ips.Library.Signals;
  5. using Ips.Library.SvrHub;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection.Metadata;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Ips.AdcAlgorithm
  13. {
  14. public class AdcController : IAdController
  15. {
  16. public AdcController()
  17. {
  18. getAdcSrcFileName = GetAdcSrcFileName;
  19. }
  20. static readonly string CliPath = Path.Combine(IpsPath.CliRootDir, "adc");
  21. static readonly string CliFile = Path.Combine(CliPath, "adc.exe");
  22. AdcOptions runOptions;
  23. Func<AdcOptions, int, string> getAdcSrcFileName;
  24. public async Task<ExeResult<AdcResult>> StartAdc(AdcOptions options, Action<string> handLine = null, CancellationToken token = default)
  25. {
  26. #region 远程采集
  27. string str = BuildString(options);
  28. var svr=SvrHub.Instance.GetOne(EnumSvrType.CapSvr, options.CardCode);
  29. var res = await HttpHelper.PostRequestAsync<AdcResult>(svr.BaseHttpAddr.AppendUrlSuffix("api/ad/startad"), options);
  30. if (res.code == 200)
  31. {
  32. return ExeResult.Create(res.data, str, res.data.StartTime, DateTime.Now);
  33. }
  34. else
  35. {
  36. throw new Exception(res.msg);
  37. }
  38. #endregion
  39. #region 本地采集
  40. //await Stop();
  41. //var cmd = BuildAdcCommand(options, handLine);
  42. ////开始时间+采集时长+10后认定为超时
  43. //var timeout = (int)(options.StartTime.AddSeconds(options.TimeLen + 10) - DateTime.Now).TotalSeconds;
  44. //token = token.LinkTimeout(timeout);
  45. //var cmdRes = await cmd.ExecuteAsync(token);
  46. //var result = await AdcFileUtil.BuildAdcResult(options, getAdcSrcFileName, token);
  47. //return ExeResult.Create(result, cmd.Arguments, cmdRes.StartTime, cmdRes.ExitTime, cmdRes.ExitCode);
  48. #endregion
  49. }
  50. public Task<ExeResult<AdcResult>> StartDdcKeep(AdcOptions options, Action<AdcResult> resultCallback, Action<string> handline = null, CancellationToken token = default)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. public async Task<ExeResult<AdcResult>> StartDdcOne(AdcOptions options, Action<string> handLine = null, CancellationToken token = default)
  55. {
  56. #region 远程采集
  57. string str = BuildString(options);
  58. var svr = SvrHub.Instance.GetOne(EnumSvrType.CapSvr, options.CardCode);
  59. var res = await HttpHelper.PostRequestAsync<AdcResult>(svr.BaseHttpAddr.AppendUrlSuffix("api/ad/startad"), options);
  60. if (res.code == 200)
  61. {
  62. return ExeResult.Create(res.data, str, res.data.StartTime, DateTime.Now);
  63. }
  64. else
  65. {
  66. throw new Exception(res.msg);
  67. }
  68. #endregion
  69. //await Stop();
  70. //var cmd = BuildAdcCommand(options, handLine);
  71. ////开始时间+采集时长+10后认定为超时
  72. //var timeout = (int)(options.StartTime.AddSeconds(options.TimeLen + 10) - DateTime.Now).TotalSeconds;
  73. //var tokenAndTimeout = token.LinkTimeout(timeout);
  74. //var cmdRes = await cmd.ExecuteAsync(tokenAndTimeout);
  75. //var result = await AdcFileUtil.BuildDdcResult(options, getAdcSrcFileName, token);
  76. //return ExeResult.Create(result, cmd.Arguments, cmdRes.StartTime, cmdRes.ExitTime, cmdRes.ExitCode);
  77. }
  78. public Task Stop()
  79. {
  80. return Task.Run(() =>
  81. {
  82. if (runOptions != null)
  83. {
  84. DirectoryUtil.DeleteFiles(runOptions.StorePath, "*.tmp");
  85. }
  86. ProcessUtil.KillProcessByName(CliFile);
  87. });
  88. }
  89. private string BuildString(AdcOptions options)
  90. {
  91. StringBuilder str = new StringBuilder();
  92. double mutil = options.Mutil == 1 ? 0 : options.Mutil;
  93. str.Append($"开始时间:{options.StartTime:yyyy-MM-dd HH:mm:ss}");
  94. str.Append($"时钟类型:{options.ClockType.ToString("D")}");
  95. str.Append($"触发模式:{options.TriggerMode.ToString("D")}");
  96. str.Append($"DDC频率:{options.DdcFreq.E6m()}");
  97. str.Append($"时钟频率:{options.ClockFreq.E6m()}");
  98. str.Append($"抽取倍数:{mutil}");
  99. str.Append($"通道数:{options.ChCount}");
  100. str.Append($"采集时长:{options.TimeLen}");
  101. return str.ToString();
  102. }
  103. private Command BuildAdcCommand(AdcOptions options, Action<string> handLine)
  104. {
  105. if (options.StartTime < DateTime.Now.AddSeconds(AdcConst.MinWaitTime))
  106. options.StartTime = DateTime.Now.AddSeconds(AdcConst.MinWaitTime);
  107. runOptions = options;
  108. DirectoryUtil.CreateIfNotExists(options.StorePath);
  109. var nameTime = options.StartTime.Format(SignalFile.SigTimeFileFormat);
  110. string fileName = Path.Combine(options.StorePath, nameTime + ".dat");
  111. //adc.exe -s 0 -c 0 -t 0 -d 70 -f 100 -m 128 -h 3 -o e:\data\1.dat -l 10
  112. var cmd = Cli.Wrap(CliFile)
  113. .WithWorkingDirectory(CliPath)
  114. .WithValidation(CommandResultValidation.None)
  115. .WithArguments(args =>
  116. {
  117. args.Add("-s").Add(DateTimeUtil.To1970s(options.StartTime));
  118. args.Add("-c").Add(options.ClockType.ToString("D"));
  119. args.Add("-t").Add(options.TriggerMode.ToString("D"));
  120. args.Add("-d").Add(options.DdcFreq.E6m());
  121. args.Add("-f").Add(options.ClockFreq.E6m());
  122. args.Add("-m").Add(options.Mutil == 1 ? 0 : options.Mutil);
  123. args.Add("-h").Add(options.ChCount);
  124. args.Add("-o").Add(fileName, true);
  125. args.Add("-l").Add(options.TimeLen);
  126. });
  127. if (handLine != null)
  128. {
  129. cmd = cmd.WithStandardErrorPipe(PipeTarget.ToDelegate(handLine))
  130. .WithStandardOutputPipe(PipeTarget.ToDelegate(handLine));
  131. }
  132. return cmd;
  133. }
  134. private string GetAdcSrcFileName(AdcOptions options, int chNum)
  135. {
  136. var sigTime = options.StartTime;
  137. var nameTime = sigTime.Format(SignalFile.SigTimeFileFormat);
  138. string fileName = $"{nameTime}_ch{chNum}{(options.Mutil == 1 ? "" : "_iq")}.dat";
  139. string fullName = Path.Combine(options.StorePath, fileName);
  140. if (!File.Exists(fullName))
  141. {
  142. string nameFmt = $"*{nameTime}*_ch{chNum}*.dat";
  143. var fmtFileNames = Directory.GetFiles(options.StorePath, nameFmt, SearchOption.TopDirectoryOnly);
  144. if (fmtFileNames.IsNotNullOrEmpty())
  145. {
  146. fullName = fmtFileNames.First();
  147. }
  148. else
  149. {
  150. IpsLogger.Error($"查找采集落盘文件失败,目录:{options.StorePath},格式:{nameFmt}");
  151. }
  152. }
  153. return fullName;
  154. }
  155. }
  156. }