using DBHelper_SqlSugar; using FreeRedis; using MessagePack; using MessagePack.Resolvers; 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.Core.DataTrans; using WCS.Entity; using WCS.Entity.Protocol; using DbContext = Microsoft.EntityFrameworkCore.DbContext; namespace WCS.Service { public class ProtocolProxy : ProtocolProxyBase { private readonly 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 readonly ConcurrentDictionary LastDatas = new(); protected override WCS_PROTOCOLDATA GetLastData(Db db) { if (!LastDatas.ContainsKey(this.ProtocolDataType)) { dynamic q = _setMethod.Invoke(db.Default, null); 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; } 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 (var i = 0; i < list.Count - 1; i++) { var obj = list[i]; db.Default.Insertable((object)obj).ExecuteCommand(); obj.ISLAST = false; } } var res = list.LastOrDefault(); 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.Insertable((object)last).ExecuteCommand(); last.ISLAST = false; } db.Default.Storageable(Protocol.DEVICE).ExecuteCommand(); newobj.DEVICE = Protocol.DEVICE; newobj.ISLAST = true; newobj.UPDATETIME = DateTime.Now; newobj.UPDATEUSER = user; newobj.FRAME = LogicHandler.Frame; db.Default.Insertable((object)newobj).ExecuteCommand(); return newobj; } private static readonly RedisClient Redis; public static RedisClient Yg150Redis; public static RedisClient Ygwms150Redis; public static RedisClient Ygwcs150Redis; static ProtocolProxy() { MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block); Redis = new RedisClient("212.64.66.35,database=10"); Redis.Serialize = obj => { var bytes = MessagePackSerializer.Serialize(obj); return bytes; }; Redis.DeserializeRaw = (bytes, type) => { var obj = MessagePackSerializer.Deserialize(type, bytes); return obj; }; Yg150Redis = new RedisClient("192.168.249.150,password=123456,database=1"); Ygwms150Redis = new RedisClient("192.168.249.150,password=123456,database=0"); 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; }; } //static ConcurrentDictionary Datas = new ConcurrentDictionary(); 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)) return; var data = datas[code]; data.Frame = LogicHandler.Frame; var p = data.GetType().GetProperties().FirstOrDefault(v => v.PropertyType == obj.GetType()); if (p == null) { Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + obj.GetType().Name + "的属性"); } else { p.SetValue(data, obj); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static ConcurrentDictionary AllDatas = new(); 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); //}); var pack = new DeviceDataPack { 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().First(v => v.PropertyType == type); p.SetValue(pack, coll); } var sw = new Stopwatch(); sw.Start(); Redis.Set(nameof(DeviceDataPack), pack); Ygwcs150Redis.Set(nameof(DeviceDataPack), pack); sw.Stop(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}"); Console.ResetColor(); Redis.RPush("Packs", pack); Ygwcs150Redis.RPush("Packs", pack); var len = Redis.LLen("Packs"); if (len > 150000) { Redis.LTrim("Packs", 20000, len); Ygwcs150Redis.LTrim("Packs", 20000, len); } foreach (var data in AllDatas) { data.Value.Info = ""; if (data.Value is not ProdLineData pld) continue; pld.TaskList.Clear(); pld.Frame = LogicHandler.Frame; pld.Code = data.Key; } } catch (Exception) { // ignored } } } public class PackInfo { public DateTime Frame { get; set; } public byte[] Data { get; set; } } }