diff --git a/ESPresense.Companion.csproj b/ESPresense.Companion.csproj index 4ca93a3c..2c9c6eb0 100644 --- a/ESPresense.Companion.csproj +++ b/ESPresense.Companion.csproj @@ -26,8 +26,8 @@ - - + + diff --git a/Extensions/MqttClientOptionsBuilderExtensions.cs b/Extensions/MqttClientOptionsBuilderExtensions.cs index 52fc3dec..794bb599 100644 --- a/Extensions/MqttClientOptionsBuilderExtensions.cs +++ b/Extensions/MqttClientOptionsBuilderExtensions.cs @@ -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; } diff --git a/Locators/MultiScenarioLocator.cs b/Locators/MultiScenarioLocator.cs index e5726286..1aefae21 100644 --- a/Locators/MultiScenarioLocator.cs +++ b/Locators/MultiScenarioLocator.cs @@ -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++; } };