Пример #1
0
        /// <summary>
        /// Parses the raw packet into it's basic components
        /// </summary>
        /// <param name="packet"></param>
        /// <returns>Returns true if successful, false otherwise</returns>
        public bool Parse(string packet)
        {
            try
            {
                //split packet into basic components of:
                // from callsign
                // destination callsign
                // digis
                // packet type
                // packet payload
                Position.Clear();
                RawPacket = packet;
                DataType  = PacketDataType.Unknown;

                var q = packet.IndexOf(">", StringComparison.Ordinal); //separates source and dest callsigns
                var p = packet.IndexOf(":", StringComparison.Ordinal); //separates header from information field
                if (p == -1 || q == -1 || p <= q)
                {
                    return(false);
                }

                SourcePathHeader = packet.Substring(0, p);
                DataTypeCh       = packet[p + 1];
                DataType         = AprsDataType.GetDataType(DataTypeCh);
                if (DataType == PacketDataType.Unknown)
                {
                    DataTypeCh = (char)0x00;
                }
                SourceCallsign = new Callsign(packet.Substring(0, q));
                Digis          = "";

                var s     = SourcePathHeader.Substring(q + 1); // remove source callsign and ">"
                var comma = s.IndexOf(",", StringComparison.Ordinal);
                if (comma > 0)
                {
                    //extract the destination CallSign
                    DestCallsign = new Callsign(s.Substring(0, comma));
                    Digis        = s.Substring(comma + 1);
                }
                else
                {
                    DestCallsign = new Callsign(s);
                }

                if (DataType != PacketDataType.Unknown)
                {
                    InformationField = packet.Substring(p + 2);
                }
                else
                {
                    InformationField = packet.Substring(p + 1);
                }

                if (!string.IsNullOrEmpty(Digis))
                {
                    SourcePathHeader += "," + Digis;
                }

                //parse information field
                if (InformationField.Length > 0)
                {
                    ParseInformationField();
                }
                else
                {
                    DataType = PacketDataType.Beacon;
                }

                //compute gridsquare if not given
                if (Position.IsValid() && string.IsNullOrEmpty(Position.Gridsquare))
                {
                    Position.Gridsquare = AprsUtil.LatLonToGridSquare(Position.CoordinateSet);
                }

                //validate required elements
                //todo:

                return(true);
            }
            catch (Exception ex)
            {
                RaiseParseErrorEvent(packet, ex.Message);
                return(false);
            }
        }
Пример #2
0
        public bool ParseRegEx(string packet)
        {
            try
            {
                //split packet into basic components of:
                // from callsign
                // destination callsign
                // digis
                // packet type
                // packet payload
                Position.Clear();
                RawPacket = packet;
                DataType  = PacketDataType.Unknown;
                if (_regexAprsPacket.IsMatch(packet))
                {
                    var m = _regexAprsPacket.Match(packet);
                    var g = m.Groups;
                    if (g.Count >= 5)
                    {
                        var idx = 0;
                        RawPacket      = g[idx++].Value;
                        SourceCallsign = new Callsign(g[idx++].Value);
                        DestCallsign   = new Callsign(g[idx++].Value);
                        //if less than 6 groups no digis
                        Digis            = g.Count < 6 ? string.Empty : g[idx++].Value;
                        DataTypeCh       = g[idx++].Value[0];
                        DataType         = AprsDataType.GetDataType(DataTypeCh);
                        InformationField = g[idx].Value;
                        SourcePathHeader = SourceCallsign.StationCallsign + '>' + DestCallsign.StationCallsign;
                        if (!string.IsNullOrEmpty(Digis))
                        {
                            SourcePathHeader += "," + Digis;
                        }

                        //parse information field
                        if (InformationField.Length > 0)
                        {
                            ParseInformationField();
                        }
                        else
                        {
                            DataType = PacketDataType.Beacon;
                        }

                        //compute gridsquare if not given
                        if (Position.IsValid() && string.IsNullOrEmpty(Position.Gridsquare))
                        {
                            Position.Gridsquare = AprsUtil.LatLonToGridSquare(Position.CoordinateSet);
                        }

                        return(true);
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                RaiseParseErrorEvent(packet, ex.Message);
                return(false);
            }
        }