Skip to content

Commit 1b4d736

Browse files
committed
Added exception handlers.
1 parent 41d5d66 commit 1b4d736

File tree

8 files changed

+48
-26
lines changed

8 files changed

+48
-26
lines changed

AntPlus.UnitTests/AntPlus.UnitTests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
1212
<PackageReference Include="Moq" Version="4.20.70" />
13-
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
14-
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
13+
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
14+
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
1515
<PackageReference Include="coverlet.collector" Version="6.0.2">
1616
<PrivateAssets>all</PrivateAssets>
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Examples/MAUI-gRPC/AntGrpcService/AntGrpcService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Grpc.AspNetCore" Version="2.62.0" />
18+
<PackageReference Include="Grpc.AspNetCore" Version="2.63.0" />
1919
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
2020
</ItemGroup>
2121

Examples/MAUI-gRPC/AntGrpcService/Services/DiscoveryService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3333
{
3434
// wait for request to connect to this server
3535
var result = await udpClient.ReceiveAsync(stoppingToken);
36-
_logger.LogInformation("DiscoveryService request: remote endpoint {RemoteEP}, message {Message}", result.RemoteEndPoint, result.Buffer);
36+
_logger.LogInformation("DiscoveryService request: remote endpoint {RemoteEP}, message {Message}", result.RemoteEndPoint, Encoding.ASCII.GetString(result.Buffer));
3737

3838
// I could check the request for some sort of validation but I will ignore the message for now
3939
// send response to requestor's endpoint with our service name
40-
byte[] response = Encoding.ASCII.GetBytes("DiscoveryService");
40+
byte[] response = Encoding.ASCII.GetBytes("AntGrpcServer DiscoveryService response");
4141
udpClient.Send(response, response.Length, result.RemoteEndPoint);
4242
}
4343
}

Examples/MAUI-gRPC/AntGrpcShared/AntGrpcShared.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
19-
<PackageReference Include="Grpc.Net.Client" Version="2.62.0" />
20-
<PackageReference Include="Grpc.Tools" Version="2.62.0">
18+
<PackageReference Include="Google.Protobuf" Version="3.27.0" />
19+
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
20+
<PackageReference Include="Grpc.Tools" Version="2.64.0">
2121
<PrivateAssets>all</PrivateAssets>
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2323
</PackageReference>

Examples/MAUI-gRPC/MauiAntGrpcClient/MauiAntGrpcClient.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
</ItemGroup>
6565

6666
<ItemGroup>
67-
<PackageReference Include="CommunityToolkit.Maui" Version="8.0.1" />
67+
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.0" />
6868
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
69-
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
70-
<PackageReference Include="Grpc.Net.Client" Version="2.62.0" />
71-
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.21" />
72-
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.21" />
69+
<PackageReference Include="Google.Protobuf" Version="3.27.0" />
70+
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
71+
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
72+
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.40" />
7373
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
7474
</ItemGroup>
7575

Examples/MAUI-gRPC/MauiAntGrpcClient/Services/AntChannelService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ public AntChannelService(ILogger<AntChannelService> logger, byte antChannel, Grp
5858
private async void HandleChannelResponseEvents()
5959
{
6060
_response = _client.Subscribe(new SubscribeRequest { ChannelNumber = ChannelNumber });
61-
await foreach (var update in _response.ResponseStream.ReadAllAsync())
61+
try
6262
{
63-
ResponseReceived?.Invoke(this, new GrpcAntResponse(update));
63+
await foreach (ChannelResponse? update in _response.ResponseStream.ReadAllAsync())
64+
{
65+
ResponseReceived?.Invoke(this, new GrpcAntResponse(update));
66+
}
67+
}
68+
catch (Exception ex)
69+
{
70+
_logger.LogWarning(ex, "Error occurred reading from the gRPC stream.");
71+
_response?.Dispose();
6472
}
6573
}
6674

Examples/MAUI-gRPC/MauiAntGrpcClient/Services/AntRadioService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class AntRadioService(ILogger<AntRadioService> logger, ILoggerFac
3232
public async Task FindAntRadioServerAsync()
3333
{
3434
IPEndPoint multicastEndPoint = new(grpAddress, multicastPort);
35-
byte[] req = Encoding.ASCII.GetBytes("AntRadioServer");
35+
byte[] req = Encoding.ASCII.GetBytes("MauiAntGrpcClient discovery request");
3636
UdpReceiveResult result;
3737
using UdpClient udpClient = new(AddressFamily.InterNetwork);
3838

Examples/WpfUsbStickApp/MainWindow.xaml.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SmallEarthTech.AntPlus.DeviceProfiles.BicyclePower;
55
using SmallEarthTech.AntPlus.DeviceProfiles.BikeSpeedAndCadence;
66
using SmallEarthTech.AntPlus.DeviceProfiles.FitnessEquipment;
7+
using System;
78
using System.Windows;
89
using System.Windows.Controls;
910
using System.Windows.Data;
@@ -17,15 +18,25 @@ namespace WpfUsbStickApp
1718
/// </summary>
1819
public partial class MainWindow : Window
1920
{
20-
private readonly MainWindowViewModel viewModel;
21+
private readonly MainWindowViewModel? viewModel;
2122

2223
public MainWindow()
2324
{
2425
InitializeComponent();
25-
viewModel = new MainWindowViewModel();
26-
BindingOperations.EnableCollectionSynchronization(viewModel.AntDevices, viewModel.AntDevices.CollectionLock);
27-
DataContext = viewModel;
28-
antDevices.MouseDoubleClick += AntDevices_MouseDoubleClick;
26+
27+
// the view model may throw an exception if ANT radio is not available
28+
try
29+
{
30+
viewModel = new MainWindowViewModel();
31+
BindingOperations.EnableCollectionSynchronization(viewModel.AntDevices, viewModel.AntDevices.CollectionLock);
32+
DataContext = viewModel;
33+
antDevices.MouseDoubleClick += AntDevices_MouseDoubleClick;
34+
}
35+
catch (Exception ex)
36+
{
37+
_ = MessageBox.Show(ex.Message, "Unable to initialize!", MessageBoxButton.OK, MessageBoxImage.Error);
38+
Application.Current.Shutdown();
39+
}
2940
}
3041

3142
private void AntDevices_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
@@ -86,11 +97,14 @@ private void AntDevices_MouseDoubleClick(object sender, System.Windows.Input.Mou
8697

8798
private async void Capabilities_Click(object sender, RoutedEventArgs e)
8899
{
89-
CapabilitiesWindow capabilitiesWindow = new(await viewModel.UsbAntRadio.GetDeviceCapabilities())
100+
if (viewModel != null)
90101
{
91-
Icon = Icon
92-
};
93-
capabilitiesWindow.Show();
102+
CapabilitiesWindow capabilitiesWindow = new(await viewModel.UsbAntRadio.GetDeviceCapabilities())
103+
{
104+
Icon = Icon
105+
};
106+
capabilitiesWindow.Show();
107+
}
94108
}
95109
}
96110
}

0 commit comments

Comments
 (0)