Skip to content

Commit

Permalink
Only switch scenario if better than threshold (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
DTTerastar authored Jun 5, 2023
1 parent 934cf11 commit bb4523f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Locators/MultiScenarioLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace ESPresense.Locators;

internal class MultiScenarioLocator : BackgroundService
{
private const int ConfidenceThreshold = 2;

private readonly DatabaseFactory _databaseFactory;
private readonly MqttConnectionFactory _mqttConnectionFactory;
private readonly State _state;
Expand Down Expand Up @@ -127,7 +129,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
device.LastCalculated = now;
var moved = device.Scenarios.AsParallel().Count(s => s.Locate());
var bs = device.BestScenario = device.Scenarios.Select((scenario, i) => new { scenario, i }).Where(a => a.scenario.Current).OrderByDescending(a => a.scenario.Confidence).ThenBy(a => a.i).FirstOrDefault()?.scenario;
var bs = device.Scenarios.Select((scenario, i) => new { scenario, i }).Where(a => a.scenario.Current).OrderByDescending(a => a.scenario.Confidence).ThenBy(a => a.i).FirstOrDefault()?.scenario;
if (device.BestScenario == null || bs == null || bs.Confidence - device.BestScenario.Confidence > ConfidenceThreshold)
device.BestScenario = bs;
else
bs = device.BestScenario;
var state = bs?.Room?.Name ?? bs?.Floor?.Name ?? "not_home";

if (state != device.ReportedState)
Expand Down

0 comments on commit bb4523f

Please sign in to comment.