public void Test2()
        {
            var path = @"Data\200122_12-28-09_1.csv"; // "," dateTime: "2020/01/22 12:25:55.734"

            //var path = @"Data\DJIFlightRecord_2020-01-22_13-25-55-verbose.csv"; // ";"  dateTime:"28:52.3"
            using (TextFieldParser csvParser = new TextFieldParser(path))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                //// Skip the row with the column names
                //csvParser.ReadLine();


                //var noko = File.ReadAllLines(path);



                //Processing row
                string[] headers             = csvParser.ReadFields();
                var      djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers);


                //foreach (string field in fields)
                //{
                var uavLogs = new List <UavLog>();


                if (djiHeaderDictionary.Any())
                {
                    int rowNumber = 1;
                    while (csvParser.PeekChars(1) != null)
                    {
                        rowNumber++;
                        string[] fields = csvParser.ReadFields();
                        uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber));
                    }
                }

                string csv = String.Join(",", uavLogs);

                var sortUavList1 = uavLogs;

                sortUavList1.OrderBy(x => x.DateTime).ToList();
                var sortUavList = uavLogs;
                sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime));

                TimeSpan startTimeSpan = Helpers.GetTimeSpan("00:01:00.150");
                TimeSpan endTimeStamp  = Helpers.GetTimeSpan("00:02:35.150");

                var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeStamp);



                var filePath     = Path.Combine(@"C:\Temp\", "sort_test.csv");
                var csvVideoLogs = CsvUtilities.ToCsv(",", newUavlogs);
                var saved        = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs);
            }
        }
Beispiel #2
0
        public void TrimUavLogs()
        {
            List <UavLog> uavLogs = new List <UavLog>();
            var           path    = @"Data\200122_12-28-09_1.csv"; // "," dateTime: "2020/01/22 12:25:55.734"

            using (TextFieldParser csvParser = new TextFieldParser(path))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                string[] headers             = csvParser.ReadFields();
                var      djiHeaderDictionary = CsvUtilities.GetHeaderDictionary(headers);



                if (djiHeaderDictionary.Any())
                {
                    int rowNumber = 1;
                    while (csvParser.PeekChars(1) != null)
                    {
                        rowNumber++;
                        string[] fields = csvParser.ReadFields();
                        uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber));
                    }
                }

                string csv = String.Join(",", uavLogs);
            }
            var sortUavList = uavLogs;

            sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime));

            //var noko = Helpers.GetfirstLog(uavLogs);

            TimeSpan startTimeSpan = Helpers.GetTimeSpan("01:00.15");
            TimeSpan endTimeStamp  = Helpers.GetTimeSpan("01:36.15");

            var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeStamp);


            var filePath     = Path.Combine(@"C:\Temp\", "sort_test.csv");
            var csvVideoLogs = CsvUtilities.ToCsv(",", newUavlogs);
            var saved        = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs);
        }
        public void Test1()
        {
            var path = @"Data\DJIFlightRecord_2020-01-22_[14-00-01]-TxtLogToCsv.csv"; // "," dateTime: "2020/01/22 12:25:55.734"

            //var path = @"Data\DJIFlightRecord_2020-01-22_13-25-55-verbose.csv"; // ";"  dateTime:"28:52.3"
            using (TextFieldParser csvParser = new TextFieldParser(path))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;



                //// Skip the row with the column names

                //csvParser.ReadLine();



                //var noko = File.ReadAllLines(path);



                //Processing row
                string[] headers             = csvParser.ReadFields();
                var      djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers);



                //foreach (string field in fields)

                //{

                var uavLogs = new List <UavLog>();

                if (djiHeaderDictionary.Any())
                {
                    int rowNumber = 1;
                    while (csvParser.PeekChars(1) != null)
                    {
                        rowNumber++;
                        string[] fields = csvParser.ReadFields();
                        var      index  = djiHeaderDictionary["VideoRecordTime"];

                        var noko = fields[index];
                        if (fields.Length > index && fields[index] != "0")
                        {
                            uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber));
                        }
                    }
                }
                string csv = String.Join(",", uavLogs);



                var      dictionarylog             = Helpers.SplitVideosFromUavLog(uavLogs);
                var      video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog[2]);
                var      time     = Helpers.ConvertMilisecondsToHMSmm(video1LenghInMilliseconds);
                TimeSpan timeSpan = Helpers.GetTimeSpan("03:56:22");

                var    photolog = Helpers.GetUavLogFromVideoTimeStamp(timeSpan, dictionarylog[2]);
                string filePath = null;

                //foreach (var videologs in dictionarylog)

                //{

                //    var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value);

                //    var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("yyMMdd")}_{videologs.Key}.csv";

                //    filePath = Path.Combine(@"C:\Temp\", filename);

                //    var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs);
                //}


                filePath = Path.Combine(@"C:\Temp\", "noko.csv");

                var videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog);
                var csvVideoLogs    = CsvUtilities.ToCsv(",", videoInfoModels.ToArray());

                var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs);


                CsvUtilities.ConvertCalssToCsv(videoInfoModels.ToArray(), filePath);
                //var csvString = CsvUtilities.ToCsv(",", uavLogs);
                //var saved = CsvUtilities.SaveCsvTofile(@"C:\Temp\WriteLines2.csv", csvString);
            }
        }
        public void TrimUavLogs()
        {
            int    counter = 0;
            string line;

            // Read the file and display it line by line.
            DjiSrtVideoLogModel        djiSrtVideoLogModel  = new DjiSrtVideoLogModel();
            List <DjiSrtVideoLogModel> djiSrtVideoLogModels = new List <DjiSrtVideoLogModel>();

            using (System.IO.StreamReader file = new System.IO.StreamReader(@"H:\Code\UavLogTool\UavLogToolTest\Data\DJI_0012.SRT"))
            {
                int lineCount = 0;
                int id        = 0;
                while ((line = file.ReadLine()) != null)
                {
                    int idTemp;
                    if (int.TryParse(line, out idTemp))
                    {
                        djiSrtVideoLogModel = new DjiSrtVideoLogModel();
                        id = idTemp;
                        lineCount++;
                    }
                    if (id >= 1)
                    {
                        switch (lineCount)
                        {
                        case 1:
                            djiSrtVideoLogModel.Id = id;
                            lineCount++;
                            break;

                        case 2:
                            djiSrtVideoLogModel = SrtConverter.GetTimeStamp(line, ref djiSrtVideoLogModel);
                            lineCount++;
                            break;

                        case 3:
                            djiSrtVideoLogModel = SrtConverter.GetHomePointAndDateTime(line, ref djiSrtVideoLogModel);
                            lineCount++;
                            break;

                        case 4:
                            djiSrtVideoLogModel = SrtConverter.GetGpsPointAndBarometer(line, ref djiSrtVideoLogModel);
                            lineCount++;
                            break;

                        case 5:
                            djiSrtVideoLogModel = SrtConverter.GetCameraInfo(line, ref djiSrtVideoLogModel);
                            djiSrtVideoLogModels.Add(djiSrtVideoLogModel);
                            lineCount = 0;
                            break;

                        default:
                            break;
                        }
                    }
                    counter++;
                }
            }

            var filePath = Path.Combine(@"C:\Temp\", "djiSrtVideo.csv");

            var noko         = SrtConverter.ConvertSrtToUavLog(djiSrtVideoLogModels);
            var csvVideoLogs = CsvUtilities.ToCsv(",", djiSrtVideoLogModels.ToArray());
            var saved        = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs);
        }
Beispiel #5
0
        private void LoadDjiCsv_Click(object sender, EventArgs e)
        {
            int          size   = -1;
            DialogResult result = openFileDialog.ShowDialog(); // Show the dialog.

            if (result == DialogResult.OK)                     // Test result.
            {
                string djiCsvLog = openFileDialog.FileName;
                filePathTextBox.Text = string.Format("{0}/{1}", Path.GetDirectoryName(djiCsvLog), djiCsvLog);
                filePathTextBox.Refresh();
                DialogResult messageResult = MessageBox.Show($"Do you wanna transform: '{djiCsvLog}'?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (messageResult == DialogResult.Yes)
                {
                    try
                    {
                        string extension = Path.GetExtension(djiCsvLog).ToLower();
                        if (extension != ".csv")
                        {
                            string message = "Wrong File Type";
                            string title   = "Error";
                            MessageBox.Show(message, title);
                        }

                        List <VideoInfoModel> videoInfoModels;
                        var uavLogs = new List <UavLog>();

                        using (TextFieldParser csvParser = new TextFieldParser(djiCsvLog))
                        {
                            uavLogs = CsvUtilities.GetUavLogFromDjiCsv(csvParser);
                        }

                        if (!uavLogs.Any())
                        {
                            string message = "No Video log found in file";
                            string title   = "Error";
                            MessageBox.Show(message, title);
                        }

                        var csv = String.Join(",", uavLogs);

                        var dictionarylog             = Helpers.SplitVideosFromUavLog(uavLogs);
                        var video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog.FirstOrDefault().Value);

                        foreach (var videologs in dictionarylog)
                        {
                            var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value);

                            var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("_yyyy-MM-dd_HH-mm-ss")}_{videologs.Key}.csv";
                            var saved    = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", filename), csvVideoLogs);
                        }
                        videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog);


                        if (videoInfoModels != null)
                        {
                            jsonOutput.Text       = JArray.FromObject(videoInfoModels).ToString();
                            jsonOutput.ScrollBars = ScrollBars.Vertical;
                            jsonOutput.WordWrap   = false;

                            //var csvVideoInfoModels = CsvUtilities.ToCsv(",", videoInfoModels);
                            //var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(djiCsvLog);
                            //var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels);
                            string message = "File Saved";
                            string title   = "OK";

                            MessageBox.Show(message, title);
                        }
                    }
                    catch (IOException)
                    {
                        string message = "Something Went wrong";
                        string title   = "Error";
                        MessageBox.Show(message, title);
                    }
                }
            }
        }
        public async Task <IActionResult> GetCsvFromSrt(IFormFile srtLog)
        {
            long   size      = srtLog.Length;
            string extension = Path.GetExtension(srtLog.FileName).ToLower();

            if (size > 0)
            {
                if (!extension.Equals(".SRT", StringComparison.OrdinalIgnoreCase))
                {
                    return(BadRequest("wrong file format"));
                }
            }
            else
            {
                return(BadRequest("SRT File Not Found"));
            }
            string line;

            DjiSrtVideoLogModel        djiSrtVideoLogModel  = new DjiSrtVideoLogModel();
            List <DjiSrtVideoLogModel> djiSrtVideoLogModels = new List <DjiSrtVideoLogModel>();

            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(srtLog.OpenReadStream()))
                {
                    int lineCount = 0;
                    int id        = 0;
                    while ((line = file.ReadLine()) != null)
                    {
                        int idTemp;
                        if (int.TryParse(line, out idTemp))
                        {
                            djiSrtVideoLogModel = new DjiSrtVideoLogModel();
                            id = idTemp;
                            lineCount++;
                        }
                        if (id >= 1)
                        {
                            switch (lineCount)
                            {
                            case 1:
                                djiSrtVideoLogModel.Id = id;
                                lineCount++;
                                break;

                            case 2:
                                djiSrtVideoLogModel = SrtConverter.GetTimeStamp(line, ref djiSrtVideoLogModel);
                                lineCount++;
                                break;

                            case 3:
                                djiSrtVideoLogModel = SrtConverter.GetHomePointAndDateTime(line, ref djiSrtVideoLogModel);
                                lineCount++;
                                break;

                            case 4:
                                djiSrtVideoLogModel = SrtConverter.GetGpsPointAndBarometer(line, ref djiSrtVideoLogModel);
                                lineCount++;
                                break;

                            case 5:
                                djiSrtVideoLogModel = SrtConverter.GetCameraInfo(line, ref djiSrtVideoLogModel);
                                djiSrtVideoLogModels.Add(djiSrtVideoLogModel);
                                lineCount = 0;
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    if (djiSrtVideoLogModels.Any())
                    {
                        var csvVideoInfoModels       = CsvUtilities.ToCsv(",", djiSrtVideoLogModels);
                        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(srtLog.FileName);
                        var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels);

                        return(Ok("Ok creating Srt to Csv"));
                    }
                    else
                    {
                        return(BadRequest("djiSrtVideoLogModels dont have any record"));
                    }
                }
            }
            catch (Exception)
            {
                return(BadRequest("Problem saving Srt to csv"));
            }
        }
        public async Task <IActionResult> GetCsvVideoLogInfo(IFormFile djiCsvLog)
        {
            long   size      = djiCsvLog.Length;
            string extension = Path.GetExtension(djiCsvLog.FileName).ToLower();

            if (size > 0)
            {
                if (extension != ".csv")
                {
                    return(BadRequest("wrong file format"));
                }
            }
            else
            {
                return(BadRequest("CSV File Not Found"));
            }

            List <VideoInfoModel> videoInfoModels;

            using (TextFieldParser csvParser = new TextFieldParser(djiCsvLog.OpenReadStream()))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                //Processing row
                string[] headers             = csvParser.ReadFields();
                var      djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers);


                var uavLogs = new List <UavLog>();

                if (djiHeaderDictionary.Any())
                {
                    int rowNumber = 1;
                    while (csvParser.PeekChars(1) != null)
                    {
                        rowNumber++;
                        string[] fields = csvParser.ReadFields();
                        var      index  = djiHeaderDictionary["VideoRecordTime"];

                        var noko = fields[index];

                        if (fields.Length > index && !string.IsNullOrEmpty(fields[index]) && fields[index] != "0")
                        {
                            uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber));
                        }
                    }
                }
                if (!uavLogs.Any())
                {
                    return(BadRequest("No Video log found in file"));
                }

                var csv = String.Join(",", uavLogs);

                //TODO not working
                //var uavlogsSort = uavLogs.OrderBy(l => l.DateTime).ToList();

                var dictionarylog             = Helpers.SplitVideosFromUavLog(uavLogs);
                var video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog.FirstOrDefault().Value);



                foreach (var videologs in dictionarylog)
                {
                    var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value);


                    var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("_yyyy-MM-dd_HH-mm-ss")}_{videologs.Key}.csv";
                    var saved    = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", filename), csvVideoLogs);
                }
                videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog);
            }

            if (videoInfoModels != null)
            {
                var csvVideoInfoModels       = CsvUtilities.ToCsv(",", videoInfoModels);
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(djiCsvLog.FileName);
                var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels);
                return(Ok(videoInfoModels));
            }



            return(BadRequest("Something Went wrong"));
        }
        public async Task <IActionResult> TrimCsvLog(IFormFile uavLogsCsv, string startTime, string endTime)
        {
            long   csvFilLength     = uavLogsCsv.Length;
            string csvFileExtension = Path.GetExtension(uavLogsCsv.FileName).ToLower();

            if (csvFilLength > 0)
            {
                if (csvFileExtension != ".csv")
                {
                    return(BadRequest("wrong CSV file format"));
                }
            }
            else
            {
                return(BadRequest("CSV File Not Found"));
            }
            List <UavLog> uavLogs = new List <UavLog>();

            using (TextFieldParser csvParser = new TextFieldParser(uavLogsCsv.OpenReadStream()))
            {
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                string[] headers             = csvParser.ReadFields();
                var      djiHeaderDictionary = CsvUtilities.GetHeaderDictionary(headers);



                if (djiHeaderDictionary.Any())
                {
                    int rowNumber = 1;
                    while (csvParser.PeekChars(1) != null)
                    {
                        rowNumber++;
                        string[] fields = csvParser.ReadFields();
                        uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber));
                    }
                }

                string csv = String.Join(",", uavLogs);
            }
            var sortUavList = uavLogs;

            sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime));



            //var newUavlogs = Helpers.TrimUavLogs(sortUavList, "01:00:22", "01:36:22");
            TimeSpan startTimeSpan = Helpers.GetTimeSpan(startTime);

            TimeSpan endTimeSpan = Helpers.GetTimeSpan(endTime);

            if (startTimeSpan == TimeSpan.Zero)
            {
                return(BadRequest($"cant get video start time from string '{startTime}'"));
            }

            if (endTimeSpan == TimeSpan.Zero)
            {
                return(BadRequest($"cant get video end time from string '{endTime}'"));
            }

            var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeSpan);

            var videoInfoModels = Helpers.GetVideoInfoModels(newUavlogs);


            if (videoInfoModels != null)
            {
                var csvVideoInfoModels       = CsvUtilities.ToCsv(",", videoInfoModels);
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(uavLogsCsv.FileName);
                var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels);



                // convert string to stream
                byte[] byteArray = Encoding.UTF8.GetBytes(string.Concat(csvVideoInfoModels));
                var    stream    = new MemoryStream(byteArray);
                return(File(stream, "application/octet-stream", $"{ fileNameWithoutExtension}_resume.csv"));
            }
            return(BadRequest("No file Created"));
        }