ProtocolProxy.cs 5.6 KB

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