Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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("-------------------------------------------------------------------------------");
            }
        }