using AutoMapper.Internal; using CapMachine.Wpf.Models; using CapMachine.Wpf.PrismEvent; using Microsoft.Extensions.Caching.Memory; using Prism.Events; using Prism.Mvvm; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CapMachine.Wpf.Services { /// /// 机器实时数据服务 /// public class MachineRtDataService : BindableBase { /// /// 事件聚合器 /// private IEventAggregator _EventAggregator { get; set; } /// /// ScanTask扫描Task /// static Task ScanTask { get; set; } /// /// 仪表数据集合 /// public List ListMeterRtData { get; set; } /// /// 扫描线程使能 /// public bool ThreadEnable { get; set; } = true; /// /// 历史数据 /// public List HistoryData = new List(); /// /// 实例化函数 /// /// public MachineRtDataService(IEventAggregator eventAggregator) { //ConcurrentDictionary keyValuePairs = new ConcurrentDictionary(); Stopwatch stopwatch = new Stopwatch(); //第一次计时 stopwatch.Start(); //启动Stopwatch for (var i = 0; i < 10000000; i++) { keyValuePairs.TryAdd(DateTime.UtcNow, new RecordInfo() { CreateTime = DateTime.UtcNow, WorkCondition = "dadf", Temp = 23.3 }); //keyValuePairs.Add(new RecordInfo() //{ // CreateTime = DateTime.UtcNow, // WorkCondition = "dadf", // Temp = 23.3 //}); } stopwatch.Stop(); //停止Stopwatch Console.WriteLine("Add Elapsed output runTime:{0}", stopwatch.Elapsed.ToString()); //事件服务 _EventAggregator = eventAggregator; //实例化集合 ListMeterRtData = new List() { new MeterRtDataModel(){ MeterName = "EVA风量", RtUIControlTitle="EVA风量" + Environment.NewLine + "m³/h", RtUIControlTitleIndex="Title5", Station = 1, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV5", RtSVUIControlIndex="SV5", RtPVUIControlIndex="PV5", MaxValue=500, MinValue=0, Accuracy=1, Unit="m³/h", MeterEnableStatePLCAddress="V30.3", }, new MeterRtDataModel(){//目前是DB表 MeterName = "中间轴转速",//原来中间轴转速,先全部改为电机转速-开发过程中某个时刻中间轴转速改为电机转速 RtUIControlTitle="中间轴转速" + Environment.NewLine + "(r/min)", RtUIControlTitleIndex="Title3", Station = 2, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV3", RtSVUIControlIndex="SV3", RtPVUIControlIndex="PV3", MaxValue=4000, MinValue=0, Accuracy=0, Unit="(r/min)", MeterEnableStatePLCAddress="", }, new MeterRtDataModel(){ MeterName = "加热电力",//加热电力 RtUIControlTitle="加热电力" + Environment.NewLine + "(KW)", RtUIControlTitleIndex="Title7", Station = 3, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV7", RtSVUIControlIndex="SV7", RtPVUIControlIndex="PV7", MaxValue=30, MinValue=0, Accuracy=2, Unit="(KW)", MeterEnableStatePLCAddress="V30.4", }, new MeterRtDataModel(){ MeterName = "加湿电力", RtUIControlTitle="加湿电力" + Environment.NewLine + "(KW)", RtUIControlTitleIndex="Title8", Station = 4, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV8", RtSVUIControlIndex="SV8", RtPVUIControlIndex="PV8", MaxValue=18, MinValue=0, Accuracy=2, Unit="(KW)", MeterEnableStatePLCAddress="V30.5", }, new MeterRtDataModel(){//目前是DB表 MeterName = "EMPCV电流",//EMPCV电力 RtUIControlTitle="EMPCV电流" + Environment.NewLine + "(A)", RtUIControlTitleIndex="Title9", Station = 5, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV9", RtSVUIControlIndex="SV9", RtPVUIControlIndex="PV9", MaxValue=1, MinValue=0, Accuracy=2, Unit="(A)", MeterEnableStatePLCAddress="V30.6", }, new MeterRtDataModel(){ MeterName = "INJ压力", RtUIControlTitle="INJ压力" + Environment.NewLine + "(MPa)", RtUIControlTitleIndex="Title10", Station = 6, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV10", RtSVUIControlIndex="SV10", RtPVUIControlIndex="PV10", MaxValue=5, MinValue=0, Accuracy=3, Unit="(MPa)", MeterEnableStatePLCAddress="V30.7", }, new MeterRtDataModel(){ MeterName = "冷媒流量", RtUIControlTitle="冷媒流量" + Environment.NewLine + "(kg/h)", RtUIControlTitleIndex="Title11", Station = 7, RtAddressPV = "101", RtAddressMV = "105", RtMVUIControlIndex="MV11", RtSVUIControlIndex="SV11", RtPVUIControlIndex="PV11", MaxValue=500, MinValue=0, Accuracy=1, Unit="(kg/h)", MeterEnableStatePLCAddress="V31.0", }, }; PubRtDataStart(); } private Random random = new Random(); /// ///发布实时数据 /// private void PubRtDataStart() { ScanTask = Task.Run(async () => { while (ThreadEnable) { try { await Task.Delay(1000); _EventAggregator.GetEvent().Publish(new List() { new Models.ChartRtValue(){Name="室温0",Value=random.NextDouble()*100,Unit="℃"}, new Models.ChartRtValue(){Name="室温1",Value=random.NextDouble()*100,Unit="℃"}, new Models.ChartRtValue(){Name="室温2",Value=random.NextDouble()*100,Unit="℃"}, new Models.ChartRtValue(){Name="室温3",Value=random.NextDouble()*100,Unit="℃"}, new Models.ChartRtValue(){Name="室温4",Value=random.NextDouble()*100,Unit="℃"}, new Models.ChartRtValue(){Name="室温5",Value=random.NextDouble()*100,Unit="℃"}, }); } catch (Exception ex) { var dd = 1; } } }); } } }