public async Task SendReceiverCommandAndForgetAsync(IReceiverCommand command) { if (!_isListening && _isConnectionClosed) { throw new PioneerReceiverException("When no listener is defined, a new Receiver object have to be created for every Send Command "); } var rawCommand = CreateRawCommand(command); await SendAsync(rawCommand); }
internal static string ConvertToRawCommand(IReceiverCommand command, IReceiverCommandDefinition commandDefinition) { if (commandDefinition?.CommandTemplate is null) { throw new PioneerReceiverException($"Template is undefined for: {command.KeyValue.Key}"); } if (command.KeyValue.Value is null) { return(commandDefinition.CommandTemplate); } if (commandDefinition is null) { throw new PioneerReceiverException($"Unknown command: {command.KeyValue.Key}"); } if (commandDefinition.CommandParameterType != command.KeyValue.Value.GetType()) { throw new PioneerReceiverException($"Wrong command type: '{command.KeyValue.Value.GetType()}'. " + $"Expected: '{commandDefinition.ResponseParameterType}'"); } string parameter = null; if (command.KeyValue.Value is UpDown direction) { parameter = direction == UpDown.Down ? "D" : "U"; } if (command.KeyValue.Value is OnOff button) { parameter = button == OnOff.On ? "O" : "F"; } if (command.KeyValue.Value is InputType inputType) { parameter = ((int)inputType).ToString("00"); } if (command.KeyValue.Value is ListeningMode listeningMode) { parameter = ((int)listeningMode).ToString("0000"); } return(commandDefinition.CommandTemplate.WildcardReplace('*', parameter)); }
private string CreateRawCommand(IReceiverCommand command) { var commandDefinition = _commands.FirstOrDefault(d => d.CommandName == command?.KeyValue.Key); return(ConvertToRawCommand(command, commandDefinition)); }
public async Task <IReceiverResponse> SendReceiverCommandAndTryWaitForResponseAsync(IReceiverCommand command, TimeSpan timeout) { if (!_isListening && _isConnectionClosed) { throw new PioneerReceiverException("When no listener is defined, a new Receiver object have to be created for every Send Command "); } var commandDefinition = _commands.FirstOrDefault(c => c.CommandName == command.KeyValue.Key); var receiverResponse = new ReceiverResponse(); var observableSendResponse = Observable.Create <int>(async obs => { var disposable = _listenerObservable .Do(_ => Debug.WriteLine($"A Thread - Listen for Response Observe: {Thread.CurrentThread.ManagedThreadId}")) .Timeout(timeout) .Where(r => !(r?.Data is null)) .Subscribe( rawResponse => { Debug.WriteLine($"Response: {rawResponse?.Data}"); Debug.WriteLine($"Thread - Listen for Response Subscription: {Thread.CurrentThread.ManagedThreadId}"); var matchedResponse = ResponseParserHelper.MatchResponse(commandDefinition, rawResponse); if (!(matchedResponse is null)) { receiverResponse = ConvertToResponse(commandDefinition, rawResponse, matchedResponse); obs.OnNext(1); obs.OnCompleted(); } }, ex => { if (ex is TimeoutException) { receiverResponse.WaitingForResponseTimedOut = true; obs.OnNext(0); obs.OnCompleted(); } else { obs.OnNext(0); obs.OnError(ex); } }, () => { obs.OnNext(0); obs.OnCompleted(); }); await SendAsync(CreateRawCommand(command)); return(disposable); }); await observableSendResponse; return(receiverResponse); }