Files
CSI-Z420-Tablet-Multi-Funct…/ViewModels/MainViewModel.cs

148 lines
4.6 KiB
C#
Raw Normal View History

2026-05-05 15:31:24 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Threading.Tasks;
2026-05-06 16:41:32 +08:00
using System.Windows;
2026-05-05 15:31:24 +08:00
using System.Windows.Threading;
using TabletTester2025.Models;
using TabletTester2025.Services;
2026-05-14 15:22:17 +08:00
using .Views;
2026-05-05 15:31:24 +08:00
namespace TabletTester2025.ViewModels
{
public partial class MainViewModel : ObservableObject
{
private readonly IPlcService _plc;
private readonly DatabaseService _db;
private readonly ExcelExportService _excel;
private readonly AlarmService _alarm;
private readonly PlcConfiguration _plcConfig;
private DispatcherTimer _timer;
2026-05-16 17:47:46 +08:00
private bool _isConnecting;
private bool _isUpdatingRealtime;
2026-05-05 15:31:24 +08:00
2026-05-06 16:41:32 +08:00
2026-05-05 15:31:24 +08:00
[ObservableProperty] private string _plcStatus = "断开";
[ObservableProperty] private string _currentTime;
[ObservableProperty] private string _globalAlarm;
2026-05-16 16:58:57 +08:00
public StationViewModel Tester { get; }
2026-05-05 15:31:24 +08:00
public IAsyncRelayCommand ExportAllCommand { get; }
2026-05-06 16:41:32 +08:00
public IAsyncRelayCommand OpenSettingsCommand { get; }
public IAsyncRelayCommand OpenHistoryCommand { get; }
public IAsyncRelayCommand OpenCalibrationCommand { get; }
2026-05-14 15:22:17 +08:00
public IAsyncRelayCommand ShowDataCommand { get; }
2026-05-06 16:41:32 +08:00
2026-05-05 15:31:24 +08:00
public MainViewModel(IPlcService plc, DatabaseService db, ExcelExportService excel, AlarmService alarm, PlcConfiguration plcConfig)
{
_plc = plc;
_db = db;
_excel = excel;
_alarm = alarm;
_plcConfig = plcConfig;
_alarm.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(AlarmService.CurrentAlarm)) GlobalAlarm = _alarm.CurrentAlarm; };
2026-05-16 16:58:57 +08:00
Tester = new StationViewModel(1, _plc, _plcConfig, _db, _excel, _alarm);
2026-05-05 15:31:24 +08:00
ExportAllCommand = new AsyncRelayCommand(ExportAllAsync);
2026-05-16 17:47:46 +08:00
_timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
2026-05-05 15:31:24 +08:00
_timer.Tick += OnTimerTick;
_ = ConnectToPlc();
_timer.Start();
2026-05-06 16:41:32 +08:00
2026-05-18 15:18:28 +08:00
OpenSettingsCommand = new AsyncRelayCommand(() =>
{
var window = new SettingsWindow();
2026-05-19 17:14:29 +08:00
window.Owner = Application.Current.MainWindow;
2026-05-18 15:18:28 +08:00
if (window.ShowDialog() == true)
Tester.ApplyPharmaDefaults();
return Task.CompletedTask;
});
2026-05-06 16:41:32 +08:00
OpenHistoryCommand = new AsyncRelayCommand(() => { new HistoryWindow().ShowDialog(); return Task.CompletedTask; });
2026-05-14 15:22:17 +08:00
// 跳转到 PlcDataPage 页面
2026-05-15 09:04:36 +08:00
ShowDataCommand = new AsyncRelayCommand(async () =>
{
// 用你项目里已有的PLC实例假设叫 _plcClient
2026-05-19 18:22:00 +08:00
var window = new ShowData(_plc, _plcConfig);
2026-05-19 17:14:29 +08:00
window.Owner = Application.Current.MainWindow;
2026-05-15 09:04:36 +08:00
window.ShowDialog();
});
2026-05-05 15:31:24 +08:00
}
private async Task ConnectToPlc()
{
2026-05-16 17:47:46 +08:00
if (_isConnecting)
return;
try
{
_isConnecting = true;
PlcStatus = "连接中";
await _plc.ConnectAsync();
2026-05-18 14:06:04 +08:00
PlcStatus = await _plc.CheckConnectionAsync() ? "已连接" : "连接失败";
2026-05-16 17:47:46 +08:00
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isConnecting = false;
}
2026-05-05 15:31:24 +08:00
}
private async void OnTimerTick(object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToString("HH:mm:ss");
2026-05-16 17:47:46 +08:00
if (!_plc.IsConnected)
{
await ConnectToPlc();
return;
}
2026-05-18 14:06:04 +08:00
if (!await _plc.CheckConnectionAsync())
{
PlcStatus = "连接失败";
return;
}
2026-05-16 17:47:46 +08:00
PlcStatus = "已连接";
if (_isUpdatingRealtime)
return;
try
{
_isUpdatingRealtime = true;
await Tester.UpdateRealTimeData();
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isUpdatingRealtime = false;
}
2026-05-05 15:31:24 +08:00
}
private async Task ExportAllAsync()
{
var batches = _db.GetBatches(null, 1000);
2026-05-16 16:58:57 +08:00
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"片剂四用仪检测报告_{DateTime.Now:yyyyMMddHHmmss}.xlsx");
2026-05-05 15:31:24 +08:00
_excel.ExportToExcel(batches, path);
System.Windows.MessageBox.Show($"报告已导出到:{path}");
}
}
2026-05-16 16:58:57 +08:00
}