| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 | 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<Type, object[]> LastDatas = new ConcurrentDictionary<Type, object[]>();        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<WCS_PROTOCOLDATA>();            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<PackInfo> Packs = new ConcurrentQueue<PackInfo>();        private static Dictionary<string, Playerback> Clients = new Dictionary<string, Playerback>();        private static RedisClient Redis;        static ProtocolProxy()        {            MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);            //Redis = new RedisClient("192.168.249.120,password=123456,database=11");            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;            };        }        //static ConcurrentDictionary<string, DeviceData> Datas = new ConcurrentDictionary<string, DeviceData>();        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<string, string> LastInfo = new Dictionary<string, string>();        public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();        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);                //});                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();                Redis.RPush("Packs", pack);                var len = Redis.LLen("Packs");                if (len > 150000)                {                    Redis.LTrim("Packs", 20000, len);                }                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)            {            }            //Datas.Clear();        }    }    public class PackInfo    {        public DateTime Frame { get; set; }        public byte[] Data { get; set; }    }}
 |