123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- using PlcSiemens.Core.Extension;
- using ServiceCenter.SqlSugars;
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Text;
- using WCS.Core;
- using WCS.Entity.Protocol.BCR;
- using WCS.Entity.Protocol.DataStructure;
- using WCS.Entity.Protocol.RGV;
- using WCS.Entity.Protocol.Robot;
- using WCS.Entity.Protocol.SRM;
- using WCS.Entity.Protocol.Station;
- using WCS.Entity.Protocol.Truss;
- using WCS.WorkEngineering.Extensions;
- using WCS.WorkEngineering.Worlds;
- namespace WCS.WorkEngineering.Systems
- {
- /// <summary>
- /// 数据采集系统
- /// </summary>
- [BelongTo(typeof(DataCollectionWorld))]
- [Description("数据采集系统")]
- public class DataCollectionSysyem : DeviceSystem<SRM>
- {
- public static DeviceDataPack pack = new DeviceDataPack();
- private static object locker = new object();
- public DataCollectionSysyem()
- {
- var gs = Device.All.SelectMany(v => v.Protocols.Select(d => new { DB = $"{d.Value.DBInfo.No}:{d.Value.DBInfo.PLCInfo.IP}", d.Value.Position, TypeStr = d.Key, Dev = v }))
- .GroupBy(v => v.DB);
- foreach (var g in gs)
- {
- var min = g.OrderBy(v => v.Position).First();
- var max = g.OrderByDescending(v => v.Position).First();
- var t = Type.GetType(min.TypeStr);
- min.Dev.Protocol(t, this.World);
- max.Dev.Protocol(t, this.World);
- }
- }
- /// <summary>
- /// 所有设备数据
- /// Key 是不同设备所使用的类型 例如DeviceDataCollection<SRMData>
- /// value 不同设备的具体数据
- /// </summary>
- public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
- protected override bool ParallelDo => true;
- protected override bool SaveLogsToFile => true;
- public override bool Select(Device dev)
- {
- return dev.Code == "SRM1";
- }
- public override void Do(SRM obj)
- {
- var sw = new Stopwatch();
- sw.Start();
- var pack = new DeviceDataPack();
- pack.Frame = DateTime.Now;
- var sql = new StringBuilder();
- sql.Append("INSERT INTO ");
- var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
- foreach (var p in ps)
- {
- if (!p.PropertyType.IsArray)
- continue;
- var dev = p.PropertyType.GetElementType();
- if (dev.GetInterfaces().Any(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")))
- {
- var t = p.PropertyType.GetElementType();
- var protType = GetProtocolType(t);
- var arr = Device.All.Where(v => v.HasProtocol(protType))
- .Select(v =>
- {
- try
- {
- var obj = Activator.CreateInstance(t);
- t.GetProperty("Code").SetValue(obj, v.Code);
- dynamic protObj = v.Protocol(protType, World);
- protObj.Copy(obj);
- var value = GetSql(t, obj);
- t.GetProperty("Frame").SetValue(obj, protObj.Frame);
- //var value = ServiceCenter.Extensions.TypeExtension.Copy(protObj, t);
- //t.GetProperty("Data").SetValue(obj, value);
- sql.Append(value);
- return obj;
- }
- catch (Exception ex)
- {
- return null;
- }
- }).Where(v => v != null).ToArray();
- var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- m = m.MakeGenericMethod(t);
- var arr2 = m.Invoke(null, new object[] { arr });
- m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- m = m.MakeGenericMethod(t);
- var arr3 = m.Invoke(null, new object[] { arr2 });
- p.SetValue(pack, arr3);
- }
- }
- //Redis.RPush("Packs", pack);
- //if (Redis.LLen("Packs") > 50000)
- //{
- // Redis.LTrim("Packs", 5000, -1);
- //}
- sw.Stop();
- //var sw = new Stopwatch();
- //sw.Start();
- //var pack = new DeviceDataPack();
- //pack.Frame = DateTime.Now;
- //var ps = pack.GetType().GetProperties();
- //foreach (var p in ps)
- //{
- // if (!p.PropertyType.IsClass) continue;
- // var packAct = Activator.CreateInstance(p.PropertyType);
- // var prs = p.PropertyType.GetProperties();
- // foreach (var pr in prs)
- // {
- // if (!pr.PropertyType.IsArray) continue;
- // var yt = pr.PropertyType.GetElementType();
- // if (yt.IsClass)
- // {
- // var pros = yt.GetProperties();
- // //var entType = yt.GetGenericArguments()[0];
- // //var protType = GetProtocolType(entType);
- // var dataAct = Activator.CreateInstance(yt);
- // Parallel.ForEach(pros, pro =>
- // {
- // try
- // {
- // if (pro.PropertyType != typeof(DateTime))
- // {
- // if (pro.PropertyType != typeof(string))
- // {
- // var protType = GetProtocolType(pro.PropertyType);
- // var dev = Device.All
- // .Where(v => v.HasProtocol(protType)).Select(v =>
- // {
- // try
- // {
- // var obj = Activator.CreateInstance(pro.PropertyType);
- // pro.PropertyType.GetProperty("Code").SetValue(obj, v.Code);
- // var a = v.Protocol(protType, World);
- // var value = v.Protocol(protType, World).Copy(pro.PropertyType);
- // pro.SetValue(obj, value);
- // return obj;
- // }
- // catch (Exception ex)
- // {
- // return null;
- // }
- // }).FirstOrDefault(v => v != null);
- // if (dev != null)
- // {
- // pro.SetValue(dataAct, dev);
- // }
- // }
- // else
- // {
- // }
- // }
- // }
- // catch (Exception e)
- // {
- // Console.WriteLine(e);
- // }
- // });
- // var a = 1;
- // //var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // //m = m.MakeGenericMethod(yt);
- // //var arr2 = m.Invoke(null, new object[] { datasAct });
- // //m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // //m = m.MakeGenericMethod(yt);
- // //var arr3 = m.Invoke(null, new object[] { arr2 });
- // //p.SetValue(pack, arr3);
- // //var entType = yt.GetGenericArguments()[0];
- // //var protType = GetProtocolType(entType);
- // //var arr = Device.All.Where(v => v.HasProtocol(protType))
- // // .Select(v =>
- // // {
- // // try
- // // {
- // // var obj = Activator.CreateInstance(yt);
- // // yt.GetProperty("Code").SetValue(obj, v.Code);
- // // //var value = v.Protocol(protType, World).Copy(entType);
- // // //t.GetProperty("Data").SetValue(obj, value);
- // // return obj;
- // // }
- // // catch (Exception ex)
- // // {
- // // return null;
- // // }
- // // }).Where(v => v != null).ToArray();
- // //var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // //m = m.MakeGenericMethod(yt);
- // //var arr2 = m.Invoke(null, new object[] { arr });
- // //m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // //m = m.MakeGenericMethod(yt);
- // //var arr3 = m.Invoke(null, new object[] { arr2 });
- // //p.SetValue(pack, arr3);
- // }
- // }
- // //var t = p.PropertyType.GetElementType();
- // //if (t.IsGenericType)
- // //{
- // // var entType = t.GetGenericArguments()[0];
- // // var protType = GetProtocolType(entType);
- // // var arr = Device.All.Where(v => v.HasProtocol(protType))
- // // .Select(v =>
- // // {
- // // try
- // // {
- // // var obj = Activator.CreateInstance(t);
- // // t.GetProperty("Code").SetValue(obj, v.Code);
- // // //var value = v.Protocol(protType, World).Copy(entType);
- // // //t.GetProperty("Data").SetValue(obj, value);
- // // return obj;
- // // }
- // // catch (Exception ex)
- // // {
- // // return null;
- // // }
- // // }).Where(v => v != null).ToArray();
- // // var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // // m = m.MakeGenericMethod(t);
- // // var arr2 = m.Invoke(null, new object[] { arr });
- // // m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- // // m = m.MakeGenericMethod(t);
- // // var arr3 = m.Invoke(null, new object[] { arr2 });
- // // p.SetValue(pack, arr3);
- // //}
- //}
- ////Redis.RPush("Packs", pack);
- ////if (Redis.LLen("Packs") > 50000)
- ////{
- //// Redis.LTrim("Packs", 5000, -1);
- ////}
- //sw.Stop();
- //list.Add(new WorkTimes { Total = sw.ElapsedMilliseconds, Key = "采集数据" });
- return;
- //var sw = new Stopwatch();
- //sw.Start();
- //try
- //{
- // var sw1 = new Stopwatch();
- // sw1.Start();
- // var gs = AllDatas.GroupBy(v => v.Value.GetType());
- // DeviceDataPack pack = new DeviceDataPack();
- // pack.Frame = DateTime.Now;
- // 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, DateTime.Now, value);
- // var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
- // p.SetValue(pack, coll);
- // }
- // sw1.Stop();
- // World.Log($"数据解析耗时:{sw1.ElapsedMilliseconds}");
- // var sw2 = new Stopwatch();
- // sw2.Start();
- // SqlSugarHelper.Do(_db =>
- // {
- // try
- // {
- // var sw3 = new Stopwatch();
- // sw3.Start();
- // var sql = new StringBuilder();
- // sql.Append("INSERT INTO ");
- // var db = _db.PLC;
- // if (pack.SRMDatas != null && pack.SRMDatas.Datas.Any())
- // {
- // Parallel.ForEach(pack.SRMDatas.Datas, x =>
- // {
- // if (x.D520 != null) Set(sql, x.D520.CreateSql);
- // if (x.D521 != null) Set(sql, x.D521.CreateSql);
- // if (x.D537 != null) Set(sql, x.D537.CreateSql);
- // });
- // }
- // if (pack.RGVDatas != null && pack.RGVDatas.Datas.Any())
- // {
- // Parallel.ForEach(pack.RGVDatas.Datas, x =>
- // {
- // if (x.D520 != null) Set(sql, x.D520.CreateSql);
- // if (x.D521 != null) Set(sql, x.D521.CreateSql);
- // if (x.D81 != null) Set(sql, x.D81.CreateSql);
- // });
- // }
- // if (pack.StationDatas != null && pack.StationDatas.Datas.Any())
- // {
- // Parallel.ForEach(pack.StationDatas.Datas, x =>
- // {
- // if (x.D520 != null) Set(sql, x.D520.CreateSql);
- // if (x.D521 != null) Set(sql, x.D521.CreateSql);
- // if (x.D523 != null) Set(sql, x.D523.CreateSql);
- // if (x.D90 != null) Set(sql, x.D90.CreateSql);
- // if (x.D91 != null) Set(sql, x.D91.CreateSql);
- // if (x.D80 != null) Set(sql, x.D80.CreateSql);
- // if (x.D81 != null) Set(sql, x.D81.CreateSql);
- // if (x.D83 != null) Set(sql, x.D83.CreateSql);
- // if (x.D524 != null) Set(sql, x.D524.CreateSql);
- // if (x.D525 != null) Set(sql, x.D525.CreateSql);
- // if (x.D530 != null) Set(sql, x.D530.CreateSql);
- // if (x.D5531 != null) Set(sql, x.D5531.CreateSql);
- // if (x.DR530 != null) Set(sql, x.DR530.CreateSql);
- // if (x.DR531 != null) Set(sql, x.DR531.CreateSql);
- // });
- // }
- // if (pack.RobotData != null && pack.RobotData.Datas.Any())
- // {
- // Parallel.ForEach(pack.RobotData.Datas, x =>
- // {
- // if (x.D520 != null) Set(sql, x.D520.CreateSql);
- // if (x.D521 != null) Set(sql, x.D521.CreateSql);
- // if (x.D522 != null) Set(sql, x.D522.CreateSql);
- // });
- // }
- // if (pack.TrussData != null && pack.TrussData.Datas.Any())
- // {
- // Parallel.ForEach(pack.TrussData.Datas, x =>
- // {
- // if (x.D520 != null) Set(sql, x.D520.CreateSql);
- // if (x.D521 != null) Set(sql, x.D521.CreateSql);
- // if (x.D523 != null) Set(sql, x.D523.CreateSql);
- // });
- // }
- // sw3.Stop();
- // World.Log($"转换SQL耗时:{sw3.ElapsedMilliseconds}");
- // var sw4 = new Stopwatch();
- // sw4.Start();
- // var sqlText = sql.ToString();
- // db.Ado.ExecuteCommand(sql.ToString());
- // sw4.Stop();
- // World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
- // }
- // catch (Exception e)
- // {
- // World.Log($"数据采集错误1:{e.StackTrace}"); Console.WriteLine(e);
- // }
- // });
- // sw2.Stop();
- // World.Log($"数据保存数据库总耗时间:{sw2.ElapsedMilliseconds}");
- //}
- //catch (Exception e)
- //{
- // World.Log($"数据采集错误:{e.StackTrace}");
- //}
- //sw.Stop();
- //World.Log($"数据采集总耗时:{sw.ElapsedMilliseconds}");
- }
- public void Set(StringBuilder sql, string cSql)
- {
- lock (locker)
- {
- sql.Append(cSql);
- }
- }
- private Type GetProtocolType(Type source)
- {
- var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
- return t;
- }
- public string GetSql(Type type, object entity)
- {
- string sql = string.Empty;
- var db = new SqlSugarHelper().PLC;
- var ty = entity.GetType().GetProperty("CreateSql");
- switch (type.Name)
- {
- case nameof(WCS_SRM520):
- var item = entity as WCS_SRM520;
- sql = GetString(db.Insertable(item).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_SRM521):
- var item1 = entity as WCS_SRM521;
- sql = GetString(db.Insertable(item1).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_SRM537):
- var item2 = entity as WCS_SRM537;
- sql = GetString(db.Insertable(item2).ToSqlString());
- ty.SetValue(entity, GetString(db.Insertable(item2).ToSqlString()));
- break;
- case nameof(WCS_RGV520):
- var item3 = entity as WCS_RGV520; sql = GetString(db.Insertable(item3).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_RGV521):
- var item4 = entity as WCS_RGV521; sql = GetString(db.Insertable(item4).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_BCR80):
- var item5 = entity as WCS_BCR80; sql = GetString(db.Insertable(item5).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_BCR81):
- var item6 = entity as WCS_BCR81; sql = GetString(db.Insertable(item6).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_BCR83):
- var item7 = entity as WCS_BCR83; sql = GetString(db.Insertable(item7).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station520):
- var item8 = entity as WCS_Station520; sql = GetString(db.Insertable(item8).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station521):
- var item9 = entity as WCS_Station521; sql = GetString(db.Insertable(item9).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station523):
- var item10 = entity as WCS_Station523; sql = GetString(db.Insertable(item10).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station524):
- var item11 = entity as WCS_Station524; sql = GetString(db.Insertable(item11).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station525):
- var item12 = entity as WCS_Station525; sql = GetString(db.Insertable(item12).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station90):
- var item13 = entity as WCS_Station90; sql = GetString(db.Insertable(item13).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Station91):
- var item14 = entity as WCS_Station91; sql = GetString(db.Insertable(item14).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Truss520):
- var item15 = entity as WCS_Truss520; sql = GetString(db.Insertable(item15).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Truss521):
- var item16 = entity as WCS_Truss521; sql = GetString(db.Insertable(item16).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Truss523):
- var item17 = entity as WCS_Truss523; sql = GetString(db.Insertable(item17).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Truss530):
- var item18 = entity as WCS_Truss530; sql = GetString(db.Insertable(item18).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Truss531):
- var item19 = entity as WCS_Truss531; sql = GetString(db.Insertable(item19).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Robot520):
- var item20 = entity as WCS_Robot520; sql = GetString(db.Insertable(item20).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Robot521):
- var item21 = entity as WCS_Robot521; sql = GetString(db.Insertable(item21).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Robot522):
- var item22 = entity as WCS_Robot522; sql = GetString(db.Insertable(item22).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Robot530):
- var item23 = entity as WCS_Robot530; sql = GetString(db.Insertable(item23).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- case nameof(WCS_Robot531):
- var item24 = entity as WCS_Robot531; sql = GetString(db.Insertable(item24).ToSqlString());
- ty.SetValue(entity, sql);
- break;
- }
- return sql;
- }
- public string GetString(string value)
- {
- return value.Replace("INSERT INTO ", "")
- .Replace(",N'", ",'")
- .Replace("\0", "")
- .Replace("wcs_", "")
- .Replace("(N'", "('") + "\r";
- }
- }
- /// <summary>
- /// 设备报警
- /// </summary>
- public class EquipmentAlarm
- {
- /// <summary>
- /// 设备号
- /// </summary>
- public string Code { get; set; }
- /// <summary>
- /// 内容
- /// </summary>
- public string Msg { get; set; }
- /// <summary>
- /// 时间
- /// </summary>
- public DateTime Time { get; set; }
- }
- }
|