private async void InputPortWatcher() { if (!_semaphores.TryGetValue(ListTypeEnum.CrosspointChange, out var semaphore)) { return; } while (true) { try { if (_cancellationTokenSource.IsCancellationRequested) { throw new OperationCanceledException(_cancellationTokenSource.Token); } if (!_responseDictionary.TryRemove(ListTypeEnum.CrosspointChange, out var response)) { await semaphore.WaitAsync(_cancellationTokenSource.Token).ConfigureAwait(false); } if (response == null && !_responseDictionary.TryRemove(ListTypeEnum.CrosspointChange, out response)) { continue; } var crosspoints = response.Select(line => { var lineParams = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (lineParams.Length >= 2 && short.TryParse(lineParams[0], out var outPort) && outPort == _device.OutputPorts[0] && short.TryParse(lineParams[1], out var inPort)) { return(new CrosspointInfo(inPort, outPort)); } return(null); }).FirstOrDefault(c => c != null); if (crosspoints == null) { continue; } OnInputPortChangeReceived?.Invoke(this, new EventArgs <CrosspointInfo>(crosspoints)); } catch (Exception ex) { if (ex is OperationCanceledException) { Logger.Debug("Input Port Watcher cancelled"); } return; } } }
private void IoListProcess(IList <string> listResponse, ListTypeEnum listType) { var ports = new List <IRouterPort>(); var crosspoints = new List <CrosspointInfo>(); foreach (var line in listResponse) { var lineParams = line.Split(' '); try { switch (listType) { case ListTypeEnum.Input: case ListTypeEnum.Output: { var split = line.Split(new [] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (split.Length < 1 || !short.TryParse(split[0], out var numer)) { throw new ArgumentException("Too few parameters"); } ports.Add(new RouterPort(numer, split.ElementAtOrDefault(1) ?? string.Empty)); break; } case ListTypeEnum.CrosspointChange: case ListTypeEnum.CrosspointStatus: { crosspoints.Add(new CrosspointInfo(short.Parse(lineParams[1]), short.Parse(lineParams[0]))); break; } } } catch (Exception ex) { Logger.Error($"Failed to generate port from response. [Line: {line} message: {ex.Message}]"); Debug.WriteLine($"Failed to generate port from response. [\n{line}\n{ex.Message}]"); } } switch (listType) { case ListTypeEnum.Input: { OnInputPortsListReceived?.Invoke(this, new EventArgs <IEnumerable <IRouterPort> >(ports)); _inputPortsListRequested = false; break; } case ListTypeEnum.Output: { OnOutputPortsListReceived?.Invoke(this, new EventArgs <IEnumerable <IRouterPort> >(ports)); _outputPortsListRequested = false; break; } case ListTypeEnum.CrosspointStatus: { OnInputPortChangeReceived?.Invoke(this, new EventArgs <IEnumerable <CrosspointInfo> >(crosspoints)); _currentInputPortRequested = false; break; } case ListTypeEnum.CrosspointChange: { OnInputPortChangeReceived?.Invoke(this, new EventArgs <IEnumerable <CrosspointInfo> >(crosspoints)); break; } } }