Skip to content

Commit

Permalink
fix null-ref exception
Browse files Browse the repository at this point in the history
  • Loading branch information
fgheysels committed Jan 2, 2025
1 parent 48d1538 commit b5266c1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@
public class InfluxQlResults
{
public IEnumerable<InfluxQlResult> Results { get; set; }

/// <summary>
/// Returns true if this <see cref="InfluxQlResults"/> object contains data; otherwise false.
/// </summary>
public bool ContainsData
{
get
{
return Results != null && Results.Any(r => r.ContainsData);
}
}
}

public class InfluxQlResult
{
public IEnumerable<InfluxQlSerie> Series { get; set; }

/// <summary>
/// Returns true if this <see cref="InfluxQlResult"/> object contains data, otherwise false.
/// </summary>
public bool ContainsData
{
get
{
return Series != null && Series.Any(s => s.Values != null && s.Values.Any());
}
}
}

public class InfluxQlSerie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ namespace Fg.Homewizard.EnergyApi.Controllers
public class ElectricityController : ControllerBase
{
private readonly EnergyConsumptionRetriever _energyService;
private readonly ILogger<ElectricityController> _logger;

public ElectricityController(EnergyConsumptionRetriever energyService, ILogger<ElectricityController> logger)
public ElectricityController(EnergyConsumptionRetriever energyService)
{
_energyService = energyService;
_logger = logger;
}

[HttpGet("daily")]
[Produces( "application/json", "text/csv")]
[Produces("application/json", "text/csv")]
public async Task<IActionResult> GetDailyElectricityData(DateTimeOffset fromDate, DateTimeOffset toDate)
{
var result = await _energyService.GetElectricityConsumptionForPeriodAsync(fromDate, toDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,36 @@ public async Task<IEnumerable<PowerUsage>> GetElectricityConsumptionForPeriodAsy

var results = new List<PowerUsage>();

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;
Expand Down

0 comments on commit b5266c1

Please sign in to comment.