diff --git a/CentralHub.Api.Tests/MeasurementControllerTests.cs b/CentralHub.Api.Tests/MeasurementControllerTests.cs index c742a03..3b546c0 100644 --- a/CentralHub.Api.Tests/MeasurementControllerTests.cs +++ b/CentralHub.Api.Tests/MeasurementControllerTests.cs @@ -15,6 +15,8 @@ public class MeasurementControllerTests private ITrackerRepository _trackerRepository; private IMeasurementRepository _aggregatedMeasurementRepository; + private LocalizationService _localizationService; + private int _roomId = 0; private int _trackerId = 0; @@ -48,6 +50,12 @@ public void Setup() NullLogger.Instance, _aggregatedMeasurementRepository, _trackerRepository); + + _localizationService = new LocalizationService( + NullLogger.Instance, + _aggregatedMeasurementRepository, + _roomRepository + ); } [TearDown] @@ -70,7 +78,7 @@ public async Task TestAddMeasurements() var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements); await _measurementController.AddMeasurements(addMeasurementsRequest, default); - var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_trackerId][0].Measurements; + var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements; Assert.That(addedMeasurements, Has.Count.EqualTo(measurements.Length)); Assert.That(addedMeasurements, Does.Contain(measurements[0])); @@ -87,8 +95,55 @@ public async Task TrackersFilteredFromMeasurements() var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements); await _measurementController.AddMeasurements(addMeasurementsRequest, default); - var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_trackerId][0].Measurements; + var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements; Assert.That(addedMeasurements, Has.Count.EqualTo(0)); } + + [Test] + public async Task CalibrateMeasurements() + { + var measurements = new Measurement[] { + new Measurement("12:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10), + new Measurement("ab:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20) + }; + + var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements); + await _measurementController.AddMeasurements(addMeasurementsRequest, default); + + var measurements2 = new Measurement[] { + new Measurement("13:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10), + new Measurement("ac:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20), + new Measurement("14:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10), + new Measurement("ab:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20) + }; + + var addMeasurementsRequest2 = new AddMeasurementsRequest(_trackerId, measurements2); + await _measurementController.AddMeasurements(addMeasurementsRequest2, default); + await _localizationService.AggregateMeasurementsAsync(default); + + var measurements3 = new Measurement[] { + new Measurement("13:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10), + new Measurement("ac:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20), + new Measurement("14:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10), + new Measurement("ab:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20) + }; + + var addMeasurementsRequest3 = new AddMeasurementsRequest(_trackerId, measurements3); + await _measurementController.AddMeasurements(addMeasurementsRequest3, default); + await _localizationService.AggregateMeasurementsAsync(default); + + var lastAggregatedMeasurements = (await _measurementController.GetAggregateMeasurements(_roomId, default)).AggregatedMeasurements.Last(); + + Assert.That(lastAggregatedMeasurements.BluetoothMedian, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.WifiMedian, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.BluetoothMean, Is.EqualTo(1f)); + Assert.That(lastAggregatedMeasurements.WifiMean, Is.EqualTo(1f)); + Assert.That(lastAggregatedMeasurements.BluetoothMax, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.WifiMax, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.BluetoothMin, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.WifiMin, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.TotalBluetoothDeviceCount, Is.EqualTo(1)); + Assert.That(lastAggregatedMeasurements.TotalWifiDeviceCount, Is.EqualTo(1)); + } } \ No newline at end of file diff --git a/CentralHub.Api.Tests/MockTrackerRepository.cs b/CentralHub.Api.Tests/MockTrackerRepository.cs index 3941781..fb4d7dc 100644 --- a/CentralHub.Api.Tests/MockTrackerRepository.cs +++ b/CentralHub.Api.Tests/MockTrackerRepository.cs @@ -89,6 +89,6 @@ public Task AddUnregisteredTracker(string wifiMacAddress, string bluetoothMacAdd public Task> GetRegisteredTrackers(CancellationToken cancellationToken) { - return Task.FromResult(RoomDto.Trackers.ToList().AsEnumerable()); + return Task.FromResult(RoomDto.Trackers.ToArray().AsEnumerable()); } } diff --git a/CentralHub.Api/Controllers/MeasurementController.cs b/CentralHub.Api/Controllers/MeasurementController.cs index 0c90818..bbd5c80 100644 --- a/CentralHub.Api/Controllers/MeasurementController.cs +++ b/CentralHub.Api/Controllers/MeasurementController.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using CentralHub.Api.Dtos; using CentralHub.Api.Model; using CentralHub.Api.Model.Requests.Localization; using CentralHub.Api.Model.Responses.AggregatedMeasurements; @@ -61,38 +62,22 @@ public async Task GetAggregateMeasurements(in return GetAggregatedMeasurementsResponse.CreateUnsuccessful(); } - return GetAggregatedMeasurementsResponse.CreateSuccessful( - aggregatedMeasurements.Select(am => new AggregatedMeasurements( - am.AggregatedMeasurementDtoId, - am.StartTime, - am.EndTime, - am.MeasurementGroupCount, - am.BluetoothMedianDeviceCount, - am.BluetoothMeanDeviceCount, - am.BluetoothMaxDeviceCount, - am.BluetoothMinDeviceCount, - am.TotalBluetoothDeviceCount, - am.WifiMedianDeviceCount, - am.WifiMeanDeviceCount, - am.WifiMaxDeviceCount, - am.WifiMinDeviceCount, - am.TotalWifiDeviceCount)).ToImmutableArray()); + return GetAggregatedMeasurementsResponse + .CreateSuccessful(CreateMeasurements(aggregatedMeasurements)); } [HttpGet("range")] public async Task GetAggregateMeasurements(int roomId, DateTime timeStart, DateTime timeEnd, CancellationToken token) { - var aggregatedMeasurements = await GetAggregateMeasurements(roomId, token); + var aggregatedMeasurements = await _measurementRepository.GetAggregatedMeasurementsAsync(roomId, timeStart, timeEnd, token); - if (!aggregatedMeasurements.Success) + if (aggregatedMeasurements == null) { return GetAggregatedMeasurementsResponse.CreateUnsuccessful(); } return GetAggregatedMeasurementsResponse - .CreateSuccessful(aggregatedMeasurements.AggregatedMeasurements! - .Where(am => am.StartTime >= timeStart && am.EndTime <= timeEnd) - .ToImmutableArray()); + .CreateSuccessful(CreateMeasurements(aggregatedMeasurements)); } [HttpGet("first")] @@ -109,4 +94,28 @@ public async Task GetFirstAggreg return GetFirstAggregatedMeasurementsDateTimeResponse.CreateSuccessful(possibleFirstDateTime.Value); } + + private static IReadOnlyList CreateMeasurements(IEnumerable aggregatedMeasurements) + { + var recentAggregatedMeasurements = aggregatedMeasurements.Where(am => am.EndTime > (DateTime.UtcNow - TimeSpan.FromDays(1))); + var wifiCalibrationNumber = recentAggregatedMeasurements.Min(am => am.WifiMinDeviceCount); + var bluetoothCalibrationNumber = recentAggregatedMeasurements.Min(am => am.BluetoothMinDeviceCount); + + return aggregatedMeasurements.Select(am => new AggregatedMeasurements( + am.AggregatedMeasurementDtoId, + am.StartTime, + am.EndTime, + am.MeasurementGroupCount, + am.BluetoothMedianDeviceCount - bluetoothCalibrationNumber, + am.BluetoothMeanDeviceCount - bluetoothCalibrationNumber, + am.BluetoothMaxDeviceCount - bluetoothCalibrationNumber, + am.BluetoothMinDeviceCount - bluetoothCalibrationNumber, + am.TotalBluetoothDeviceCount - bluetoothCalibrationNumber, + am.WifiMedianDeviceCount - wifiCalibrationNumber, + am.WifiMeanDeviceCount - wifiCalibrationNumber, + am.WifiMaxDeviceCount - wifiCalibrationNumber, + am.WifiMinDeviceCount - wifiCalibrationNumber, + am.TotalWifiDeviceCount - wifiCalibrationNumber) + ).ToImmutableArray(); + } } diff --git a/CentralHub.Api/MeasurementGroup.cs b/CentralHub.Api/MeasurementGroup.cs index 8777ebd..ce183a3 100644 --- a/CentralHub.Api/MeasurementGroup.cs +++ b/CentralHub.Api/MeasurementGroup.cs @@ -7,7 +7,7 @@ public class MeasurementGroup { public MeasurementGroup(IEnumerable measurements) { - Timestamp = DateTime.Now; + Timestamp = DateTime.UtcNow; Measurements = measurements.ToImmutableArray(); } public DateTime Timestamp { get; } diff --git a/CentralHub.Api/Services/LocalizationService.cs b/CentralHub.Api/Services/LocalizationService.cs index f930db9..7b257db 100644 --- a/CentralHub.Api/Services/LocalizationService.cs +++ b/CentralHub.Api/Services/LocalizationService.cs @@ -72,7 +72,7 @@ private static AggregatedMeasurementDto CreateAggregatedMeasurement(RoomDto room var measurements = measurementGroups.SelectMany(mg => mg.Measurements); var bluetooth = measurements.Where(m => m.Type == Measurement.Protocol.Bluetooth); var wifi = measurements.Where(m => m.Type == Measurement.Protocol.Wifi); - var now = DateTime.Now; + var now = DateTime.UtcNow; var startMeasurementTime = measurementGroups.Count != 0 ? measurementGroups.Min(mg => mg.Timestamp) : now - _sleepTime; var endMeasurementTime = measurementGroups.Count != 0 ? measurementGroups.Max(mg => mg.Timestamp) : now; @@ -144,7 +144,7 @@ private static IEnumerable FilterMeasurements(IEnumerable m.Rssi >= -75); + return filteredMeasurements.Values; } private static int MaxDevices(IEnumerable numMatchingProtocolPerGroup)