using Newtonsoft.Json;
using ServiceCenter;
using ServiceCenter.Extensions;
using ServiceCenter.Redis;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.DataStructure;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;
using WCS.Service.Worlds;
using WCS.WorkEngineering.Extensions;
namespace WCS.Service.Systems
{
///
/// 数据采集系统
///
[BelongTo(typeof(DataCollectionWorld))]
[Description("数据采集系统")]
public class DataCollectionSysyem : DeviceSystem
{
public List ConvList;
public List> Conv91List;
public List> Bcrs;
public List Srms;
public DataCollectionSysyem()
{
ConvList = Device.All.Where(v => v.HasProtocol(typeof(IStation523))).Select(v => new Station(v, this.World)).ToList();
Conv91List = Device.All.Where(v => v.HasProtocol(typeof(IStation91))).Select(v => new Device(v, this.World)).ToList();
Bcrs = Device.All.Where(v => v.HasProtocol(typeof(IBCR81))).Select(v => new Device(v, this.World)).ToList();
Srms = Device.All.Where(v => v.HasProtocol(typeof(ISRM520))).Select(v => new SRM(v, this.World)).ToList();
}
protected override bool ParallelDo => true;
protected override bool SaveLogsToFile => true;
public override bool Select(Device dev)
{
return dev.Code == "SRM1";
}
public override void Do(SRM obj)
{
//try
//{
// SqlSugarHelper.Do(db =>
// {
// DeviceDataPack pack = new DeviceDataPack();
// //处理输送线
// List DevList = new List();
// foreach (var item in ConvList)
// {
// var dev = new StationData()
// {
// Code = item.Entity.Code,
// Frame = DateTime.Now,
// D520 = TypeExtension.Mapper(item.Data),
// D521 = TypeExtension.Mapper(item.Data2),
// D523 = TypeExtension.Mapper(item.Data3),
// D90 = new WCS_Station90(),
// D91 = new WCS_Station91(),
// D80 = new WCS_BCR80(),
// D81 = new WCS_BCR81()
// };
// if (Conv91List.Any(v => v.Entity.Code == item.Entity.Code))
// {
// var d91 = Conv91List.First(v => v.Entity.Code == item.Entity.Code);
// dev.D91 = TypeExtension.Mapper(d91.Data);
// }
// if (Bcrs.Any(v => v.Entity.Code == item.Entity.Code))
// {
// var d81 = Bcrs.First(v => v.Entity.Code == item.Entity.Code);
// dev.D81 = TypeExtension.Mapper(d81.Data);
// }
// //保存报警
// RedisHub.WMS.Set($"{nameof(EquipmentAlarm)}:{item.Entity.Code}", JsonConvert.SerializeObject(new EquipmentAlarm()
// {
// Code = item.Entity.Code,
// Msg = item.Data3.Alarm.ToString(),
// Time = DateTime.Now
// }));
// DevList.Add(dev);
// }
// //保存模式
// var status = ConvList.Select(v => new Tuple(v.Entity.Code, v.Data2.Mode.ToString()));
// RedisHub.WMS.Set($"ConvStatus", JsonConvert.SerializeObject(status));
// pack.StationDatas = new DeviceDataCollection(DateTime.Now, DevList.ToArray());
// //处理堆垛机
// List srmList = new List();
// foreach (var item in Srms)
// {
// var dev = new SRMData()
// {
// Code = item.Entity.Code,
// Frame = DateTime.Now,
// D520 = TypeExtension.Mapper(item.Data),
// D521 = TypeExtension.Mapper(item.Data2),
// D537 = TypeExtension.Mapper(item.Data3),
// };
// //保存报警
// RedisHub.WMS.Set($"{nameof(EquipmentAlarm)}:{item.Entity.Code}", JsonConvert.SerializeObject(new EquipmentAlarm()
// {
// Code = item.Entity.Code,
// Msg = item.Data3.Alarm.ToString(),
// Time = DateTime.Now
// }));
// srmList.Add(dev);
// }
// //保存模式
// var status1 = Srms.Select(v => new Tuple(v.Entity.Code, v.Data2.AutoStatus.ToString()));
// RedisHub.WMS.Set($"SrmStatus", JsonConvert.SerializeObject(status1));
// pack.SRMDatas = new DeviceDataCollection(DateTime.Now, srmList.ToArray());
// pack.Frame = DateTime.Now;
// //保存到Redis
// RedisHub.Monitor.RPush("Packs", pack);
// RedisHub.Monitor.Set(nameof(DeviceDataPack), pack);
// if (RedisHub.Monitor.LLen("Packs") > 500000)
// {
// RedisHub.Monitor.LTrim("Packs", 5000, -1);
// }
// //保存到数据库
// var plcData = new WCS_PlcData()
// {
// AddWho = "WCS",
// WAREHOUSE = ServiceHub.WarehouseName,
// CONTENT = JsonConvert.SerializeObject(pack, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }),
// };
// db.Default.Insertable(plcData).ExecuteCommand();
// });
//}
//catch (Exception ex)
//{
// //World.Log(ex.StackTrace, ServiceCenter.Logs.LogLevelEnum.Mid);
//}
}
}
///
/// 设备报警
///
public class EquipmentAlarm
{
///
/// 设备号
///
public string Code { get; set; }
///
/// 内容
///
public string Msg { get; set; }
///
/// 时间
///
public DateTime Time { get; set; }
}
}