ProtocolProxyBase.cs 4.8 KB

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