123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- using Newtonsoft.Json;
- using PlcSiemens.Core.Extension;
- using ServiceCenter.Extensions;
- using ServiceCenter.Redis;
- using ServiceCenter.SqlSugars;
- using SqlSugar;
- 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(MainWorld))]
- [Description("数据采集系统")]
- public class DataCollectionSysyem : DeviceSystem<Device<IStation520>>
- {
- 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 == "1";
- }
- public override void Do(Device<IStation520> objDev)
- {
- var sql = new StringBuilder();
- try
- {
- var sw = new Stopwatch();
- sw.Start();
- var pack = new DeviceDataPack();
- var frame = DateTime.Now;
- pack.Frame = World.Frame;
- sql.Append("INSERT INTO ");
- var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
- var db = new SqlSugarHelper().PLC;
- Parallel.ForEach(ps, p =>
- {
- if (!p.PropertyType.IsArray) return;
- var t = p.PropertyType.GetElementType();
- if (t.IsGenericType)
- {
- var entType = t.GetGenericArguments()[0];
- var protType = GetProtocolType(entType);
- if (protType == null) return;
- var devices = Device.All.Where(v => v.HasProtocol(protType));
- List<object> arr = new List<object>();
- Parallel.ForEach(devices, x =>
- {
- try
- {
- var protObj = x.Protocol(protType, World) as ProtocolProxyBase;
- if (protObj.Frame < DateTime.Now.AddYears(-24))
- {
- protObj.Frame = frame;
- }
- if (protObj.Db.failed)
- {
- return;
- }
- var obj = Activator.CreateInstance(t);
- t.GetProperty("Code").SetValue(obj, x.Code);
- var value = WCS.Core.Extentions.Copy(protObj, entType, frame);
- t.GetProperty("Data").SetValue(obj, value);
- t.GetProperty("Frame").SetValue(obj, protObj.Frame);
- entType.GetProperty("Code").SetValue(value, x.Code);
- arr.Add(obj);
- }
- catch
- {
- }
- });
- 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);
- }
- });
- var sw3 = new Stopwatch();
- sw3.Start();
- //开始存储设备信息
- RedisHub.Monitor.RPush("Packs", pack);
- if (RedisHub.Monitor.LLen("Packs") > 50000)
- {
- RedisHub.Monitor.LTrim("Packs", 5000, -1);
- }
- #region 存储设备报警信息
- List<EquipmentAlarm> equipmentAlarms = new List<EquipmentAlarm>();
- equipmentAlarms.AddRange(pack.Robot522.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
- {
- Code = x.Code,
- Msg = x.Data.Alarm.ToString(),
- Time = x.Frame
- }));
- equipmentAlarms.AddRange(pack.SRM537.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
- {
- Code = x.Code,
- Msg = x.Data.Alarm.ToString(),
- Time = x.Frame
- }));
- equipmentAlarms.AddRange(pack.Station523.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
- {
- Code = x.Code,
- Msg = x.Data.Alarm.ToString(),
- Time = x.Frame
- }));
- equipmentAlarms.AddRange(pack.Truss523.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
- {
- Code = x.Code,
- Msg = x.Data.Alarm.ToString(),
- Time = x.Frame
- }));
- RedisHub.Default.Set(nameof(EquipmentAlarm), JsonConvert.SerializeObject(equipmentAlarms));
- #endregion 存储设备报警信息
- #region 存储设备状态信息
- List<EquipmentStatus> equipmentStatus = new List<EquipmentStatus>();
- equipmentStatus.AddRange(pack.RGV521.Where(x => x.Data.WorkMode != 0).Select(x => new EquipmentStatus()
- {
- Code = x.Code,
- con = x.Data.WorkMode.GetDescription(),
- Status = x.Data.WorkMode.ToInt(),
- Time = x.Frame
- }));
- equipmentStatus.AddRange(pack.Robot521.Where(x => x.Data.RobotMode != 0).Select(x => new EquipmentStatus()
- {
- Code = x.Code,
- con = x.Data.RobotMode.GetDescription(),
- Status = x.Data.RobotMode.ToInt(),
- Time = x.Frame
- }));
- equipmentStatus.AddRange(pack.SRM521.Where(x => x.Data.AutoStatus != 0).Select(x => new EquipmentStatus()
- {
- Code = x.Code,
- con = x.Data.AutoStatus.GetDescription(),
- Status = x.Data.AutoStatus.ToInt(),
- Time = x.Frame
- }));
- equipmentStatus.AddRange(pack.Station521.Where(x => x.Data.Mode != 0).Select(x => new EquipmentStatus()
- {
- Code = x.Code,
- con = x.Data.Mode.GetDescription(),
- Status = x.Data.Mode.ToInt(),
- Time = x.Frame
- }));
- equipmentStatus.AddRange(pack.Truss521.Where(x => x.Data.Status != 0).Select(x => new EquipmentStatus()
- {
- Code = x.Code,
- con = x.Data.Status.GetDescription(),
- Status = x.Data.Status.ToInt(),
- Time = x.Frame
- }));
- RedisHub.Default.Set(nameof(EquipmentStatus), JsonConvert.SerializeObject(equipmentStatus));
- #endregion 存储设备状态信息
- sw3.Stop();
- World.Log($"redis存储耗时:{sw3.ElapsedMilliseconds}");
- var sw4 = new Stopwatch();
- sw4.Start();
- Parallel.ForEach(pack.GetType().GetProperties().OrderBy(x => x.Name), ps =>
- {
- try
- {
- if (ps.PropertyType == typeof(ProtocolData<WCS_BCR80>[]))
- {
- if (pack.BCR80.Any())
- db.Insertable(pack.BCR80.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR81>[]))
- {
- if (pack.BCR81.Any())
- db.Insertable(pack.BCR81.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR83>[]))
- {
- if (pack.BCR83.Any())
- db.Insertable(pack.BCR83.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV520>[]))
- {
- if (pack.RGV520.Any())
- db.Insertable(pack.RGV520.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV521>[]))
- {
- if (pack.RGV521.Any())
- db.Insertable(pack.RGV521.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot520>[]))
- {
- if (pack.Robot520.Any())
- db.Insertable(pack.Robot520.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot521>[]))
- {
- if (pack.Robot521.Any())
- db.Insertable(pack.Robot521.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot522>[]))
- {
- if (pack.Robot522.Any())
- db.Insertable(pack.Robot522.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot530>[]))
- {
- if (pack.Robot530.Any())
- db.Insertable(pack.Robot530.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot531>[]))
- {
- if (pack.Robot531.Any())
- db.Insertable(pack.Robot531.Select(x => x.Data).ToList()).ToSqlString();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM520>[]))
- {
- if (pack.SRM520.Any())
- db.Insertable(pack.SRM520.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM521>[]))
- {
- if (pack.SRM521.Any())
- db.Insertable(pack.SRM521.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM537>[]))
- {
- if (pack.SRM537.Any())
- db.Insertable(pack.SRM537.Select(x => x.Data).Where(x => x.Alarm == 0).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station520>[]))
- {
- if (pack.Station520.Any())
- db.Insertable(pack.Station520.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station521>[]))
- {
- if (pack.Station521.Any())
- db.Insertable(pack.Station521.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station523>[]))
- {
- if (pack.Station523.Any())
- db.Insertable(pack.Station523.Select(x => x.Data).ToList()).UseParameter()
- .ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station524>[]))
- {
- if (pack.Station524.Any())
- db.Insertable(pack.Station524.Select(x => x.Data).ToList()).UseParameter()
- .ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station525>[]))
- {
- if (pack.Station525.Any())
- db.Insertable(pack.Station525.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station90>[]))
- {
- if (pack.Station90.Any())
- db.Insertable(pack.Station90.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station91>[]))
- {
- if (pack.Station91.Any())
- db.Insertable(pack.Station91.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss520>[]))
- {
- if (pack.Truss520.Any())
- db.Insertable(pack.Truss520.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss521>[]))
- {
- if (pack.Truss521.Any())
- db.Insertable(pack.Truss521.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss523>[]))
- {
- if (pack.Truss523.Any())
- db.Insertable(pack.Truss523.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss530>[]))
- {
- if (pack.Truss530.Any())
- db.Insertable(pack.Truss530.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss531>[]))
- {
- if (pack.Truss531.Any())
- db.Insertable(pack.Truss531.Select(x => x.Data).ToList()).ExecuteCommand();
- }
- }
- catch (Exception e)
- {
- World.Log($"{e.Message}:{e.StackTrace}");
- }
- });
- sw4.Stop();
- World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
- sw.Stop();
- World.Log($"数据采集耗时:{sw.ElapsedMilliseconds}");
- }
- catch (Exception e)
- {
- World.Log($"错误内容:{e.Message}");
- }
- }
- 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"));
- var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
- return t;
- }
- private object AppendLock = new object();
- public StringBuilder Append(StringBuilder sql, string value)
- {
- lock (AppendLock)
- {
- return sql.Append(value);
- }
- }
- 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; }
- }
- /// <summary>
- /// 设备状态信息
- /// </summary>
- public class EquipmentStatus
- {
- /// <summary>
- /// 设备号
- /// </summary>
- public string Code { get; set; }
- /// <summary>
- /// 内容
- /// </summary>
- public string con { get; set; }
- /// <summary>
- /// 内容
- /// </summary>
- public int Status { get; set; }
- /// <summary>
- /// 时间
- /// </summary>
- public DateTime Time { get; set; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class Quest<T>
- {
- public T Data { get; set; }
- }
- }
|