Beispiel #1
0
        private FileHeader ReadHeader()
        {
            var buff = new byte[128];
            var len  = reader.Read(buff, 0, 128);

            if (len < buff.Length)
            {
                throw new InvalidDataException("错误的文件格式");
            }
            var header = new FileHeader();

            header.FileType      = buff.GetString(0, 3);
            header.Version       = buff[3];
            header.Size          = BitConverter.ToInt16(buff, 4);
            header.MachineSN     = Unity.GetDeviceNumber(buff, 6);
            header.BootVersion   = buff.GetByteString(14, 3);
            header.SoftVersion   = buff.GetByteString(17, 3);
            header.SumRecords    = BitConverter.ToInt32(buff, 20);
            header.UploadRecord  = BitConverter.ToInt32(buff, 24);
            header.NextUpLoadFtp = BitConverter.ToInt32(buff, 28);
            return(header);
        }
Beispiel #2
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);
        }