123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- using ServiceCenter.Extensions;
- using ServiceCenter.SqlSugars;
- using SqlSugar;
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using System.Text;
- using WCS.Core;
- using WCS.Entity.Protocol.DataStructure;
- using WCS.Entity.Protocol.HUB;
- using WCS.Entity.Protocol.Robot;
- using WCS.Entity.Protocol.Station;
- 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()
- {
- }
- /// <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 db = new SqlSugarHelper().PLC;
- //var beforeTime = DateTime.Now.AddDays(-13);
- var beforeTime = DateTime.Now.AddDays(-1);
- var startTime = beforeTime.AddHours(-beforeTime.Hour).AddMinutes(-beforeTime.Minute).AddSeconds(-beforeTime.Second);
- var endTime = startTime.AddDays(+1).AddSeconds(-1);
- var robot521 = db.Queryable<WCS_Robot521>().Where(x => x.Frame >= startTime && x.Frame <= endTime).ToList().GroupBy(x => x.Code).ToList();
- #region 处理机械臂
- Parallel.ForEach(robot521, infos =>
- {
- // 获取第一条数据与最后一条数据
- var start = infos.OrderBy(x => x.Frame).First();
- var end = infos.OrderByDescending(x => x.Frame).First();
- //通过是否有最后一条数据来确定当前数据是否有分析存储过
- if (db.Queryable<DevRunInfo>().Any(x => x.Code == infos.Key && x.Frame == end.Frame && x.Alarm == null)) return;
- //获取前一天最后的一条数据
- var yesterEnd = db.Queryable<DevRunInfo>().Where(x => x.Code == infos.Key && x.Frame < endTime).OrderByDescending(x => x.Frame).First();
- List<DevRunInfo> runInfos = new List<DevRunInfo>();
- foreach (var info in infos)
- {
- if (info.Frame == start.Frame) //当天的第一条数据
- {
- if (yesterEnd == null) //如果没有前一天的最后一条数据,就有用当天的第一条数据状态做计算
- {
- runInfos.Add(new DevRunInfo()
- {
- Frame = info.Frame,
- Code = info.Code,
- RunMode = info.RobotMode.GetDescription(),
- RunStatus = info.RunStatus.GetDescription(),
- StartTime = startTime,
- EndTime = info.Frame,
- Duration = Convert.ToUInt64((info.Frame - startTime).TotalMilliseconds)
- });
- }
- else //如果有就用前一天的最后一条数据做计算
- {
- runInfos.Add(new DevRunInfo()
- {
- Frame = info.Frame,
- Code = info.Code,
- RunMode = yesterEnd.RunMode,
- RunStatus = yesterEnd.RunStatus,
- StartTime = startTime,
- EndTime = info.Frame,
- Duration = Convert.ToUInt64((info.Frame - startTime).TotalMilliseconds)
- });
- }
- }
- else if (info.Frame == end.Frame) //当天的最后一条数据
- {
- runInfos.Add(new DevRunInfo()
- {
- Frame = info.Frame,
- Code = info.Code,
- RunMode = info.RobotMode.GetDescription(),
- RunStatus = info.RunStatus.GetDescription(),
- StartTime = info.Frame,
- EndTime = endTime,
- Duration = Convert.ToUInt64((endTime - info.Frame).TotalMilliseconds)
- });
- }
- else //中间数据
- {
- if (start.RobotMode == info.RobotMode && start.RunStatus == info.RunStatus) continue;
- runInfos.Add(new DevRunInfo()
- {
- Frame = info.Frame,
- Code = info.Code,
- RunMode = start.RobotMode.GetDescription(),
- RunStatus = start.RunStatus.GetDescription(),
- StartTime = start.Frame,
- EndTime = info.Frame,
- Duration = Convert.ToUInt64((info.Frame - start.Frame).TotalMilliseconds)
- });
- start = info;
- }
- }
- db.Insertable(runInfos).ExecuteCommand();
- });
- var robot522 = db.Queryable<WCS_Robot522>().Where(x => x.Frame >= startTime && x.Frame <= endTime).ToList().GroupBy(x => x.Code).ToList();
- //Parallel.ForEach(robot522, infos =>
- //{
- // var start = infos.OrderBy(x => x.Frame).First();
- // var end = infos.OrderByDescending(x => x.Frame).First();
- // if (db.Queryable<DevRunInfo>().Any(x => x.Frame == end.Frame && x.RunMode == null && x.RunStatus == null)) return;
- // List<DevRunInfo> runInfos = new List<DevRunInfo>();
- // foreach (var info in infos)
- // {
- // if (info.Frame == start.Frame)
- // {
- // runInfos.Add(new DevRunInfo()
- // {
- // Frame = info.Frame,
- // Code = info.Code,
- // Alarm = info.Alarm.ToString(),
- // StartTime = startTime,
- // EndTime = info.Frame,
- // Duration = Convert.ToUInt64((info.Frame - startTime).Milliseconds)
- // });
- // }
- // else if (info.Frame == end.Frame)
- // {
- // runInfos.Add(new DevRunInfo()
- // {
- // Frame = info.Frame,
- // Code = info.Code,
- // Alarm = info.Alarm.ToString(),
- // StartTime = info.Frame,
- // EndTime = endTime,
- // Duration = Convert.ToUInt64((endTime - info.Frame).Milliseconds)
- // });
- // }
- // else
- // {
- // if (start.Alarm != info.Alarm)
- // {
- // runInfos.Add(new DevRunInfo()
- // {
- // Frame = info.Frame,
- // Code = info.Code,
- // Alarm = info.Alarm.ToString(),
- // StartTime = start.Frame,
- // EndTime = info.Frame,
- // Duration = Convert.ToUInt64((info.Frame - start.Frame).Milliseconds)
- // });
- // start = info;
- // }
- // }
- // }
- // db.Insertable(runInfos).ExecuteCommand();
- //});
- #endregion 处理机械臂
- var a = 1;
- //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 =>
- // {
- // 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()).ExecuteCommand();
- // }
- // 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).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();
- // }
- // });
- // 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; }
- }
- }
|