Files
CSI-Z420-Tablet-Multi-Funct…/ViewModels/MainViewModel.cs
2026-05-19 18:22:00 +08:00

148 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using TabletTester2025.Models;
using TabletTester2025.Services;
using .Views;
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;
private bool _isConnecting;
private bool _isUpdatingRealtime;
[ObservableProperty] private string _plcStatus = "断开";
[ObservableProperty] private string _currentTime;
[ObservableProperty] private string _globalAlarm;
public StationViewModel Tester { get; }
public IAsyncRelayCommand ExportAllCommand { get; }
public IAsyncRelayCommand OpenSettingsCommand { get; }
public IAsyncRelayCommand OpenHistoryCommand { get; }
public IAsyncRelayCommand OpenCalibrationCommand { get; }
public IAsyncRelayCommand ShowDataCommand { get; }
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; };
Tester = new StationViewModel(1, _plc, _plcConfig, _db, _excel, _alarm);
ExportAllCommand = new AsyncRelayCommand(ExportAllAsync);
_timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_timer.Tick += OnTimerTick;
_ = ConnectToPlc();
_timer.Start();
OpenSettingsCommand = new AsyncRelayCommand(() =>
{
var window = new SettingsWindow();
window.Owner = Application.Current.MainWindow;
if (window.ShowDialog() == true)
Tester.ApplyPharmaDefaults();
return Task.CompletedTask;
});
OpenHistoryCommand = new AsyncRelayCommand(() => { new HistoryWindow().ShowDialog(); return Task.CompletedTask; });
// 跳转到 PlcDataPage 页面
ShowDataCommand = new AsyncRelayCommand(async () =>
{
// 用你项目里已有的PLC实例假设叫 _plcClient
var window = new ShowData(_plc, _plcConfig);
window.Owner = Application.Current.MainWindow;
window.ShowDialog();
});
}
private async Task ConnectToPlc()
{
if (_isConnecting)
return;
try
{
_isConnecting = true;
PlcStatus = "连接中";
await _plc.ConnectAsync();
PlcStatus = await _plc.CheckConnectionAsync() ? "已连接" : "连接失败";
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isConnecting = false;
}
}
private async void OnTimerTick(object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToString("HH:mm:ss");
if (!_plc.IsConnected)
{
await ConnectToPlc();
return;
}
if (!await _plc.CheckConnectionAsync())
{
PlcStatus = "连接失败";
return;
}
PlcStatus = "已连接";
if (_isUpdatingRealtime)
return;
try
{
_isUpdatingRealtime = true;
await Tester.UpdateRealTimeData();
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isUpdatingRealtime = false;
}
}
private async Task ExportAllAsync()
{
var batches = _db.GetBatches(null, 1000);
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"片剂四用仪检测报告_{DateTime.Now:yyyyMMddHHmmss}.xlsx");
_excel.ExportToExcel(batches, path);
System.Windows.MessageBox.Show($"报告已导出到:{path}");
}
}
}