ProtocolProxy.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.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. }
  54. private object Copy(object obj, Type type)
  55. {
  56. var res = Activator.CreateInstance(type);
  57. foreach (var p in type.GetProperties())
  58. {
  59. var p2 = obj.GetType().GetProperty(p.Name);
  60. if (p2 != null && p2.PropertyType == p.PropertyType)
  61. {
  62. p.SetValue(res, p2.GetValue(obj));
  63. }
  64. }
  65. return res;
  66. }
  67. }
  68. }