diff --git a/CentralHub.Api.Model/Responses/Measurements/GetLatestOccupancyResponse.cs b/CentralHub.Api.Model/Responses/Measurements/GetLatestOccupancyResponse.cs new file mode 100644 index 0000000..2b6d487 --- /dev/null +++ b/CentralHub.Api.Model/Responses/Measurements/GetLatestOccupancyResponse.cs @@ -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); + } +} \ No newline at end of file diff --git a/CentralHub.Api/Controllers/MeasurementController.cs b/CentralHub.Api/Controllers/MeasurementController.cs index 14e7323..423b9bf 100644 --- a/CentralHub.Api/Controllers/MeasurementController.cs +++ b/CentralHub.Api/Controllers/MeasurementController.cs @@ -87,7 +87,29 @@ public async Task GetFirstAggreg return GetFirstAggregatedMeasurementsDateTimeResponse.CreateSuccessful(possibleFirstDateTime.Value); } - private static IReadOnlyList CreateMeasurements(IReadOnlyCollection aggregatedMeasurements) + [HttpGet("occupancy/latest")] + public async Task 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 CreateMeasurements(IEnumerable aggregatedMeasurements) { var recentAggregatedMeasurements = aggregatedMeasurements .Where(am => am.EndTime > (DateTime.UtcNow - TimeSpan.FromDays(1)))