Device.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System.Collections.Concurrent;
  2. using System.Reflection;
  3. using System.Text.Json.Serialization;
  4. namespace WCS.Core;
  5. public class EntityEx<T>
  6. {
  7. public EntityEx(T ent)
  8. {
  9. Entity = ent;
  10. }
  11. public T Entity { get; }
  12. public override string ToString()
  13. {
  14. return Entity.ToString();
  15. }
  16. }
  17. public class Device
  18. {
  19. private static readonly ConcurrentDictionary<string, Device> all = new();
  20. private readonly ConcurrentDictionary<World, ConcurrentDictionary<Type, object>> ProtocolObjsOfWorld = new();
  21. public Device(string code)
  22. {
  23. if (all.ContainsKey(code))
  24. throw new Exception($"{code}设备号重复");
  25. Code = code;
  26. all[code] = this;
  27. }
  28. public static IEnumerable<Device> All => all.Values;
  29. public string Code { get; set; } = "";
  30. public Dictionary<string, ProtocolInfo> Protocols { get; } = new();
  31. [JsonIgnore]
  32. public IEnumerable<Device> Targets
  33. {
  34. get { return GetFlags("Target").Select(v => all[v]).ToList(); }
  35. }
  36. [JsonIgnore]
  37. public IEnumerable<Device> Sources
  38. {
  39. get { return All.Where(v => v.GetFlags("Target").Contains(Code)).ToArray(); }
  40. }
  41. /// <summary>
  42. /// 设备组
  43. /// </summary>
  44. [JsonIgnore]
  45. public IEnumerable<Device> DeviceGroup
  46. {
  47. get { return GetFlags("DeviceGroup").Select(v => all[v]).ToList(); }
  48. }
  49. [JsonIgnore]
  50. public Device? Parent
  51. {
  52. get
  53. {
  54. var code = GetFlag("Parent");
  55. if (string.IsNullOrEmpty(code))
  56. return null;
  57. return Find(code);
  58. }
  59. set => SetFlag("Parent", value.Code);
  60. }
  61. public Dictionary<string, List<string>> Flags { get; } = new();
  62. public static Device Find(string code)
  63. {
  64. if (all.TryGetValue(code, out var dev))
  65. return dev;
  66. throw new Exception($"未找到设备{code}");
  67. }
  68. public ConcurrentDictionary<Type, object> ProtocolObjs(World world)
  69. {
  70. if (!ProtocolObjsOfWorld.TryGetValue(world, out var pobjs))
  71. {
  72. pobjs = new ConcurrentDictionary<Type, object>();
  73. ProtocolObjsOfWorld[world] = pobjs;
  74. }
  75. return pobjs;
  76. }
  77. public void AddProtocol(Type type, ProtocolInfo info)
  78. {
  79. if (!Protocols.TryAdd(type.AssemblyQualifiedName, info))
  80. throw new Exception($"{Code}重复添加协议{type.Name}");
  81. }
  82. public void AddProtocol<T>(ProtocolInfo info)
  83. {
  84. AddProtocol(typeof(T), info);
  85. }
  86. public bool HasProtocol(Type type)
  87. {
  88. return Protocols.ContainsKey(type.AssemblyQualifiedName);
  89. }
  90. public bool HasProtocol<T>()
  91. {
  92. return HasProtocol(typeof(T));
  93. }
  94. public T Protocol<T>(World world)
  95. {
  96. return (T)Protocol(typeof(T), world);
  97. }
  98. public object Protocol(Type protocolType, World world)
  99. {
  100. if (!HasProtocol(protocolType))
  101. throw new Exception($"{Code}不包含协议:{protocolType.Name}");
  102. if (!ProtocolObjs(world).ContainsKey(protocolType))
  103. {
  104. var type = typeof(Generator<,>);
  105. type = type.MakeGenericType(protocolType, Configs.ProtocolProxyBaseType);
  106. var m = type.GetMethod("Create", BindingFlags.Public | BindingFlags.Static);
  107. var info = Protocols[protocolType.AssemblyQualifiedName];
  108. var obj = m.Invoke(null, new object[] { new object[] { this, info, protocolType, world } });
  109. ProtocolObjs(world)[protocolType] = obj;
  110. }
  111. return ProtocolObjs(world)[protocolType];
  112. }
  113. public override string ToString()
  114. {
  115. return Code;
  116. }
  117. /// <summary>
  118. /// 添加设备组信息
  119. /// </summary>
  120. /// <param name="codes"></param>
  121. /// <exception cref="Exception"></exception>
  122. public void AddDeviceGroup(params string[] codes)
  123. {
  124. foreach (var code in codes)
  125. {
  126. if (HasFlag("DeviceGroup", code))
  127. throw new Exception($"{Code}子设备:{code}");
  128. AddFlag("DeviceGroup", code);
  129. }
  130. }
  131. public void AddTarget(params string[] codes)
  132. {
  133. foreach (var code in codes)
  134. {
  135. if (HasFlag("Target", code))
  136. throw new Exception($"{Code}已经存在目的地:{code}");
  137. AddFlag("Target", code);
  138. }
  139. }
  140. public Device AddFlag(string key, string flag)
  141. {
  142. if (!Flags.ContainsKey(key))
  143. Flags[key] = new List<string>();
  144. Flags[key].Add(flag);
  145. return this;
  146. }
  147. public void SetFlag(string flag)
  148. {
  149. SetFlag("Globals", flag);
  150. }
  151. public void SetFlag(string key, string flag)
  152. {
  153. var list = new List<string>();
  154. list.Add(flag);
  155. Flags[key] = list;
  156. }
  157. public IEnumerable<string> GetFlags(string key)
  158. {
  159. if (!Flags.ContainsKey(key))
  160. return new List<string>();
  161. return Flags[key];
  162. }
  163. public string? GetFlag(string key)
  164. {
  165. return GetFlags(key).FirstOrDefault();
  166. }
  167. public bool HasFlag(string key, string flag)
  168. {
  169. if (!Flags.ContainsKey(key))
  170. return false;
  171. return Flags[key].Contains(flag);
  172. }
  173. public bool HasFlag(string flag)
  174. {
  175. return HasFlag("Globals", flag);
  176. }
  177. public Device AddFlag<T>(T flag) where T : struct, Enum
  178. {
  179. var key = typeof(T).AssemblyQualifiedName;
  180. if (Flags.ContainsKey(key))
  181. {
  182. dynamic value = Enum.Parse<T>(Flags[key].First());
  183. var str = (value | flag).ToString();
  184. SetFlag(key, str);
  185. }
  186. else
  187. {
  188. var str = flag.ToString();
  189. SetFlag(key, str);
  190. }
  191. return this;
  192. }
  193. public bool HasFlag<T>(T flag) where T : struct, Enum
  194. {
  195. var key = typeof(T).AssemblyQualifiedName;
  196. if (!Flags.ContainsKey(key))
  197. return false;
  198. dynamic value = Enum.Parse<T>(Flags[key].First());
  199. return value.HasFlag(flag);
  200. }
  201. }
  202. public class Device<T> : EntityEx<Device>
  203. {
  204. public Device(Device device, World world) : base(device)
  205. {
  206. World = world;
  207. Data = Entity.Protocol<T>(world);
  208. }
  209. public World World { get; private set; }
  210. public T Data { get; set; }
  211. public override string ToString()
  212. {
  213. return Entity.Code;
  214. }
  215. }
  216. public class Device<T, T2> : Device<T>
  217. {
  218. public Device(Device device, World world) : base(device, world)
  219. {
  220. Data2 = Entity.Protocol<T2>(world);
  221. }
  222. public T2 Data2 { get; set; }
  223. }
  224. public class Device<T, T2, T3> : Device<T, T2>
  225. {
  226. public Device(Device device, World world) : base(device, world)
  227. {
  228. Data3 = Entity.Protocol<T3>(world);
  229. }
  230. public T3 Data3 { get; set; }
  231. }
  232. public class Device<T, T2, T3, T4> : Device<T, T2, T3>
  233. {
  234. public Device(Device device, World world) : base(device, world)
  235. {
  236. Data4 = Entity.Protocol<T4>(world);
  237. }
  238. public T4 Data4 { get; set; }
  239. }
  240. public class Device<T, T2, T3, T4, T5> : Device<T, T2, T3, T4>
  241. {
  242. public Device(Device device, World world) : base(device, world)
  243. {
  244. Data5 = Entity.Protocol<T5>(world);
  245. }
  246. public T5 Data5 { get; set; }
  247. }
  248. public class Device<T, T2, T3, T4, T5, T6> : Device<T, T2, T3, T4, T5>
  249. {
  250. public Device(Device device, World world) : base(device, world)
  251. {
  252. Data6 = Entity.Protocol<T6>(world);
  253. }
  254. public T6 Data6 { get; set; }
  255. }