Device.cs 8.1 KB

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