Skip to content

Commit

Permalink
Fix the GetStationTwinAsync to be handle non existing twins (#2074)
Browse files Browse the repository at this point in the history
* Change get twin to return null when non existing device

* Change to be like other patterns
  • Loading branch information
Mandur authored Oct 17, 2023
1 parent d9848b6 commit d301be6
Showing 1 changed file with 2 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
namespace LoRaTools.IoTHubImpl
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LoRaWan;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public sealed class IoTHubRegistryManager : IDeviceRegistryManager, IDisposable
{
Expand Down Expand Up @@ -83,9 +77,6 @@ public IRegistryPageResult<ILoRaDeviceTwin> GetAllLoRaDevices()
return new IoTHubLoRaDeviceTwinPageResult(q);
}

public async Task<IStationTwin> GetStationTwinAsync(StationEui stationEui, CancellationToken? cancellationToken = null)
=> new IoTHubStationTwin(await this.instance.GetTwinAsync(stationEui.ToString(), cancellationToken ?? CancellationToken.None));

public IRegistryPageResult<ILoRaDeviceTwin> GetLastUpdatedLoRaDevices(DateTime lastUpdateDateTime)
{
var formattedDateTime = lastUpdateDateTime.ToString(Constants.RoundTripDateTimeStringFormat, CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -116,5 +107,7 @@ public async Task<ILoRaDeviceTwin> GetLoRaDeviceTwinAsync(string deviceId, Cance

public async Task<IDeviceTwin> GetTwinAsync(string deviceId, CancellationToken? cancellationToken = null)
=> await this.instance.GetTwinAsync(deviceId, cancellationToken ?? CancellationToken.None) is { } twin ? new IoTHubDeviceTwin(twin) : null;
public async Task<IStationTwin> GetStationTwinAsync(StationEui stationEui, CancellationToken? cancellationToken = null)
=> await this.instance.GetTwinAsync(stationEui.ToString(), cancellationToken ?? CancellationToken.None) is { } twin ? new IoTHubStationTwin(twin) : null;
}
}

0 comments on commit d301be6

Please sign in to comment.