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.DataStructure; using WCS.WorkEngineering.Extensions; using WCS.WorkEngineering.Worlds; namespace WCS.WorkEngineering.Systems { /// /// 数据采集系统 /// //[BelongTo(typeof(DataCollectionWorld))] [Description("数据采集系统")] public class DataCollectionSysyem : DeviceSystem { 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.Where(x => !x.Key.Contains("10.30.37.211") && !x.Key.Contains("10.30.37.217") && !x.Key.Contains("10.30.37.223"))) //{ // 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); //} } /// /// 所有设备数据 /// Key 是不同设备所使用的类型 例如DeviceDataCollection /// value 不同设备的具体数据 /// public static ConcurrentDictionary AllDatas = new ConcurrentDictionary(); 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) { 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); } } } /// /// 设备报警 /// public class EquipmentAlarm { /// /// 设备号 /// public string Code { get; set; } /// /// 内容 /// public string Msg { get; set; } /// /// 时间 /// public DateTime Time { get; set; } } }