Ejemplo n.º 1
0
        /// <summary>
        /// 获取消息报文对象列表
        /// </summary>
        /// <param name="dataArray">消息报文字节数组</param>
        /// <returns></returns>
        public static List <Datagram> GetDatagramList(Byte[] dataArray)
        {
            if (dataArray.Length < 22)
            {
                throw new AirException("命令格式错误。", ResponseCode.CommandFormatError);
            }

            List <Byte[]> byteArrayList    = new List <byte[]>();
            List <Byte[]> newByteArrayList = new List <byte[]>();

            GetByteArrayList(dataArray, 0, ref byteArrayList);
            Descaping(byteArrayList, ref newByteArrayList);

            MessageHead     mh           = new MessageHead();
            MessageBody     mb           = new MessageBody();
            Datagram        d            = new Datagram();
            List <Datagram> datagramList = new List <Datagram>();

            foreach (var byteArray in newByteArrayList)
            {
                if (!Enum.IsDefined(typeof(MessageType), byteArray[0]))
                {
                    throw new AirException("不支持该类型的命令。", ResponseCode.NonsupportType);
                }

                mh.Type      = (MessageType)byteArray[0];
                mh.Length    = (UInt16)((byteArray[1] << 8) + byteArray[2]);
                mh.SeqNumber = (UInt32)((byteArray[3] << 24) + (byteArray[4] << 16) + (byteArray[5] << 8) + byteArray[6]);
                mh.Reserved  = (UInt32)((byteArray[7] << 16) + (byteArray[8] << 8) + byteArray[9]);
                mh.Crc       = (UInt16)((byteArray[10] << 8) + byteArray[11]);

                if (!Enum.IsDefined(typeof(MessageId), (UInt16)((byteArray[12] << 8) + byteArray[13])))
                {
                    throw new AirException("不支持该操作。", ResponseCode.NonsupportOperation);
                }

                mb.MsgId = (MessageId)((byteArray[12] << 8) + byteArray[13]);
                mb.DevId = ((UInt64)byteArray[14] << 56) + ((UInt64)byteArray[15] << 48) + ((UInt64)byteArray[16] << 40) + ((UInt64)byteArray[17] << 32)
                           + ((UInt64)byteArray[18] << 24) + ((UInt64)byteArray[19] << 16) + ((UInt64)byteArray[20] << 8) + byteArray[21];

                List <Parameter> pmtList = new List <Parameter>();
                GetParameterList(byteArray, 22, ref pmtList);

                if (pmtList.Count > 0)
                {
                    mb.PmtList = pmtList;

                    if (Crc.GetCrc(mb.GetBody()) != mh.Crc)
                    {
                        throw new AirException("消息体CRC校验错误。", ResponseCode.CrcCheckError);
                    }
                }
                else
                {
                    throw new AirException("参数格式错误。", ResponseCode.ParameterFormatError);
                }

                d.Head = mh;
                d.Body = mb;
                datagramList.Add(d);
            }

            return(datagramList);
        }