Пример #1
0
 public ItemData <short> ReadInt16(DeviceAddress address)
 {
     try
     {
         return(new ItemData <short>(accessor.ReadInt16(FindPosition(address)), 0, QUALITIES.QUALITY_GOOD));
     }
     catch { return(new ItemData <short>(0, 0, QUALITIES.QUALITY_BAD)); }
 }
Пример #2
0
        public override short GetShort()
        {
            var c = _accessor.ReadInt16(Position);

            Position += 2;

            //conform to how the index was written
            return(Number.FlipEndian(c));
        }
Пример #3
0
        public override short GetShort()
        {
            var littleEndian = _accessor.ReadInt16(Ix(NextGetIndex(2)));

            if (bigEndian)
            {
                return(Number.FlipEndian(littleEndian));
            }
            return(littleEndian);
        }
Пример #4
0
 /// <summary>
 /// Read SharedMemory
 /// </summary>
 /// <param name="MemViewAccessor"></param>
 public void GetSharedMemory(MemoryMappedViewAccessor MemViewAccessor)
 {
     _logger?.LogTrace("Read Shared Memory");
     c2e                    = GetControlArray(MemViewAccessor);
     ProcessID              = MemViewAccessor.ReadInt16(4);
     ResultCode             = MemViewAccessor.ReadInt16(8);
     Size                   = MemViewAccessor.ReadUInt16(12);
     SharedMemoryBufferSize = MemViewAccessor.ReadUInt16(16);
     Data                   = new byte[Size];
     for (int i = 0; i < Size; i++)
     {
         Data[i] = MemViewAccessor.ReadByte(24 + i);
     }
 }
Пример #5
0
        /// <summary>
        /// Read Terrain Height
        /// </summary>
        /// <param name="line"></param>
        /// <param name="sample"></param>
        /// <returns>terrain height in m relative to the reference radius (1737.4 km)</returns>
        public float LineSampleToTerrainOffset(int line, int sample)
        {
            const int height = 30400;
            const int width  = 30400;
            const int stride = width * 2;

            if (line < 0 || line >= height || sample < 0 || sample >= width)
            {
                throw new Exception(@"line,sample out-of-bounds");
            }

            int   index = line * stride + sample * 2;
            short h     = _accessor.ReadInt16(index);

            return(0.5f * h);
        }
Пример #6
0
        internal short ReadInt16(long translatedRowIndex, long translatedColumnIndex)
        {
            var columnMetadata = Metadata.Columns[translatedColumnIndex];
            var dataOffset     = columnMetadata.DataOffset;

            var valueOffset = dataOffset + translatedRowIndex * sizeof(short);

            return(View.ReadInt16(valueOffset));
        }
Пример #7
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 public Int16 ReadInt16(long inPosition)
 {
     try
     {
         return(_sharedMemoryAccesor.ReadInt16(inPosition));
     }
     catch
     {
         return(0);
     }
 }
Пример #8
0
        public static BitmapInfoHeader GetInfoHeader(MemoryMappedViewAccessor Reader, out long MMPosition)
        {
            MMPosition = 18;
            var header = new BitmapInfoHeader();

            // HeaderSize = reader.ReadInt32(),
            header.BitmapWidth = Reader.ReadInt32(MMPosition);
            MMPosition        += 4;

            header.BitmapHeight = Reader.ReadInt32(MMPosition);
            MMPosition         += 4;

            header.ColorPlanes = Reader.ReadInt16(MMPosition);
            MMPosition        += 2;

            header.BitsPerPixel = Reader.ReadInt16(MMPosition);
            MMPosition         += 2;

            header.CompressinoMethod = Reader.ReadInt32(MMPosition);
            MMPosition += 4;

            header.ColorDataSize = Reader.ReadInt32(MMPosition);
            MMPosition          += 4;

            header.HorizantalResolution = Reader.ReadInt32(MMPosition);
            MMPosition += 4;

            header.VerticalResolution = Reader.ReadInt32(MMPosition);
            MMPosition += 4;

            header.Colors = Reader.ReadInt32(MMPosition);
            MMPosition   += 4;

            header.IColors = Reader.ReadInt32(MMPosition);
            MMPosition    += 4;

            return(header);
        }
Пример #9
0
        public short ReadInt16()
        {
            long position = _position + _origin;

            if (position + 2 > _length)
            {
                throw new IOException(SR.BlobReadOutOfBound);
            }

            var value = _accessor.ReadInt16(position);

            _position += 2;

            return(value);
        }
Пример #10
0
        public short GetShort()
        {
            const long size = 2;

            if (_position + size > _size)
            {
                _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left");
                throw new OverflowException();
            }

            var value = _va.ReadInt16(_offset + _position);

            _position += size;

            return(value);
        }
Пример #11
0
        /// <summary>
        /// Reads the <see cref="short"/> at the specified index using the byte order
        /// specified by <see cref="ByteBuffer.Order"/>. No validation is done on
        /// <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index to begin reading bytes.</param>
        /// <returns>The <see cref="short"/> at the specified index.</returns>
        protected short LoadInt16(int index)
        {
            int   baseOffset = offset + index;
            short bytes      = 0;

            if (order == Endianness.BigEndian)
            {
                bytes  = (short)(accessor.ReadByte(baseOffset) << 8);
                bytes |= (short)(accessor.ReadByte(baseOffset + 1) & 0xFF);
            }
            else
            {
                //bytes = (short)(accessor.ReadByte(baseOffset + 1) << 8);
                //bytes |= (short)(accessor.ReadByte(baseOffset) & 0xFF);
                bytes = accessor.ReadInt16(baseOffset);
            }
            return(bytes);
        }
Пример #12
0
        private static int BinarySearch(MemoryMappedViewAccessor acc, int length, short value)
        {
            int i    = 0;
            int num2 = length - 1;

            while (i <= num2)
            {
                int num3 = i + ((num2 - i) >> 1);
                int num4 = acc.ReadInt16(12 + num3 * 11).CompareTo(value);
                if (num4 == 0)
                {
                    return(num3);
                }
                if (num4 < 0)
                {
                    i = num3 + 1;
                }
                else
                {
                    num2 = num3 - 1;
                }
            }
            return(-1);
        }
        /// <summary>Performs many reads and writes of various data types against the accessor.</summary>
        private static void AssertWritesReads(MemoryMappedViewAccessor acc)
        {
            // Successful reads and writes at the beginning for each data type
            AssertWriteRead <bool>(false, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos));
            AssertWriteRead <bool>(true, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos));
            AssertWriteRead <byte>(42, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos));
            AssertWriteRead <char>('c', 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos));
            AssertWriteRead <decimal>(9, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos));
            AssertWriteRead <double>(10, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos));
            AssertWriteRead <short>(11, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos));
            AssertWriteRead <int>(12, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos));
            AssertWriteRead <long>(13, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos));
            AssertWriteRead <sbyte>(14, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos));
            AssertWriteRead <float>(15, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos));
            AssertWriteRead <ushort>(16, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos));
            AssertWriteRead <uint>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos));
            AssertWriteRead <ulong>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos));

            // Successful reads and writes at the end for each data type
            long end = acc.Capacity;

            AssertWriteRead <bool>(false, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos));
            AssertWriteRead <bool>(true, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos));
            AssertWriteRead <byte>(42, end - sizeof(byte), (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos));
            AssertWriteRead <char>('c', end - sizeof(char), (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos));
            AssertWriteRead <decimal>(9, end - sizeof(decimal), (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos));
            AssertWriteRead <double>(10, end - sizeof(double), (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos));
            AssertWriteRead <short>(11, end - sizeof(short), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos));
            AssertWriteRead <int>(12, end - sizeof(int), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos));
            AssertWriteRead <long>(13, end - sizeof(long), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos));
            AssertWriteRead <sbyte>(14, end - sizeof(sbyte), (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos));
            AssertWriteRead <float>(15, end - sizeof(float), (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos));
            AssertWriteRead <ushort>(16, end - sizeof(ushort), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos));
            AssertWriteRead <uint>(17, end - sizeof(uint), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos));
            AssertWriteRead <ulong>(17, end - sizeof(ulong), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos));

            // Failed reads and writes just at the border of the end. This triggers different exception types
            // for some types than when we're completely beyond the end.
            long beyondEnd = acc.Capacity + 1;

            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadChar(beyondEnd - sizeof(char)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint)));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong)));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c'));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0));
            AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0));

            // Failed reads and writes well past the end
            beyondEnd = acc.Capacity + 20;
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadChar(beyondEnd - sizeof(char)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong)));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c'));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0));
        }
Пример #14
0
 public short  ReadInt16(int offset)
 {
     return(_f.ReadInt16(offset));
 }
 /// <summary>
 /// Reads a 16-bit integer of a specific Endianness from the accessor and converts it to the current system Endianness.
 /// </summary>
 /// <param name="mem">The specified MemoryMappedViewAccessor.</param>
 /// <param name="position">The number of bytes into the accessor at which to begin reading.</param>
 /// <param name="convertFrom">The Endianness to convert from.</param>
 /// <returns>The value that was read as converted to the current system Endianness.</returns>
 public static short ReadInt16(this MemoryMappedViewAccessor mem, long position, Endianness convertFrom)
 {
     return(mem.ReadInt16(position).ConvertToSystemEndian(convertFrom));
 }
Пример #16
0
 public short ReadInt16(long offset)
 {
     Reserve(offset, 2);
     return(m_view.ReadInt16(offset - m_offset));
 }
Пример #17
0
        public static void SDTCompression(int year, int month, float E = 0.7f)
        {
            //Stopwatch sw = Stopwatch.StartNew();
            string path = string.Concat(m_Path, "\\", year.ToString(), "-", month.ToString());

            using (FileStream stream = File.Open(path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (FileStream outstream = File.Create(path + ".sdt"))
                {
                    outstream.Write(new byte[0x100], 0, 0x100);
                    BinaryWriter w = new BinaryWriter(outstream);
                    using (MemoryMappedFile mapp = MemoryMappedFile.CreateFromFile(stream, "map1", stream.Length,
                                                                                   MemoryMappedFileAccess.ReadWrite, HandleInheritability.Inheritable, false))
                    {
                        int    days  = DateTime.DaysInMonth(year, month);
                        long[] ps    = new long[days + 1];
                        long[] ps1   = new long[days + 1];
                        long[] sizes = new long[days];
                        MemoryMappedViewAccessor acc1 = mapp.CreateViewAccessor(0, 8 * days);
                        long begin = 0;
                        ps[0] = acc1.ReadInt64(begin);
                        for (int i = 0; i < days; i++)
                        {
                            begin    += 8;
                            ps[i + 1] = (i == days - 1 ? stream.Length : acc1.ReadInt64(begin));
                            sizes[i]  = ps[i + 1] - ps[i];
                        }
                        acc1.Dispose();
                        for (int i = 0; i < days; i++)
                        {
                            if (ps[i] < 0x100 || sizes[i] <= 0)
                            {
                                continue;
                            }
                            using (MemoryMappedViewAccessor acc = mapp.CreateViewAccessor(ps[i], sizes[i]))
                            {
                                ps1[i] = outstream.Position;
                                int           len  = acc.ReadInt32(8);
                                int           len1 = len * 11 + 12;
                                HDataFormat[] list = new HDataFormat[len];
                                w.Write(acc.ReadInt32(0));
                                w.Write(acc.ReadInt32(4));
                                w.Write(len);
                                outstream.Write(new byte[len1 - 12], 0, len1 - 12);
                                long pos = 12;
                                int  off = 0;
                                for (int j = 0; j < len; j++)
                                {
                                    short id; byte type; int count; int offset;
                                    id             = acc.ReadInt16(pos);
                                    type           = acc.ReadByte(pos + 2);
                                    count          = acc.ReadInt32(pos + 3);
                                    offset         = acc.ReadInt32(pos + 7);
                                    list[j].ID     = id;
                                    list[j].Type   = (DataType)type;
                                    list[j].Offset = off;//此处可采取三次到五次抽样得到E和TLM
                                    if (count < 3)
                                    {
                                        long pos2 = len1 + offset;
                                        for (int m = 0; m < count; m++)
                                        {
                                            w.Write(acc.ReadInt32(pos2));
                                            pos2 += 4;
                                            w.Write(acc.ReadSingle(pos2));
                                            pos2 += 4;
                                        }
                                        continue;
                                    }
                                    else
                                    {
                                        switch (list[j].Type)
                                        {
                                        case DataType.FLOAT:
                                        {
                                            int   crt = 0; int net = 0;
                                            float crv = 0; float nev = 0;
                                            int   maxt = 0; int mint = maxt; int sumt = 0;
                                            float minv = 0; float maxv = minv; float sumv = 0;
                                            int   old_time = 0; int time = 0;
                                            float mem = 0; float old_mem = 0;
                                            long  pp   = len1 + offset;
                                            long  pos2 = pp + 16;
                                            for (int c = 0; c < 9; c++)
                                            {
                                                crt = acc.ReadInt32(pp);
                                                pp += 4;
                                                crv = acc.ReadSingle(pp);
                                                pp += 4;
                                                if (c > 0)
                                                {
                                                    float cv = crv - nev;
                                                    int   ct = crt - net;
                                                    if (c == 1)
                                                    {
                                                        time = crt;
                                                        mem  = crv;
                                                        maxt = mint = ct;
                                                        minv = maxv = cv;
                                                    }
                                                    else
                                                    {
                                                        if (cv > maxv)
                                                        {
                                                            maxv = cv;
                                                        }
                                                        if (cv < minv)
                                                        {
                                                            minv = cv;
                                                        }
                                                        if (ct > maxt)
                                                        {
                                                            maxt = ct;
                                                        }
                                                        if (ct < mint)
                                                        {
                                                            mint = ct;
                                                        }
                                                    }
                                                    sumv += cv;
                                                    sumt += ct;
                                                }
                                                else
                                                {
                                                    old_mem  = crv;
                                                    old_time = crt;
                                                }
                                                nev = crv;
                                                net = crt;
                                            }
                                            int   TLM = (sumt - maxt - mint) / 2;
                                            float E1  = E * (sumv - maxv - minv) / 6;
                                            int   sum = 1;
                                            //old_time = now_time = new_time = 0;
                                            float timespan;
                                            w.Write(old_time);
                                            w.Write(old_mem);
                                            float k1, k2, k;
                                            timespan = time - old_time;
                                            k        = (mem - old_mem) / timespan;
                                            k1       = k + (E1 / timespan);
                                            k2       = 2 * k - k1;
                                            for (int m = 2; m < count; m++)
                                            {
                                                if (timespan >= TLM || k < k2 || k > k1)
                                                {
                                                    ++sum;
                                                    w.Write(old_time);
                                                    w.Write(old_mem);
                                                    k1 = k + (E1 / timespan);
                                                    k2 = 2 * k - k1;
                                                }
                                                old_time = time;
                                                old_mem  = mem;
                                                time     = acc.ReadInt32(pos2);
                                                pos2    += 4;
                                                mem      = acc.ReadSingle(pos2);
                                                pos2    += 4;
                                                timespan = time - old_time;
                                                k        = (mem - old_mem) / timespan;
                                            }
                                            list[j].Count = sum;
                                            off          += sum * 8;
                                        }
                                        break;

                                        case DataType.WORD:
                                        case DataType.SHORT:
                                        {
                                            int   crt = 0; int net = 0;
                                            short crv = 0; short nev = 0;
                                            int   maxt = 0; int mint = maxt; int sumt = 0;
                                            int   minv = 0; int maxv = minv; int sumv = 0;
                                            int   old_time = 0; int time = 0;
                                            short mem = 0; short old_mem = 0;
                                            long  pp   = len1 + offset;
                                            long  pos2 = pp + 12;
                                            for (int c = 0; c < 9; c++)
                                            {
                                                crt = acc.ReadInt32(pp);
                                                pp += 4;
                                                crv = acc.ReadInt16(pp);
                                                pp += 2;
                                                if (c > 0)
                                                {
                                                    int cv = crv - nev;
                                                    int ct = crt - net;
                                                    if (c == 1)
                                                    {
                                                        time = crt;
                                                        maxt = mint = ct;
                                                        mem  = crv;
                                                        minv = maxv = cv;
                                                    }
                                                    else
                                                    {
                                                        if (cv > maxv)
                                                        {
                                                            maxv = cv;
                                                        }
                                                        if (cv < minv)
                                                        {
                                                            minv = cv;
                                                        }
                                                        if (ct > maxt)
                                                        {
                                                            maxt = ct;
                                                        }
                                                        if (ct < mint)
                                                        {
                                                            mint = ct;
                                                        }
                                                    }
                                                    sumv += cv;
                                                    sumt += ct;
                                                }
                                                else
                                                {
                                                    old_mem  = crv;
                                                    old_time = crt;
                                                }
                                                nev = crv;
                                                net = crt;
                                            }
                                            int   TLM = (sumt - maxt - mint) / 2;
                                            float E1  = E * (sumv - maxv - minv) / 6;
                                            int   sum = 1;
                                            float timespan;
                                            w.Write(old_time);
                                            w.Write(old_mem);
                                            float k1, k2, k;
                                            timespan = time - old_time;
                                            k        = (mem - old_mem) / timespan;
                                            k1       = k + (E1 / timespan);
                                            k2       = 2 * k - k1;
                                            for (int m = 2; m < count; m++)
                                            {
                                                if (timespan >= TLM || k < k2 || k > k1)
                                                {
                                                    ++sum;
                                                    w.Write(old_time);
                                                    w.Write(old_mem);
                                                    k1 = k + (E1 / timespan);
                                                    k2 = 2 * k - k1;
                                                }
                                                old_time = time;
                                                old_mem  = mem;
                                                time     = acc.ReadInt32(pos2);
                                                pos2    += 4;
                                                mem      = acc.ReadInt16(pos2);
                                                pos2    += 2;
                                                timespan = time - old_time;
                                                k        = (mem - old_mem) / timespan;
                                            }

                                            list[j].Count = sum;
                                            off          += sum * 8;
                                        }
                                        break;

                                        default:
                                        {
                                            byte[] buffer = new byte[count * dataLen[type]];
                                            stream.Seek(ps[i] + len1 + offset, SeekOrigin.Begin);
                                            stream.Read(buffer, 0, buffer.Length);
                                            outstream.Write(buffer, 0, buffer.Length);
                                            list[j].Count = count;
                                            off          += buffer.Length;
                                        }
                                        break;
                                        }
                                        pos += 11;
                                    }
                                }
                                outstream.Seek(ps1[i] + 12, SeekOrigin.Begin);
                                for (int j = 0; j < len; j++)
                                {
                                    w.Write(list[j].ID);
                                    w.Write((byte)list[j].Type);
                                    w.Write(list[j].Count);
                                    w.Write(list[j].Offset);
                                }
                                ps1[i + 1] = outstream.Seek(0, SeekOrigin.End);
                            }
                        }
                        outstream.Seek(0, SeekOrigin.Begin);
                        for (int i = 0; i < days + 1; i++)
                        {
                            w.Write(ps1[i]);
                        }
                    }
                }
            }
        }
Пример #18
0
        public static IEnumerable <HistoryData> LoadFromFile(DateTime start, DateTime end, short ID, bool sdt = false)
        {
            string path = string.Concat(m_Path, "\\", start.Year.ToString(), "-", start.Month.ToString(), sdt ? ".sdt" : ".bin");//bin-sdt

            if (!File.Exists(path))
            {
                yield break;
            }
            int day1       = start.Day;
            int startTicks = new SqlDateTime(start).TimeTicks; //开始日期部分的4位数据
            int endTicks   = new SqlDateTime(end).TimeTicks;
            int ln         = end.Day - day1 + 1;               //日期天数

            using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long         filelen   = stream.Length;        //文件长度
                BinaryReader reader    = new BinaryReader(stream);
                long[]       positions = new long[ln];         //每日数据指针(指向第一条数据,包括当日数据索引区)
                stream.Seek((day1 - 1) * 8, SeekOrigin.Begin); ///找到对应的开始日期索引位置

                for (int i = 0; i < ln; i++)
                {
                    positions[i] = reader.ReadInt64();//读入时间段内每日数据长度值
                }
                long[] sizes = new long[ln];
                for (int i = 0; i < ln; i++)
                {
                    if (positions[i] >= filelen)
                    {
                        break;                                       //如果读入长度超过文件大小则退出
                    }
                    stream.Seek(positions[i] + 8, SeekOrigin.Begin); //定位文件指针到当日数据开头
                    sizes[i] = reader.ReadInt32();                   //sizes为当日该标签数
                }
                //reader.Close();
                HistoryData data = HistoryData.Empty;
                //stream.Read(new byte[]
                using (MemoryMappedFile mapp = MemoryMappedFile.CreateFromFile(stream, Guid.NewGuid().ToString(), filelen, MemoryMappedFileAccess.Read,
                                                                               HandleInheritability.Inheritable, false))
                {
                    for (int k = 0; k < ln; k++)//先读入当日索引区
                    {
                        if (positions[k] < 0x100 || sizes[k] <= 0 || positions[k] + sizes[k] > filelen)
                        {
                            continue;
                        }
                        //if (sizes[k] == 0) continue;
                        long     pos   = 0;
                        int      count = 0;
                        int      day   = 0;
                        int      len   = 0;
                        DataType type  = DataType.NONE;
                        using (MemoryMappedViewAccessor acc1 = mapp.CreateViewAccessor(positions[k], 12 + sizes[k] * 11, MemoryMappedFileAccess.Read)) //12是头的长度,11是一个格式字段的长度
                        {
                            day = acc1.ReadInt32(0);                                                                                                   //当日日期部分
                            int index = BinarySearch(acc1, (int)sizes[k], ID);                                                                         //找到当天 指定标签的记录索引
                            if (index >= 0)
                            {
                                index = index * 11 + 12;//如找到,则定位到当日数据的元数据(相对位移)
                            }
                            //sw.Stop();
                            else
                            {
                                continue;
                            }
                            byte tp = acc1.ReadByte(index + 2);                                                                        //读入数据类型
                            type  = (DataType)tp;
                            len   = dataLen[tp];                                                                                       //4,6,8分别为存储的标签长度,其中4字节是时间戳
                            count = acc1.ReadInt32(index + 3);                                                                         //读入数量
                            pos   = positions[k] + 12 + sizes[k] * 11 + acc1.ReadInt32(index + 7);                                     //指针指向当日当前标签第一条记录
                        }
                        using (MemoryMappedViewAccessor acc2 = mapp.CreateViewAccessor(pos, count * len, MemoryMappedFileAccess.Read)) //重新从头定位文件指针到数据区
                        {
                            pos = 0;
                            int j = 0;
                            if (k == 0)                                                      //判断是否为起始日期或结束日期
                            {
                                int ind = BinarySearchTime(acc2, 0, count, len, startTicks); //根据时间排序方式二分法查找当日当前时间节点的数据,如为第一日
                                if (ind < 0)
                                {
                                    ind = ~ind;
                                }
                                j   += ind;
                                pos += ind * len;
                            }
                            if (k == ln - 1)
                            {
                                int ind = BinarySearchTime(acc2, 0, count, len, endTicks);//如果为最后一日的数据,则按结束时间定位
                                count = ind >= 0 ? ind : ~ind;
                            }
                            while (j++ < count)
                            {
                                data.ID        = ID;
                                data.TimeStamp = new SqlDateTime(day, acc2.ReadInt32(pos)).Value; //日期在前(4位)
                                pos           += 4;                                               //数据区也是4位
                                switch (type)
                                {
                                case DataType.BOOL:
                                    data.Value.Boolean = acc2.ReadBoolean(pos);
                                    pos++;
                                    break;

                                case DataType.BYTE:
                                    data.Value.Byte = acc2.ReadByte(pos);
                                    pos++;
                                    break;

                                case DataType.WORD:
                                case DataType.SHORT:
                                    data.Value.Int16 = acc2.ReadInt16(pos);
                                    pos += 2;
                                    break;

                                case DataType.INT:
                                    data.Value.Int32 = acc2.ReadInt32(pos);
                                    pos += 4;
                                    break;

                                case DataType.FLOAT:
                                    data.Value.Single = acc2.ReadSingle(pos);
                                    pos += 4;
                                    break;
                                }
                                yield return(data);
                            }
                        }
                    }
                }
                reader.Close();
            }
            yield break;
        }
Пример #19
0
        public static IEnumerable <HistoryData> LoadFromFile(DateTime start, DateTime end, bool sdt = false)
        {
            //Stopwatch sw = Stopwatch.StartNew();
            //文件的组织格式:头文件:31,ln为间隔日期,position为指向日期段的指针,sizes为日期段的长度。
            //每日的抬头:按ID次序,包含每个TAG的数量,arr为每个日期所有的标签、每标签数量、数据类型、位置指针。
            //按时间排序,每个标签的值、时间戳。
            string path = string.Concat(m_Path, "\\", start.Year.ToString(), "-", start.Month.ToString(), sdt ? ".sdt" : ".bin");

            if (!File.Exists(path))
            {
                yield break;
            }
            int day1       = start.Day;
            int startTicks = new SqlDateTime(start).TimeTicks;
            int endTicks   = new SqlDateTime(end).TimeTicks;
            int ln         = end.Day - day1 + 1;

            using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BinaryReader reader    = new BinaryReader(stream);
                long[]       positions = new long[ln + 1];
                long[]       sizes     = new long[ln];
                stream.Seek((day1 - 1) * 8, SeekOrigin.Begin);
                positions[0] = reader.ReadInt64();
                for (int i = 0; i < ln; i++)
                {
                    positions[i + 1] = reader.ReadInt64();
                    sizes[i]         = positions[i + 1] - positions[i];//每一天数据的长度
                }
                //reader.Close();
                HistoryData data = HistoryData.Empty;
                using (MemoryMappedFile mapp = MemoryMappedFile.CreateFromFile(stream, Guid.NewGuid().ToString(), 0, MemoryMappedFileAccess.Read,
                                                                               HandleInheritability.Inheritable, false))
                {
                    for (int k = 0; k < ln; k++)
                    {
                        if (positions[k] < 0x100 || sizes[k] <= 0 || positions[k] + sizes[k] > stream.Length)
                        {
                            continue;
                        }
                        using (MemoryMappedViewAccessor acc = mapp.CreateViewAccessor(positions[k], sizes[k], MemoryMappedFileAccess.Read))
                        {
                            long pos = 0;
                            int  day = acc.ReadInt32(pos);
                            pos += 8;
                            int count = acc.ReadInt32(pos);
                            pos += 4;
                            HDataFormat[] arr = new HDataFormat[count];
                            for (int i = 0; i < count; i++)
                            {
                                arr[i].ID   = acc.ReadInt16(pos);
                                pos        += 2;
                                arr[i].Type = (DataType)acc.ReadByte(pos);
                                pos++;
                                arr[i].Count = acc.ReadInt32(pos);//4个字节是预留
                                pos         += 8;
                            }
                            long tempos = pos;
                            for (int i = 0; i < count; i++)
                            {
                                int con = arr[i].Count;
                                int j   = 0;
                                pos = tempos + acc.ReadInt32(i * 11 + 19);
                                long     pf   = pos;
                                DataType type = arr[i].Type;
                                int      len  = dataLen[(int)type];
                                if (k == 0)        //判断是否为起始日期或结束日期
                                {
                                    int ind = BinarySearchTime(acc, pf, con, len, startTicks);
                                    if (ind < 0)
                                    {
                                        ind = ~ind;
                                    }
                                    j   += ind;
                                    pos += ind * len;
                                }
                                if (k == ln - 1)
                                {
                                    int index = BinarySearchTime(acc, pf, con, len, endTicks);
                                    con = index >= 0 ? index : ~index;
                                }
                                while (j++ < con)
                                {
                                    data.ID        = arr[i].ID;
                                    data.TimeStamp = new SqlDateTime(day, acc.ReadInt32(pos)).Value;
                                    pos           += 4;
                                    switch (type)
                                    {
                                    case DataType.BOOL:
                                        data.Value.Boolean = acc.ReadBoolean(pos);
                                        pos++;
                                        break;

                                    case DataType.BYTE:
                                        data.Value.Byte = acc.ReadByte(pos);
                                        pos++;
                                        break;

                                    case DataType.WORD:
                                    case DataType.SHORT:
                                        data.Value.Int16 = acc.ReadInt16(pos);
                                        pos += 2;
                                        break;

                                    case DataType.INT:
                                        data.Value.Int32 = acc.ReadInt32(pos);
                                        pos += 4;
                                        break;

                                    case DataType.FLOAT:
                                        data.Value.Single = acc.ReadSingle(pos);
                                        pos += 4;
                                        break;
                                    }
                                    yield return(data);
                                }
                            }
                        }
                    }
                }
            }
            yield break;
        }