From 2cefa5f21b3c00ac67ba4d3924342249600b67c7 Mon Sep 17 00:00:00 2001 From: Vit Nemecky Date: Thu, 15 Feb 2024 23:03:08 +0100 Subject: [PATCH] Enable scanning of devices powered by BuWizz (#22) Co-authored-by: Vit Nemecky --- .../BrickController2/UI/Pages/DevicePage.xaml | 2 + .../UI/ViewModels/DevicePageViewModel.cs | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/BrickController2/BrickController2/UI/Pages/DevicePage.xaml b/BrickController2/BrickController2/UI/Pages/DevicePage.xaml index 8faff9f5..e9133929 100644 --- a/BrickController2/BrickController2/UI/Pages/DevicePage.xaml +++ b/BrickController2/BrickController2/UI/Pages/DevicePage.xaml @@ -129,6 +129,8 @@ + + diff --git a/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs b/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs index 32140b7d..5b2f9a09 100644 --- a/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs +++ b/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs @@ -46,15 +46,18 @@ public DevicePageViewModel( RenameCommand = new SafeCommand(async () => await RenameDeviceAsync()); BuWizzOutputLevelChangedCommand = new SafeCommand(outputLevel => SetBuWizzOutputLevel(outputLevel)); BuWizz2OutputLevelChangedCommand = new SafeCommand(outputLevel => SetBuWizzOutputLevel(outputLevel)); + ScanCommand = new SafeCommand(async () => await ScanAsync(), () => Device.CanBePowerSource && !_deviceManager.IsScanning); } public Device Device { get; } public bool IsBuWizzDevice => Device.DeviceType == DeviceType.BuWizz; public bool IsBuWizz2Device => Device.DeviceType == DeviceType.BuWizz2; + public bool CanBePowerSource => Device.CanBePowerSource; public ICommand RenameCommand { get; } public ICommand BuWizzOutputLevelChangedCommand { get; } public ICommand BuWizz2OutputLevelChangedCommand { get; } + public ICommand ScanCommand { get; } public int BuWizzOutputLevel { get; set; } = 1; public int BuWizz2OutputLevel { get; set; } = 1; @@ -214,6 +217,66 @@ await _dialogService.ShowMessageBoxAsync( } } + private async Task ScanAsync() + { + if (!_deviceManager.IsBluetoothOn) + { + await _dialogService.ShowMessageBoxAsync( + Translate("Warning"), + Translate("BluetoothIsTurnedOff"), + Translate("Ok"), + _disappearingTokenSource.Token); + } + + var percent = 0; + var scanResult = true; + await _dialogService.ShowProgressDialogAsync( + true, + async (progressDialog, token) => + { + if (!_isDisappearing) + { + using (var cts = new CancellationTokenSource()) + using (_disappearingTokenSource.Token.Register(() => cts.Cancel())) + { + Task scanTask = null; + try + { + scanTask = _deviceManager.ScanAsync(cts.Token); + + while (!token.IsCancellationRequested && percent <= 100 && !scanTask.IsCompleted) + { + progressDialog.Percent = percent; + await Task.Delay(100, token); + percent += 1; + } + } + catch (Exception) + { } + + cts.Cancel(); + + if (scanTask != null) + { + scanResult = await scanTask; + } + } + } + }, + Translate("Scanning"), + Translate("SearchingForDevices"), + Translate("Cancel")); + + if (!scanResult && !_isDisappearing) + { + await _dialogService.ShowMessageBoxAsync( + Translate("Warning"), + Translate("ErrorDuringScanning"), + Translate("Ok"), + CancellationToken.None); + } + } + private void OnDeviceDisconnected(Device device) { }