Skip to content

Commit

Permalink
Fixed DayStatistics peak/standby times returning a different date-par…
Browse files Browse the repository at this point in the history
…t than requested
  • Loading branch information
pyrocumulus committed Feb 24, 2020
1 parent 27b5489 commit 360662c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
20 changes: 17 additions & 3 deletions src/PVOutput.Net/Modules/StatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,25 @@ public Task<PVOutputArrayResponse<IStatusHistory>> GetHistoryForPeriodAsync(Date
/// <param name="systemId">Retrieve statuses for a specific system. <strong>Note: this is a donation only parameter.</strong></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<PVOutputArrayResponse<IDayStatistics>> GetDayStatisticsForPeriodAsync(DateTime fromDateTime, DateTime toDateTime, int? systemId = null, CancellationToken cancellationToken = default)
public Task<PVOutputResponse<IDayStatistics>> GetDayStatisticsForPeriodAsync(DateTime fromDateTime, DateTime toDateTime, int? systemId = null, CancellationToken cancellationToken = default)
{
var handler = new RequestHandler(Client);
return handler.ExecuteArrayRequestAsync<IDayStatistics>(
new GetDayStatisticsRequest { Date = fromDateTime.Date, From = fromDateTime, To = toDateTime, SystemId = systemId }, cancellationToken);
var response = handler.ExecuteSingleItemRequestAsync<IDayStatistics>(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<IDayStatistics> AddRequestedDate(Task<PVOutputResponse<IDayStatistics>> 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;
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions tests/PVOutput.Net.Tests/Modules/Status/StatusServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down

0 comments on commit 360662c

Please sign in to comment.