Device.cs 8.6 KB

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