Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latest occupancy estimation #46

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(bool success, int? occupancy)
{
Success = success;
EstimatedOccupancy = occupancy;
}

public bool Success { get; }

public int? EstimatedOccupancy { get; }
mads256h marked this conversation as resolved.
Show resolved Hide resolved

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

public static GetLatestOccupancyResponse CreateSuccessful(int occupancy)
{
return new GetLatestOccupancyResponse(true, occupancy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Reflection.Metadata;
using System.Text.Json.Serialization;

namespace CentralHub.Api.Model.Responses.AggregatedMeasurements;

public sealed class SetDevicesPerPersonResponse
{
[JsonConstructor]
public SetDevicesPerPersonResponse(bool success, float? bluetoothDevicesPerPerson, float? wifiDevicesPerPerson)
{
Success = success;
BluetoothDevicesPerPerson = bluetoothDevicesPerPerson;
WifiDevicesPerPerson = wifiDevicesPerPerson;
}

public bool Success { get; }
public float? BluetoothDevicesPerPerson { get; }
public float? WifiDevicesPerPerson { get; }

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

public static SetDevicesPerPersonResponse CreateSuccessful(float bluetoothDevicesPerPerson, float wifiDevicesPerPerson)
{
return new SetDevicesPerPersonResponse(true, bluetoothDevicesPerPerson, wifiDevicesPerPerson);
}
}
62 changes: 61 additions & 1 deletion CentralHub.Api/Controllers/MeasurementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using CentralHub.Api.Model.Responses.AggregatedMeasurements;
using CentralHub.Api.Model.Responses.Measurements;
using CentralHub.Api.Services;
using CentralHub.Api.Threading;
using Microsoft.AspNetCore.Mvc;

namespace CentralHub.Api.Controllers;


[ApiController]
[Route("/measurements")]
public sealed class MeasurementController(
Expand All @@ -16,6 +18,10 @@ public sealed class MeasurementController(
IMeasurementRepository measurementRepository)
: ControllerBase
{
private static CancellableMutex<OccupancySettings> _occupancySettings =
new CancellableMutex<OccupancySettings>(
new OccupancySettings(1.5f, 2.4f));

[HttpPost("add")]
public async Task<AddMeasurementsResponse> AddMeasurements(AddMeasurementsRequest addMeasurementsRequest, CancellationToken token)
{
Expand Down Expand Up @@ -87,7 +93,50 @@ 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, CancellationToken cancellationToken)
{
var aggregatedMeasurements = (await measurementRepository.GetAggregatedMeasurementsAsync(roomId, cancellationToken)).Last();
Taoshix marked this conversation as resolved.
Show resolved Hide resolved

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

var occupancy = await _occupancySettings.Lock(os =>
{
return (int)Math.Round(
aggregatedMeasurements.BluetoothCount / os.BluetoothDevicesPerPerson +
aggregatedMeasurements.WifiCount / os.WifiDevicesPerPerson);
}, cancellationToken);


return GetLatestOccupancyResponse.CreateSuccessful(occupancy);
}

[HttpPost("settings/set")]
public async Task<SetDevicesPerPersonResponse> SetDevicesPerPerson(
float bluetoothDevicesPerPerson,
float wifiDevicesPerPerson,
CancellationToken cancellationToken)
{
if (bluetoothDevicesPerPerson < 0 || wifiDevicesPerPerson < 0)
{
return SetDevicesPerPersonResponse.CreateUnsuccessful();
}

await _occupancySettings.Lock(os =>
{
os.BluetoothDevicesPerPerson = bluetoothDevicesPerPerson;
os.WifiDevicesPerPerson = wifiDevicesPerPerson;
}, cancellationToken);

return SetDevicesPerPersonResponse
.CreateSuccessful(
bluetoothDevicesPerPerson, wifiDevicesPerPerson);
}

private static IReadOnlyList<AggregatedMeasurements> CreateMeasurements(IEnumerable<AggregatedMeasurementDto> aggregatedMeasurements)
{
return aggregatedMeasurements.Select(am => new AggregatedMeasurements(
am.AggregatedMeasurementDtoId,
Expand All @@ -97,4 +146,15 @@ private static IReadOnlyList<AggregatedMeasurements> CreateMeasurements(IReadOnl
am.WifiCount)
).ToImmutableArray();
}

private struct OccupancySettings
{
public OccupancySettings(float bluetoothDevicesPerPerson, float wifiDevicesPerPerson)
{
BluetoothDevicesPerPerson = bluetoothDevicesPerPerson;
WifiDevicesPerPerson = wifiDevicesPerPerson;
}
public float BluetoothDevicesPerPerson { get; set; }
public float WifiDevicesPerPerson { get; set; }
}
}
Loading