75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 事件聚合器
|
|
/// </summary>
|
|
private IEventAggregator _EventAggregator { get; set; }
|
|
|
|
/// <summary>
|
|
/// ScanTask扫描Task
|
|
/// </summary>
|
|
static Task ScanTask { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 扫描线程使能
|
|
/// </summary>
|
|
public bool ThreadEnable { get; set; } = true;
|
|
|
|
public PlcRtDataService(IEventAggregator eventAggregator)
|
|
{
|
|
//事件服务
|
|
_EventAggregator = eventAggregator;
|
|
|
|
|
|
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="室温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;
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
}
|
|
}
|
|
}
|