ProtocolProxyBase.cs 5.1 KB

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