ProtocolProxyBase.cs 5.4 KB

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