ScannerViewModel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using GeneralData;
  4. using GlobalData;
  5. using System.Windows;
  6. using UICommon.DataType;
  7. namespace DeviceScanner.ViewModels;
  8. public partial class ScannerViewModel : ObservableObject
  9. {
  10. public ScannerViewModel(DeviceCollection deviceCollection, IDialogService dialogService)
  11. {
  12. this.DeviceInfo = new()
  13. {
  14. DeviceModel = DeviceModel.JetKepler,
  15. DeviceSubModel = KeplerSubModel.JetKepler_2300.ToString(),
  16. SoftwareVersion = "1.0.0.12",
  17. IP = "192.168.250.122",
  18. Port = 50003,
  19. };
  20. this._DialogService = dialogService;
  21. this.ScanningDevices = [];
  22. this._DeviceCollection = deviceCollection;
  23. this.EnableShader = false;
  24. this.ShaderVisibility = Visibility.Collapsed;
  25. }
  26. private readonly IDialogService _DialogService;
  27. private readonly DeviceCollection _DeviceCollection;
  28. [ObservableProperty]
  29. private ObservableDictionary<string, DeviceInfo_VM> _ScanningDevices;
  30. [ObservableProperty]
  31. private DeviceInfo_VM _DeviceInfo;
  32. [ObservableProperty]
  33. private bool _EnableShader;
  34. [ObservableProperty]
  35. private Visibility _ShaderVisibility;
  36. [RelayCommand]
  37. private void CreateDevice(DeviceInfo_VM deviceInfo)
  38. {
  39. IDialogParameters paras = new DialogParameters
  40. {
  41. { "Device", deviceInfo }
  42. };
  43. this._DialogService.Show("CreateDevice", paras, null);
  44. }
  45. [RelayCommand]
  46. private void Scan(string para)
  47. {
  48. switch (para)
  49. {
  50. case "Start":
  51. this.ShaderVisibility = Visibility.Visible;
  52. this.EnableShader = true;
  53. if (this._FakeDataTimer is not null)
  54. return;
  55. this.ScanningDevices.Clear();
  56. this._FakeDataTimer = new(FakeDataTimerCallback, null, 5000, 5000);
  57. break;
  58. case "Stop":
  59. this.ShaderVisibility = Visibility.Collapsed;
  60. this.EnableShader = false;
  61. this._FakeDataTimer?.Dispose();
  62. this._FakeDataTimer = null;
  63. break;
  64. case "Clear":
  65. if (this._FakeDataTimer is not null)
  66. return;
  67. this.ScanningDevices.Clear();
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. private Timer? _FakeDataTimer;
  74. private void FakeDataTimerCallback(object? state)
  75. {
  76. Random rnd = new();
  77. DeviceModel model = (DeviceModel)rnd.Next(0, 2);
  78. int i = rnd.Next(0, 4);
  79. object subModel = model switch
  80. {
  81. DeviceModel.JetKepler => (KeplerSubModel)i,
  82. DeviceModel.Proxima => (ProximaSubModel)i,
  83. _ => string.Empty
  84. };
  85. DeviceInfo_VM device = new()
  86. {
  87. DeviceModel = model,
  88. DeviceSubModel = subModel.ToString(),
  89. SoftwareVersion = $"1.0.0.{rnd.Next(1, 20)}",
  90. IP = $"192.168.250.{rnd.Next(1, 255)}",
  91. Port = 50003,
  92. };
  93. Application.Current.Dispatcher?.Invoke(() =>
  94. {
  95. this.ScanningDevices.Add(device.IP, device);
  96. });
  97. }
  98. }