Exemplo n.º 1
0
        public static List <PsnBinLogAdvancedLocationInfo> ApplyAdvancedFragmentationFixed(string fileName, IList <IPsnPagesLocationInfo> psnLogFragmentInfos)
        {
            var resultLocations = new List <PsnBinLogAdvancedLocationInfo>();
            var extractor       = PsnPageExtractorFactory.Extractor;

            using (var br = new AdvancedBinaryReader(File.OpenRead(fileName), false)) {
                // Сперва выбираем фрагменты с хорошими первыми страницами:
                foreach (var locationInfo in psnLogFragmentInfos)
                {
                    try {
                        br.BaseStream.Seek(extractor.PsnPageSize * locationInfo.PagesInterval.Begin, SeekOrigin.Begin);
                        var firstPageHeaderBytes = br.ReadBytes(extractor.PsnPageHeaderLength);

                        br.BaseStream.Seek(extractor.PsnPageSize * locationInfo.PagesInterval.End, SeekOrigin.Begin);
                        var lastPageHeaderBytes = br.ReadBytes(extractor.PsnPageHeaderLength);


                        var firstHeader = extractor.GetHeaderFromRealDevice(firstPageHeaderBytes);
                        var lastHeader  = extractor.GetHeaderFromRealDevice(lastPageHeaderBytes);

                        if (firstHeader.PageInfo == PsnPageInfo.NormalPage && lastHeader.PageInfo == PsnPageInfo.NormalPage)
                        {
                            if (locationInfo.TimesInterval.Begin.HasValue && firstHeader.CreatedAt.HasValue)
                            {
                                //if (locationInfo.TimesInterval.Begin.Value.EqualToEvenSecond(firstHeader.CreatedAt.Value)) {
                                if (lastHeader.CreatedAt.HasValue)
                                {
                                    if (lastHeader.CreatedAt.Value > firstHeader.CreatedAt.Value)
                                    {
                                        resultLocations.Add(
                                            new PsnBinLogAdvancedLocationInfo(
                                                fileName,
                                                new PsnPageNumberAndTime(locationInfo.PagesInterval.Begin, firstHeader.CreatedAt.Value),
                                                new PsnPageNumberAndTime(locationInfo.PagesInterval.End, lastHeader.CreatedAt.Value)));
                                    }
                                }
                            }
                        }
                    }
                    catch {
                        // TODO: remove empty catch;
                    }
                }
                // Сортировка фрагментов по дате и указание последней записи
                if (resultLocations.Count > 1)
                {
                    var orderedLocations = resultLocations.OrderBy(loc => loc.FirstPageInfo.Time.Value).ToList();
                    var lastLocation     = orderedLocations.Last();
                    orderedLocations.RemoveAt(orderedLocations.Count - 1);
                    orderedLocations.Add(new PsnBinLogAdvancedLocationInfo(fileName, lastLocation.FirstPageInfo, lastLocation.LastPageInfo)
                    {
                        IsLastPsnLogOnDevice = true
                    });
                    resultLocations = orderedLocations;
                }

                return(resultLocations);
            }
        }
        public ICommandConfiguration BuildConfiguration()
        {
            // PERFOMANCE INCREASE POSSIBILITY: need fastest work with stream, closing it, then filling fields and vars
            using (var br = new AdvancedBinaryReader(File.OpenRead(_filename), false)) {
                br.BaseStream.Seek(2048, SeekOrigin.Begin);
                var memoryStatusByte = br.ReadByte();
                var configStatusByte = br.ReadByte();

                var verificationStatusBytes = br.ReadBytes(32);

                var meterChannelConfigVerificationStatuses = new List <IMetersChannelsConfigurationVerificationStatus>();
                for (int i = 0; i < verificationStatusBytes.Length; ++i)
                {
                    var status = verificationStatusBytes[i];
                    ChannelConfigVerificationStatus st;
                    switch (status)
                    {
                    case 0:
                        st = ChannelConfigVerificationStatus.NoMeterFoundInTable;
                        break;

                    case 1:
                        st = ChannelConfigVerificationStatus.VerificationSuccess;
                        break;

                    case 2:
                        st = ChannelConfigVerificationStatus.ErrorDuringConfigurationWriting;
                        break;

                    case 3:
                        st = ChannelConfigVerificationStatus.NoLinkWithMeter;
                        break;

                    case 4:
                        st = ChannelConfigVerificationStatus.VerificationInProgress;
                        break;

                    default:
                        st = ChannelConfigVerificationStatus.Unknown;
                        break;
                    }
                    meterChannelConfigVerificationStatuses.Add(new MetersChannelsConfigurationVerificationStatusSimple(i, st));
                }
                br.ReadByte();
                var second = br.ReadByte();
                var minute = br.ReadByte();
                var hour   = br.ReadByte();
                var day    = br.ReadByte();
                var month  = br.ReadByte();
                var year   = br.ReadByte();

                DateTime?dateTime;
                try {
                    dateTime = new DateTime(2000 + year, month, day, hour, minute, second);
                }
                catch {
                    dateTime = null;
                }

                var psnProtocolType = br.ReadUInt16();

                return(new CommandConfigurationSimple(
                           dateTime,
                           (memoryStatusByte & 0x01) == 0x01,
                           (memoryStatusByte & 0x02) == 0x02,
                           (memoryStatusByte & 0x04) == 0x04,
                           (memoryStatusByte & 0x08) == 0x08,

                           (configStatusByte & 0x01) == 0x01,
                           (configStatusByte & 0x02) == 0x02,
                           (configStatusByte & 0x04) == 0x04,
                           (configStatusByte & 0x08) == 0x08,
                           (configStatusByte & 0x10) == 0x10,
                           (configStatusByte & 0x20) == 0x20,
                           (configStatusByte & 0x40) == 0x40,

                           meterChannelConfigVerificationStatuses,
                           psnProtocolType
                           ));
            }
        }