using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.Tracing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WCS.Core { public class Protocols { protected static ConcurrentDictionary> ItemOfType = new ConcurrentDictionary>(); public static bool HasProtocol(Type type, string code) { return ItemOfType[type].ContainsKey(code); } public static ProtocolInfo Get(Type type, string code) { ItemOfType[type].TryGetValue(code, out var info); return info; } public static void Add(Type protocolType, string code, ProtocolInfo info) { if (!ItemOfType.TryGetValue(protocolType, out var item)) { item = new ConcurrentDictionary(); ItemOfType[protocolType] = item; } if (!item.TryAdd(code, info)) throw new Exception($"Protocols {protocolType} 添加了重复的设备号"); } /// /// 实例化Device,并未实例化Protocol /// /// /// public static List Generate(World world) { List list = new List(); var gs = ItemOfType.SelectMany(v => v.Value.Select(d => new { code = d.Key, proto = d.Value, type = v.Key })).GroupBy(v=>v.code).ToList(); foreach (var g in gs) { var dev = new Device { Code = g.Key, World = world }; list.Add(dev); } return list; } } public class Protocols : Protocols { public static void Add(string code, ProtocolInfo info) { Protocols.Add(typeof(T), code, info); } public static ProtocolInfo Get(string code) { return Get(typeof(T), code); } } }