示例#1
0
        internal static ViscaResponse ProcessResponse( byte[] packet, ViscaCommand pendingCmd, out bool matchesPending )
        {
            byte src = (byte)( packet[0] >> 4 );
            byte socket = (byte)( packet[1] & 0x0F );
            byte replyType = (byte)( packet[1] >> 4 );

            matchesPending = false;

            if( packet.Last() != 0xFF ) throw new ArgumentException( "Invalid Data:  Does not end with expected 0xFF" );

            ViscaResponse response =  null;

            switch( replyType )
            {
                case 4: //Ack
                    if( pendingCmd == null ) throw new InvalidOperationException();

                    response = new Acknowledgement();
                    matchesPending = true;
                    break;

                case 5: //Completion (commands/inquiries)
                    if( packet.Length == 3 ) //Command Complete
                    {
                        response = new CommandComplete();
                    }
                    else
                    {
                        matchesPending = true;
                        response = (ViscaInquiryResponse)Activator.CreateInstance( pendingCmd.InquiryResponseType );
                    }
                    break;

                case 6: //Error
                    if( packet.Length != 4 ) throw new ArgumentException( "Invalid Data: Error Packet not correct." );
                    response = new CommandError( packet[2] );

                    if( pendingCmd != null ) matchesPending = true;

                    break;

                default: throw new NotSupportedException();
            }

            response.SourceAddress = src;
            response.Socket = socket;

            if( response is ViscaInquiryResponse )
            {
                //process only the meat data of the packet (strip out header and footer bytes)
                byte[] data = new byte[packet.Length - 3];

                Array.Copy( packet, 2, data, 0, packet.Length - 3 );

                ( (ViscaInquiryResponse)response ).ProcessResponse_Internal( data );
            }

            return response;
        }
        public void ViscaDynamicCloneCommandComparsions()
        {
            ViscaMemoryRecall recallCmd = new ViscaMemoryRecall(0x01, 0x01);

            recallCmd.UsePreset(0x02);

            ViscaCommand recallCloneCmd = recallCmd.Clone();

            Assert.That(recallCloneCmd != null, Is.True, "Clonned Command != null");
            Assert.That(recallCloneCmd == null, Is.False, "Clonned Command == null");
            Assert.That(null == recallCloneCmd, Is.False, "null == Clonned Command");

            recallCmd.UsePreset(0x03);
            Assert.That(recallCmd == recallCloneCmd, Is.False, "recallCmd with Preset 3 and Clonned Command with Preset 2 are equal");

            recallCmd.UsePreset(0x02);
            Assert.That(recallCmd == recallCloneCmd, Is.True, "recallCmd with Preset 2 and Clonned Command with Preset 2 are not equal");
        }