EphTleUtil2.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using CliWrap;
  2. using CliWrap.Buffered;
  3. using Ips.Library.Basic;
  4. using Ips.Library.Entity;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Ips.EphAlgorithm
  11. {
  12. public class EphTleUtil2
  13. {
  14. const string fmtTime = "yyyy-MM-dd HH:mm:ss";
  15. static readonly string CliDir = PathUtil.GetBasePath("ipscli", "ephtle");
  16. static string CliFile
  17. {
  18. get
  19. {
  20. string fullName = Path.Combine(CliDir, "ephtle.exe");
  21. if (!File.Exists(fullName))
  22. {
  23. string newFile = Directory.EnumerateFiles(CliDir, "ephtle*.exe").FirstOrDefault();
  24. if (newFile != null) fullName = newFile;
  25. }
  26. return fullName;
  27. }
  28. }
  29. private static object obj = new object();
  30. private static BufferedCommandResult RunCli(Command cli, CancellationToken token)
  31. {
  32. lock (obj)
  33. {
  34. return cli.ExecuteBufferedAsync(token).GetAwaiter().GetResult();
  35. }
  36. }
  37. public static ExeResult<EphResult> Get(string line1, string line2, DateTime time, CancellationToken token = default)
  38. {
  39. var cli = Cli.Wrap(CliFile)
  40. .WithArguments(new[] { line1, line2, time.ToString(fmtTime) });
  41. var res = RunCli(cli, token);
  42. var result = EphResult.FromString(res.StandardOutput);
  43. result.Time = time;
  44. return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError);
  45. }
  46. public static ExeResult<EphResult[]> GetList(string line1, string line2, DateTime[] times, CancellationToken token = default)
  47. {
  48. const string CommandName = "list";
  49. List<string> args = new List<string>();
  50. args.AddRange(new[] { CommandName, line1, line2 });
  51. args.AddRange(times.Select(m => m.ToString(fmtTime)));
  52. var cli = Cli.Wrap(CliFile).WithArguments(args);
  53. var res = RunCli(cli, token);
  54. var result = EphResult.FromListString(res.StandardOutput);
  55. return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError);
  56. }
  57. public static ExeResult<EphResult[]> GetRange(string line1, string line2, DateTime startTime, DateTime endTime, int interval = 1, CancellationToken token = default)
  58. {
  59. const string CommandName = "range";
  60. var cli = Cli.Wrap(CliFile)
  61. .WithArguments(new[] { CommandName, line1, line2, startTime.ToString(fmtTime), endTime.ToString(fmtTime), "-i", interval.ToString() });
  62. var res = RunCli(cli, token);
  63. var result = EphResult.FromListString(res.StandardOutput);
  64. return ExeResult.Create(result, cli.Arguments, res.StartTime, res.ExitTime, res.ExitCode, res.StandardError);
  65. }
  66. }
  67. }