Ejemplo n.º 1
0
        internal static string MatchResponse(IReceiverCommandDefinition commandDefinition, IRawResponseData response)
        {
            var responseWithOutWildcard = SplitNameFromParameter(commandDefinition.ResponseTemplate, response.Data);

            return(responseWithOutWildcard.response == responseWithOutWildcard.template
                ? responseWithOutWildcard.parameter
                : null);
        }
        internal static ReceiverResponse ConvertToResponse(
            IReceiverCommandDefinition commandDefinition,
            IRawResponseData data,
            string parameter)
        {
            var response = new ReceiverResponse
            {
                ResponseTime = data.TimeStamp,
                WaitingForResponseTimedOut = false,
                ResponseToCommand          = commandDefinition.CommandName.ToString()
            };

            if (commandDefinition.ResponseParameterType == typeof(OnOff))
            {
                response.ResponseValue = parameter == "0";
            }

            if (commandDefinition.ResponseParameterType == typeof(IRangeValue))
            {
                response.ResponseValue = ResponseConverterHelper.Convert(commandDefinition.CommandName, parameter);
            }

            if (commandDefinition.ResponseParameterType == typeof(InputType))
            {
                if (int.TryParse(parameter, out var inputTypeNumber))
                {
                    response.ResponseValue = (InputType)inputTypeNumber;
                }
                else
                {
                    response.ResponseValue = InputType.Unknown;
                }
            }

            if (commandDefinition.ResponseParameterType == typeof(ListeningMode))
            {
                if (int.TryParse(parameter, out var listeningModeNumber))
                {
                    response.ResponseValue = (ListeningMode)listeningModeNumber;
                }
                else
                {
                    response.ResponseValue = ListeningMode.Unknown;
                }
            }

            return(response);
        }
        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));
        }