From 360662c4a30afbee4dacada3cf975c79602fb6f5 Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 25 Feb 2020 00:26:37 +0100 Subject: [PATCH] Fixed DayStatistics peak/standby times returning a different date-part than requested --- src/PVOutput.Net/Modules/StatusService.cs | 20 ++++++++++++++++--- .../Modules/Status/StatusServiceTests.cs | 9 ++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/PVOutput.Net/Modules/StatusService.cs b/src/PVOutput.Net/Modules/StatusService.cs index e7c7217..75df167 100644 --- a/src/PVOutput.Net/Modules/StatusService.cs +++ b/src/PVOutput.Net/Modules/StatusService.cs @@ -58,11 +58,25 @@ public Task> GetHistoryForPeriodAsync(Date /// Retrieve statuses for a specific system. Note: this is a donation only parameter. /// /// - public Task> GetDayStatisticsForPeriodAsync(DateTime fromDateTime, DateTime toDateTime, int? systemId = null, CancellationToken cancellationToken = default) + public Task> GetDayStatisticsForPeriodAsync(DateTime fromDateTime, DateTime toDateTime, int? systemId = null, CancellationToken cancellationToken = default) { var handler = new RequestHandler(Client); - return handler.ExecuteArrayRequestAsync( - new GetDayStatisticsRequest { Date = fromDateTime.Date, From = fromDateTime, To = toDateTime, SystemId = systemId }, cancellationToken); + var response = handler.ExecuteSingleItemRequestAsync(new GetDayStatisticsRequest { Date = fromDateTime.Date, From = fromDateTime, To = toDateTime, SystemId = systemId }, cancellationToken); + + return response.ContinueWith(antecedent => AddRequestedDate(antecedent, fromDateTime.Date), cancellationToken, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); + } + + private static PVOutputResponse AddRequestedDate(Task> response, DateTime requestedDate) + { + IDayStatistics statistics = response.Result.Value; + statistics.PeakTime = requestedDate.Add(statistics.PeakTime.TimeOfDay); + + if (statistics.StandbyPowerTime != null) + { + statistics.StandbyPowerTime = requestedDate.Add(statistics.StandbyPowerTime.Value.TimeOfDay); + } + + return response.Result; } /// diff --git a/tests/PVOutput.Net.Tests/Modules/Status/StatusServiceTests.cs b/tests/PVOutput.Net.Tests/Modules/Status/StatusServiceTests.cs index c244d2b..bc0eed7 100644 --- a/tests/PVOutput.Net.Tests/Modules/Status/StatusServiceTests.cs +++ b/tests/PVOutput.Net.Tests/Modules/Status/StatusServiceTests.cs @@ -48,12 +48,15 @@ public async Task StatusService_GetDayStatistics_CallsCorrectUri() PVOutputClient client = TestUtility.GetMockClient(out MockHttpMessageHandler testProvider); testProvider.ExpectUriFromBase(GETSTATUS_URL) - .WithQueryString("d=20200131&from=14:00&to=15:00&stats=1") - .RespondPlainText(STATUS_RESPONSE_DAYSTATISTICS_SMALL); + .WithQueryString("d=20200131&from=10:00&to=16:00&stats=1") + .RespondPlainText(STATUS_RESPONSE_DAYSTATISTICS_MEDIUM); - var response = await client.Status.GetDayStatisticsForPeriodAsync(new DateTime(2020, 1, 31, 14, 0, 0), new DateTime(2019, 1, 31, 15, 0, 0)); + var response = await client.Status.GetDayStatisticsForPeriodAsync(new DateTime(2020, 1, 31, 10, 0, 0), new DateTime(2019, 1, 31, 16, 0, 0)); testProvider.VerifyNoOutstandingExpectation(); AssertStandardResponse(response); + + Assert.AreEqual(new DateTime(2020, 1, 31, 14, 40, 0), response.Value.PeakTime); + Assert.AreEqual(new DateTime(2020, 1, 31, 15, 5, 0), response.Value.StandbyPowerTime); } /*