Skip to content

Commit

Permalink
Don't show nodes as devices if configured with location and stationary (
Browse files Browse the repository at this point in the history
  • Loading branch information
DTTerastar authored Sep 7, 2023
1 parent a84f555 commit 1e71024
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
4 changes: 2 additions & 2 deletions ESPresense.Companion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<PackageReference Include="MathNet.Numerics.Providers.MKL" Version="5.0.0" />
<PackageReference Include="MathNet.Spatial" Version="0.6.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="7.0.10" />
<PackageReference Include="MQTTnet" Version="4.3.0.858" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.0.858" />
<PackageReference Include="MQTTnet" Version="4.3.1.873" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.1.873" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="sqlite-net-base" Version="1.8.116" />
Expand Down
8 changes: 4 additions & 4 deletions Extensions/MqttClientOptionsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public static class MqttClientOptionsBuilderExtensions
{
public static MqttClientOptionsBuilder WithConfig(this MqttClientOptionsBuilder mcob, ConfigMqtt mqtt)
{
Log.Logger.Information("Connecting to mqtt server at " + (mqtt.Port != null ? "{@host}:{@port}" : "{@host}")+"...", mqtt.Host ?? "localhost", mqtt.Port);
Log.Logger.Information("Connecting to mqtt server at " + (mqtt.Port != null ? "{@host}:{@port}" : "{@host}") + "...", mqtt.Host ?? "localhost", mqtt.Port);

mcob
.WithTcpServer(mqtt.Host ?? "localhost", mqtt.Port)
.WithCredentials(mqtt.Username, mqtt.Password);
if (mqtt.Ssl != null)
mcob.WithTls(o =>
mcob.WithTlsOptions(o =>
{
o.UseTls = mqtt.Ssl ?? false;
o.AllowUntrustedCertificates = true;
o.UseTls(mqtt.Ssl ?? false);
o.WithAllowUntrustedCertificates();
});
return mcob;
}
Expand Down
96 changes: 53 additions & 43 deletions Locators/MultiScenarioLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,63 +46,73 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var deviceId = parts[2];
var nodeId = parts[3];
bool isNode = deviceId.StartsWith("node:");
if (!_state.Nodes.TryGetValue(nodeId, out var node))
if (!_state.Nodes.TryGetValue(nodeId, out var rx))
{
_state.Nodes[nodeId] = node = new Node(nodeId);
_state.Nodes[nodeId] = rx = new Node(nodeId);
if (_telemetry.UnknownNodes.Add(nodeId))
Log.Warning("Unknown node {nodeId}", nodeId);
}
if (deviceId.StartsWith("node:") && _state.Nodes.TryGetValue(deviceId.Substring(5), out var tx) && _state.Nodes.TryGetValue(nodeId, out var rx))
if (rx.HasLocation && tx.HasLocation && rx.Stationary && tx.Stationary)
tx.RxNodes.GetOrAdd(nodeId, new RxNode { Tx = tx, Rx = rx }).ReadMessage(arg.ApplicationMessage.PayloadSegment);
if (node.HasLocation)
if (isNode && _state.Nodes.TryGetValue(deviceId.Substring(5), out var tx))
{
_telemetry.Messages++;
var device = _state.Devices.GetOrAdd(deviceId, id =>
if (tx is { HasLocation: true, Stationary: true })
{
var d = new Device(id) { Check = true };
foreach (var scenario in GetScenarios(d)) d.Scenarios.Add(scenario);
return d;
});
_telemetry.Devices = _state.Devices.Count;
var dirty = device.Nodes.GetOrAdd(nodeId, new DeviceNode { Device = device, Node = node }).ReadMessage(arg.ApplicationMessage.PayloadSegment);
if (dirty) _telemetry.Moved++;
if (device.Check)
if (rx is { HasLocation: true, Stationary: true }) // both nodes are stationary
tx.RxNodes.GetOrAdd(nodeId, new RxNode { Tx = tx, Rx = rx }).ReadMessage(arg.ApplicationMessage.PayloadSegment);
}
else isNode = false; // if transmitter is not stationary, treat it as a device
} else isNode = false; // if transmitter is not configured, treat it as a device
if (!isNode)
{
if (rx.HasLocation)
{
if (_state.ConfigDeviceById.TryGetValue(deviceId, out var cdById))
_telemetry.Messages++;
var device = _state.Devices.GetOrAdd(deviceId, id =>
{
device.Track = true;
if (!string.IsNullOrWhiteSpace(cdById.Name))
device.Name = cdById.Name;
var d = new Device(id) { Check = true };
foreach (var scenario in GetScenarios(d)) d.Scenarios.Add(scenario);
return d;
});
_telemetry.Devices = _state.Devices.Count;
var dirty = device.Nodes.GetOrAdd(nodeId, new DeviceNode { Device = device, Node = rx }).ReadMessage(arg.ApplicationMessage.PayloadSegment);
if (dirty) _telemetry.Moved++;
if (device.Check)
{
if (_state.ConfigDeviceById.TryGetValue(deviceId, out var cdById))
{
device.Track = true;
if (!string.IsNullOrWhiteSpace(cdById.Name))
device.Name = cdById.Name;
}
else if (!string.IsNullOrWhiteSpace(device.Name) && _state.ConfigDeviceByName.TryGetValue(device.Name, out _))
device.Track = true;
else if (!string.IsNullOrWhiteSpace(device.Id) && _state.IdsToTrack.Any(a => a.IsMatch(device.Id)))
device.Track = true;
else if (!string.IsNullOrWhiteSpace(device.Name) && _state.NamesToTrack.Any(a => a.IsMatch(device.Name)))
device.Track = true;
else
device.Track = false;
device.Check = false;
_telemetry.Tracked = _state.Devices.Values.Count(a => a.Track);
}
else if (!string.IsNullOrWhiteSpace(device.Name) && _state.ConfigDeviceByName.TryGetValue(device.Name, out _))
device.Track = true;
else if (!string.IsNullOrWhiteSpace(device.Id) && _state.IdsToTrack.Any(a => a.IsMatch(device.Id)))
device.Track = true;
else if (!string.IsNullOrWhiteSpace(device.Name) && _state.NamesToTrack.Any(a => a.IsMatch(device.Name)))
device.Track = true;
else
device.Track = false;
device.Check = false;
if (device.Track)
foreach (var ad in device.HassAutoDiscovery)
await ad.Send(mc);
_telemetry.Tracked = _state.Devices.Values.Count(a => a.Track);
if (device.Track && dirty)
_dirty.Add(device);
}
else
{
_telemetry.Skipped++;
}
if (device.Track)
foreach (var ad in device.HassAutoDiscovery)
await ad.Send(mc);
if (device.Track && dirty)
_dirty.Add(device);
}
else
{
_telemetry.Skipped++;
}
};

Expand Down

0 comments on commit 1e71024

Please sign in to comment.