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 : 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 == "1";
}
public override void Do(Device objDev)
{
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