using CliWrap; using CliWrap.Buffered; using Ips.Library.Basic; using Ips.Library.Entity; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ips.EphAlgorithm { public class EphTleUtil2 { const string fmtTime = "yyyy-MM-dd HH:mm:ss"; static readonly string CliDir = PathUtil.GetBasePath("ipscli", "ephtle"); static string CliFile { get { string fullName = Path.Combine(CliDir, "ephtle.exe"); if (!File.Exists(fullName)) { string newFile = Directory.EnumerateFiles(CliDir, "ephtle*.exe").FirstOrDefault(); if (newFile != null) fullName = newFile; } return fullName; } } private static object obj = new object(); private static BufferedCommandResult RunCli(Command cli, CancellationToken token) { lock (obj) { return cli.ExecuteBufferedAsync(token).GetAwaiter().GetResult(); } } public static ExeResult Get(string line1, string line2, DateTime time, CancellationToken token = default) { var cli = Cli.Wrap(CliFile) .WithArguments(new[] { line1, line2, time.ToString(fmtTime) }); var res = RunCli(cli, token); var result = EphResult.FromString(res.StandardOutput); result.Time = time; return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } public static ExeResult GetList(string line1, string line2, DateTime[] times, CancellationToken token = default) { const string CommandName = "list"; List args = new List(); args.AddRange(new[] { CommandName, line1, line2 }); args.AddRange(times.Select(m => m.ToString(fmtTime))); var cli = Cli.Wrap(CliFile).WithArguments(args); var res = RunCli(cli, token); var result = EphResult.FromListString(res.StandardOutput); return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } public static ExeResult GetRange(string line1, string line2, DateTime startTime, DateTime endTime, int interval = 1, CancellationToken token = default) { const string CommandName = "range"; var cli = Cli.Wrap(CliFile) .WithArguments(new[] { CommandName, line1, line2, startTime.ToString(fmtTime), endTime.ToString(fmtTime), "-i", interval.ToString() }); var res = RunCli(cli, token); var result = EphResult.FromListString(res.StandardOutput); return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } } }