using DBHelper; using FreeRedis; using MessagePack; using MessagePack.Resolvers; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Linq.Dynamic.Core; using System.Reflection; using WCS.Core; using WCS.Entity; using WCS.Entity.Protocol; namespace WCS.Service { public class ProtocolProxy : ProtocolProxyBase { private MethodInfo SetMethod; public ProtocolProxy(string id, WCS_DATABLOCK db, ushort start, WCS_DEVICEPROTOCOL protocol) : base(id, db, start, protocol) { SetMethod = typeof(DbContext).GetMethod("Set", new Type[] { }).MakeGenericMethod(this.ProtocolDataType); } private static ConcurrentDictionary LastDatas = new ConcurrentDictionary(); protected override WCS_PROTOCOLDATA GetLastData(DB db) { if (!LastDatas.ContainsKey(this.ProtocolDataType)) { dynamic q = SetMethod.Invoke(db.Default, null); try { q = DynamicQueryableExtensions.Where(q, "ISLAST==@0", true); q = DynamicQueryableExtensions.OrderBy(q, "ID"); q = DynamicQueryableExtensions.Select(q, "new(it.DEVICE.CODE as Code,it as Data)"); var arr = Enumerable.ToArray(q); LastDatas[this.ProtocolDataType] = arr; } catch (Exception ex) { throw; } } dynamic datas = LastDatas[this.ProtocolDataType]; var list = new List(); foreach (var data in datas) { if (data.Code == PROTOCOL.DEVICE.CODE) list.Add(data.Data); } if (list.Count > 1) { for (int i = 0; i < list.Count - 1; i++) { var obj = list[i]; db.Default.Attach(obj); obj.ISLAST = false; } db.Default.SaveChanges(); } var res = Enumerable.LastOrDefault(list); return res; } private static int total; protected override WCS_PROTOCOLDATA SaveNewData(DB db, WCS_PROTOCOLDATA last, WCS_PROTOCOLDATA newobj, string user) { total++; if (last != null) { db.Default.Attach(last); last.ISLAST = false; } db.Default.Attach(PROTOCOL.DEVICE); newobj.DEVICE = PROTOCOL.DEVICE; newobj.ISLAST = true; newobj.UPDATETIME = DateTime.Now; newobj.UPDATEUSER = user; newobj.FRAME = LogicHandler.Frame; db.Default.Add(newobj); db.Default.SaveChanges(); return newobj; } //static ConcurrentQueue Packs = new ConcurrentQueue(); private static Dictionary Clients = new Dictionary(); private static RedisClient Redis; //public static RedisClient LiaotcRedis; //public static RedisClient YGWMS150Redis; //public static RedisClient YGWCS150Redis; static ProtocolProxy() { MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block); //Redis = new RedisClient("192.168.249.120,password=123456,database=11"); #region 公用云服务器 Redis = new RedisClient("212.64.66.35,password=Aa123456,database=10"); Redis.Serialize = obj => { var bytes = MessagePackSerializer.Serialize(obj); return bytes; }; Redis.DeserializeRaw = (bytes, type) => { var obj = MessagePackSerializer.Deserialize(type, bytes); return obj; }; #endregion 公用云服务器 //#region 私用云服务器 //LiaotcRedis = new RedisClient("liaotc.com,database=10"); //LiaotcRedis.Serialize = obj => //{ // var bytes = MessagePackSerializer.Serialize(obj); // return bytes; //}; //LiaotcRedis.DeserializeRaw = (bytes, type) => //{ // var obj = MessagePackSerializer.Deserialize(type, bytes); // return obj; //}; //#endregion 私用云服务器 //#region 永冠服务 //YGWCS150Redis = new RedisClient("192.168.249.150,password=123456,database=10"); //YGWCS150Redis.Serialize = obj => //{ // var bytes = MessagePackSerializer.Serialize(obj); // return bytes; //}; //YGWCS150Redis.DeserializeRaw = (bytes, type) => //{ // var obj = MessagePackSerializer.Deserialize(type, bytes); // return obj; //}; //#endregion 永冠服务 } public override void Publish(string code, WCS_PROTOCOLDATA obj) { try { var Datas = AllDatas; if (code.StartsWith("SRM")) { if (!Datas.ContainsKey(code)) Datas[code] = new SCData { Code = code }; } else if (code.StartsWith("RGV")) { if (!Datas.ContainsKey(code)) Datas[code] = new RGVData { Code = code }; } else if (code == "Robot") { if (!Datas.ContainsKey(code)) Datas[code] = new RobotData { Code = code }; } else if (code.Length == 4) { if (!Datas.ContainsKey(code)) Datas[code] = new StationData { Code = code }; } if (Datas.ContainsKey(code)) { var data = Datas[code]; data.Frame = LogicHandler.Frame; var p = data.GetType().GetProperties().Where(v => v.PropertyType == obj.GetType()).FirstOrDefault(); if (p == null) { Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性"); } else { p.SetValue(data, obj); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } private static Dictionary LastInfo = new Dictionary(); public static ConcurrentDictionary AllDatas = new ConcurrentDictionary(); public static void Do() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"更改:{total}"); Console.ResetColor(); total = 0; try { var gs = AllDatas.GroupBy(v => v.Value.GetType()); //Parallel.ForEach(gs, g => //{ // var value = g.Select(v => v.Value).ToArray(); // var etype = g.Key; // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype); // var coll = Activator.CreateInstance(type, LogicHandler.Frame, value); // Redis.Set(etype.Name, coll); // YGWCS150Redis.Set(etype.Name, coll); // LiaotcRedis.Set(etype.Name, coll); //}); DeviceDataPack pack = new DeviceDataPack(); pack.Frame = LogicHandler.Frame; foreach (var g in gs) { var value = g.Select(v => v.Value).ToArray(); var etype = g.Key; var type = typeof(DeviceDataCollection<>).MakeGenericType(etype); var coll = Activator.CreateInstance(type, LogicHandler.Frame, value); var p = pack.GetType().GetProperties().Where(v => v.PropertyType == type).First(); p.SetValue(pack, coll); } var sw = new Stopwatch(); sw.Start(); Redis.Set(nameof(DeviceDataPack), pack); sw.Stop(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}"); Console.ResetColor(); //var se = new Stopwatch(); //se.Start(); //YGWCS150Redis.Set(nameof(DeviceDataPack), pack); //se.Stop(); //Console.ForegroundColor = ConsoleColor.Blue; //Console.WriteLine($"YGWCS150Redis耗时{se.ElapsedMilliseconds}"); //Console.ResetColor(); //var sr = new Stopwatch(); //sr.Start(); //LiaotcRedis.Set(nameof(DeviceDataPack), pack); //sr.Stop(); //Console.ForegroundColor = ConsoleColor.Blue; //Console.WriteLine($"LiaotcRedis耗时{sr.ElapsedMilliseconds}"); //Console.ResetColor(); ///// var st = new Stopwatch(); st.Start(); Redis.RPush("Packs", pack); st.Stop(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine($"Redis耗时{st.ElapsedMilliseconds}"); Console.ResetColor(); //var sy = new Stopwatch(); //sy.Start(); //YGWCS150Redis.RPush("Packs", pack); //sy.Stop(); //Console.ForegroundColor = ConsoleColor.Blue; //Console.WriteLine($"YGWCS150Redis耗时{sy.ElapsedMilliseconds}"); //Console.ResetColor(); //var su = new Stopwatch(); //su.Start(); //LiaotcRedis.RPush("Packs", pack); //su.Stop(); //Console.ForegroundColor = ConsoleColor.Blue; //Console.WriteLine($"LiaotcRedis耗时{su.ElapsedMilliseconds}"); //Console.ResetColor(); //Redis.RPush("Packs", pack); //YGWCS150Redis.RPush("Packs", pack); //LiaotcRedis.RPush("Packs", pack); if (Redis.LLen("Packs") > 100000) { Redis.LTrim("Packs", 5000, -1); } //if (YGWCS150Redis.LLen("Packs") > 200000) //{ // YGWCS150Redis.LTrim("Packs", 20000, -1); //} //if (LiaotcRedis.LLen("Packs") > 100000) //{ // LiaotcRedis.LTrim("Packs", 5000, -1); //} foreach (var data in AllDatas) { LastInfo[data.Key] = data.Value.Info; data.Value.Info = ""; if (data.Value is ProdLineData) { var pld = data.Value as ProdLineData; pld.TaskList.Clear(); pld.Frame = LogicHandler.Frame; pld.Code = data.Key; } } } catch (Exception ex) { } } } public class PackInfo { public DateTime Frame { get; set; } public byte[] Data { get; set; } } }