From b5266c1ee7a544f38ae870283fba301d2749b53d Mon Sep 17 00:00:00 2001 From: Frederik Gheysels Date: Thu, 2 Jan 2025 21:12:03 +0100 Subject: [PATCH] fix null-ref exception --- .../Clients/InfluxQlResults.cs | 22 ++++++++++ .../Controllers/ElectricityController.cs | 6 +-- .../Models/PowerUsage.cs | 8 ++-- .../Services/EnergyConsumptionRetriever.cs | 40 ++++++++++++------- 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Clients/InfluxQlResults.cs b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Clients/InfluxQlResults.cs index 71fe477..d42dfee 100644 --- a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Clients/InfluxQlResults.cs +++ b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Clients/InfluxQlResults.cs @@ -3,11 +3,33 @@ public class InfluxQlResults { public IEnumerable Results { get; set; } + + /// + /// Returns true if this object contains data; otherwise false. + /// + public bool ContainsData + { + get + { + return Results != null && Results.Any(r => r.ContainsData); + } + } } public class InfluxQlResult { public IEnumerable Series { get; set; } + + /// + /// Returns true if this object contains data, otherwise false. + /// + public bool ContainsData + { + get + { + return Series != null && Series.Any(s => s.Values != null && s.Values.Any()); + } + } } public class InfluxQlSerie diff --git a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Controllers/ElectricityController.cs b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Controllers/ElectricityController.cs index 605acfd..89ba669 100644 --- a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Controllers/ElectricityController.cs +++ b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Controllers/ElectricityController.cs @@ -8,16 +8,14 @@ namespace Fg.Homewizard.EnergyApi.Controllers public class ElectricityController : ControllerBase { private readonly EnergyConsumptionRetriever _energyService; - private readonly ILogger _logger; - public ElectricityController(EnergyConsumptionRetriever energyService, ILogger logger) + public ElectricityController(EnergyConsumptionRetriever energyService) { _energyService = energyService; - _logger = logger; } [HttpGet("daily")] - [Produces( "application/json", "text/csv")] + [Produces("application/json", "text/csv")] public async Task GetDailyElectricityData(DateTimeOffset fromDate, DateTimeOffset toDate) { var result = await _energyService.GetElectricityConsumptionForPeriodAsync(fromDate, toDate); diff --git a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Models/PowerUsage.cs b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Models/PowerUsage.cs index 9a0804f..376964c 100644 --- a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Models/PowerUsage.cs +++ b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Models/PowerUsage.cs @@ -3,9 +3,9 @@ public class PowerUsage { public DateTime Timestamp { get; set; } - public decimal PowerImportReading { get; set; } - public decimal PowerExportReading { get; set; } - public decimal PowerImport { get; set; } - public decimal PowerExport { get; set; } + public decimal? PowerImportReading { get; set; } + public decimal? PowerExportReading { get; set; } + public decimal? PowerImport { get; set; } + public decimal? PowerExport { get; set; } } } diff --git a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Services/EnergyConsumptionRetriever.cs b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Services/EnergyConsumptionRetriever.cs index 8e4c889..f441b05 100644 --- a/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Services/EnergyConsumptionRetriever.cs +++ b/energy-api/src/Fg.Homewizard.EnergyApi/Fg.Homewizard.EnergyApi/Services/EnergyConsumptionRetriever.cs @@ -30,24 +30,36 @@ public async Task> GetElectricityConsumptionForPeriodAsy var results = new List(); - var electricitySerie = series.Results.First().Series.First(); - - for (int i = 0; i < electricitySerie.Values.Count(); i++) + if (series.ContainsData) { - PowerUsage usage = new PowerUsage() - { - Timestamp = ((JsonElement)electricitySerie.Values.ElementAt(i).ElementAt(0)).GetDateTime(), - PowerImportReading = ((JsonElement)electricitySerie.Values.ElementAt(i).ElementAt(1)).GetDecimal(), - PowerExportReading = ((JsonElement)electricitySerie.Values.ElementAt(i).ElementAt(2)).GetDecimal() - }; + var electricitySerie = series.Results.First().Series.First(); - if (i != 0) + for (int i = 0; i < electricitySerie.Values.Count(); i++) { - usage.PowerExport = usage.PowerExportReading - results[i - 1].PowerExportReading; - usage.PowerImport = usage.PowerImportReading - results[i - 1].PowerImportReading; - } + var dataEntry = electricitySerie.Values.ElementAt(i); + + var dateTimeObject = dataEntry.ElementAt(0); + var powerImportObject = dataEntry.ElementAt(1); + var powerExportObject = dataEntry.ElementAt(2); - results.Add(usage); + PowerUsage usage = new PowerUsage + { + Timestamp = ((JsonElement)dateTimeObject).GetDateTime(), + PowerImportReading = powerImportObject != null ? ((JsonElement)powerImportObject).GetDecimal() : null, + PowerExportReading = powerExportObject != null ? ((JsonElement)powerExportObject).GetDecimal() : null + }; + + if (i != 0 && + usage.PowerImportReading != null && usage.PowerExportReading != null && + results[i - 1].PowerImportReading != null && results[i - 1].PowerExportReading != null) + { + usage.PowerExport = usage.PowerExportReading.Value - results[i - 1].PowerExportReading.Value; + usage.PowerImport = usage.PowerImportReading.Value - results[i - 1].PowerImportReading.Value; + } + + results.Add(usage); + + } } return results;