| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Ips.Library.Entity
- {
- /// <summary>
- /// 时隙
- /// </summary>
- public class Timeslot
- {
- /// <summary>
- /// 名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 起始样点
- /// </summary>
- public int Start { get; set; }
- /// <summary>
- /// 信号长度
- /// </summary>
- public int Length { get; set; }
- public Timeslot() { }
- public Timeslot(string result)
- {
- FromString(result);
- }
- public void FromString(string result)
- {
- if (string.IsNullOrWhiteSpace(result)) return;
- var items = result.Split(',');
- if (items.Length == 3
- && int.TryParse(items[1], out int start)
- && int.TryParse(items[2], out int length))
- {
- Name = items[0];
- Start = start;
- Length = length;
- }
- else
- {
- throw new ArgumentException(result, nameof(result));
- }
- }
- public static Timeslot[] FromListString(string result, string itemsep = "\r\n")
- {
- if (string.IsNullOrWhiteSpace(result))
- return Array.Empty<Timeslot>();
- var items = result.Split(itemsep.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- Timeslot[] results = new Timeslot[items.Length];
- for (int i = 0; i < items.Length; i++)
- {
- results[i] = new Timeslot(items[i]);
- }
- return results;
- }
- public override string ToString()
- {
- return $"{Name},{Start},{Length}";
- }
- }
- public static class TimeslotExtensions
- {
- public static int[] ToTimeslotArray(this IEnumerable<Timeslot> timeslots)
- {
- List<int> results = new List<int>();
- foreach(var ts in timeslots)
- {
- results.Add(ts.Start);
- results.Add(ts.Length);
- }
- return results.ToArray();
- }
- }
- }
|