Device.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. //object Copy(object obj, Type type)
  86. //{
  87. // var res = Activator.CreateInstance(type);
  88. // foreach (var p in type.GetProperties())
  89. // {
  90. // var p2 = obj.GetType().GetProperty(p.Name);
  91. // if (p2 != null && p2.PropertyType == p.PropertyType)
  92. // {
  93. // p.SetValue(res, p2.GetValue(obj));
  94. // }
  95. // }
  96. // return res;
  97. //}
  98. public override string ToString()
  99. {
  100. return Code;
  101. }
  102. [JsonIgnore]
  103. public IEnumerable<Device> Targets
  104. {
  105. get
  106. {
  107. return GetFlags("Target").Select(v => Device.all[v]).ToList();
  108. }
  109. }
  110. [JsonIgnore]
  111. public IEnumerable<Device> Sources
  112. {
  113. get
  114. {
  115. return Device.All.Where(v => v.GetFlags("Target").Contains(this.Code)).ToArray();
  116. }
  117. }
  118. /// <summary>
  119. /// 设备组
  120. /// </summary>
  121. [JsonIgnore]
  122. public IEnumerable<Device> DeviceGroup
  123. {
  124. get
  125. {
  126. return GetFlags("DeviceGroup").Select(v => Device.all[v]).ToList();
  127. }
  128. }
  129. /// <summary>
  130. /// 添加设备组信息
  131. /// </summary>
  132. /// <param name="codes"></param>
  133. /// <exception cref="Exception"></exception>
  134. public void AddDeviceGroup(params string[] codes)
  135. {
  136. foreach (var code in codes)
  137. {
  138. if (HasFlag("DeviceGroup", code))
  139. throw new Exception($"{this.Code}子设备:{code}");
  140. AddFlag("DeviceGroup", code);
  141. }
  142. }
  143. public void AddTarget(params string[] codes)
  144. {
  145. foreach (var code in codes)
  146. {
  147. if (HasFlag("Target", code))
  148. throw new Exception($"{this.Code}已经存在目的地:{code}");
  149. AddFlag("Target", code);
  150. }
  151. }
  152. [JsonIgnore]
  153. public Device? Parent
  154. {
  155. get
  156. {
  157. var code = GetFlag("Parent");
  158. if (string.IsNullOrEmpty(code))
  159. return null;
  160. return Device.Find(code);
  161. }
  162. set
  163. {
  164. SetFlag("Parent", value.Code);
  165. }
  166. }
  167. public Dictionary<string, List<string>> Flags { get; private set; } = new Dictionary<string, List<string>>();
  168. public void AddFlag(string key, string flag)
  169. {
  170. if (!Flags.ContainsKey(key))
  171. Flags[key] = new List<string>();
  172. Flags[key].Add(flag);
  173. }
  174. public void SetFlag(string key, string flag)
  175. {
  176. var list = new List<string>();
  177. list.Add(flag);
  178. Flags[key] = list;
  179. }
  180. public IEnumerable<string> GetFlags(string key)
  181. {
  182. if (!Flags.ContainsKey(key))
  183. return new List<string>();
  184. return Flags[key];
  185. }
  186. public string? GetFlag(string key)
  187. {
  188. return GetFlags(key).FirstOrDefault();
  189. }
  190. public bool HasFlag(string key, string flag)
  191. {
  192. if (!Flags.ContainsKey(key))
  193. return false;
  194. return Flags[key].Contains(flag);
  195. }
  196. public void AddFlag(string flag)
  197. {
  198. SetFlag("Globals", flag);
  199. }
  200. public bool HasFlag(string flag)
  201. {
  202. return HasFlag("Globals", flag);
  203. }
  204. public void AddFlag<T>(T flag) where T : struct, Enum
  205. {
  206. var key = typeof(T).AssemblyQualifiedName;
  207. if (Flags.ContainsKey(key))
  208. {
  209. dynamic value = Enum.Parse<T>(Flags[key].First());
  210. var str = (value | flag).ToString();
  211. SetFlag(key, str);
  212. }
  213. else
  214. {
  215. var str = flag.ToString();
  216. SetFlag(key, str);
  217. }
  218. }
  219. public bool HasFlag<T>(T flag) where T : struct, Enum
  220. {
  221. var key = typeof(T).AssemblyQualifiedName;
  222. if (!Flags.ContainsKey(key))
  223. return false;
  224. dynamic value = Enum.Parse<T>(Flags[key].First());
  225. return value.HasFlag(flag);
  226. }
  227. }
  228. public class Device<T> : EntityEx<Device>
  229. {
  230. public T Data { get; set; }
  231. public Device(Device device, World world) : base(device)
  232. {
  233. Data = Entity.Protocol<T>(world);
  234. }
  235. public override string ToString()
  236. {
  237. return Entity.Code;
  238. }
  239. }
  240. public class Device<T, T2> : Device<T>
  241. {
  242. public T2 Data2 { get; set; }
  243. public Device(Device device, World world) : base(device, world)
  244. {
  245. Data2 = Entity.Protocol<T2>(world);
  246. }
  247. }
  248. public class Device<T, T2, T3> : Device<T, T2>
  249. {
  250. public T3 Data3 { get; set; }
  251. public Device(Device device, World world) : base(device, world)
  252. {
  253. Data3 = Entity.Protocol<T3>(world);
  254. }
  255. }
  256. public class Device<T, T2, T3, T4> : Device<T, T2, T3>
  257. {
  258. public T4 Data4 { get; set; }
  259. public Device(Device device, World world) : base(device, world)
  260. {
  261. Data4 = Entity.Protocol<T4>(world);
  262. }
  263. }
  264. public class Device<T, T2, T3, T4, T5> : Device<T, T2, T3, T4>
  265. {
  266. public T5 Data5 { get; set; }
  267. public Device(Device device, World world) : base(device, world)
  268. {
  269. Data5 = Entity.Protocol<T5>(world);
  270. }
  271. }
  272. public class Device<T, T2, T3, T4, T5, T6> : Device<T, T2, T3, T4, T5>
  273. {
  274. public T6 Data6 { get; set; }
  275. public Device(Device device, World world) : base(device, world)
  276. {
  277. Data6 = Entity.Protocol<T6>(world);
  278. }
  279. }
  280. }