ScannerViewModel.cs 3.0 KB

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