ScannerViewModel.cs 2.6 KB

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