Timeslot.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Ips.Library.Entity
  5. {
  6. /// <summary>
  7. /// 时隙
  8. /// </summary>
  9. public class Timeslot
  10. {
  11. /// <summary>
  12. /// 名称
  13. /// </summary>
  14. public string Name { get; set; }
  15. /// <summary>
  16. /// 起始样点
  17. /// </summary>
  18. public int Start { get; set; }
  19. /// <summary>
  20. /// 信号长度
  21. /// </summary>
  22. public int Length { get; set; }
  23. public Timeslot() { }
  24. public Timeslot(string result)
  25. {
  26. FromString(result);
  27. }
  28. public void FromString(string result)
  29. {
  30. if (string.IsNullOrWhiteSpace(result)) return;
  31. var items = result.Split(',');
  32. if (items.Length == 3
  33. && int.TryParse(items[1], out int start)
  34. && int.TryParse(items[2], out int length))
  35. {
  36. Name = items[0];
  37. Start = start;
  38. Length = length;
  39. }
  40. else
  41. {
  42. throw new ArgumentException(result, nameof(result));
  43. }
  44. }
  45. public static Timeslot[] FromListString(string result, string itemsep = "\r\n")
  46. {
  47. if (string.IsNullOrWhiteSpace(result))
  48. return Array.Empty<Timeslot>();
  49. var items = result.Split(itemsep.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  50. Timeslot[] results = new Timeslot[items.Length];
  51. for (int i = 0; i < items.Length; i++)
  52. {
  53. results[i] = new Timeslot(items[i]);
  54. }
  55. return results;
  56. }
  57. public override string ToString()
  58. {
  59. return $"{Name},{Start},{Length}";
  60. }
  61. }
  62. public static class TimeslotExtensions
  63. {
  64. public static int[] ToTimeslotArray(this IEnumerable<Timeslot> timeslots)
  65. {
  66. List<int> results = new List<int>();
  67. foreach(var ts in timeslots)
  68. {
  69. results.Add(ts.Start);
  70. results.Add(ts.Length);
  71. }
  72. return results.ToArray();
  73. }
  74. }
  75. }