AsciiProtocolUtils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Xml.Serialization;
  8. using Wit;
  9. using Wit.SDK;
  10. using Wit.SDK.Modular.Sensor.Utils;
  11. using Wit.SDK.Utils;
  12. namespace Wit.SDK.Modular.Sensor.Utils
  13. {
  14. /// <summary>
  15. /// ASCII协议解析
  16. /// </summary>
  17. public class AsciiProtocolUtils
  18. {
  19. /// <summary>
  20. /// 通过AsciiKeyPattern解析字符串,解析后返回key的字典和值
  21. /// </summary>
  22. /// <param name="input">输入的待解析字符串</param>
  23. /// <param name="asciiKeyPatterns">输入前必须先解析好key和pattern</param>
  24. /// <returns></returns>
  25. public static Dictionary<string, string> ResolvesAsciiByAsciiKeyPatterns(string input, List<AsciiKeyPattern> asciiKeyPatterns)
  26. {
  27. Dictionary<string, string> dic = new Dictionary<string, string>();
  28. foreach (var asciiKeyPattern in asciiKeyPatterns)
  29. {
  30. dic = ResolvesAsciiByAsciiKeyPattern(input, asciiKeyPattern);
  31. if (dic.Keys.Count > 0)
  32. {
  33. break;
  34. }
  35. }
  36. return dic;
  37. }
  38. /// <summary>
  39. /// 通过AsciiKeyPattern解析字符串,解析后返回key的字典和值
  40. ///
  41. /// </summary>
  42. /// <param name="input">输入的待解析字符串</param>
  43. /// <param name="asciiKeyPattern">输入前必须先解析好key和pattern</param>
  44. /// <returns></returns>
  45. public static Dictionary<string, string> ResolvesAsciiByAsciiKeyPattern(string input, AsciiKeyPattern asciiKeyPattern)
  46. {
  47. Dictionary<string, string> dic = new Dictionary<string, string>();
  48. string matchs = @asciiKeyPattern.pattern + "\r\n";
  49. //string matchs = @asciiKeyPattern.pattern;
  50. input = input + "\r\n";
  51. MatchCollection mc = Regex.Matches(input, matchs);
  52. if (mc.Count > 0)
  53. {
  54. GroupCollection groupCollection = mc[0].Groups;
  55. for (int i = 0; i < asciiKeyPattern.keys.Length && i + 1 < groupCollection.Count; i++)
  56. {
  57. dic[asciiKeyPattern.keys[i]] = groupCollection[i + 1].Value;
  58. }
  59. }
  60. return dic;
  61. }
  62. /// <summary>
  63. /// 得到带key的表达式得到它的key和正则表达式
  64. /// </summary>
  65. /// <param name="asciiKeyMatche"></param>
  66. /// <returns>返回解析是否成功</returns>
  67. public static bool GetAsciiKeyPatternKeyAndPattern(AsciiKeyPattern asciiKeyPattern)
  68. {
  69. //得到key
  70. List<string> list = new List<string>();
  71. MatchCollection mc = Regex.Matches(asciiKeyPattern.keyPattern, @"\${(.*?)}");
  72. foreach (Match m in mc)
  73. list.Add(m.Groups[1].Value);
  74. asciiKeyPattern.keys = list.ToArray();
  75. //得到正则表达式
  76. asciiKeyPattern.pattern = Regex.Replace(asciiKeyPattern.keyPattern, @"\${(.*?)}", "(.*?)");
  77. return true;
  78. }
  79. }
  80. /// <summary>
  81. /// Ascii协议解析表达式
  82. /// </summary>
  83. public class AsciiKeyPattern
  84. {
  85. public AsciiKeyPattern(string keyPattern)
  86. {
  87. this.keyPattern = keyPattern;
  88. }
  89. public AsciiKeyPattern()
  90. {
  91. }
  92. [XmlIgnore]
  93. private int _sort = 0;
  94. public int sort
  95. {
  96. get => _sort;
  97. set
  98. {
  99. _sort = value;
  100. }
  101. }
  102. [XmlIgnore]
  103. private string _keyPattern = "";
  104. public string keyPattern
  105. {
  106. get => _keyPattern;
  107. set
  108. {
  109. _keyPattern = value;
  110. if (value != null)
  111. {
  112. AsciiProtocolUtils.GetAsciiKeyPatternKeyAndPattern(this);
  113. }
  114. }
  115. }
  116. [XmlIgnore]
  117. public string[] _keys = new string[0];
  118. public string[] keys
  119. {
  120. get => _keys;
  121. set
  122. {
  123. _keys = value;
  124. }
  125. }
  126. [XmlIgnore]
  127. public string _pattern = "";
  128. public string pattern
  129. {
  130. get => _pattern;
  131. set
  132. {
  133. _pattern = value;
  134. }
  135. }
  136. }
  137. }