public ItemData <float> ReadFloat(DeviceAddress address) { try { return(new ItemData <float>(accessor.ReadSingle(FindPosition(address)), 0, QUALITIES.QUALITY_GOOD)); } catch { return(new ItemData <float>(0f, 0, QUALITIES.QUALITY_BAD)); } }
public override float GetFloat() { var c = _accessor.ReadSingle(Position); Position += 4; //conform to how the index was written return(Number.FlipEndian(c)); }
public static SharedMemoryState Read(MemoryMappedViewAccessor accessor, int currentIndex) { var id = accessor.ReadInt32(0); if (id <= currentIndex) { return(null); } //var clipTimeLeftMillis = accessor.ReadDouble(4); //var clipTimeRightMillis = accessor.ReadDouble(12); var selectedInputLeft = accessor.ReadInt32(16); var selectedInputRight = accessor.ReadInt32(20); var selectedOutputLeft = accessor.ReadInt32(24); var selectedOutputRight = accessor.ReadInt32(28); var gain = accessor.ReadSingle(32); var irLen = accessor.ReadInt32(36); var idx = 40; var irLeft = new float[irLen]; var irRight = new float[irLen]; for (int i = 0; i < irLen; i++) { irLeft[i] = accessor.ReadSingle(idx); idx += 4; } for (int i = 0; i < irLen; i++) { irRight[i] = accessor.ReadSingle(idx); idx += 4; } return(new SharedMemoryState { Gain = gain, Id = id, IrLeft = irLeft, IrLength = irLen, IrRight = irRight, SelectedInputLeft = selectedInputLeft, SelectedInputRight = selectedInputRight, SelectedOutputLeft = selectedOutputLeft, SelectedOutputRight = selectedOutputRight }); }
internal float ReadFloat(long translatedRowIndex, long translatedColumnIndex) { var columnMetadata = Metadata.Columns[translatedColumnIndex]; var dataOffset = columnMetadata.DataOffset; var valueOffset = dataOffset + translatedRowIndex * sizeof(float); return(View.ReadSingle(valueOffset)); }
/// <summary> /// NOTE: This was getFloat() in the JDK /// </summary> public override float GetSingle() { if (order == Endianness.BigEndian) { byte[] bytes = new byte[4]; for (int i = 3; i >= 0; i--) { bytes[i] = accessor.ReadByte(Ix(NextGetIndex())); } return(BitConverter.ToSingle(bytes, 0)); } else { // LUCENENET NOTE: This assumes that .NET core will continue // making the format little endian on big endian platforms return(accessor.ReadSingle(Ix(NextGetIndex(4)))); } }
public Star this[long i] { get { if (i > capacity) { throw new ArgumentOutOfRangeException(); } var pos = i * StarSizeInBytes; var star = new Star { X = view.ReadDouble(pos) }; pos += sizeof(double); star.Y = view.ReadDouble(pos); pos += sizeof(double); star.Z = view.ReadDouble(pos); pos += sizeof(double); star.AbsoluteMagnitude = view.ReadSingle(pos); pos += sizeof(float); star.Temperature = view.ReadSingle(pos); return(star); } set { if (i > capacity) { throw new ArgumentOutOfRangeException(); } var pos = i * StarSizeInBytes; view.Write(pos, value.X); pos += sizeof(double); view.Write(pos, value.Y); pos += sizeof(double); view.Write(pos, value.Z); pos += sizeof(double); view.Write(pos, value.AbsoluteMagnitude); pos += sizeof(float); view.Write(pos, value.Temperature); } }
/// <summary> /// /// </summary> /// <returns></returns> public float ReadSingle(long inPosition) { try { return(_sharedMemoryAccesor.ReadSingle(inPosition)); } catch { return(0); } }
private void refreshValue() { try { while (true) { float w = accessor.ReadSingle(28); label2.Invoke((MethodInvoker)(() => label2.Text = w.ToString())); w = (w > 255) ? 255 : w; port.Write(BitConverter.GetBytes((Int32)Math.Round(w)), 0, 1); Thread.Sleep(16); } } catch { } }
public float ReadSingle() { long position = _position + _origin; if (position + 4 > _length) { throw new IOException(SR.BlobReadOutOfBound); } var value = _accessor.ReadSingle(position); _position += 4; return(value); }
public float GetFloat() { const long size = 4; 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.ReadSingle(_offset + _position); _position += size; return(value); }
static Telemetry ReadAllValues(MemoryMappedViewAccessor accessor, int buffOffset, VarHeader[] varHeaders) { var result = new Telemetry(); var maps = new Dictionary <VarType, Func <int, object> >() { { VarType.irInt, (offset) => accessor.ReadInt32(offset) }, { VarType.irBitField, (offset) => accessor.ReadInt32(offset) }, { VarType.irDouble, (offset) => accessor.ReadDouble(offset) }, { VarType.irBool, (offset) => accessor.ReadBoolean(offset) }, { VarType.irFloat, (offset) => accessor.ReadSingle(offset) } }; var arryMaps = new Dictionary <VarType, Func <int, int, object> >() { { VarType.irInt, (size, offset) => GetArrayData <int>(accessor, size, offset) }, { VarType.irBitField, (size, offset) => GetArrayData <int>(accessor, size, offset) }, { VarType.irDouble, (size, offset) => GetArrayData <double>(accessor, size, offset) }, { VarType.irFloat, (size, offset) => GetArrayData <float>(accessor, size, offset) }, { VarType.irBool, (size, offset) => GetArrayData <bool>(accessor, size, offset) } }; for (var i = 0; i < varHeaders.Length; i++) { var varHeader = varHeaders[i]; var offset = buffOffset + varHeader.offset; if (varHeader.type == VarType.irChar) { throw new NotSupportedException(); } object value; if (varHeader.count != 1) { value = arryMaps[varHeader.type](varHeader.count, offset); } else { value = maps[varHeader.type](offset); } result.Add(varHeader.name, value); } return(result); }
public float ReadSingle(int offset) { return(_f.ReadSingle(offset)); }
public object GetData(string name) { if (!IsInitialized || Header == null) { return(null); } if (!VarHeaders.ContainsKey(name)) { return(null); } var varOffset = VarHeaders[name].Offset; var count = VarHeaders[name].Count; switch (VarHeaders[name].Type) { case CVarHeader.VarType.IrChar: var bytes = new byte[count]; _fileMapView.ReadArray(Header.Buffer + varOffset, bytes, 0, count); return(Encoding.Default.GetString(bytes).TrimEnd('\0')); case CVarHeader.VarType.IrBool: if (count > 1) { var data = new bool[count]; _fileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } return(_fileMapView.ReadBoolean(Header.Buffer + varOffset)); case CVarHeader.VarType.IrInt: case CVarHeader.VarType.IrBitField: if (count > 1) { var data = new int[count]; _fileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } return(_fileMapView.ReadInt32(Header.Buffer + varOffset)); case CVarHeader.VarType.IrFloat: if (count > 1) { var data = new float[count]; _fileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } return(_fileMapView.ReadSingle(Header.Buffer + varOffset)); case CVarHeader.VarType.IrDouble: if (count > 1) { var data = new double[count]; _fileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } return(_fileMapView.ReadDouble(Header.Buffer + varOffset)); default: return(null); } }
public float GetX() => view.ReadSingle(8);
public object GetData(string name) { if (IsInitialized && Header != null) { CVarHeader header = null; if (VarHeaders.TryGetValue(name, out header)) { int varOffset = header.Offset; int count = header.Count; if (header.Type == CVarHeader.VarType.irChar) { byte[] data = new byte[count]; FileMapView.ReadArray <byte>(Header.Buffer + varOffset, data, 0, count); return(System.Text.Encoding.Default.GetString(data).TrimEnd(new char[] { '\0' })); } else if (header.Type == CVarHeader.VarType.irBool) { if (count > 1) { bool[] data = new bool[count]; FileMapView.ReadArray <bool>(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadBoolean(Header.Buffer + varOffset)); } } else if (header.Type == CVarHeader.VarType.irInt || header.Type == CVarHeader.VarType.irBitField) { if (count > 1) { int[] data = new int[count]; FileMapView.ReadArray <int>(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadInt32(Header.Buffer + varOffset)); } } else if (header.Type == CVarHeader.VarType.irFloat) { if (count > 1) { float[] data = new float[count]; FileMapView.ReadArray <float>(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadSingle(Header.Buffer + varOffset)); } } else if (header.Type == CVarHeader.VarType.irDouble) { if (count > 1) { double[] data = new double[count]; FileMapView.ReadArray <double>(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadDouble(Header.Buffer + varOffset)); } } } } return(null); }
public Single GetValueSingle(long offset) { return(_viewAccessor.ReadSingle(offset)); }
public object GetData(string name) { if (IsInitialized && Header != null) { if (VarHeaders.ContainsKey(name)) { int varOffset = VarHeaders[name].Offset; int count = VarHeaders[name].Count; if (VarHeaders[name].Type == CVarHeader.VarType.irChar) { byte[] data = new byte[count]; FileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(encoding.GetString(data).TrimEnd(new char[] { '\0' })); } else if (VarHeaders[name].Type == CVarHeader.VarType.irBool) { if (count > 1) { bool[] data = new bool[count]; FileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadBoolean(Header.Buffer + varOffset)); } } else if (VarHeaders[name].Type == CVarHeader.VarType.irInt || VarHeaders[name].Type == CVarHeader.VarType.irBitField) { if (count > 1) { int[] data = new int[count]; FileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadInt32(Header.Buffer + varOffset)); } } else if (VarHeaders[name].Type == CVarHeader.VarType.irFloat) { if (count > 1) { float[] data = new float[count]; FileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadSingle(Header.Buffer + varOffset)); } } else if (VarHeaders[name].Type == CVarHeader.VarType.irDouble) { if (count > 1) { double[] data = new double[count]; FileMapView.ReadArray(Header.Buffer + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadDouble(Header.Buffer + varOffset)); } } } } return(null); }
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]); } } } } }
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; }
/// <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)); }
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; }
public object GetData(string name) { if (!IsStarted || Header == null) { return(null); } if (!VarHeaders.TryGetValue(name, out var requestedHeader)) { return(null); } int varOffset = requestedHeader.Offset; int count = requestedHeader.Count; switch (requestedHeader.Type) { case VarType.irChar: { byte[] data = new byte[count]; FileMapView.ReadArray(Header.Offset + varOffset, data, 0, count); return(_encoding.GetString(data).TrimEnd(trimChars)); } case VarType.irBool: { if (count > 1) { bool[] data = new bool[count]; FileMapView.ReadArray(Header.Offset + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadBoolean(Header.Offset + varOffset)); } } case VarType.irInt: case VarType.irBitField: { if (count > 1) { int[] data = new int[count]; FileMapView.ReadArray(Header.Offset + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadInt32(Header.Offset + varOffset)); } } case VarType.irFloat: { if (count > 1) { float[] data = new float[count]; FileMapView.ReadArray(Header.Offset + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadSingle(Header.Offset + varOffset)); } } case VarType.irDouble: { if (count > 1) { double[] data = new double[count]; FileMapView.ReadArray(Header.Offset + varOffset, data, 0, count); return(data); } else { return(FileMapView.ReadDouble(Header.Offset + varOffset)); } } default: return(null); } }