Device.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. public 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 Device AddFlag(string key, string flag)
  156. {
  157. if (!Flags.ContainsKey(key))
  158. Flags[key] = new List<string>();
  159. Flags[key].Add(flag);
  160. return this;
  161. }
  162. public void SetFlag(string flag)
  163. {
  164. SetFlag("Globals", flag);
  165. }
  166. public void SetFlag(string key, string flag)
  167. {
  168. var list = new List<string>();
  169. list.Add(flag);
  170. Flags[key] = list;
  171. }
  172. public IEnumerable<string> GetFlags(string key)
  173. {
  174. if (!Flags.ContainsKey(key))
  175. return new List<string>();
  176. return Flags[key];
  177. }
  178. public string? GetFlag(string key)
  179. {
  180. return GetFlags(key).FirstOrDefault();
  181. }
  182. public bool HasFlag(string key, string flag)
  183. {
  184. if (!Flags.ContainsKey(key))
  185. return false;
  186. return Flags[key].Contains(flag);
  187. }
  188. public bool HasFlag(string flag)
  189. {
  190. return HasFlag("Globals", flag);
  191. }
  192. public Device AddFlag<T>(T flag) where T : struct, Enum
  193. {
  194. var key = typeof(T).AssemblyQualifiedName;
  195. if (Flags.ContainsKey(key))
  196. {
  197. dynamic value = Enum.Parse<T>(Flags[key].First());
  198. var str = (value | flag).ToString();
  199. SetFlag(key, str);
  200. }
  201. else
  202. {
  203. var str = flag.ToString();
  204. SetFlag(key, str);
  205. }
  206. return this;
  207. }
  208. public bool HasFlag<T>(T flag) where T : struct, Enum
  209. {
  210. var key = typeof(T).AssemblyQualifiedName;
  211. if (!Flags.ContainsKey(key))
  212. return false;
  213. dynamic value = Enum.Parse<T>(Flags[key].First());
  214. return value.HasFlag(flag);
  215. }
  216. }
  217. public class Device<T> : EntityEx<Device>
  218. {
  219. public World World { get; private set; }
  220. public T Data { get; set; }
  221. public Device(Device device, World world) : base(device)
  222. {
  223. this.World = world;
  224. Data = Entity.Protocol<T>(world);
  225. }
  226. public override string ToString()
  227. {
  228. return Entity.Code;
  229. }
  230. }
  231. public class Device<T, T2> : Device<T>
  232. {
  233. public T2 Data2 { get; set; }
  234. public Device(Device device, World world) : base(device, world)
  235. {
  236. Data2 = Entity.Protocol<T2>(world);
  237. }
  238. }
  239. public class Device<T, T2, T3> : Device<T, T2>
  240. {
  241. public T3 Data3 { get; set; }
  242. public Device(Device device, World world) : base(device, world)
  243. {
  244. Data3 = Entity.Protocol<T3>(world);
  245. }
  246. }
  247. public class Device<T, T2, T3, T4> : Device<T, T2, T3>
  248. {
  249. public T4 Data4 { get; set; }
  250. public Device(Device device, World world) : base(device, world)
  251. {
  252. Data4 = Entity.Protocol<T4>(world);
  253. }
  254. }
  255. public class Device<T, T2, T3, T4, T5> : Device<T, T2, T3, T4>
  256. {
  257. public T5 Data5 { get; set; }
  258. public Device(Device device, World world) : base(device, world)
  259. {
  260. Data5 = Entity.Protocol<T5>(world);
  261. }
  262. }
  263. public class Device<T, T2, T3, T4, T5, T6> : Device<T, T2, T3, T4, T5>
  264. {
  265. public T6 Data6 { get; set; }
  266. public Device(Device device, World world) : base(device, world)
  267. {
  268. Data6 = Entity.Protocol<T6>(world);
  269. }
  270. }
  271. }