Exemple #1
0
        /// <summary>
        /// 获取消息体字节数组。
        /// <para>如果 <see cref="DESKey"/> 不为空,则使用 <see cref="DES"/> 密钥加密消息体。</para>
        /// </summary>
        /// <returns></returns>
        public byte[] GetBody()
        {
            List <byte> mb = new List <byte> {
                (byte)((ushort)(MessageId) >> 8),
                (byte)(MessageId)
            };

            for (int i = 24; i >= 0; i -= 8)
            {
                mb.Add((byte)(GatewayId >> i));
            }

            for (int j = 24; j >= 0; j -= 8)
            {
                mb.Add((byte)(LuminaireId >> j));
            }

            if (ParameterList != null)
            {
                foreach (var pmt in ParameterList)
                {
                    mb.AddRange(pmt.GetParameter());
                }
            }
            else
            {
                for (int k = 24; k >= 0; k -= 8)
                {
                    mb.Add((byte)(((uint)ErrorCode) >> k));
                }

                if (ErrorInfo != null)
                {
                    mb.AddRange(ErrorInfo.ToByteArray());
                }
            }

            if (DESKey != null)
            {
                if (DESKey.Length != 8)
                {
                    throw new ArgumentNullException(nameof(DESKey), $"{typeof(DES)} 密钥长度不正确。");
                }
                else
                {
                    DESHelper des = new DESHelper();
                    return(des.Encrypt(DESKey, mb.ToArray()));
                }
            }
            else
            {
                return(mb.ToArray());
            }
        }