Example #1
0
        /// <summary>
        /// Gets the <see cref="Command"/> encoded by given command string.
        /// </summary>
        /// <param name="commandString">The command string to parse.</param>
        /// <returns>The <see cref="Command"/> encoded by given command string.</returns>
        public static Command GetCommand(string commandString)
        {
            if (commandString.StartsWith("IRCODE"))
            {
                return(IrCommand.Parse(commandString));
            }

            if (commandString.StartsWith("KEYBOARD"))
            {
                return(KeyboardCommand.Parse(commandString));
            }

            if (commandString.StartsWith("SETCH"))
            {
                return(SetChCommand.Parse(commandString));
            }

            if (commandString.StartsWith("FORCECH"))
            {
                return(ForceChCommand.Parse(commandString));
            }

            if (commandString.StartsWith("TELEPORT"))
            {
                return(TeleportCommand.Parse(commandString));
            }

            return(null);
        }
        /// <summary>
        /// Handles commands for the TiVo published to MQTT.
        /// </summary>
        /// <param name="e">Event args.</param>
        protected override async void Mqtt_MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e)
        {
            var message = e.ApplicationMessage.ConvertPayloadToString();

            _log.LogInformation("MQTT message received for topic " + e.ApplicationMessage.Topic + ": " + message);

            var commandType = e.ApplicationMessage.Topic.Replace(TopicRoot + "/controls/", string.Empty).Replace("/set", string.Empty);

            Command command = null;

            if (commandType == "setCh")
            {
                var messageParts = message.Split('.');
                if (int.TryParse(messageParts[0], out int channel))
                {
                    if (messageParts.Length == 1)
                    {
                        command = new SetChCommand {
                            Channel = channel
                        };
                    }
                    else if (messageParts.Length == 2 && int.TryParse(messageParts[0], out int subchannel))
                    {
                        command = new SetChCommand {
                            Channel = channel, Subchannel = subchannel
                        };
                    }
                }
            }

            if (commandType == "forceCh")
            {
                var messageParts = message.Split('.');
                if (int.TryParse(messageParts[0], out int channel))
                {
                    if (messageParts.Length == 1)
                    {
                        command = new ForceChCommand {
                            Channel = channel
                        };
                    }
                    else if (messageParts.Length == 2 && int.TryParse(messageParts[0], out int subchannel))
                    {
                        command = new ForceChCommand {
                            Channel = channel, Subchannel = subchannel
                        };
                    }
                }
            }

            if (commandType == "irCode")
            {
                command = new IrCommand {
                    IrCode = message
                }
            }
            ;

            if (commandType == "teleport")
            {
                command = new TeleportCommand {
                    TeleportCode = message
                }
            }
            ;

            if (commandType == "keyboard")
            {
                command = new KeyboardCommand {
                    KeyboardCode = message
                }
            }
            ;

            if (command != null)
            {
                await _client.SendCommandAsync(command)
                .ConfigureAwait(false);
            }
        }