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
{
///
/// 数据采集系统
///
[BelongTo(typeof(MainWorld))]
[Description("数据采集系统")]
public class DataCollectionSysyem : DeviceSystem>
{
public static DeviceDataPack pack = new DeviceDataPack();
private static object locker = new object();
public DataCollectionSysyem()
{
}
///
/// 所有设备数据
/// 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)
{
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().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().Any(x => x.Code == infos.Key && x.Frame == end.Frame && x.Alarm == null)) return;
//获取前一天最后的一条数据
var yesterEnd = db.Queryable().Where(x => x.Code == infos.Key && x.Frame < endTime).OrderByDescending(x => x.Frame).First();
List runInfos = new List();
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().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().Any(x => x.Frame == end.Frame && x.RunMode == null && x.RunStatus == null)) return;
// List runInfos = new List();
// 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