12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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<Type, ConcurrentDictionary<string, ProtocolInfo>> ItemOfType = new ConcurrentDictionary<Type, ConcurrentDictionary<string, ProtocolInfo>>();
- 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;
- }
- /// <summary>
- /// 实例化Device,并未实例化Protocol
- /// </summary>
- /// <param name="world"></param>
- /// <returns></returns>
- public static List<Device> Generate(World world)
- {
- List<Device> list = new List<Device>();
- 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 };
- //foreach (var i in g)
- //{
- // //dev.Protocol(i.type);
- //}
- list.Add(dev);
- }
- return list;
- }
- }
- public class Protocols<T> : Protocols
- {
- static ConcurrentDictionary<string, ProtocolInfo> Items
- {
- get
- {
- if (!ItemOfType.TryGetValue(typeof(T), out var item))
- {
- item = new ConcurrentDictionary<string, ProtocolInfo>();
- ItemOfType[typeof(T)] = item;
- }
- return item;
- }
- }
- public static void Add(string code, ProtocolInfo info)
- {
- if (!Items.TryAdd(code, info))
- throw new Exception($"Protocols<{typeof(T)}>.Add()添加了重复的设备号");
- }
- public static ProtocolInfo Get(string code)
- {
- return Get(typeof(T), code);
- }
- }
- }
|