using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml.Serialization; using Wit; using Wit.SDK; using Wit.SDK.Modular.Sensor.Utils; using Wit.SDK.Utils; namespace Wit.SDK.Modular.Sensor.Utils { /// /// ASCII协议解析 /// public class AsciiProtocolUtils { /// /// 通过AsciiKeyPattern解析字符串,解析后返回key的字典和值 /// /// 输入的待解析字符串 /// 输入前必须先解析好key和pattern /// public static Dictionary ResolvesAsciiByAsciiKeyPatterns(string input, List asciiKeyPatterns) { Dictionary dic = new Dictionary(); foreach (var asciiKeyPattern in asciiKeyPatterns) { dic = ResolvesAsciiByAsciiKeyPattern(input, asciiKeyPattern); if (dic.Keys.Count > 0) { break; } } return dic; } /// /// 通过AsciiKeyPattern解析字符串,解析后返回key的字典和值 /// /// /// 输入的待解析字符串 /// 输入前必须先解析好key和pattern /// public static Dictionary ResolvesAsciiByAsciiKeyPattern(string input, AsciiKeyPattern asciiKeyPattern) { Dictionary dic = new Dictionary(); string matchs = @asciiKeyPattern.pattern + "\r\n"; //string matchs = @asciiKeyPattern.pattern; input = input + "\r\n"; MatchCollection mc = Regex.Matches(input, matchs); if (mc.Count > 0) { GroupCollection groupCollection = mc[0].Groups; for (int i = 0; i < asciiKeyPattern.keys.Length && i + 1 < groupCollection.Count; i++) { dic[asciiKeyPattern.keys[i]] = groupCollection[i + 1].Value; } } return dic; } /// /// 得到带key的表达式得到它的key和正则表达式 /// /// /// 返回解析是否成功 public static bool GetAsciiKeyPatternKeyAndPattern(AsciiKeyPattern asciiKeyPattern) { //得到key List list = new List(); MatchCollection mc = Regex.Matches(asciiKeyPattern.keyPattern, @"\${(.*?)}"); foreach (Match m in mc) list.Add(m.Groups[1].Value); asciiKeyPattern.keys = list.ToArray(); //得到正则表达式 asciiKeyPattern.pattern = Regex.Replace(asciiKeyPattern.keyPattern, @"\${(.*?)}", "(.*?)"); return true; } } /// /// Ascii协议解析表达式 /// public class AsciiKeyPattern { public AsciiKeyPattern(string keyPattern) { this.keyPattern = keyPattern; } public AsciiKeyPattern() { } [XmlIgnore] private int _sort = 0; public int sort { get => _sort; set { _sort = value; } } [XmlIgnore] private string _keyPattern = ""; public string keyPattern { get => _keyPattern; set { _keyPattern = value; if (value != null) { AsciiProtocolUtils.GetAsciiKeyPatternKeyAndPattern(this); } } } [XmlIgnore] public string[] _keys = new string[0]; public string[] keys { get => _keys; set { _keys = value; } } [XmlIgnore] public string _pattern = ""; public string pattern { get => _pattern; set { _pattern = value; } } } }