123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- using Newtonsoft.Json;
- using PlcSiemens.Core.Extension;
- using ServiceCenter.Extensions;
- using ServiceCenter.Logs;
- using ServiceCenter.Redis;
- using ServiceCenter.SqlSugars;
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using System.Data.SqlTypes;
- 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(DataWorld))]
- [Description("数据采集系统")]
- public class DataCollectionSysyem : SystemBase
- {
- 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);
- }
- }
- public override List<object> GetObjects()
- {
- return new List<object>();
- }
- public override void Update(List<WorkTimes> list)
- {
- try
- {
- var sw = new Stopwatch();
- sw.Start();
- #region 处理数据
- var sw1 = new Stopwatch();
- sw1.Start();
- var pack = new DeviceDataPack();
- var frame = DateTime.Now;
- pack.Frame = World.Frame;
- var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
- 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, TimeZoneInfo.ConvertTimeToUtc(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);
- }
- });
- sw1.Stop();
- #endregion 处理数据
- #region 存储监控数据
- var sw2 = new Stopwatch();
- sw2.Start();
- //开始存储设备信息
- RedisHub.Monitor.RPush("Packs", pack);
- if (RedisHub.Monitor.LLen("Packs") > 70000)
- {
- RedisHub.Monitor.LTrim("Packs", 5000, -1);
- }
- sw2.Stop();
- #endregion 存储监控数据
- #region 存储设备报警信息
- var sw3 = new Stopwatch();
- sw3.Start();
- 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.SRM523.Where(x => x.Data.Alarm1 != 0 || x.Data.Alarm2 != 0).Select(x => new EquipmentAlarm()
- {
- Code = x.Code,
- Msg = $"{Convert.ToString(x.Data.Alarm1)},{Convert.ToString(x.Data.Alarm2)}",
- 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));
- sw3.Stop();
- #endregion 存储设备报警信息
- #region 存储设备状态信息
- var sw4 = new Stopwatch();
- sw4.Start();
- 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));
- sw4.Stop();
- #endregion 存储设备状态信息
- #region 存储数采数据
- var sw5 = new Stopwatch();
- sw5.Start();
- RedisHub.Monitor.RPush("DataCollectionpacks", pack);
- sw5.Stop();
- #endregion 存储数采数据
- sw.Stop();
- World.Log($"业务耗时:[{sw.ElapsedMilliseconds}]--处理数据:[{sw1.ElapsedMilliseconds}]--存储监控数据:[{sw2.ElapsedMilliseconds}]--存储设备报警信息:[{sw3.ElapsedMilliseconds}]--存储设备状态数据:[{sw4.ElapsedMilliseconds}]--存储数采数据:[{sw5.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; }
- }
- }
|