DeviceManagerViewModel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer");
  40. try
  41. {
  42. object o = deviceInfo.DeviceModel switch
  43. {
  44. DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
  45. DeviceModel.Proxima => Process.Start(Path.Combine(path, "Proxima", "ProximaAnalizer.exe")),
  46. _ => MessageBox.Show("Undefined Type")
  47. };
  48. }
  49. catch
  50. {
  51. }
  52. }
  53. [RelayCommand]
  54. private void Remote(DeviceInfo_VM deviceInfo)
  55. {
  56. //string path = Path.Combine(System.Environment.CurrentDirectory, "Analizer");
  57. try
  58. {
  59. object o = deviceInfo.DeviceModel switch
  60. {
  61. //DeviceModel.JetKepler => Process.Start(Path.Combine(path, "Kepler", "KeplerAnalizer.exe")),
  62. DeviceModel.Proxima => Process.Start(@"E:\Furnace\Furnace\FurnaceUI\FurnaceUI.exe"),
  63. _ => MessageBox.Show("Undefined Type")
  64. };
  65. }
  66. catch
  67. {
  68. }
  69. }
  70. [RelayCommand]
  71. private async Task Delete(DeviceInfo_VM deviceInfo)
  72. {
  73. if (deviceInfo.Guid is null || deviceInfo.Guid == Guid.Empty)
  74. return;
  75. MessageBoxResult result = MessageBox.Show($"确认删除 {deviceInfo.DeviceModel}-{deviceInfo.DeviceName} ?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
  76. if (result != MessageBoxResult.Yes)
  77. return;
  78. await this._UICaller.RemoveDevice(deviceInfo.Guid.Value);
  79. await this.Refresh();
  80. //this._DeviceCollection.DeviceList.Remove(deviceInfo.Guid.Value);
  81. //MessageBox.Show($"{deviceInfo.DeviceModel}-{deviceInfo.DeviceName} 已删除");
  82. }
  83. [RelayCommand]
  84. private void Setting(DeviceInfo_VM deviceInfo)
  85. {
  86. IDialogParameters para = new DialogParameters() { { "Device", deviceInfo } };
  87. this._DialogService.Show("EditDevice", para, null);
  88. }
  89. [RelayCommand]
  90. private async Task Refresh()
  91. {
  92. IEnumerable<DeviceInfo> devices = await this._UICaller.RequestDeviceLists();
  93. if (devices is null || !devices.Any())
  94. return;
  95. this._DeviceCollection.DeviceList.Clear();
  96. foreach (DeviceInfo device in devices)
  97. {
  98. if (device.Guid is null)
  99. continue;
  100. this._DeviceCollection.DeviceList[device.Guid.Value] = device.Adapt<DeviceInfo_VM>();
  101. }
  102. }
  103. }