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
{
///
/// 数据采集系统
///
[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)
{
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)
{
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";
}
}
///
/// 设备报警
///
public class EquipmentAlarm
{
///
/// 设备号
///
public string Code { get; set; }
///
/// 内容
///
public string Msg { get; set; }
///
/// 时间
///
public DateTime Time { get; set; }
}
}