ProtocolProxyBase.cs 11 KB

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