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
{
///
/// 数据采集
///
[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 GetObjects()
{
return new List();
}
public override void Update(List 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 arr = new List();
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 equipmentAlarms = new List();
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 = new List();
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";
}
}
///
/// 设备报警
///
public class EquipmentAlarm
{
///
/// 设备号
///
public string Code { get; set; }
///
/// 内容
///
public string Msg { get; set; }
///
/// 时间
///
public DateTime Time { get; set; }
}
///
/// 设备状态信息
///
public class EquipmentStatus
{
///
/// 设备号
///
public string Code { get; set; }
///
/// 内容
///
public string con { get; set; }
///
/// 内容
///
public int Status { get; set; }
///
/// 时间
///
public DateTime Time { get; set; }
}
///
///
///
///
public class Quest
{
public T Data { get; set; }
}
}