예제 #1
0
        public static Message ConvertFrom(byte[] buffer)
        {
            if (!Validate(buffer))
            {
                Log(buffer);
                return(null);
            }

            var        length = buffer.ReadShort(2);
            string     key    = Unity.GetDeviceNumber(buffer, 4);
            DeviceInfo device = DeviceCache.Current.Get(key);

            if (device == null)
            {
                device                 = new DeviceInfo();
                device.OrgId           = 0;
                device.ModelCode       = 0;
                device.KindCode        = 0;
                device.DeviceNumber    = key;
                device.OnLineTime      = DateTime.Now;
                device.DeviceStatus    = 1;
                device.SoftwareVersion = "00000000";// Unity.GetMechineVersion(buffer, 15);
            }

            var messageType = buffer[13];// (short)(BitConverter.ToInt16(buffer, 12) >> 8);

            var msg = new Message()
            {
                Length = length,
                Type   = messageType,
                Device = device
            };

            if (messageType == (short)MessageType.Beep)
            {
                return(msg);
            }

            msg.Key = buffer[14];

            if (messageType == (short)MessageType.Login)
            {
                if (device.PkId == 0)
                {
                    device.SoftwareVersion = Unity.GetMechineVersion(buffer, 15);
                    DeviceCache.Current.Set(device);
                }
            }

            int KEY_LENGTH = 1;
            var dataLength = length - PAKAGE_FLAG_LENGTH;

            if (dataLength > 0)
            {
                dataLength -= KEY_LENGTH;

                if (messageType == (short)MessageType.Detail || messageType == 160)
                {
                    msg.Currency = Unity.GetCurrency(buffer, 15, msg.Key);

                    if (msg.Device != null)
                    {
                        msg.Currency.OrgId           = msg.Device.OrgId;
                        msg.Currency.DeviceKindCode  = msg.Device.KindCode;
                        msg.Currency.DeviceModelCode = msg.Device.ModelCode;
                        msg.Currency.DeviceNumber    = msg.Device.DeviceNumber;
                    }
                }

                else
                {
                    var index = 15;
                    if (messageType == (short)MessageType.DownLoadBlackTable)
                    {
                        dataLength += KEY_LENGTH;
                        index      -= KEY_LENGTH;
                    }
                    var data = new byte[dataLength];
                    Array.Copy(buffer, index, data, 0, dataLength);
                    msg.Datas = data;
                }
            }

            return(msg);
        }