136 lines
4.4 KiB
C#
136 lines
4.4 KiB
C#
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(() => { new SettingsWindow().ShowDialog(); return Task.CompletedTask; });
|
||
OpenHistoryCommand = new AsyncRelayCommand(() => { new HistoryWindow().ShowDialog(); return Task.CompletedTask; });
|
||
OpenCalibrationCommand = new AsyncRelayCommand(() => { MessageBox.Show("校准功能待实现"); return Task.CompletedTask; });
|
||
|
||
// 跳转到 PlcDataPage 页面
|
||
|
||
ShowDataCommand = new AsyncRelayCommand(async () =>
|
||
{
|
||
// 用你项目里已有的PLC实例(假设叫 _plcClient)
|
||
var window = new ShowData(_plc);
|
||
window.ShowDialog();
|
||
});
|
||
}
|
||
|
||
private async Task ConnectToPlc()
|
||
{
|
||
if (_isConnecting)
|
||
return;
|
||
|
||
try
|
||
{
|
||
_isConnecting = true;
|
||
PlcStatus = "连接中";
|
||
await _plc.ConnectAsync();
|
||
PlcStatus = _plc.IsConnected ? "已连接" : "连接失败";
|
||
}
|
||
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;
|
||
}
|
||
|
||
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}");
|
||
}
|
||
}
|
||
}
|