using ServiceCenter.Logs; using ServiceCenter.Redis; using System.Collections.Concurrent; using System.ComponentModel; using WCS.Core; using WCS.Entity.Protocol.DataStructure; using WCS.WorkEngineering.Extensions; using WCS.WorkEngineering.Worlds; namespace WCS.WorkEngineering.Systems { /// /// 数据采集系统 /// // [BelongTo(typeof(DataCollectionWorld))] [Description("数据采集系统")] public class DataCollectionSysyem : DeviceSystem { public static DeviceDataPack pack = new DeviceDataPack(); 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 == "SRM1"; } public override void Do(SRM obj) { try { var gs = AllDatas.GroupBy(v => v.Value.GetType()); DeviceDataPack pack = new DeviceDataPack(); pack.Frame = DateTime.Now; foreach (var g in gs) { var value = g.Select(v => v.Value).ToArray(); var etype = g.Key; var type = typeof(DeviceDataCollection<>).MakeGenericType(etype); var coll = Activator.CreateInstance(type, DateTime.Now, value); var p = pack.GetType().GetProperties().First(v => v.PropertyType == type); p.SetValue(pack, coll); } RedisHub.Monitor.RPush("Packs", pack); RedisHub.Monitor.Set(nameof(DeviceDataPack), pack); if (RedisHub.Monitor.LLen("Packs") > 500000) { RedisHub.Monitor.LTrim("Packs", 5000, -1); } } catch (Exception e) { throw new KnownException($"数据采集错误:{e.StackTrace}", LogLevelEnum.Low); } } } /// /// 设备报警 /// public class EquipmentAlarm { /// /// 设备号 /// public string Code { get; set; } /// /// 内容 /// public string Msg { get; set; } /// /// 时间 /// public DateTime Time { get; set; } } }