ProtocolProxy.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. RedisHub.SetDebugContextType(redisConnection.Key);
  41. Configs.DebugRedisUrl = redisConnection.ConnectionString;
  42. break;
  43. case "WMS":
  44. RedisHub.SetWMSContextType(redisConnection.Key);
  45. break;
  46. }
  47. }
  48. #endregion 初始化Redis连接
  49. }
  50. protected override void DataChanged()
  51. {
  52. }
  53. private object Copy(object obj, Type type)
  54. {
  55. var res = Activator.CreateInstance(type);
  56. foreach (var p in type.GetProperties())
  57. {
  58. var p2 = obj.GetType().GetProperty(p.Name);
  59. if (p2 != null && p2.PropertyType == p.PropertyType)
  60. {
  61. p.SetValue(res, p2.GetValue(obj));
  62. }
  63. }
  64. return res;
  65. }
  66. }
  67. }