diff --git a/src/IoTHub.Portal.Infrastructure/Services/LoRaWanManagementService.cs b/src/IoTHub.Portal.Infrastructure/Services/LoRaWanManagementService.cs index e2199400d..56fb25c0f 100644 --- a/src/IoTHub.Portal.Infrastructure/Services/LoRaWanManagementService.cs +++ b/src/IoTHub.Portal.Infrastructure/Services/LoRaWanManagementService.cs @@ -6,7 +6,6 @@ namespace IoTHub.Portal.Infrastructure.Services using System.Net.Http.Headers; using System.Net.Http.Json; using System.Reflection; - using System.Text; using System.Text.Json; using System.Threading.Tasks; using IoTHub.Portal.Application.Services; @@ -38,9 +37,17 @@ public async Task ExecuteLoRaDeviceMessage(string deviceId, ArgumentNullException.ThrowIfNull(deviceId, nameof(deviceId)); ArgumentNullException.ThrowIfNull(commandDto, nameof(commandDto)); + // Convert the hex frame to a byte array + var hexFrame = Enumerable.Range(0, commandDto.Frame.Length / 2) + .Select(x => Convert.ToByte(commandDto.Frame.Substring(x * 2, 2), 16)) + .ToArray(); + + // Convert the byte array to a base64 string + var rawPayload = Convert.ToBase64String(hexFrame); + var body = new LoRaCloudToDeviceMessage { - RawPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(commandDto.Frame)), + RawPayload = rawPayload, Fport = commandDto.Port, Confirmed = commandDto.Confirmed }; diff --git a/src/IoTHub.Portal.Infrastructure/Startup/AzureServiceCollectionExtension.cs b/src/IoTHub.Portal.Infrastructure/Startup/AzureServiceCollectionExtension.cs index 4022404fa..0f1b277d0 100644 --- a/src/IoTHub.Portal.Infrastructure/Startup/AzureServiceCollectionExtension.cs +++ b/src/IoTHub.Portal.Infrastructure/Startup/AzureServiceCollectionExtension.cs @@ -25,8 +25,6 @@ namespace IoTHub.Portal.Infrastructure.Startup using IoTHub.Portal.Models.v10.LoRaWAN; using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Provisioning.Service; - using Microsoft.EntityFrameworkCore; - using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Polly; @@ -62,13 +60,13 @@ private static IServiceCollection AddLoRaWanSupport(this IServiceCollection serv return services; } - var transientHttpErrorPolicy = HttpPolicyExtensions - .HandleTransientHttpError() - .OrResult(c => c.StatusCode == HttpStatusCode.NotFound) - .WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(100)); + _ = services.AddTransient() + .AddTransient, LoRaWanDeviceService>(); - _ = services.AddHttpClient("RestClient") - .AddPolicyHandler(transientHttpErrorPolicy); + var transientHttpErrorPolicy = HttpPolicyExtensions + .HandleTransientHttpError() + .OrResult(c => c.StatusCode == HttpStatusCode.NotFound) + .WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(100)); _ = services.AddHttpClient((sp, client) => { @@ -109,9 +107,8 @@ private static IServiceCollection ConfigureMappers(this IServiceCollection servi private static IServiceCollection ConfigureServices(this IServiceCollection services) { - return services.AddTransient() - .AddTransient, DeviceService>() - .AddTransient, LoRaWanDeviceService>(); + return services + .AddTransient, DeviceService>(); } private static IServiceCollection ConfigureHealthCheck(this IServiceCollection services)