ProtocolProxy.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using MessagePack;
  2. using Newtonsoft.Json;
  3. using ServiceCenter;
  4. using ServiceCenter.Redis;
  5. using WCS.Core;
  6. using WCS.Entity.Protocol.DataStructure;
  7. namespace WCS.Service
  8. {
  9. public class ProtocolProxy : ProtocolProxyBase
  10. {
  11. public static DeviceDataPack DataPack { get; set; } = new DeviceDataPack();
  12. public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType, World world) : base(dev, info, protocolType, world)
  13. {
  14. #region 初始化Redis连接
  15. var redisConnectionStrings = RedisHub.Default.Check("RedisConnectionStrings") ?? throw new Exception("请在Redis中配置RedisConnectionStrings");
  16. var configs = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);
  17. if (configs != null)
  18. {
  19. if (configs.All(v => v.Key != "Monitor")) throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");
  20. }
  21. foreach (var redisConnection in configs!)
  22. {
  23. RedisHub.CreateContext(redisConnection.ConnectionString, redisConnection.Key);
  24. switch (redisConnection.Key)
  25. {
  26. case "Monitor":
  27. RedisHub.SetMonitorContextType(redisConnection.Key);
  28. RedisHub.Monitor.Serialize = obj =>
  29. {
  30. var bytes = MessagePackSerializer.Serialize(obj);
  31. return bytes;
  32. };
  33. RedisHub.Monitor.DeserializeRaw = (bytes, type) =>
  34. {
  35. var obj = MessagePackSerializer.Deserialize(type, bytes);
  36. return obj;
  37. };
  38. break;
  39. case "DebugRedisUrl":
  40. Configs.DebugRedisUrl = redisConnection.ConnectionString;
  41. break;
  42. }
  43. }
  44. #endregion 初始化Redis连接
  45. }
  46. protected override void DataChanged()
  47. {
  48. //if (Device.HasProtocol(typeof(IStation520)))
  49. //{
  50. // if (DataPack.StationDatas == null)
  51. // {
  52. // DataPack.StationDatas = new DeviceDataCollection<StationData>();
  53. // }
  54. // DataPack.StationDatas.Datas.Append(new StationData()
  55. // {
  56. // Code = Device.Code,
  57. // D520 = Copy(this, typeof(IStation520).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(IStation520).Name) != null && v != this.GetType()).First()) as WCS_Station520,
  58. // D521 = Copy(this, typeof(IStation521).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(IStation521).Name) != null && v != this.GetType()).First()) as WCS_Station521,
  59. // D523 = Copy(this, typeof(IStation523).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(IStation523).Name) != null && v != this.GetType()).First()) as WCS_Station523,
  60. // }); ;
  61. // DataPack.StationDatas.Frame = DateTime.Now;
  62. //}
  63. //else if (Device.HasProtocol(typeof(ISRM520)))
  64. //{
  65. // if (DataPack.SRMDatas == null)
  66. // {
  67. // DataPack.SRMDatas = new DeviceDataCollection<SRMData>();
  68. // }
  69. // DataPack.SRMDatas.Datas.Append(new SRMData()
  70. // {
  71. // Code = Device.Code,
  72. // D520 = Copy(this, typeof(ISRM520).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(ISRM520).Name) != null && v != this.GetType()).First()) as WCS_SRM520,
  73. // D521 = Copy(this, typeof(ISRM521).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(ISRM521).Name) != null && v != this.GetType()).First()) as WCS_SRM521,
  74. // D537 = Copy(this, typeof(ISRM537).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(ISRM537).Name) != null && v != this.GetType()).First()) as WCS_SRM537,
  75. // });
  76. // DataPack.SRMDatas.Frame = DateTime.Now;
  77. //}
  78. //else if (Device.HasProtocol(typeof(IBCR81)))
  79. //{
  80. // if (DataPack.BcrDatas == null)
  81. // {
  82. // DataPack.BcrDatas = new DeviceDataCollection<BCRData>();
  83. // }
  84. // DataPack.BcrDatas.Datas.Append(new BCRData()
  85. // {
  86. // Code = Device.Code,
  87. // Bcr81 = Copy(this, typeof(IBCR81).Assembly.GetTypes().Where(v => v.IsClass).Where(v => v.GetInterface(typeof(IBCR81).Name) != null && v != this.GetType()).First()) as WCS_BCR81,
  88. // });
  89. // DataPack.BcrDatas.Frame = DateTime.Now;
  90. //}
  91. //DataPack.Frame = DateTime.Now;
  92. }
  93. private object Copy(object obj, Type type)
  94. {
  95. var res = Activator.CreateInstance(type);
  96. foreach (var p in type.GetProperties())
  97. {
  98. var p2 = obj.GetType().GetProperty(p.Name);
  99. if (p2 != null && p2.PropertyType == p.PropertyType)
  100. {
  101. p.SetValue(res, p2.GetValue(obj));
  102. }
  103. }
  104. return res;
  105. }
  106. }
  107. }