private async Task <OnvifClient> GetOnvifClient() { try { using (_ = await onvifClientLock.LockAsync(Token).ConfigureAwait(false)) { if (onvifClientMain == null) { Uri deviceUri = new Uri(CameraSettings.CameraHost); var credential = new NetworkCredential(CameraSettings.Login, CameraSettings.Password); var timeout = TimeSpan.FromSeconds(60); var connectionParameters = new ConnectionParameters(deviceUri, credential, timeout); var onvifClientTemp = new OnvifClient(connectionParameters, timeout); await onvifClientTemp.ConnectAsync(Token).ConfigureAwait(false); this.onvifClientMain = onvifClientTemp; } return(this.onvifClientMain); } } catch (Exception ex) { Trace.TraceError(Invariant($"[{CameraSettings.Name}]Failed to Connect to {CameraSettings.CameraHost} with {ex.GetFullMessage()}.")); throw; } }
private async Task ReceiveEvents() { while (!Token.IsCancellationRequested) { OnvifClient onvifClient = null; try { await EnqueueEventsListeningInfo(false).ConfigureAwait(false); onvifClient = await GetOnvifClient().ConfigureAwait(false); onvifClient.EventReceived += OnvifClient_EventReceived; await EnqueueEventsListeningInfo(true).ConfigureAwait(false); await onvifClient.ReceiveAsync(Token).ConfigureAwait(false); } catch (Exception ex) { if (!ex.IsCancelException()) { await ClearOnvifClient().ConfigureAwait(false); } await Task.Delay(1000).ConfigureAwait(false); throw; } finally { if (onvifClient != null) { onvifClient.EventReceived -= OnvifClient_EventReceived; } await EnqueueEventsListeningInfo(false).ConfigureAwait(false); } } }