示例#1
0
    /// <summary>
    /// Send a named Key Press to the TV
    /// </summary>
    /// <param name="keyName"></param>
    public static void SendKey(
        string keyName)
    {
        try
        {
            //  A special encoding to avoid affecting the TV during testing
            if (Config.TvAddress != Config.IpAddress)
            {
                //  Open and close the TCP connection every time,
                //  as we don't know when the TV has been turned off
                using (TcpClient conn = new TcpClient(Config.TvAddress, TvPort))
                {
                    //  First, authenticate the local IP Address and Mac address with the TV
                    MemoryStream msMsg = new MemoryStream();
                    var          msPkt = conn.GetStream();

                    msMsg.AppendBytes(new byte[] { 0x64, 0x00 });
                    msMsg.AppendBase64(Config.IpAddress);
                    msMsg.AppendBase64(MacAddress);
                    msMsg.AppendBase64(RemoteName);

                    msPkt.AppendBytes(new byte[] { 0x00 });
                    msPkt.AppendString(AppName);
                    msPkt.AppendCountedBytes(msMsg.ToArray());

                    //  Then send the named key
                    msMsg = new MemoryStream();

                    msMsg.AppendBytes(new byte[] { 0x00, 0x00, 0x00 });
                    msMsg.AppendBase64("KEY_" + keyName);

                    msPkt.AppendBytes(new byte[] { 0x00 });
                    msPkt.AppendString(AppName);
                    msPkt.AppendCountedBytes(msMsg.ToArray());
                }
            }
        }
        catch (System.Exception ex)
        {
            logger.Error(ex, "Can't send key press '{0}'", keyName);
        }
    }