ProtocolProxyBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using System.Net.Sockets;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace WCS.Core
  12. {
  13. public abstract class ProtocolProxyBase : IProtocolProxy
  14. {
  15. string Id = Guid.NewGuid().ToString();
  16. public ProtocolInfo Info { get; private set; }
  17. public ushort BytesCount { get; private set; }
  18. Dictionary<string, PlcItem> Items = new Dictionary<string, PlcItem>();
  19. Type ProtocolType, ProtocolDataType;
  20. public Device Device { get; private set; }
  21. DataBlock Db;
  22. public ProtocolProxyBase(Device dev, ProtocolInfo info,Type protocolType)
  23. {
  24. this.Device = dev;
  25. this.Info = info;
  26. Db = info.DBInfo.Ex(Device.World);
  27. Db.DbChanged += Db_DbChanged;
  28. ProtocolType = protocolType;
  29. var bitStart = info.Position * 8;//偏移量,按位算
  30. //this.Start = start;
  31. //PlcDB db = new PlcDB(null, 50, 100);
  32. var lst = this.GetType().GetInterfaces().ToList();
  33. var ps = lst.SelectMany(v => v.GetProperties()).ToArray();
  34. foreach (var p in ps)
  35. {
  36. if (p.GetIndexParameters().Length > 0)
  37. continue;
  38. if (typeof(ProtocolProxyBase).GetProperty(p.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null)
  39. {
  40. continue;
  41. }
  42. #region 计算偏移量
  43. var modeNum = 0;
  44. var t = p.PropertyType;
  45. if (t.IsEnum)
  46. t = t.GetEnumUnderlyingType();
  47. if (t == typeof(bool))//bit类型,db块中无须补齐16位
  48. {
  49. modeNum = 1;
  50. }
  51. else if (t.IsPrimitive && Marshal.SizeOf(t) == 1)//byte char 等单字节类型,db块中无须补齐16位
  52. {
  53. modeNum = 8;
  54. }
  55. else//其他类型,db块中必须补齐16位
  56. {
  57. modeNum = 16;
  58. }
  59. var mod = bitStart % modeNum;
  60. if (mod > 0)
  61. {
  62. bitStart += modeNum - mod;
  63. }
  64. #endregion
  65. byte arrlen = 0;
  66. byte strLen = 0;
  67. if (t.IsArray)
  68. {
  69. var attr = p.GetCustomAttributes(typeof(MaxLengthAttribute), true).FirstOrDefault() as MaxLengthAttribute;
  70. if (attr != null)
  71. arrlen = (byte)attr.Length;
  72. }
  73. else if (p.PropertyType == typeof(string))
  74. {
  75. var attr = p.GetCustomAttributes(typeof(StringLengthAttribute), true).FirstOrDefault() as StringLengthAttribute;
  76. strLen = (byte)attr.MaximumLength;
  77. }
  78. var m = typeof(DataBlock).GetMethod("Regist");
  79. m = m.MakeGenericMethod(p.PropertyType);
  80. var item = m.Invoke(Db, new object[] { this, Id, p.Name, bitStart, arrlen, strLen }) as PlcItem;
  81. Items.Add(p.Name, item);
  82. bitStart += item.DataSizeOfBits;
  83. BytesCount += item.DataSize;
  84. }
  85. //ProtocolType = this.PROTOCOL.DB.GetProtocolType();
  86. ProtocolDataType = ProtocolType.Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(ProtocolType.Name) != null && v != this.GetType()).First();
  87. }
  88. byte[] Data = new byte[0];
  89. private void Db_DbChanged(byte[] data)
  90. {
  91. var pos = Info.Position - Db.Start;
  92. var bytes = data.Skip(pos).Take(BytesCount).ToArray();
  93. if (!Data.SequenceEqual(bytes))
  94. {
  95. Data = bytes;
  96. DataChanged();
  97. }
  98. }
  99. protected abstract void DataChanged();
  100. #region
  101. public void AddEvent(string eventName, Delegate delgate)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public void RemoveEvent(string eventName, Delegate delgate)
  106. {
  107. throw new NotImplementedException();
  108. }
  109. public T CallReturn<T>(string methodName, params object[] args)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. public void CallVoid(string methodName, params object[] args)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. public T Get<T>(string propertyName)
  118. {
  119. var item = Items[propertyName] as PlcItem<T>;
  120. var res = item.Value;
  121. Ltc.Log($"{propertyName}:{res}", LogLevel.Low, ErrorType.Kown);
  122. return res;
  123. }
  124. public void Set<T>(string propertyName, T value)
  125. {
  126. var item = Items[propertyName] as PlcItem<T>;
  127. item.Value = value;
  128. Ltc.Log($"{propertyName}={value}", LogLevel.Low, ErrorType.Kown);
  129. }
  130. #endregion
  131. }
  132. }