DeviceManagerViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Device;
  2. using Mapster;
  3. using ServiceBase;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. using UICommon.DataType;
  9. namespace DeviceManagement.ViewModels;
  10. public partial class DeviceManagerViewModel : ObservableObject
  11. {
  12. public DeviceManagerViewModel(DeviceCollection deviceCollection, IDialogService dialogService, IUICaller caller)
  13. {
  14. this._DeviceCollection = deviceCollection;
  15. this._DialogService = dialogService;
  16. this._UICaller = caller;
  17. deviceCollection.DeviceList.CollectionChanged += DeviceList_CollectionChanged;
  18. this.DeviceList_CollectionChanged(deviceCollection.DeviceList, null);
  19. }
  20. private void DeviceList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs? e)
  21. {
  22. if (sender is not ObservableDictionary<Guid, DeviceInfo_VM> deviceList)
  23. return;
  24. this.Devices.Clear();
  25. foreach (DeviceInfo_VM device in deviceList.Values)
  26. {
  27. this.Devices[device.DeviceModel] ??= [];
  28. this.Devices[device.DeviceModel].Add(device);
  29. }
  30. }
  31. private readonly IDialogService _DialogService;
  32. private readonly DeviceCollection _DeviceCollection;
  33. private readonly IUICaller _UICaller;
  34. [ObservableProperty]
  35. private ObservableDictionary<DeviceModel, ObservableCollection<DeviceInfo_VM>> _Devices = [];
  36. [RelayCommand]
  37. private void CheckDetail(DeviceInfo_VM deviceInfo)
  38. {
  39. DirectoryInfo directory = new(AppDomain.CurrentDomain.BaseDirectory);
  40. if (directory.Parent is not DirectoryInfo mainFolder)
  41. {
  42. MessageBox.Show("Folder not exist");
  43. return;
  44. }
  45. string path = Path.Combine(mainFolder.FullName, "Analizer");
  46. try
  47. {
  48. object o = deviceInfo.DeviceModel switch
  49. {
  50. DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
  51. DeviceModel.Proxima => Process.Start(Path.Combine(path, "Proxima", "ProximaAnalizer.exe")),
  52. _ => MessageBox.Show("Undefined Type")
  53. };
  54. }
  55. catch
  56. {
  57. MessageBox.Show("Folder not exist");
  58. }
  59. }
  60. [RelayCommand]
  61. private void Remote(DeviceInfo_VM deviceInfo)
  62. {
  63. //string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer");
  64. try
  65. {
  66. object o = deviceInfo.DeviceModel switch
  67. {
  68. //DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
  69. DeviceModel.Proxima => Process.Start(@"E:\Furnace\Furnace\FurnaceUI\FurnaceUI.exe"),
  70. _ => MessageBox.Show("Undefined Type")
  71. };
  72. }
  73. catch
  74. {
  75. }
  76. }
  77. [RelayCommand]
  78. private async Task Delete(DeviceInfo_VM deviceInfo)
  79. {
  80. if (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty)
  81. return;
  82. MessageBoxResult result = MessageBox.Show($"确认删除 {deviceInfo.DeviceModel}-{deviceInfo.DeviceName} ?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
  83. if (result != MessageBoxResult.Yes)
  84. return;
  85. await this._UICaller.RemoveDevice(deviceInfo.Guid.Value);
  86. await this.Refresh();
  87. //this._DeviceCollection.DeviceList.Remove(deviceInfo.Guid.Value);
  88. //MessageBox.Show($"{deviceInfo.DeviceModel}-{deviceInfo.DeviceName} 已删除");
  89. }
  90. [RelayCommand]
  91. private void Setting(DeviceInfo_VM deviceInfo)
  92. {
  93. IDialogParameters para = new DialogParameters() { { "Device", deviceInfo } };
  94. this._DialogService.Show("EditDevice", para, null);
  95. }
  96. [RelayCommand]
  97. private async Task Refresh()
  98. {
  99. IEnumerable<DeviceInfo> devices = await this._UICaller.RequestDeviceLists();
  100. if (devices is null || !devices.Any())
  101. return;
  102. this._DeviceCollection.DeviceList.Clear();
  103. foreach (DeviceInfo device in devices)
  104. {
  105. if (device.Guid is null)
  106. continue;
  107. this._DeviceCollection.DeviceList[device.Guid.Value] = device.Adapt<DeviceInfo_VM>();
  108. }
  109. }
  110. }