public static Vibe2020DataModel ConvertToDataModel(DeviceDataModel deviceData) { Vibe2020DataModel model = new Vibe2020DataModel(); if (deviceData?.AccelData != null && deviceData.AccelData.Count > 0) { model.AccelData_Raw = new int[] { deviceData.AccelData[0], deviceData.AccelData[1], deviceData.AccelData[2] }; model.AccelData = new double[] { ScaleAccelerometer(deviceData.AccelData[0]), ScaleAccelerometer(deviceData.AccelData[1]), ScaleAccelerometer(deviceData.AccelData[2]) }; } if (deviceData?.GyroData != null && deviceData.GyroData.Count > 0) { Span <int> data = new int[4] { deviceData.GyroData[0], deviceData.GyroData[1], deviceData.GyroData[2], deviceData.GyroData[3] }; Span <byte> bytes = MemoryMarshal.Cast <int, byte>(data); model.GyroData_Raw = GyroConversionHelper.CombineBytes(bytes).ToArray(); model.GyroData = GyroConversionHelper.GetGyroscopeDetails(model.GyroData_Raw).ToArray(); } model.ResultStatus = (ResultStatus)deviceData.ResultStatus; model.TransactionTime = new DateTime(deviceData.TransactionTime).ToLocalTime(); model.CpuTemp = deviceData.CpuTemp; return(model); }
//Pretty inefficient, but no worries. This doesn't require efficient speed private static void ReadFile(string fileName, string convertedFile, double timeTaken, int accelBytes = 12, int gyroBytes = 20, int rtcBytes = 8, int cpuBytes = 8) { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (FileStream newFile = new FileStream(convertedFile, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(newFile, System.Text.Encoding.UTF8)) { long fileLength = fs.Length; int segmentSize = accelBytes + gyroBytes + rtcBytes + cpuBytes; const string header = "ACCEL_X_RAW,ACCEL_Y_RAW,ACCEL_Z_RAW,ACCEL_X,ACCEL_Y,ACCEL_Z," + "DIAG_STAT_RAW,GYRO_X_Raw,GYRO_Y_RAW,GYRO_Z_RAW,ACCEL_X_RAW,ACCEL_Y_RAW,ACCEL_Z_RAW,TEMP_RAW,SPS_RAW,CHECKSUM_RAW," + "DIAG_STAT,GYRO_X,GYRO_Y,GYRO_Z,ACCEL_X,ACCEL_Y,ACCEL_Z,TEMP,SPS,CHECKSUM," + "TRANSACTION_TIME_TICKS,CPU_TEMP"; sw.WriteLine(header); const char comma = ','; Console.WriteLine($"File size is {fs.Length} bytes and estimated datasets is {fs.Length / segmentSize}. Write Speed is estimated at {(fs.Length / segmentSize) / timeTaken} datasets per second!"); int bufferCounter = 0; int bufferSize = 200; for (long i = 0; i < fs.Length; i += segmentSize) { byte[] bytes = new byte[segmentSize]; fs.Read(bytes, 0, segmentSize); Span <byte> segment = new Span <byte>(bytes); var accelSlice = segment.Slice(0, accelBytes); var accelX = accelBytes == 12 ? BitConverter.ToInt32(accelSlice.ToArray(), 0) : BitConverter.ToDouble(accelSlice.ToArray(), 0); var accelY = accelBytes == 12 ? BitConverter.ToInt32(accelSlice.ToArray(), 4) : BitConverter.ToDouble(accelSlice.ToArray(), 8); var accelZ = accelBytes == 12 ? BitConverter.ToInt32(accelSlice.ToArray(), 8) : BitConverter.ToDouble(accelSlice.ToArray(), 16); var gyroSlice = segment.Slice(accelBytes, gyroBytes); var gyro = MemoryMarshal.Cast <byte, short>(gyroSlice).ToArray(); var rtcSlice = segment.Slice((accelBytes + gyroBytes), rtcBytes); var time = BitConverter.ToInt64(rtcSlice); var cpuSlice = segment.Slice((accelBytes + gyroBytes + rtcBytes), cpuBytes); var cpuTemp = BitConverter.ToDouble(cpuSlice); string line = string.Join(comma, accelX); line += comma; line += string.Join(comma, accelY); line += comma; line += string.Join(comma, accelZ); line += comma; line += string.Join(comma, gyro); //Non scaled line += comma; line += string.Join(comma, GyroConversionHelper.GetGyroscopeDetails(gyroSlice).ToArray()); //scaled line += comma; line += time; line += comma; line += cpuTemp; if (bufferCounter++ >= bufferSize) { sw.Flush(); bufferCounter = 0; } sw.WriteLine(line); } } } } }
public static void ConvertGyroData() { string filename = Path.Combine("Data", "Gyroscope.binary"); string newFile = Path.Combine("Converted", "Gyroscope"); Directory.CreateDirectory("Converted"); DateTime initialTime = DateTime.Now; DateTime currentTime = DateTime.Now; bool isInitialDateTime = true; int newFileCounter = 0; //current number of files converted based on fileLineSize long index = 1; long fileLineIndex = 0; //tracks amount of lines in current file const int fileLineSize = 1000000; long count = 0; string resultFile = $"{newFile}{newFileCounter++}.csv"; StreamWriter sw = File.CreateText(resultFile); using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { const int gyroBytes = 20; const int rtcBytes = 8; const int cpuBytes = 8; byte[] bytes = new byte[gyroBytes + rtcBytes + cpuBytes]; const string header = "ID,Second,DIAG_STAT_RAW,GYRO_X_Raw,GYRO_Y_RAW,GYRO_Z_RAW,ACCEL_X_RAW,ACCEL_Y_RAW,ACCEL_Z_RAW,TEMP_RAW,SPS_RAW,CHECKSUM_RAW," + "DIAG_STAT,GYRO_X,GYRO_Y,GYRO_Z,ACCEL_X,ACCEL_Y,ACCEL_Z,TEMP,SPS,CHECKSUM," + "TIMESTAMP,CPU_TEMP"; sw.WriteLine(header); while (fs.Read(bytes) != 0) { Span <byte> data = bytes; Span <byte> gyroSegment = data.Slice(0, gyroBytes); Span <byte> rtcSegment = data.Slice(gyroBytes, rtcBytes); Span <byte> cpuSegment = data.Slice(gyroBytes + rtcBytes, cpuBytes); currentTime = new DateTime(BitConverter.ToInt64(rtcSegment)); if (isInitialDateTime) { initialTime = currentTime; isInitialDateTime = false; } string csvLine = $"{index++},"; csvLine += (currentTime - initialTime).TotalSeconds.ToString("F3") + ","; count++; Span <short> burstData = GyroConversionHelper.CombineBytes(gyroSegment); csvLine += string.Join(',', burstData.ToArray()) + ","; Span <double> gyroData = GyroConversionHelper.GetGyroscopeDetails(burstData); csvLine += string.Join(',', gyroData.ToArray()) + ","; csvLine += currentTime.ToString("HH:mm:ss") + ","; csvLine += BitConverter.ToDouble(cpuSegment).ToString(); if (fileLineIndex >= fileLineSize) { //generate new csv file fileLineIndex = 0; resultFile = $"{newFile}{newFileCounter++}.csv"; sw.Flush(); sw.Close(); sw.Dispose(); sw = File.CreateText(resultFile); } sw.WriteLine(csvLine); } } using (var writer = LogFile.AppendText()) { writer.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")} - Gyroscope Records converted: {count}"); writer.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")} - Gyroscope Converted Files created: {newFileCounter}"); writer.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")} - Gyroscope TotalSeconds: {(currentTime - initialTime).TotalSeconds.ToString("F3")}"); writer.WriteLine("-------------------------------------------------------------------------------"); } }