using Device; using Mapster; using ServiceBase; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using UICommon.DataType; namespace DeviceManagement.ViewModels; public partial class DeviceManagerViewModel : ObservableObject { public DeviceManagerViewModel(DeviceCollection deviceCollection, IDialogService dialogService, IUICaller caller) { this._DeviceCollection = deviceCollection; this._DialogService = dialogService; this._UICaller = caller; deviceCollection.DeviceList.CollectionChanged += DeviceList_CollectionChanged; this.DeviceList_CollectionChanged(deviceCollection.DeviceList, null); } private void DeviceList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs? e) { if (sender is not ObservableDictionary deviceList) return; this.Devices.Clear(); foreach (DeviceInfo_VM device in deviceList.Values) { this.Devices[device.DeviceModel] ??= []; this.Devices[device.DeviceModel].Add(device); } } private readonly IDialogService _DialogService; private readonly DeviceCollection _DeviceCollection; private readonly IUICaller _UICaller; [ObservableProperty] private ObservableDictionary> _Devices = []; [RelayCommand] private void CheckDetail(DeviceInfo_VM deviceInfo) { DirectoryInfo directory = new(AppDomain.CurrentDomain.BaseDirectory); if (directory.Parent is not DirectoryInfo mainFolder) { MessageBox.Show("Folder not exist"); return; } string path = Path.Combine(mainFolder.FullName, "Analizer"); try { object o = deviceInfo.DeviceModel switch { DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")), DeviceModel.Proxima => Process.Start(Path.Combine(path, "Proxima", "ProximaAnalizer.exe")), _ => MessageBox.Show("Undefined Type") }; } catch { MessageBox.Show("Folder not exist"); } } [RelayCommand] private void Remote(DeviceInfo_VM deviceInfo) { //string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer"); try { object o = deviceInfo.DeviceModel switch { //DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")), DeviceModel.Proxima => Process.Start(@"E:\Furnace\Furnace\FurnaceUI\FurnaceUI.exe"), _ => MessageBox.Show("Undefined Type") }; } catch { } } [RelayCommand] private async Task Delete(DeviceInfo_VM deviceInfo) { if (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty) return; MessageBoxResult result = MessageBox.Show($"确认删除 {deviceInfo.DeviceModel}-{deviceInfo.DeviceName} ?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result != MessageBoxResult.Yes) return; await this._UICaller.RemoveDevice(deviceInfo.Guid.Value); await this.Refresh(); //this._DeviceCollection.DeviceList.Remove(deviceInfo.Guid.Value); //MessageBox.Show($"{deviceInfo.DeviceModel}-{deviceInfo.DeviceName} 已删除"); } [RelayCommand] private void Setting(DeviceInfo_VM deviceInfo) { IDialogParameters para = new DialogParameters() { { "Device", deviceInfo } }; this._DialogService.Show("EditDevice", para, null); } [RelayCommand] private async Task Refresh() { IEnumerable devices = await this._UICaller.RequestDeviceLists(); if (devices is null || !devices.Any()) return; this._DeviceCollection.DeviceList.Clear(); foreach (DeviceInfo device in devices) { if (device.Guid is null) continue; this._DeviceCollection.DeviceList[device.Guid.Value] = device.Adapt(); } } }