Skip to content

Commit

Permalink
added an endpoint that provides the latest occupancy estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-NS committed Dec 5, 2023
1 parent b052cb5 commit 6805679
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;

namespace CentralHub.Api.Model.Responses.AggregatedMeasurements;

public sealed class GetLatestOccupancyResponse
{
[JsonConstructor]
public GetLatestOccupancyResponse(int? occupancy, bool success)
{
EstimatedOccupancy = occupancy;
Success = success;
}

public bool Success { get; }

public int? EstimatedOccupancy { get; }

public static GetLatestOccupancyResponse CreateUnsuccessful()
{
return new GetLatestOccupancyResponse(null, false);
}

public static GetLatestOccupancyResponse CreateSuccessful(int occupancy)
{
return new GetLatestOccupancyResponse(occupancy, true);
}
}
24 changes: 23 additions & 1 deletion CentralHub.Api/Controllers/MeasurementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,29 @@ public async Task<GetFirstAggregatedMeasurementsDateTimeResponse> GetFirstAggreg
return GetFirstAggregatedMeasurementsDateTimeResponse.CreateSuccessful(possibleFirstDateTime.Value);
}

private static IReadOnlyList<AggregatedMeasurements> CreateMeasurements(IReadOnlyCollection<AggregatedMeasurementDto> aggregatedMeasurements)
[HttpGet("occupancy/latest")]
public async Task<GetLatestOccupancyResponse> GetLatestEstimatedOccupancy(
int roomId,
float wifiDevicesPerPerson,
float bluetoothDevicesPerPerson,
CancellationToken cancellationToken)
{
var aggregatedMeasurements = (await measurementRepository.GetAggregatedMeasurementsAsync(roomId, cancellationToken)).Last();

if (aggregatedMeasurements == null)
{
return GetLatestOccupancyResponse.CreateUnsuccessful();
}

var occupancy =
(int)Math.Round(
bluetoothDevicesPerPerson == 0 ? 0 : aggregatedMeasurements.BluetoothCount / bluetoothDevicesPerPerson +
wifiDevicesPerPerson == 0 ? 0 : aggregatedMeasurements.BluetoothCount / wifiDevicesPerPerson);

return GetLatestOccupancyResponse.CreateSuccessful(occupancy);
}

private static IReadOnlyList<AggregatedMeasurements> CreateMeasurements(IEnumerable<AggregatedMeasurementDto> aggregatedMeasurements)
{
var recentAggregatedMeasurements = aggregatedMeasurements
.Where(am => am.EndTime > (DateTime.UtcNow - TimeSpan.FromDays(1)))
Expand Down

0 comments on commit 6805679

Please sign in to comment.