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 EphTleUtil { const string fmtTime = "yyyy-MM-dd HH:mm:ss"; static readonly string CliFile = PathUtil.GetAssemblyPath("ipscli", "ephtle", "ephtle.exe"); static readonly object obj = new object(); public static async Task> Get(string line1, string line2, DateTime time, CancellationToken token = default) { var cli = Cli.Wrap(CliFile) .WithArguments(new[] { line1, line2, time.ToString(fmtTime) }); var res = await RunEphCliAsync(cli, token); var result = new EphResult(res.StandardOutput); return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } public static async Task> 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 = await RunEphCliAsync(cli, token); var result = EphResult.FromListString(res.StandardOutput); return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } public static async Task> 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 =await RunEphCliAsync(cli, token); var result = EphResult.FromListString(res.StandardOutput); return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError); } private static async Task RunEphCliAsync(Command cli, CancellationToken token) { try { Monitor.Enter(obj); return await cli.ExecuteBufferedAsync(token); } finally { Monitor.Exit(obj); } } } }