新增控制传感器的线程保持传感器与图表的独立
This commit is contained in:
parent
b19325cd2a
commit
f770f4b9fd
|
|
@ -365,7 +365,13 @@
|
|||
|
||||
<!-- 右侧按钮组 -->
|
||||
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
|
||||
<Button Content="检测确定" Width="70" Height="30" Margin="5,0" Background="#E6F3FF"/>
|
||||
<Button x:Name="BtnSensorControl"
|
||||
Content="检测开始"
|
||||
Width="70" Height="30"
|
||||
Margin="5,0"
|
||||
Background="#E6F3FF"
|
||||
Click="BtnSensorControl_Click"/>
|
||||
|
||||
<Button Content="保存" Width="50" Height="30" Margin="5,0"/>
|
||||
<Button Content="完成" Width="50" Height="30" Margin="5,0" Background="#90EE90"/>
|
||||
</StackPanel>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
using guoke;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Wpf;
|
||||
using SqlSugar;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
|
@ -22,6 +20,9 @@ public partial class MainWindow : Window
|
|||
private readonly MenuController menu;
|
||||
private readonly ChartManager chartManager;
|
||||
private readonly CancellationTokenSource chartCts = new CancellationTokenSource();
|
||||
private readonly SensorChartManager sensorManager;
|
||||
|
||||
private bool isDetecting = false; // 传感器检测状态
|
||||
|
||||
public MainWindow(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> eventService)
|
||||
{
|
||||
|
|
@ -31,6 +32,14 @@ public partial class MainWindow : Window
|
|||
db = databaseService;
|
||||
even = eventService;
|
||||
menu = new MenuController(this);// 初始化菜单控制器
|
||||
sensorManager = new SensorChartManager(this)
|
||||
{
|
||||
Sensor1 = Sensor1,
|
||||
Sensor2 = Sensor2,
|
||||
Sensor3 = Sensor3
|
||||
};
|
||||
|
||||
sensorManager.Stop();
|
||||
|
||||
// 记录窗口初始化日志
|
||||
log.Info("MainWindow", "主窗口已通过依赖注入初始化");
|
||||
|
|
@ -50,6 +59,12 @@ public partial class MainWindow : Window
|
|||
chartManager.StartAutoUpdate(myChart1, chartCts.Token);
|
||||
chartManager.StartAutoUpdate(myChart2, chartCts.Token);
|
||||
|
||||
Sensor1.SensorName = "温度传感器";
|
||||
Sensor1.Value = -20;
|
||||
|
||||
Sensor2.SensorName = "湿度传感器";
|
||||
Sensor2.Value = 80;
|
||||
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
|
|
@ -58,12 +73,7 @@ public partial class MainWindow : Window
|
|||
scope.guokeCheckToCreate<aaa>();//检查并创建表
|
||||
even.TriggerEvent("GeneralEvent", this, new GeneralEventArgs("测试", 1));
|
||||
}
|
||||
public class aaa : BaseTableModel
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class aaa : BaseTableModel{}
|
||||
|
||||
#region 菜单点击按钮绑定
|
||||
private void OnFilePageButtonClick(object sender, RoutedEventArgs e) => menu.OpenFilePage();
|
||||
|
|
@ -74,28 +84,33 @@ public partial class MainWindow : Window
|
|||
private void OnSysSetPageButtonClick(object sender, RoutedEventArgs e) => menu.OpenSysSetPage();
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 关闭和测试按钮
|
||||
/// <summary>
|
||||
/// 按钮点击触发关闭页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) => WindowHelper.CloseWindow(this);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 测试按钮
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnDemoButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
//LineBarChartDemo lineBarChartDemo = new LineBarChartDemo();
|
||||
//lineBarChartDemo.Show();
|
||||
}
|
||||
private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) => WindowHelper.CloseWindow(this); /// 按钮点击触发关闭页面
|
||||
private void OnDemoButtonClick(object sender, RoutedEventArgs e){} /// 测试按钮
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 控制传感器的采集开始和结束
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void BtnSensorControl_Click(object sender, RoutedEventArgs e) {
|
||||
if (!isDetecting)
|
||||
{
|
||||
// 开始检测
|
||||
sensorManager.Start();
|
||||
BtnSensorControl.Content = "检测确定";
|
||||
isDetecting = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 停止检测
|
||||
sensorManager.Stop();
|
||||
BtnSensorControl.Content = "检测开始";
|
||||
isDetecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等级选择下拉框变化事件处理
|
||||
/// </summary>
|
||||
|
|
|
|||
65
Services/SensorChartManager.cs
Normal file
65
Services/SensorChartManager.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using System.Windows;
|
||||
using WpfApp.src.components;
|
||||
|
||||
namespace WpfApp.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// SensorChart 管理器 - 负责传感器数据采集和更新
|
||||
/// </summary>
|
||||
public class SensorChartManager
|
||||
{
|
||||
private readonly Window dispatcherOwner;
|
||||
private CancellationTokenSource cts;
|
||||
private readonly Random rand = new Random();
|
||||
|
||||
// 绑定的传感器控件
|
||||
public SensorChart Sensor1 { get; set; }
|
||||
public SensorChart Sensor2 { get; set; }
|
||||
public SensorChart Sensor3 { get; set; }
|
||||
|
||||
public SensorChartManager(Window owner)
|
||||
{
|
||||
dispatcherOwner = owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启动模拟数据采集
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
if (Sensor1 == null || Sensor2 == null || Sensor3 == null)
|
||||
throw new InvalidOperationException("请先绑定传感器控件");
|
||||
|
||||
cts = new CancellationTokenSource();
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (!cts.Token.IsCancellationRequested)
|
||||
{
|
||||
// 模拟数据
|
||||
double s1 = rand.NextDouble() * 100 - 50;
|
||||
double s2 = rand.NextDouble() * 100 - 50;
|
||||
double s3 = rand.NextDouble() * 100 - 50;
|
||||
|
||||
// 更新 UI
|
||||
dispatcherOwner.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Sensor1.SetSensorData("传感器1", s1);
|
||||
Sensor2.SetSensorData("传感器2", s2);
|
||||
Sensor3.SetSensorData("传感器3", s3);
|
||||
});
|
||||
|
||||
await Task.Delay(100); // 每100毫秒采集一次
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止采集
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
cts?.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user