コード例 #1
0
        /// <summary>
        /// Expects CBus message bytes with ETX and STX stripped and check sum already verified
        /// </summary>
        /// <param name="CommandBytes"></param>
        /// <param name="Command"></param>
        /// <returns></returns>
        public bool TryParseCommand(byte[] CommandBytes, int CommandLength, bool IsMonitoredSAL, bool IsShortFormMessage, out CBusSALCommand Command)
        {
            int  dataPointer;
            byte CBusApplicationAddress;

            if (CBusSALCommand.TryParseApplicationId(CommandBytes, CommandLength, IsMonitoredSAL, IsShortFormMessage, out dataPointer, out CBusApplicationAddress))
            {
                //var appAddress = IsShortFormMessage ? CommandBytes[1] : CommandBytes[2];

                var maping = AddressMap.SingleOrDefault(m => m.Address == CBusApplicationAddress);
                if (maping != null)
                {
                    switch (maping.ApplicationType)
                    {
                    case CBusProtcol.ApplicationTypes.LIGHTING:
                    {
                        CBusLightingCommand lightingCommand;
                        if (CBusLightingCommand.TryParseReply(CommandBytes, CommandLength, IsShortFormMessage, CBusApplicationAddress, ref dataPointer, out lightingCommand))
                        {
                            Command = lightingCommand;
                            return(true);
                        }
                        break;
                    }

                    case CBusProtcol.ApplicationTypes.TRIGGER:
                    {
                        CBusTriggerCommand triggerCommand;
                        if (CBusTriggerCommand.TryParse(CommandBytes, CommandLength, IsShortFormMessage, CBusApplicationAddress, ref dataPointer, out triggerCommand))
                        {
                            Command = triggerCommand;
                            return(true);
                        }
                        break;
                    }

                    default:
                        break;
                    }
                }
                else
                {
                    //Valid message but unknown application type
                }
            }
            else
            {
                //Could not get application address
            }

            Command = null;
            return(false);
        }
コード例 #2
0
        /// <summary>
        /// When receiving a command from the wire parse using this method into a CBus Command
        /// </summary>
        /// <param name="CommandBytes"></param>
        /// <param name="Command"></param>
        /// <returns></returns>
        internal static bool TryParseReply(
            byte[] CommandBytes, int CommandLength, bool IsShortFormMessage, byte CBusApplicationAddress,
            ref int dataPointer, out CBusLightingCommand Command)
        {
            //Process Contained Commands
            var lightingCommandList = new List <LightingCommand>();

            if (TryProcessApplicationData(CommandBytes, CommandLength, lightingCommandList, ref dataPointer))
            {
                //Use any old header not part of reply
                Command = new CBusLightingCommand(CBusHeader.PPM, CBusApplicationAddress, lightingCommandList);
                return(true);
            }

            Command = null;
            return(false);
        }
コード例 #3
0
        internal static bool TryParseCommand(byte[] CommandBytes, int CommandLength, out CBusLightingCommand Command)
        {
            int dataPointer = 0;

            Command = null;
            var lightingCommandList = new List <LightingCommand>();

            CBusHeader header = null;

            //Datapointer=0
            if (!CBusHeader.TryParse(CommandBytes[dataPointer++], out header))
            {
                return(false);
            }



            //Datapointer=1
            var appAddress = CommandBytes[dataPointer++];

            switch (header.AddressType)
            {
            case CBusHeader.CBusHeader_AddressType.Point_To_Multi:
                break;

            case CBusHeader.CBusHeader_AddressType.Point_To_Point:
                //Skip 1 byte address
                dataPointer++;
                break;

            case CBusHeader.CBusHeader_AddressType.Point_To_Point_Multi:
                //Skip 2 byte address
                dataPointer += 2;
                break;
            }


            //Datapointer=3
            if (CommandBytes[dataPointer] != 0x01 && CommandBytes[dataPointer] != 0x00)
            {
                return(false);
            }

            //If this byte 01 then next byte must be 00
            if (CommandBytes[dataPointer++] == 0x01)
            {
                if (CommandBytes[dataPointer++] != 0x00)
                {
                    return(false);
                }
            }



            //Process Contained Commands

            //Subtract 1 as this is the payload length excluding checksum
            if (TryProcessApplicationData(CommandBytes, (CommandLength - 1), lightingCommandList, ref dataPointer))
            {
                Command = new CBusLightingCommand(header, appAddress, lightingCommandList);
                return(true);
            }
            else
            {
                return(false);
            }
        }