123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using CliWrap;
- using Ips.Library.Basic;
- using Ips.Library.Entity;
- using Ips.Library.Signals;
- using Ips.Library.SvrHub;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection.Metadata;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ips.AdcAlgorithm
- {
- public class AdcController : IAdController
- {
- public AdcController()
- {
- getAdcSrcFileName = GetAdcSrcFileName;
- }
- static readonly string CliPath = Path.Combine(IpsPath.CliRootDir, "adc");
- static readonly string CliFile = Path.Combine(CliPath, "adc.exe");
- AdcOptions runOptions;
- Func<AdcOptions, int, string> getAdcSrcFileName;
- public async Task<ExeResult<AdcResult>> StartAdc(AdcOptions options, Action<string> handLine = null, CancellationToken token = default)
- {
- #region 远程采集
- string str = BuildString(options);
- var svr=SvrHub.Instance.GetOne(EnumSvrType.CapSvr, options.CardCode);
- var res = await HttpHelper.PostRequestAsync<AdcResult>(svr.BaseHttpAddr.AppendUrlSuffix("api/ad/startad"), options);
- if (res.code == 200)
- {
- return ExeResult.Create(res.data, str, res.data.StartTime, DateTime.Now);
- }
- else
- {
- throw new Exception(res.msg);
- }
- #endregion
- #region 本地采集
- //await Stop();
- //var cmd = BuildAdcCommand(options, handLine);
- ////开始时间+采集时长+10后认定为超时
- //var timeout = (int)(options.StartTime.AddSeconds(options.TimeLen + 10) - DateTime.Now).TotalSeconds;
- //token = token.LinkTimeout(timeout);
- //var cmdRes = await cmd.ExecuteAsync(token);
- //var result = await AdcFileUtil.BuildAdcResult(options, getAdcSrcFileName, token);
- //return ExeResult.Create(result, cmd.Arguments, cmdRes.StartTime, cmdRes.ExitTime, cmdRes.ExitCode);
- #endregion
- }
- public Task<ExeResult<AdcResult>> StartDdcKeep(AdcOptions options, Action<AdcResult> resultCallback, Action<string> handline = null, CancellationToken token = default)
- {
- throw new NotImplementedException();
- }
- public async Task<ExeResult<AdcResult>> StartDdcOne(AdcOptions options, Action<string> handLine = null, CancellationToken token = default)
- {
- #region 远程采集
- string str = BuildString(options);
- var svr = SvrHub.Instance.GetOne(EnumSvrType.CapSvr, options.CardCode);
- var res = await HttpHelper.PostRequestAsync<AdcResult>(svr.BaseHttpAddr.AppendUrlSuffix("api/ad/startad"), options);
- if (res.code == 200)
- {
- return ExeResult.Create(res.data, str, res.data.StartTime, DateTime.Now);
- }
- else
- {
- throw new Exception(res.msg);
- }
- #endregion
- //await Stop();
- //var cmd = BuildAdcCommand(options, handLine);
- ////开始时间+采集时长+10后认定为超时
- //var timeout = (int)(options.StartTime.AddSeconds(options.TimeLen + 10) - DateTime.Now).TotalSeconds;
- //var tokenAndTimeout = token.LinkTimeout(timeout);
- //var cmdRes = await cmd.ExecuteAsync(tokenAndTimeout);
- //var result = await AdcFileUtil.BuildDdcResult(options, getAdcSrcFileName, token);
- //return ExeResult.Create(result, cmd.Arguments, cmdRes.StartTime, cmdRes.ExitTime, cmdRes.ExitCode);
- }
- public Task Stop()
- {
- return Task.Run(() =>
- {
- if (runOptions != null)
- {
- DirectoryUtil.DeleteFiles(runOptions.StorePath, "*.tmp");
- }
- ProcessUtil.KillProcessByName(CliFile);
- });
- }
- private string BuildString(AdcOptions options)
- {
- StringBuilder str = new StringBuilder();
- double mutil = options.Mutil == 1 ? 0 : options.Mutil;
- str.Append($"开始时间:{options.StartTime:yyyy-MM-dd HH:mm:ss}");
- str.Append($"时钟类型:{options.ClockType.ToString("D")}");
- str.Append($"触发模式:{options.TriggerMode.ToString("D")}");
- str.Append($"DDC频率:{options.DdcFreq.E6m()}");
- str.Append($"时钟频率:{options.ClockFreq.E6m()}");
- str.Append($"抽取倍数:{mutil}");
- str.Append($"通道数:{options.ChCount}");
- str.Append($"采集时长:{options.TimeLen}");
- return str.ToString();
- }
- private Command BuildAdcCommand(AdcOptions options, Action<string> handLine)
- {
- if (options.StartTime < DateTime.Now.AddSeconds(AdcConst.MinWaitTime))
- options.StartTime = DateTime.Now.AddSeconds(AdcConst.MinWaitTime);
- runOptions = options;
- DirectoryUtil.CreateIfNotExists(options.StorePath);
- var nameTime = options.StartTime.Format(SignalFile.SigTimeFileFormat);
- string fileName = Path.Combine(options.StorePath, nameTime + ".dat");
- //adc.exe -s 0 -c 0 -t 0 -d 70 -f 100 -m 128 -h 3 -o e:\data\1.dat -l 10
- var cmd = Cli.Wrap(CliFile)
- .WithWorkingDirectory(CliPath)
- .WithValidation(CommandResultValidation.None)
- .WithArguments(args =>
- {
- args.Add("-s").Add(DateTimeUtil.To1970s(options.StartTime));
- args.Add("-c").Add(options.ClockType.ToString("D"));
- args.Add("-t").Add(options.TriggerMode.ToString("D"));
- args.Add("-d").Add(options.DdcFreq.E6m());
- args.Add("-f").Add(options.ClockFreq.E6m());
- args.Add("-m").Add(options.Mutil == 1 ? 0 : options.Mutil);
- args.Add("-h").Add(options.ChCount);
- args.Add("-o").Add(fileName, true);
- args.Add("-l").Add(options.TimeLen);
- });
- if (handLine != null)
- {
- cmd = cmd.WithStandardErrorPipe(PipeTarget.ToDelegate(handLine))
- .WithStandardOutputPipe(PipeTarget.ToDelegate(handLine));
- }
- return cmd;
- }
- private string GetAdcSrcFileName(AdcOptions options, int chNum)
- {
- var sigTime = options.StartTime;
- var nameTime = sigTime.Format(SignalFile.SigTimeFileFormat);
- string fileName = $"{nameTime}_ch{chNum}{(options.Mutil == 1 ? "" : "_iq")}.dat";
- string fullName = Path.Combine(options.StorePath, fileName);
- if (!File.Exists(fullName))
- {
- string nameFmt = $"*{nameTime}*_ch{chNum}*.dat";
- var fmtFileNames = Directory.GetFiles(options.StorePath, nameFmt, SearchOption.TopDirectoryOnly);
- if (fmtFileNames.IsNotNullOrEmpty())
- {
- fullName = fmtFileNames.First();
- }
- else
- {
- IpsLogger.Error($"查找采集落盘文件失败,目录:{options.StorePath},格式:{nameFmt}");
- }
- }
- return fullName;
- }
- }
- }
|