diff --git a/MainWindow.xaml b/MainWindow.xaml
index 6360984..0f6e403 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -365,7 +365,13 @@
-
+
+
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 2575b7a..8a2c745 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -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 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();//检查并创建表
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 关闭和测试按钮
- ///
- /// 按钮点击触发关闭页面
- ///
- ///
- ///
- private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) => WindowHelper.CloseWindow(this);
-
-
- ///
- /// 测试按钮
- ///
- ///
- ///
- 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
+
+ ///
+ /// 控制传感器的采集开始和结束
+ ///
+ ///
+ ///
+ private void BtnSensorControl_Click(object sender, RoutedEventArgs e) {
+ if (!isDetecting)
+ {
+ // 开始检测
+ sensorManager.Start();
+ BtnSensorControl.Content = "检测确定";
+ isDetecting = true;
+ }
+ else
+ {
+ // 停止检测
+ sensorManager.Stop();
+ BtnSensorControl.Content = "检测开始";
+ isDetecting = false;
+ }
+ }
+
///
/// 等级选择下拉框变化事件处理
///
diff --git a/Services/SensorChartManager.cs b/Services/SensorChartManager.cs
new file mode 100644
index 0000000..de798c7
--- /dev/null
+++ b/Services/SensorChartManager.cs
@@ -0,0 +1,65 @@
+using System.Windows;
+using WpfApp.src.components;
+
+namespace WpfApp.Services
+{
+ ///
+ /// SensorChart 管理器 - 负责传感器数据采集和更新
+ ///
+ 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;
+ }
+
+ ///
+ /// 启动模拟数据采集
+ ///
+ 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);
+ }
+
+ ///
+ /// 停止采集
+ ///
+ public void Stop()
+ {
+ cts?.Cancel();
+ }
+ }
+}