Exemplo n.º 1
0
        public void CanSerializeSampleFile()
        {
            // serialize sample ADX file from http://www.adif.org/304/ADIF_304.htm#ADX_File_Format
            var dataObject = new ADIFData();

            dataObject.Header                   = new ADIFHeader();
            dataObject.Header.ADIFVersion       = ADIFIO.ADIFVersion;
            dataObject.Header.ProgramID         = "monolog";
            dataObject.Header.UserDefinedFields = new List <ADIFUserDefinedFieldHeader>();
            dataObject.Header.UserDefinedFields.Add(new ADIFUserDefinedFieldHeader {
                FieldID = 1, Type = "N", Value = "EPC"
            });
            dataObject.Header.UserDefinedFields.Add(new ADIFUserDefinedFieldHeader {
                FieldID = 2, Type = "E", Enum = "{S,M,L}", Value = "SWEATERSIZE"
            });
            dataObject.Header.UserDefinedFields.Add(new ADIFUserDefinedFieldHeader {
                FieldID = 3, Type = "E", Range = "{5:20}", Value = "SHOESIZE"
            });

            dataObject.Records = new List <ADIFRecord>();

            var firstRecord = new ADIFRecord();

            firstRecord.QSODate  = "19900620";
            firstRecord.TimeOn   = "1523";
            firstRecord.Callsign = "VK9NS";
            firstRecord.Band     = "20M";
            firstRecord.Mode     = "RTTY";

            firstRecord.UserDefinedFields = new List <ADIFUserDefinedFieldRecord>();
            firstRecord.UserDefinedFields.Add(new ADIFUserDefinedFieldRecord("SWEATERSIZE", "M"));
            firstRecord.UserDefinedFields.Add(new ADIFUserDefinedFieldRecord("SHOESIZE", "11"));

            firstRecord.AppDefinedFields = new List <ADIFAppDefinedFieldRecord>();
            firstRecord.AppDefinedFields.Add(new ADIFAppDefinedFieldRecord("MONOLOG", "Compression", "s", "off"));

            var secondRecord = new ADIFRecord();

            secondRecord.QSODate  = "20101022";
            secondRecord.TimeOn   = "0111";
            secondRecord.Callsign = "ON4UN";
            secondRecord.Band     = "40M";
            secondRecord.Mode     = "PSK";
            secondRecord.Submode  = "PSK63";

            secondRecord.UserDefinedFields = new List <ADIFUserDefinedFieldRecord>();
            secondRecord.UserDefinedFields.Add(new ADIFUserDefinedFieldRecord("EPC", "32123"));

            secondRecord.AppDefinedFields = new List <ADIFAppDefinedFieldRecord>();
            secondRecord.AppDefinedFields.Add(new ADIFAppDefinedFieldRecord("MONOLOG", "COMPRESSION", "s", "off"));

            dataObject.Records.Add(firstRecord);
            dataObject.Records.Add(secondRecord);

            var adx = ADIFIO.ConvertToADX(dataObject);

            ADIFIO.SaveADXFile(dataObject, "C:\\data.xml");
        }
Exemplo n.º 2
0
        public void BuildHeader(ADIFRecord adifRecord, StringBuilder csvRows)
        {
            int totalfields  = adifRecord.Fields.Count;
            int fieldCounter = 1;

            foreach (var adifField in adifRecord.Fields)
            {
                if (fieldCounter > 1)
                {
                    AppendRows(csvRows, adifField.Name, fieldCounter, totalfields);
                }
                fieldCounter++;
            }
        }
Exemplo n.º 3
0
        private void BuildRow(ADIFRecord adifRecord, Row row, RowType rowtype)
        {
            openXmlRowBuilderStrategy = GetOpenXmlRowBuilderOption(rowtype);
            int fieldCounter = 1;

            foreach (var adifField in adifRecord.Fields)
            {
                if (fieldCounter > 1)
                {
                    openXmlRowBuilderStrategy.AppendRow(row, adifField);
                }

                fieldCounter++;
            }
        }
Exemplo n.º 4
0
        public ObservableCollection <ADIFRecord> ReadRecords(string fileName)
        {
            var records     = new ObservableCollection <ADIFRecord>();
            var adifRecords = new ObservableCollection <ADIFRecord>();

            var reader = new StreamReader(fileName, Encoding.UTF8);

            string[] separatingChar = { "<EOH>", "<eoh>" };

            //Remove LineBrake and Tabs characters
            string fileContent = reader.ReadToEnd().Replace("\n", "").Replace("\r", "").Replace("\t", "").Trim();;

            string[] contentArray = fileContent.Split(separatingChar, System.StringSplitOptions.RemoveEmptyEntries);

            MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(contentArray[1]));

            StreamReader streamReader = new StreamReader(mStrm);

            var record = new ADIFRecord()
            {
                Fields = new ObservableCollection <ADIFField>()
                {
                    new ADIFField()
                    {
                        Name = "Line", Value = $"{adifRecords.Count + 1}"
                    }
                }
            };

            try
            {
                for (; ;)
                {
                    var field = _adifFieldService.ParseField(streamReader);

                    if (field == null)
                    {
                        break;
                    }

                    if ("EOR".Equals(field.Name.ToUpper()))
                    {
                        record.FieldsCounter = record.Fields.Count;
                        var tempRecord = record.DeepCopy();

                        adifRecords.Add(tempRecord);

                        record = new ADIFRecord()
                        {
                            Fields = new ObservableCollection <ADIFField>()
                            {
                                new ADIFField()
                                {
                                    Name = "Line", Value = $"{adifRecords.Count + 1 }"
                                }
                            }
                        };
                    }
                    else
                    {
                        record.Fields.Add(field);
                    }
                }
            }
            catch (AdifException ae)
            {
                var message = ae.Message;
                MessageBox.Show($"{message} on row {adifRecords.Count + 1}.", Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Error);
                return(new ObservableCollection <ADIFRecord>());
            }
            finally
            {
                streamReader.Dispose();
                mStrm.Dispose();
                reader.Dispose();
            }

            var listHasDifferentFieldsCounterValues = adifRecords.Select(x => x.FieldsCounter).Distinct().Skip(1).Any();

            return(listHasDifferentFieldsCounterValues ? ProcessRecords(adifRecords) : adifRecords);
        }
        public void SaveFile(ObservableCollection <ADIFRecord> adifRecords, string filePath)
        {
            if (!adifRecords.Any())
            {
                return;
            }

            var fieldsToInclude = new List <string>()
            {
                "QTR OA", "INDICATIVO", "REP. ENTREGADO", "REP. RECIBIDO"
            };

            var planillaRecords = new List <ADIFRecord>();

            int recordCounter = 1;

            //Remove Unused Fields
            foreach (var adifRecord in adifRecords)
            {
                var record = new ADIFRecord();
                //Custom Fields
                record.Fields.Add(new ADIFField()
                {
                    Name = "QSO", Value = recordCounter.ToString()
                });

                foreach (var field in fieldsToInclude)
                {
                    if (field.Equals("QTR OA"))
                    {
                        var qsoDate   = adifRecord.Fields.FirstOrDefault(x => x.Name.Equals("QSO_DATE")) ?? new ADIFField();
                        var qsoTimeOn = adifRecord.Fields.Where(x => x.Name.Equals("TIME_ON")).FirstOrDefault() ?? new ADIFField();

                        string qsoLocalTime = string.Empty;

                        //Refactor on Function to get the LocalHour
                        if (qsoDate.Value.Length == 8 && qsoTimeOn.Value.Length == 6)
                        {
                            string year  = qsoDate.Value.Substring(0, 4);
                            string month = qsoDate.Value.Substring(4, 2);
                            string day   = qsoDate.Value.Substring(6, 2);

                            string hours   = qsoTimeOn.Value.Substring(0, 2);
                            string minutes = qsoTimeOn.Value.Substring(2, 2);
                            string seconds = qsoTimeOn.Value.Substring(4, 2);

                            var formattedDate = $"{year}-{month}-{day}T{hours}:{minutes}:{seconds}Z";

                            DateTime utcDate = DateTime.Parse(formattedDate, null, System.Globalization.DateTimeStyles.RoundtripKind);

                            //Make configurable depending the time zone, or use a offset
                            DateTime localDate = utcDate.AddHours(-5);

                            qsoLocalTime = $"{localDate.Hour:00}:{localDate.Minute:00}";
                        }

                        record.Fields.Add(new ADIFField()
                        {
                            Name = field, Value = qsoLocalTime
                        });
                        continue;
                    }

                    if (field.Equals("INDICATIVO"))
                    {
                        var item = adifRecord.Fields.Where(x => x.Name.Equals("CALL")).FirstOrDefault() ?? new ADIFField();
                        record.Fields.Add(new ADIFField()
                        {
                            Name = field, Value = item.Value
                        });
                        continue;
                    }

                    if (field.Equals("REP. ENTREGADO"))
                    {
                        var item = adifRecord.Fields.Where(x => x.Name.Equals("STX")).FirstOrDefault() ?? new ADIFField();
                        Int32.TryParse(item.Value, out int stx);

                        var rstRcvd = adifRecord.Fields.Where(x => x.Name.Equals("RST_RCVD")).FirstOrDefault() ?? new ADIFField();

                        record.Fields.Add(new ADIFField()
                        {
                            Name = field, Value = $"{rstRcvd.Value} {stx:000}"
                        });
                        continue;
                    }

                    if (field.Equals("REP. RECIBIDO"))
                    {
                        var item = adifRecord.Fields.Where(x => x.Name.Equals("SRX")).FirstOrDefault() ?? new ADIFField();
                        Int32.TryParse(item.Value, out int srx);

                        var rstSent = adifRecord.Fields.Where(x => x.Name.Equals("RST_SENT")).FirstOrDefault() ?? new ADIFField();

                        record.Fields.Add(new ADIFField()
                        {
                            Name = field, Value = $"{rstSent.Value} {srx:000}"
                        });
                        continue;
                    }
                }

                planillaRecords.Add(record);
                recordCounter++;
            }

            var stringBuilderList = new List <StringBuilder>();

            var csvHeader = new StringBuilder();

            var firstRecord = planillaRecords[0];

            BuildHeader(firstRecord, csvHeader);
            stringBuilderList.Add(csvHeader);

            foreach (var adifRecord in planillaRecords)
            {
                var csvRow = new StringBuilder();
                BuildRow(adifRecord, csvRow);
                stringBuilderList.Add(csvRow);
            }

            using (var w = new StreamWriter(filePath))
            {
                foreach (var sb in stringBuilderList)
                {
                    w.WriteLine(sb.ToString());
                }
                w.Flush();
            }
        }