Skip to content

Commit

Permalink
Enable scanning of devices powered by BuWizz (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Vit Nemecky <[email protected]>
  • Loading branch information
vicocz and Vit Nemecky authored Feb 15, 2024
1 parent 2133b84 commit 2cefa5f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions BrickController2/BrickController2/UI/Pages/DevicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@

</StackLayout>
</ScrollView>

<controls:FloatingActionButton ButtonColor="Red" ImageSource="{extensions:ImageResource Source=ic_search.png}" ImageColor="White" Command="{Binding ScanCommand}" IsVisible="{Binding CanBePowerSource}" HorizontalOptions="End" VerticalOptions="End" Margin="10"/>
</Grid>

<controls:Dialogs x:Name="Dialogs" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ public DevicePageViewModel(
RenameCommand = new SafeCommand(async () => await RenameDeviceAsync());
BuWizzOutputLevelChangedCommand = new SafeCommand<int>(outputLevel => SetBuWizzOutputLevel(outputLevel));
BuWizz2OutputLevelChangedCommand = new SafeCommand<int>(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;
Expand Down Expand Up @@ -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<bool> 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)
{
}
Expand Down

0 comments on commit 2cefa5f

Please sign in to comment.