Exemplo n.º 1
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 TryParse(byte[] CommandBytes, int CommandLength, bool IsShortFormMessage, byte CBusApplicationAddress,
                                      ref int dataPointer, out CBusTriggerCommand Command)
        {
            Command = null;
            var triggerCommandList = new List <TriggerCommand>();

            CBusHeader header;

            CBusHeader.TryParse(CommandBytes[0], out header);

            //As message length includes checksum, where as we just want the payload length
            var messagePayloadLenght = (CommandLength - 1);

            //Process Contained Commands
            while (dataPointer < messagePayloadLenght)
            {
                //Most Significant Bit indicates long or short format comamnd
                if ((CommandBytes[dataPointer] & 0x80) == 0)
                {
                    //Short form command
                    byte group = 0;
                    var  triggerCommandType = TriggerCommand.TriggerCommandId.UNKNOWN;

                    if (!Enum.IsDefined(typeof(TriggerCommand.TriggerCommandId), CommandBytes[dataPointer]))
                    {
                        return(false);
                    }

                    //Lower 3 bites of command
                    //var commandLength = (commandId & 0x07);

                    triggerCommandType = (TriggerCommand.TriggerCommandId)CommandBytes[dataPointer++];
                    group = CommandBytes[dataPointer++];


                    //Add the command
                    switch (triggerCommandType)
                    {
                    case TriggerCommand.TriggerCommandId.EVENT:
                    {
                        var triggerGroup = CommandBytes[dataPointer++];
                        var Action       = CommandBytes[dataPointer++];
                        var cmd          = new TriggerCommand(triggerGroup, Action);
                        triggerCommandList.Add(cmd);
                        break;
                    }

                    //Also contain level parameter
                    case TriggerCommand.TriggerCommandId.TRIGGER_MIN:
                    case TriggerCommand.TriggerCommandId.TRIGGER_MAX:
                    case TriggerCommand.TriggerCommandId.TRIGGER_KILL:
                    {
                        var triggerGroup = CommandBytes[dataPointer++];
                        var cmd          = new TriggerCommand(triggerCommandType, triggerGroup);
                        triggerCommandList.Add(cmd);
                        break;
                    }

                    case TriggerCommand.TriggerCommandId.UNKNOWN:
                    default:
                        return(false);
                    }
                }
                else
                {
                    //Long form command, not implemented
                    break;
                }
            }

            Command = new CBusTriggerCommand(header, CBusApplicationAddress, triggerCommandList);
            return(true);
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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 TryParseApplicationId(byte[] CommandBytes, int CommandLength, bool IsMonitoredSALReply, bool IsShortFormMessage, out int SALDataPointer, out byte ApplicationId)
        {
            SALDataPointer = 0;
            ApplicationId  = 0;

            if (IsMonitoredSALReply)
            {
                if (IsShortFormMessage)
                {
                    if (CommandBytes[0] == 0)
                    {
                        //No Bridge, next byte is Application Id
                        ApplicationId  = CommandBytes[1];
                        SALDataPointer = 2;
                    }
                    else
                    {
                        //No Bridge, next byte is Application Id
                        ApplicationId  = CommandBytes[2];
                        SALDataPointer = 3;
                    }
                }
                else
                {
                    if (CommandBytes[0] == 0x05)
                    {
                        ApplicationId = CommandBytes[2];

                        var NumberOfRoutes = CommandBytes[3] / CBusProtcol.ROUTE_HEADER_DIVISOR;

                        SALDataPointer = 3 + NumberOfRoutes;
                        SALDataPointer++;
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else
            {
                CBusHeader header;
                if (CBusHeader.TryParse(CommandBytes[0], out header))
                {
                    switch (header.AddressType)
                    {
                    case CBusHeader.CBusHeader_AddressType.Point_To_Point:
                        //NOT suported CAL data only
                        return(false);

                    case CBusHeader.CBusHeader_AddressType.Point_To_Multi:
                        ApplicationId  = CommandBytes[1];
                        SALDataPointer = 2;
                        SALDataPointer++;     //Skip trailing 0
                        break;

                    case CBusHeader.CBusHeader_AddressType.Point_To_Point_Multi:
                    {
                        var firstHopTarget = CommandBytes[1];
                        var NumberOfRoutes = CommandBytes[2] / CBusProtcol.ROUTE_HEADER_DIVISOR;

                        ApplicationId  = CommandBytes[2 + NumberOfRoutes];
                        SALDataPointer = 3 + NumberOfRoutes;
                        SALDataPointer++;
                        break;
                    }

                    default:
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                return(true);
            }
        }