Skip to content

Commit

Permalink
Allow for excluding id patterns (#751)
Browse files Browse the repository at this point in the history
* Add exclude_devices to config
  • Loading branch information
DTTerastar authored Sep 30, 2024
1 parent ce81b87 commit 5b55335
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/Models/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class Config
[YamlMember(Alias = "nodes")]
public ConfigNode[] Nodes { get; set; } = Array.Empty<ConfigNode>();

[YamlMember(Alias = "devices")] public ConfigDevice[] Devices { get; set; } = Array.Empty<ConfigDevice>();
[YamlMember(Alias = "devices")]
public ConfigDevice[] Devices { get; set; } = Array.Empty<ConfigDevice>();

[YamlMember(Alias = "exclude_devices")]
public ConfigDevice[] ExcludeDevices { get; set; } = Array.Empty<ConfigDevice>();

[YamlMember(Alias = "history")]
public ConfigHistory History { get; set; } = new();
Expand Down
29 changes: 28 additions & 1 deletion src/Models/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,32 @@ public IEnumerable<Scenario> GetScenarios(Device device)
//yield return new Scenario(_state.Config, new MultiFloorMultilateralizer(device, _state), "Multifloor");
yield return new Scenario(Config, new NearestNode(device), "NearestNode");
}
}

public bool ShouldTrack(Device device)
{
if (IsExcluded(device))
return false;

if (ConfigDeviceById.TryGetValue(device.Id, out var cdById))
{
if (!string.IsNullOrWhiteSpace(cdById.Name))
device.Name = cdById.Name;
return true;
}
if (!string.IsNullOrWhiteSpace(device.Name) && ConfigDeviceByName.TryGetValue(device.Name, out _))
return true;
if (!string.IsNullOrWhiteSpace(device.Id) && IdsToTrack.Any(a => a.IsMatch(device.Id)))
return true;
if (!string.IsNullOrWhiteSpace(device.Name) && NamesToTrack.Any(a => a.IsMatch(device.Name)))
return true;
return false;
}

private bool IsExcluded(Device device)
{
return Config?.ExcludeDevices.Any(d =>
(!string.IsNullOrWhiteSpace(d.Id) && !string.IsNullOrWhiteSpace(device.Id) && Glob.Parse(d.Id).IsMatch(device.Id)) ||
(!string.IsNullOrWhiteSpace(d.Name) && !string.IsNullOrWhiteSpace(device.Name) && Glob.Parse(d.Name).IsMatch(device.Name))
) ?? false;
}
}
19 changes: 1 addition & 18 deletions src/Services/DeviceTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,13 @@ private async Task CheckIdleDevicesAsync(CancellationToken stoppingToken)
}
}

private bool ShouldTrackDevice(Device device)
{
if (state.ConfigDeviceById.TryGetValue(device.Id, out var cdById))
{
if (!string.IsNullOrWhiteSpace(cdById.Name))
device.Name = cdById.Name;
return true;
}
if (!string.IsNullOrWhiteSpace(device.Name) && state.ConfigDeviceByName.TryGetValue(device.Name, out _))
return true;
if (!string.IsNullOrWhiteSpace(device.Id) && state.IdsToTrack.Any(a => a.IsMatch(device.Id)))
return true;
if (!string.IsNullOrWhiteSpace(device.Name) && state.NamesToTrack.Any(a => a.IsMatch(device.Name)))
return true;
return false;
}

private async Task<bool> CheckDeviceAsync(Device device)
{
var wasTracked = device.Track;
if (device.Check)
{
Log.Debug("Checking {Device}", device);
device.Track = ShouldTrackDevice(device);
device.Track = state.ShouldTrack(device);
device.Check = false;
}
if (device.Track != wasTracked)
Expand Down
4 changes: 4 additions & 0 deletions src/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,7 @@ devices:
- id: "keys:*"
- id: "therm:*"
- id: "iBeacon:*"

# Devices to NOT track
exclude_devices:
- id: "iBeacon:e5ca1ade-f007-ba11-0000-000000000000-*" # These are junk, we alias them to node:*

0 comments on commit 5b55335

Please sign in to comment.