Beispiel #1
0
        static void TestWriter()
        {
            Console.WriteLine("Testing SujaySarma.Sdk.FormattedIO.Csv.DelimitedFileWriter...");
            Console.WriteLine();

            DelimitedFileWriter writer = new DelimitedFileWriter(@"C:\Users\Sujay Sarma\Desktop\airports_test.txt", DelimitedFileWriter.DELIMITER_COMMA);
            DelimitedFileReader reader = new DelimitedFileReader(@"D:\Src\products\SujaySarma.Api.OurAirports\src\_data\airports.csv", DelimitedFileReader.DELIMITER_COMMA, hasHeaders: true);
            IList <Airport>     list1  = reader.GetRecordsList <Airport>(0, -1);

            Console.WriteLine($"Airports: Read -- [{list1.Count}] row(s)...");

            DateTime startTime = DateTime.Now, endTime;

            writer.WriteRecords(list1, true);
            endTime = DateTime.Now;
            Console.WriteLine($"Airports: Wrote -- [{list1.Count}] row(s) in {(endTime - startTime).ToString(@"mm\:ss\.ffff")}");
            Console.WriteLine();
        }
 /// <summary>
 /// Exports the <see cref="SignalElementState"/>s of all <see cref="SignalElement"/>s
 /// to a CSV file containing one row per <see cref="SignalDataEntry"/>.
 /// </summary>
 /// <exception cref="ArgumentException">Thrown if the specified output file already exists.</exception>
 /// <exception cref="IOException">Thrown if an error occurred writing the CSV file.</exception>
 /// <param name="fileName">An absolute or relative path indicating the desired destination file.</param>
 public void ExportToCSV(string fileName)
 {
     if (!File.Exists(fileName)) {
         using (var csv = new DelimitedFileWriter<SignalDataEntry>(
             fileName,
             getElementStatesStrings,
             getSignalElementsStrings)) {
             for (uint time = _header.StartTimeAsUnixTime - 1;
                 time <= _header.EndTimeAsUnixTime; time++) {
                 csv.WriteObject(this.GetDataEntry(time));
             }
         }
     } else {
         throw new ArgumentException("File already exists");
     }
 }
        /// <summary>
        /// Parses all <see cref="SignalDataEntry"/> objects to detect changes of the current signal program
        /// and cycle length and exports an overview of all changes to a CSV file.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown if the specified output file already exists.</exception>
        /// <exception cref="IOException">Thrown if an error occurred writing the CSV file.</exception>
        /// <param name="fileName">An absolute or relative path indicating the desired destination file.</param>
        public void ExportSignalProgramsToCSV(string fileName)
        {
            var changedDataEntries = new List<SignalDataEntry>();
            // Add the first data entry (which we'll output in any case) to populate the list.
            changedDataEntries.Add(GetDataEntry(_header.StartTimeAsUnixTime - 1));

            // We have already processed the first item, hence we don't need to subtract one
            // from the "official" start time.
            for (uint time = _header.StartTimeAsUnixTime;
                        time <= _header.EndTimeAsUnixTime; time++) {
                if (!GetDataEntry(time).HeaderEquals(changedDataEntries.Last())) {
                    changedDataEntries.Add(GetDataEntry(time));
                }
            }

            if (!File.Exists(fileName)) {
                using (var csv = new DelimitedFileWriter<SignalDataEntry>(
                    fileName,
                    arg => new[]
                    {
                        arg.Time.ToString(),
                        arg.CurrentSignalProgram.ToString(),
                        arg.CurrentCycleTimeLength.ToString()
                    },
                    () => new[]
                    {
                        "UTC", "Signal Program", "Cycle Time"
                    })) {
                    foreach (SignalDataEntry entryToWrite in changedDataEntries) {
                        csv.WriteObject(entryToWrite);
                    }
                }
            } else {
                throw new ArgumentException("File already exists");
            }
        }