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