Skip to content

Commit

Permalink
Merge branch 'master' into occupancy-enpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-NS authored Dec 7, 2023
2 parents b0bdb6a + af8f60b commit 44267f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
30 changes: 0 additions & 30 deletions CentralHub.Api.Tests/MeasurementControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,4 @@ public async Task TrackersFilteredFromMeasurements()

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);

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.BluetoothCount, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.WifiCount, Is.EqualTo(1));
}
}
11 changes: 2 additions & 9 deletions CentralHub.Api/Controllers/MeasurementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,12 @@ await _occupancySettings.Lock(os =>

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

var bluetoothCalibrationNumber = recentAggregatedMeasurements.Min(am => am.BluetoothCount);
var wifiCalibrationNumber = recentAggregatedMeasurements.Min(am => am.WifiCount);

return aggregatedMeasurements.Select(am => new AggregatedMeasurements(
am.AggregatedMeasurementDtoId,
am.StartTime,
am.EndTime,
am.BluetoothCount - bluetoothCalibrationNumber,
am.WifiCount - wifiCalibrationNumber)
am.BluetoothCount,
am.WifiCount)
).ToImmutableArray();
}

Expand Down
14 changes: 14 additions & 0 deletions CentralHub.Api/Services/TrackerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public async Task RemoveTrackerAsync(TrackerDto trackerDto, CancellationToken ca
}

room.Trackers.Remove(trackerDto);
var unregisteredTracker = await UnregisteredTrackersMutex.Lock(unregisteredTrackers =>
{
var unregisteredTracker = new UnregisteredTrackerDto()
{
BluetoothMacAddress = trackerDto.BluetoothMacAddress,
WifiMacAddress = trackerDto.WifiMacAddress,
};
unregisteredTrackers.Add(unregisteredTracker);
return unregisteredTracker;
}, cancellationToken);
_applicationDbContext.Rooms.Update(room);
try
{
Expand All @@ -120,6 +130,10 @@ public async Task RemoveTrackerAsync(TrackerDto trackerDto, CancellationToken ca
// Set the roomDto to unchanged
_applicationDbContext.Rooms.Attach(room).State = EntityState.Unchanged;
room.Trackers.Add(trackerDto);
await UnregisteredTrackersMutex.Lock(unregisteredTrackers =>
{
unregisteredTrackers.Remove(unregisteredTracker);
}, cancellationToken);
throw;
}
}
Expand Down

0 comments on commit 44267f9

Please sign in to comment.