ProtocolProxyBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Threading.Tasks;
  8. using WCS.Core.DbHelper;
  9. using WCS.Core.IL;
  10. using WCS.Entity;
  11. namespace WCS.Core.DataTrans
  12. {
  13. /// <summary>
  14. /// IL动态生成的类型所继承的基类
  15. /// </summary>
  16. public abstract class ProtocolProxyBase : IProtocolProxy
  17. {
  18. protected Type ProtocolType, ProtocolDataType;
  19. public static Func<WCS_PROTOCOLDATA, string, WCS_PROTOCOLDATA> DataChanged;
  20. private readonly Dictionary<string, PlcItem> _items = new Dictionary<string, PlcItem>();
  21. public string Id { get; set; }
  22. public WCS_DEVICEPROTOCOL Protocol { get; private set; }
  23. /// <summary>
  24. /// 起始便宜量,按字节算
  25. /// </summary>
  26. public readonly ushort _start;
  27. /// <summary>
  28. /// 长度,按字节算
  29. /// </summary>
  30. public readonly ushort _length;
  31. public WCS_DATABLOCK Db;
  32. private static readonly Dictionary<Type, object> LockObjs = new Dictionary<Type, object>();
  33. protected ProtocolProxyBase(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL deviceItem)
  34. {
  35. var type = this.GetType();
  36. if (!LockObjs.ContainsKey(type))
  37. LockObjs[type] = new object();
  38. this.Db = db;
  39. db.Ex().DataChanged += PLCData_DataChanged;
  40. this._start = start;
  41. var bitStart = start * 8;//偏移量,按位算
  42. this.Id = id;
  43. this.Protocol = deviceItem;
  44. var lst = this.GetType().GetInterfaces().ToList();
  45. var ps = lst.SelectMany(v => v.GetProperties()).ToArray();
  46. foreach (var p in ps)
  47. {
  48. if (p.GetIndexParameters().Length > 0)
  49. continue;
  50. if (typeof(ProtocolProxyBase).GetProperty(p.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null)
  51. {
  52. continue;
  53. }
  54. #region 计算偏移量
  55. var modeNum = 0;
  56. var t = p.PropertyType;
  57. if (t.IsEnum)
  58. t = t.GetEnumUnderlyingType();
  59. if (t == typeof(bool))//bit类型,db块中无须补齐16位
  60. {
  61. modeNum = 1;
  62. }
  63. else if (t.IsPrimitive && Marshal.SizeOf(t) == 1)//byte char 等单字节类型,db块中无须补齐16位
  64. {
  65. modeNum = 8;
  66. }
  67. else//其他类型,db块中必须补齐16位
  68. {
  69. modeNum = 16;
  70. }
  71. var mod = bitStart % modeNum;
  72. if (mod > 0)
  73. {
  74. bitStart += modeNum - mod;
  75. }
  76. #endregion 计算偏移量
  77. byte arrlen = 0;
  78. byte strLen = 0;
  79. if (t.IsArray)
  80. {
  81. var attr = p.GetCustomAttributes(typeof(MaxLengthAttribute), true).FirstOrDefault() as MaxLengthAttribute;
  82. if (attr != null)
  83. arrlen = (byte)attr.Length;
  84. }
  85. else if (p.PropertyType == typeof(string))
  86. {
  87. var attr = p.GetCustomAttributes(typeof(StringLengthAttribute), true).FirstOrDefault() as StringLengthAttribute;
  88. strLen = (byte)attr.MaximumLength;
  89. }
  90. var m = typeof(DataBlock).GetMethod("Regist");
  91. m = m.MakeGenericMethod(p.PropertyType);
  92. var item = m.Invoke(db.Ex(), new object[] { this.Id, p.Name, bitStart, arrlen, strLen }) as PlcItem;
  93. _items.Add(p.Name, item);
  94. bitStart += item.DataSizeOfBits;
  95. _length += item.DataSize;
  96. }
  97. ProtocolType = this.Protocol.DB.GetProtocolType();
  98. ProtocolDataType = ProtocolType.Assembly.GetTypes().Where(v => v.IsClass).First(v => v.GetInterface(ProtocolType.Name) != null && v != this.GetType());
  99. }
  100. private byte[] Data = null;
  101. private static object _locobj = new object();
  102. private void InvokeUpdate(string user, Db db)
  103. {
  104. //TODO:怀疑会有问题,需要再看一下
  105. Task.Run(() =>
  106. {
  107. SaveChangs(user, db);
  108. });
  109. }
  110. private WCS_PROTOCOLDATA _last = null;
  111. public DateTime UpdateTime;
  112. protected virtual void SaveChangs(string user, Db db)
  113. {
  114. var obj = Copy(this, ProtocolDataType) as WCS_PROTOCOLDATA;
  115. obj!.WCSVERSION = this.WcsVersion;
  116. if (_last == null)
  117. {
  118. Publish(this.Protocol.DEVICE.CODE, obj);
  119. _last = GetLastData(db);
  120. }
  121. if (_last != null)
  122. {
  123. if (Equals(obj, _last, ProtocolType))
  124. return;
  125. }
  126. this.UpdateTime = DateTime.Now;
  127. Publish(this.Protocol.DEVICE.CODE, obj);
  128. _last = SaveNewData(db, _last, obj, user);
  129. }
  130. public virtual void Publish(string code, WCS_PROTOCOLDATA obj)
  131. {
  132. }
  133. protected abstract WCS_PROTOCOLDATA GetLastData(Db db);
  134. protected abstract WCS_PROTOCOLDATA SaveNewData(Db db, WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj, string user);
  135. private void PLCData_DataChanged(Db db, byte[] data)
  136. {
  137. var temp = data.Skip(_start).Take(_length).ToArray();
  138. try
  139. {
  140. if (Data != null && Data.SequenceEqual(temp))
  141. return;
  142. var d1 = DateTime.Now;
  143. InvokeUpdate("PLC", db);
  144. var d = (DateTime.Now - d1).TotalMilliseconds;
  145. }
  146. finally { Data = temp; }
  147. }
  148. private static object Copy(object obj, Type type)
  149. {
  150. var res = Activator.CreateInstance(type);
  151. foreach (var p in type.GetProperties())
  152. {
  153. var p2 = obj.GetType().GetProperty(p.Name);
  154. if (p2 != null && p2.PropertyType == p.PropertyType)
  155. {
  156. p.SetValue(res, p2.GetValue(obj));
  157. }
  158. }
  159. return res;
  160. }
  161. private static bool Equals(object a, object b, Type type)
  162. {
  163. foreach (var p in type.GetProperties())
  164. {
  165. var v1 = p.GetValue(a);
  166. var v2 = p.GetValue(b);
  167. var attr = p.GetCustomAttribute<IgnoreChangingAttribute>();
  168. if (attr != null)
  169. {
  170. if (attr.IgnoreValueRange == 0)
  171. continue;
  172. try
  173. {
  174. var d = Math.Abs((decimal.Parse(v1.ToString())) - (decimal.Parse(v2.ToString())));
  175. if (d < attr.IgnoreValueRange)
  176. continue;
  177. }
  178. catch
  179. {
  180. Console.WriteLine("IgnoreValueRange属性只能用于数字类型");
  181. }
  182. }
  183. if (p.PropertyType == typeof(string))
  184. {
  185. v1 = v1 == null ? "" : v1.ToString();
  186. v2 = v2 == null ? "" : v2.ToString();
  187. }
  188. if (p.PropertyType.IsArray)
  189. {
  190. var m = typeof(Enumerable).GetMethods().Where(v => v.Name == nameof(Enumerable.SequenceEqual)).First(v => v.GetParameters().Length == 2);
  191. m = m.MakeGenericMethod(p.PropertyType.GetElementType()!);
  192. var res = (bool)m.Invoke(null, new[] { v1, v2 })!;
  193. if (!res)
  194. return false;
  195. }
  196. else
  197. {
  198. if (!v1!.Equals(v2))
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. #region IGenerator
  205. /// <summary>
  206. /// 索引器Get
  207. /// </summary>
  208. /// <typeparam name="T"></typeparam>
  209. /// <param name="args"></param>
  210. /// <returns></returns>
  211. protected T GetItem<T>(params object[] args)
  212. {
  213. Console.WriteLine("GetItem:" + string.Join(",", args.Select(v => v.ToString())));
  214. return default(T);
  215. }
  216. /// <summary>
  217. /// 索引器Set
  218. /// </summary>
  219. /// <param name="args"></param>
  220. /// <returns></returns>
  221. protected void SetItem(params object[] args)
  222. {
  223. Console.WriteLine("SetItem:" + ":" + string.Join(",", args.Select(v => v.ToString())));
  224. }
  225. /// <summary>
  226. /// 版本号,WCS每修改一次数据,版本号变化一次
  227. /// </summary>
  228. public int WcsVersion { get; private set; }
  229. public void Set<T>(string propertyName, T value)
  230. {
  231. var str = Protocol.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
  232. try
  233. {
  234. str += " = " + (value == null ? "null" : value.ToString());
  235. var item = _items[propertyName] as PlcItem<T>;
  236. item.Value = value;
  237. DbHelper.Db.Do(db =>
  238. {
  239. InvokeUpdate("WCS", db);
  240. });
  241. Data = null;
  242. WcsVersion++;
  243. str += " 写入成功";
  244. }
  245. catch
  246. {
  247. str += " 写入失败";
  248. throw;
  249. }
  250. finally
  251. {
  252. Ltc.Log(str);
  253. }
  254. }
  255. public void Update(string prop, object value, string user)
  256. {
  257. var item = _items[prop];
  258. item.Value = value;
  259. DbHelper.Db.Do(db =>
  260. {
  261. InvokeUpdate(user, db);
  262. });
  263. Data = null;
  264. if (user == "PLC")
  265. return;
  266. WcsVersion++;
  267. }
  268. public T Get<T>(string propertyName)
  269. {
  270. var str = Protocol.DEVICE.CODE + "." + ProtocolType.Name + "." + propertyName;
  271. try
  272. {
  273. var item = _items[propertyName] as PlcItem<T>;
  274. var res = item.Value;
  275. str += " : " + (res == null ? "null" : res.ToString());
  276. return res;
  277. }
  278. catch
  279. {
  280. str += " 读取失败";
  281. throw;
  282. }
  283. }
  284. public T CallReturn<T>(string methodName, params object[] args)
  285. {
  286. Console.WriteLine(methodName + ":" + string.Join(",", args.Select(v => v.ToString())));
  287. return (T)(object)1.321f;
  288. }
  289. public void CallVoid(string methodName, params object[] args)
  290. {
  291. Console.WriteLine(methodName + ":" + string.Join(",", args.Select(v => v.ToString())));
  292. }
  293. public void AddEvent(string eventName, Delegate delgate)
  294. {
  295. Console.WriteLine("Add:" + eventName);
  296. }
  297. public void RemoveEvent(string eventName, Delegate delgate)
  298. {
  299. Console.WriteLine("Remove:" + eventName);
  300. }
  301. #endregion IGenerator
  302. public override string ToString()
  303. {
  304. var str = this.GetType().GetProperties().Aggregate("", (current, p) => current + (p.Name + ":" + p.GetValue(this, null)!.ToString() + ","));
  305. str = str[..^1];
  306. return str;
  307. }
  308. }
  309. }