1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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;
- }
- public static void Add(Type protocolType, string code, ProtocolInfo info)
- {
- if (!ItemOfType.TryGetValue(protocolType, out var item))
- {
- item = new ConcurrentDictionary<string, ProtocolInfo>();
- ItemOfType[protocolType] = item;
- }
- if (!item.TryAdd(code, info))
- throw new Exception($"Protocols {protocolType} 添加了重复的设备号");
- }
- /// <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 };
- list.Add(dev);
- }
- return list;
- }
- }
- public class Protocols<T> : 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);
- }
- }
- }
|