void BleConnectionManager_AdvertismentReceived(object sender, AdvertismentReceivedEventArgs e) { lock (_lock) { RemoveTimedOutRecords(); var shortMac = BleUtils.ConnectionIdToMac(e.Id); if (_ignoreList.Any(m => m == shortMac)) { var proximity = BleUtils.RssiToProximity(e.Rssi); if (proximity > _workstationSettingsManager.Settings.LockProximity) { _lastAdvRecTime[shortMac] = DateTime.UtcNow; } } } }
async Task ConnectByProximity(AdvertismentReceivedEventArgs adv) { if (!isRunning) { return; } if (adv == null) { return; } if (_isConnecting == 1) { return; } if (_macListToConnect.Count == 0) { return; } var mac = BleUtils.ConnectionIdToMac(adv.Id); if (!_macListToConnect.Any(m => m == mac)) { return; } var proximity = BleUtils.RssiToProximity(adv.Rssi); var settings = _workstationSettingsManager.Settings; if (proximity < settings.UnlockProximity) { return; } if (_advIgnoreListMonitor.IsIgnored(mac)) { return; } if (!_hesAccessManager.HasAccessKey()) { return; } if (Interlocked.CompareExchange(ref _isConnecting, 1, 0) == 0) { try { var device = _bleDeviceManager.Devices.FirstOrDefault(d => d.Mac == mac && !d.IsRemote && !d.IsBoot); // Unlocked Workstation, Device not found OR Device not connected - dont add to ignore if (!_workstationUnlocker.IsConnected && (device == null || (device != null && !device.IsConnected))) { return; } try { // Unlocked Workstation, Device connected - add to ignore if (!_workstationUnlocker.IsConnected && device != null && device.IsConnected) { return; } // Locked Workstation, Device not found OR not connected - connect add to ignore if (_workstationUnlocker.IsConnected && (device == null || (device != null && !device.IsConnected))) { await _connectionFlowProcessor.ConnectAndUnlock(mac, OnUnlockAttempt); } } catch (Exception) { // Silent handling. Log is already printed inside of _connectionFlowProcessor.ConnectAndUnlock() } finally { _advIgnoreListMonitor.Ignore(mac); } } finally { Interlocked.Exchange(ref _isConnecting, 0); } } }