136 lines
4.2 KiB
C#
136 lines
4.2 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;
|
|
|
|
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 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; });
|
|
}
|
|
|
|
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}");
|
|
}
|
|
}
|
|
}
|