为传感器和曲线加上日志和事件服务
This commit is contained in:
parent
f770f4b9fd
commit
f8e65d8e52
|
|
@ -1,4 +1,5 @@
|
|||
using LiveCharts.Wpf;
|
||||
using guoke;
|
||||
using LiveCharts.Wpf;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using WpfApp.src.config;
|
||||
|
|
@ -12,11 +13,20 @@ namespace WpfApp.Services
|
|||
public class ChartManager
|
||||
{
|
||||
private readonly Dispatcher dispatcher;
|
||||
private readonly LogService log;
|
||||
private readonly DatabaseService db;
|
||||
private readonly EventService<GeneralEventArgs> even;
|
||||
|
||||
public ChartManager(Dispatcher uiDispatcher)
|
||||
{
|
||||
dispatcher = uiDispatcher;
|
||||
}
|
||||
public ChartManager(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> eventService)
|
||||
{
|
||||
log = logService;
|
||||
db = databaseService;
|
||||
even = eventService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化图表
|
||||
|
|
@ -73,18 +83,21 @@ namespace WpfApp.Services
|
|||
{
|
||||
// 模拟采集数据
|
||||
var columnData = Enumerable.Range(0, 5).Select(_ => rand.NextDouble() * 50).ToList();
|
||||
// 这里实时获取从SensorChartManager中
|
||||
var lineData = new List<LineConfig>
|
||||
{
|
||||
new LineConfig { Title = "标准1", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*20).ToList(), Stroke = Brushes.Blue },
|
||||
new LineConfig { Title = "标准2", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*30).ToList(), Stroke = Brushes.Green },
|
||||
new LineConfig { Title = "标准3", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*40).ToList(), Stroke = Brushes.Yellow }
|
||||
|
||||
//new LineConfig { Title = "标准1", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*20).ToList(), Stroke = Brushes.Blue },
|
||||
//new LineConfig { Title = "标准2", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*30).ToList(), Stroke = Brushes.Green },
|
||||
//new LineConfig { Title = "标准3", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*40).ToList(), Stroke = Brushes.Yellow }
|
||||
new LineConfig { Title = "标准1", Values = new List<double> { 10, 10, 10, 10, 10 }, Stroke = Brushes.Blue },
|
||||
new LineConfig { Title = "标准2", Values = new List<double> { 20, 20, 20, 20, 20 }, Stroke = Brushes.Green },
|
||||
new LineConfig { Title = "标准3", Values = new List<double> { 30, 30, 30, 30, 30 }, Stroke = Brushes.Yellow }
|
||||
};
|
||||
|
||||
// 更新图表(通过 Dispatcher 保证线程安全)
|
||||
UpdateChart(chart, columnData, lineData);
|
||||
|
||||
await Task.Delay(5000); // 每秒更新一次
|
||||
await Task.Delay(6000); // 每秒更新一次
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Windows;
|
||||
using guoke;
|
||||
using System.Windows;
|
||||
using WpfApp.src.components;
|
||||
|
||||
namespace WpfApp.Services
|
||||
|
|
@ -11,6 +12,10 @@ namespace WpfApp.Services
|
|||
private readonly Window dispatcherOwner;
|
||||
private CancellationTokenSource cts;
|
||||
private readonly Random rand = new Random();
|
||||
private readonly LogService log;
|
||||
private readonly DatabaseService db;
|
||||
private readonly EventService<GeneralEventArgs> even;
|
||||
|
||||
|
||||
// 绑定的传感器控件
|
||||
public SensorChart Sensor1 { get; set; }
|
||||
|
|
@ -21,6 +26,12 @@ namespace WpfApp.Services
|
|||
{
|
||||
dispatcherOwner = owner;
|
||||
}
|
||||
public SensorChartManager(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> eventService)
|
||||
{
|
||||
log = logService;
|
||||
db = databaseService;
|
||||
even = eventService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启动模拟数据采集
|
||||
|
|
@ -47,9 +58,11 @@ namespace WpfApp.Services
|
|||
Sensor1.SetSensorData("传感器1", s1);
|
||||
Sensor2.SetSensorData("传感器2", s2);
|
||||
Sensor3.SetSensorData("传感器3", s3);
|
||||
// 这里应当跨线程将传感器1 2 3的同时将实时值传递到ChartManger中
|
||||
|
||||
});
|
||||
|
||||
await Task.Delay(100); // 每100毫秒采集一次
|
||||
await Task.Delay(1000); // 每1000毫秒采集一次
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user