Files
CapMachine/CapMachine.Wpf/Services/MachineRtDataService.cs
2024-07-12 10:39:55 +08:00

240 lines
8.9 KiB
C#

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
{
/// <summary>
/// 机器实时数据服务
/// </summary>
public class MachineRtDataService : BindableBase
{
/// <summary>
/// 事件聚合器
/// </summary>
private IEventAggregator _EventAggregator { get; set; }
/// <summary>
/// ScanTask扫描Task
/// </summary>
static Task ScanTask { get; set; }
/// <summary>
/// 仪表数据集合
/// </summary>
public List<MeterRtDataModel> ListMeterRtData { get; set; }
/// <summary>
/// 扫描线程使能
/// </summary>
public bool ThreadEnable { get; set; } = true;
/// <summary>
/// 历史数据
/// </summary>
public List<RecordInfo> HistoryData = new List<RecordInfo>();
/// <summary>
/// 实例化函数
/// </summary>
/// <param name="eventAggregator"></param>
public MachineRtDataService(IEventAggregator eventAggregator)
{
//ConcurrentDictionary<DateTime, RecordInfo> keyValuePairs = new ConcurrentDictionary<DateTime, RecordInfo>();
//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<MeterRtDataModel>()
{
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();
/// <summary>
///发布实时数据
/// </summary>
private void PubRtDataStart()
{
ScanTask = Task.Run(async () =>
{
while (ThreadEnable)
{
try
{
await Task.Delay(1000);
_EventAggregator.GetEvent<ChartRtEvent>().Publish(new List<Models.ChartRtValue>()
{
new Models.ChartRtValue(){Name="EVA风量",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="中间轴转速",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="加热电力",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="加湿电力",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="EMPCV电流",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="INJ压力",Value=random.NextDouble()*100,Unit="℃"},
new Models.ChartRtValue(){Name="冷媒流量",Value=random.NextDouble()*100,Unit="℃"},
});
}
catch (Exception ex)
{
var dd = 1;
}
}
});
}
}
}