| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 | using MessagePack;using Newtonsoft.Json;using ServiceCenter;using ServiceCenter.Redis;using WCS.Core;using WCS.Entity.Protocol.DataStructure;namespace WCS.Service{    public class ProtocolProxy : ProtocolProxyBase    {        public static DeviceDataPack DataPack { get; set; } = new DeviceDataPack();        public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType, World world) : base(dev, info, protocolType, world)        {            #region 初始化Redis连接            var redisConnectionStrings = RedisHub.Default.Check("RedisConnectionStrings") ?? throw new Exception("请在Redis中配置RedisConnectionStrings");            var configs = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);            if (configs != null)            {                if (configs.All(v => v.Key != "Monitor")) throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");            }            foreach (var redisConnection in configs!)            {                RedisHub.CreateContext(redisConnection.ConnectionString, redisConnection.Key);                switch (redisConnection.Key)                {                    case "Monitor":                        RedisHub.SetMonitorContextType(redisConnection.Key);                        RedisHub.Monitor.Serialize = obj =>                        {                            var bytes = MessagePackSerializer.Serialize(obj);                            return bytes;                        };                        RedisHub.Monitor.DeserializeRaw = (bytes, type) =>                        {                            var obj = MessagePackSerializer.Deserialize(type, bytes);                            return obj;                        };                        break;                    case "DebugRedisUrl":                        RedisHub.SetDebugContextType(redisConnection.Key);                        Configs.DebugRedisUrl = redisConnection.ConnectionString;                        break;                    case "WMS":                        RedisHub.SetWMSContextType(redisConnection.Key);                        break;                }            }            #endregion 初始化Redis连接        }        protected override void DataChanged()        {        }        private object Copy(object obj, Type type)        {            var res = Activator.CreateInstance(type);            foreach (var p in type.GetProperties())            {                var p2 = obj.GetType().GetProperty(p.Name);                if (p2 != null && p2.PropertyType == p.PropertyType)                {                    p.SetValue(res, p2.GetValue(obj));                }            }            return res;        }    }}
 |