diff --git a/CapMachine.Wpf/App.xaml.cs b/CapMachine.Wpf/App.xaml.cs index 71e4e69..3bd39ed 100644 --- a/CapMachine.Wpf/App.xaml.cs +++ b/CapMachine.Wpf/App.xaml.cs @@ -57,7 +57,7 @@ namespace CapMachine.Wpf ////注册设备服务 //containerRegistry.RegisterSingleton(); - containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); //注册AutoMapper 将IAutoMapperProvider注入IOC容器,并对外提供IMapper注入类型。 containerRegistry.RegisterSingleton(); @@ -140,7 +140,7 @@ namespace CapMachine.Wpf var appVersionService = ContainerLocator.Container.Resolve(); //var appVersionService1 = ContainerLocator.Container.Resolve(); //var appVersionService2 = ContainerLocator.Container.Resolve(); - var appVersionService3 = ContainerLocator.Container.Resolve(); + var appVersionService3 = ContainerLocator.Container.Resolve(); base.OnInitialized(); } diff --git a/CapMachine.Wpf/CapMachine.Wpf.csproj b/CapMachine.Wpf/CapMachine.Wpf.csproj index dd0c4f6..0d702c7 100644 --- a/CapMachine.Wpf/CapMachine.Wpf.csproj +++ b/CapMachine.Wpf/CapMachine.Wpf.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net6.0-windows enable enable @@ -15,11 +15,13 @@ + + diff --git a/CapMachine.Wpf/Models/RecordInfo.cs b/CapMachine.Wpf/Models/RecordInfo.cs new file mode 100644 index 0000000..82f2aaf --- /dev/null +++ b/CapMachine.Wpf/Models/RecordInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CapMachine.Wpf.Models +{ + /// + /// 记录的信息 + /// 曲线缓存用 + /// + public class RecordInfo + { + /// + /// 工况 + /// + public string? WorkCondition { get; set; } + + /// + /// 温度 + /// + public double Temp { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreateTime { get; set; } + } +} diff --git a/CapMachine.Wpf/PrismEvent/ChartActionType.cs b/CapMachine.Wpf/PrismEvent/ChartActionType.cs new file mode 100644 index 0000000..c025da1 --- /dev/null +++ b/CapMachine.Wpf/PrismEvent/ChartActionType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CapMachine.Wpf.PrismEvent +{ + public enum ChartActionType:byte + { + ReLoadChart=1, + } +} diff --git a/CapMachine.Wpf/PrismEvent/ChartRtActionEvent.cs b/CapMachine.Wpf/PrismEvent/ChartRtActionEvent.cs new file mode 100644 index 0000000..2d3d2a4 --- /dev/null +++ b/CapMachine.Wpf/PrismEvent/ChartRtActionEvent.cs @@ -0,0 +1,19 @@ +using CapMachine.Wpf.Models; +using Prism.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CapMachine.Wpf.PrismEvent +{ + /// + /// Chart图表操作的动作事件 + /// + public class ChartRtActionEvent : PubSubEvent + { + + } + +} diff --git a/CapMachine.Wpf/Services/MachineRtDataService.cs b/CapMachine.Wpf/Services/MachineRtDataService.cs new file mode 100644 index 0000000..9206b2c --- /dev/null +++ b/CapMachine.Wpf/Services/MachineRtDataService.cs @@ -0,0 +1,238 @@ +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; + } + + } + + }); + } + } +} diff --git a/CapMachine.Wpf/Services/PlcRtDataService.cs b/CapMachine.Wpf/Services/PlcRtDataService.cs deleted file mode 100644 index 7a2efe5..0000000 --- a/CapMachine.Wpf/Services/PlcRtDataService.cs +++ /dev/null @@ -1,74 +0,0 @@ -using CapMachine.Wpf.PrismEvent; -using Prism.Events; -using Prism.Mvvm; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CapMachine.Wpf.Services -{ - public class PlcRtDataService:BindableBase - { - /// - /// 事件聚合器 - /// - private IEventAggregator _EventAggregator { get; set; } - - /// - /// ScanTask扫描Task - /// - static Task ScanTask { get; set; } - - - /// - /// 扫描线程使能 - /// - public bool ThreadEnable { get; set; } = true; - - public PlcRtDataService(IEventAggregator eventAggregator) - { - //事件服务 - _EventAggregator = eventAggregator; - - - 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; - } - - } - - }); - } - } -} diff --git a/CapMachine.Wpf/ViewModels/DialogChartRtConfigViewModel.cs b/CapMachine.Wpf/ViewModels/DialogChartRtConfigViewModel.cs index 20fcd94..4ef2938 100644 --- a/CapMachine.Wpf/ViewModels/DialogChartRtConfigViewModel.cs +++ b/CapMachine.Wpf/ViewModels/DialogChartRtConfigViewModel.cs @@ -2,6 +2,7 @@ using CapMachine.Core; using CapMachine.Model; using CapMachine.Wpf.Models; +using CapMachine.Wpf.Services; using ImTools; using Prism.Commands; using Prism.Services.Dialogs; @@ -17,37 +18,13 @@ namespace CapMachine.Wpf.ViewModels { public class DialogChartRtConfigViewModel : DialogViewModel { - public DialogChartRtConfigViewModel(IFreeSql freeSql, IMapper mapper) + public DialogChartRtConfigViewModel(IFreeSql freeSql, IMapper mapper, MachineRtDataService machineRtDataService) { this.Title = "曲线编辑"; //加载曲线数据源 ChartSrcDataListViewItems = new ObservableCollection(); - ChartSrcDataListViewItems.Add(new ChartSrcData() - { - Index = 1, - Name = "温度", - Selected = true - }); - ChartSrcDataListViewItems.Add(new ChartSrcData() - { - Index = 2, - Name = "压力", - Selected = true - }); - ChartSrcDataListViewItems.Add(new ChartSrcData() - { - Index = 3, - Name = "湿度", - Selected = false - }); - ChartSrcDataListViewItems.Add(new ChartSrcData() - { - Index = 4, - Name = "过冷度", - Selected = false - }); - + ChartYAxisDataListViewItems = new ObservableCollection() { new ChartYAxisDto(){ @@ -74,8 +51,8 @@ namespace CapMachine.Wpf.ViewModels }; FreeSql = freeSql; this.Mapper = mapper; - - RefreshChartSourceData(); + MachineRtDataService = machineRtDataService; + LoadChartSourceData(); RefreshChartSelectedData(); RefreshChartYAxisData(); } @@ -84,11 +61,36 @@ namespace CapMachine.Wpf.ViewModels /// FreeSql /// public IFreeSql FreeSql { get; } + /// /// AutoMap映射 /// public IMapper Mapper { get; } + /// + /// 数据服务 + /// + public MachineRtDataService MachineRtDataService { get; } + + + /// + /// 加载ChartSourceData + /// + private void LoadChartSourceData() + { + foreach (var item in MachineRtDataService.ListMeterRtData) + { + ChartSrcDataListViewItems.Add(new ChartSrcData() + { + Index=1, + Name=item.MeterName, + Selected=false, + }); + } + + } + + /// /// 加载数据 /// @@ -98,6 +100,7 @@ namespace CapMachine.Wpf.ViewModels //Mapper.Map>(data); //ChartSelectDataListViewItems = new ObservableCollection(Mapper.Map>(data)); + } /// @@ -105,13 +108,25 @@ namespace CapMachine.Wpf.ViewModels /// private void RefreshChartSelectedData() { - var ChartSelectedData = FreeSql.Select().Include(a=>a.ConfigChartYAxis).ToList(); + var ChartSelectedData = FreeSql.Select().Include(a => a.ConfigChartYAxis).ToList(); ChartSelectDataListViewItems.Clear(); foreach (var item in ChartSelectedData) { ChartSelectDataListViewItems.Add(Mapper.Map(item)); } + //选中后源数据状态被选中 + foreach (var item in ChartSelectedData) + { + var data = ChartSrcDataListViewItems.FindFirst(a => a.Name == item.Name); + if (data != null) + { + //选中 + data.Selected = true; + } + } + + //SelectedChartSelectData = null; //Mapper.Map>(data); @@ -345,7 +360,7 @@ namespace CapMachine.Wpf.ViewModels //数据库保存 FreeSql.Update() .Where(a => a.Id == SelectedChartSelectData.Id) - .Set(a=>a.ConfigChartYAxisId, SelectedChartYAxisData.Id) + .Set(a => a.ConfigChartYAxisId, SelectedChartYAxisData.Id) .ExecuteAffrows(); //刷新图表 diff --git a/CapMachine.Wpf/ViewModels/RealTimeChartViewModel.cs b/CapMachine.Wpf/ViewModels/RealTimeChartViewModel.cs index 557d7cf..4ecc80a 100644 --- a/CapMachine.Wpf/ViewModels/RealTimeChartViewModel.cs +++ b/CapMachine.Wpf/ViewModels/RealTimeChartViewModel.cs @@ -1,5 +1,7 @@ using CapMachine.Core; +using CapMachine.Wpf.PrismEvent; using Prism.Commands; +using Prism.Events; using Prism.Services.Dialogs; using SharpDX.Direct3D9; using System; @@ -12,10 +14,11 @@ namespace CapMachine.Wpf.ViewModels /// /// 实例化函数 /// - public RealTimeChartViewModel(IDialogService dialogService, IFreeSql freeSql) + public RealTimeChartViewModel(IDialogService dialogService, IFreeSql freeSql, IEventAggregator eventAggregator) { DialogService = dialogService; FreeSql = freeSql; + EventAggregator = eventAggregator; } /// @@ -28,6 +31,11 @@ namespace CapMachine.Wpf.ViewModels /// public IFreeSql FreeSql { get; } + /// + /// 事件聚合器 + /// + public IEventAggregator EventAggregator { get; } + private DelegateCommand _ChartConfigCmd; /// /// 编辑曲线数据 @@ -60,8 +68,8 @@ namespace CapMachine.Wpf.ViewModels { //程序名称 var ReturnValue = par.Parameters.GetValue("Name"); - - + //返回数据,刷新Chart + EventAggregator.GetEvent().Publish(ChartActionType.ReLoadChart); } else if (par.Result == ButtonResult.Cancel) { @@ -72,7 +80,7 @@ namespace CapMachine.Wpf.ViewModels }); } - //#region 曲线配置 + #region 曲线配置 //private LightningChart _LightningChartInstance; ///// @@ -127,6 +135,6 @@ namespace CapMachine.Wpf.ViewModels //} - //#endregion + #endregion } } diff --git a/CapMachine.Wpf/Views/RealTimeChartView.xaml.cs b/CapMachine.Wpf/Views/RealTimeChartView.xaml.cs index 036b859..b6c4e44 100644 --- a/CapMachine.Wpf/Views/RealTimeChartView.xaml.cs +++ b/CapMachine.Wpf/Views/RealTimeChartView.xaml.cs @@ -5,6 +5,7 @@ using Arction.Wpf.Charting.SeriesXY; using Arction.Wpf.Charting.Views.ViewXY; using CapMachine.Wpf.Models; using CapMachine.Wpf.PrismEvent; +using CapMachine.Wpf.Services; using CapMachine.Wpf.ViewModels; using NLog; using Prism.Events; @@ -24,16 +25,21 @@ namespace CapMachine.Wpf.Views /// 定时器 /// static System.Timers.Timer CurTimer { get; set; } - + private IEventAggregator _EventAggregator { get; set; } + public MachineRtDataService MachineRtDataService { get; } /// /// 曲线配置 /// - public ChartConfig ChartConfigInfo { get; set; } + public ChartConfig ChartConfigInfo { get; set; } - - public RealTimeChartView(IEventAggregator eventAggregator) + /// + /// 实例化函数 + /// + /// + /// + public RealTimeChartView(IEventAggregator eventAggregator, MachineRtDataService machineRtDataService) { InitializeComponent(); @@ -44,7 +50,10 @@ namespace CapMachine.Wpf.Views //事件服务 _EventAggregator = eventAggregator; + MachineRtDataService = machineRtDataService; + _EventAggregator.GetEvent().Subscribe(GetChartRtEvent); + _EventAggregator.GetEvent().Subscribe(ReLoadData); var CreateAxisY = new AxisY(); CreateAxisY.Title.Text = Name; @@ -56,6 +65,17 @@ namespace CapMachine.Wpf.Views ListChartRtSeries.Add(new ChartRtSeries(CreateAxisY, lightningChart1, "室温0")); } + /// + /// 重新加载数据 + /// 更新曲线和Y轴设置 + /// + /// + /// + private void ReLoadData(ChartActionType type) + { + throw new NotImplementedException(); + } + /// /// 获取曲线的实时值 @@ -82,7 +102,7 @@ namespace CapMachine.Wpf.Views } }); - + }