class OGNClient::Message

Generic OGN flavoured APRS parser

You can pass any raw OGN flavoured APRS message string to the parse class method and receive an instance of the appropriate subclass (Comment, Receiver or Sender) or nil if the message string could not be parsed.

Comment example:

raw = "# aprsc 2.0.14-g28c5a6a 29 Jun 2014 07:46:15 GMT GLIDERN1 37.187.40.234:14580"
obj = OGNClient::Message.parse(raw)   # => #<OGNClient::Comment:0x007feaf1012898>
obj.comment   # => "aprsc 2.0.14-g28c5a6a 29 Jun 2014 07:46:15 GMT GLIDERN1 37.187.40.234:14580"

Sender example:

raw = "FLRDF0A52>APRS,qAS,LSTB:/220132h4658.70N/00707.72Ez090/054/A=001424 id06DF0A52 +020fpm +0.0rot 55.2dB 0e -6.2kHz"
obj = OGNClient::Message.parse(raw)   # => #<OGNClient::Sender:0x007feaec1daba8>
obj.id   # => "DF0A52"

Malformed example:

raw = "FOOBAR>not a valid message"
obj = OGNClient::Message.parse(raw)   # => nil

Constants

POSITION_PATTERN

Attributes

altitude[R]
callsign[R]
ground_speed[R]
heading[R]
latitude[R]
longitude[R]
raw[R]
receiver[R]
time[R]

Public Class Methods

parse(raw, date: nil) click to toggle source
   # File lib/ogn_client/message.rb
45 def self.parse(raw, date: nil)
46   fail(OGNClient::MessageError, "raw message must be String but is #{raw.class}") unless raw.is_a? String
47   raw = raw.chomp.force_encoding('ASCII-8BIT').encode('UTF-8')
48   OGNClient::SenderBeacon.new.send(:parse, raw, date: date) ||
49     OGNClient::ReceiverStatus.new.send(:parse, raw, date: date) ||
50     OGNClient::ReceiverBeacon.new.send(:parse, raw, date: date) ||
51     OGNClient::Comment.new.send(:parse, raw) ||
52     fail(OGNClient::MessageError, "message payload parsing failed: `#{raw}'")
53 end

Public Instance Methods

to_s() click to toggle source
   # File lib/ogn_client/message.rb
55 def to_s
56   @raw
57 end

Private Instance Methods

altitude=(raw) click to toggle source
    # File lib/ogn_client/message.rb
106 def altitude=(raw)
107   @altitude = (raw.to_i / 3.2808).round
108 end
callsign=(raw) click to toggle source
   # File lib/ogn_client/message.rb
77 def callsign=(raw)
78   @callsign = raw
79 end
ground_speed=(raw) click to toggle source
    # File lib/ogn_client/message.rb
114 def ground_speed=(raw)
115   @ground_speed = (raw.to_i * 1.852).round
116 end
heading=(raw) click to toggle source
    # File lib/ogn_client/message.rb
110 def heading=(raw)
111   @heading = raw.to_i
112 end
latitude=(raw) click to toggle source
    # File lib/ogn_client/message.rb
101 def latitude=(raw)
102   raw.first.match /(\d{2})([\d.]+)([NS])/
103   @latitude = (($1.to_f + ("#{$2}#{raw.last}".to_f / 60)) * ($3 == 'N' ? 1 : -1)).round(6)
104 end
longitude=(raw) click to toggle source
   # File lib/ogn_client/message.rb
96 def longitude=(raw)
97   raw.first.match /(\d{3})([\d.]+)([EW])/
98   @longitude = (($1.to_f + ("#{$2}#{raw.last}".to_f / 60)) * ($3 == 'E' ? 1 : -1)).round(6)
99 end
parse(raw, date: nil) click to toggle source
   # File lib/ogn_client/message.rb
61 def parse(raw, date: nil)
62   @raw = raw
63   @date = Date.parse(date) if date
64   raw.match POSITION_PATTERN do |match|
65     %i(callsign receiver time altitude).each do |attr|
66       send("#{attr}=", match[attr]) if match[attr]
67     end
68     self.heading = match[:heading] if match[:heading] && match[:heading] != '000'
69     self.ground_speed = match[:ground_speed] if match[:ground_speed] && match[:heading] != '000'
70     self.longitude = [match[:longitude], match[:longitude_enhancement]] if match[:longitude]
71     self.latitude = [match[:latitude], match[:latitude_enhancement]] if match[:latitude]
72     self
73   end or fail(OGNClient::MessageError, "message position parsing failed: `#{@raw}'")
74   self
75 end
receiver=(raw) click to toggle source
   # File lib/ogn_client/message.rb
81 def receiver=(raw)
82   @receiver = raw
83 end
time=(raw) click to toggle source
   # File lib/ogn_client/message.rb
85 def time=(raw)
86   if @date
87     time = Time.new(@date.year, @date.month, @date.day, raw[0,2], raw[2,2], raw[4,2], 0)
88   else
89     now = Time.now.utc
90     time = Time.new(now.year, now.month, now.day, raw[0,2], raw[2,2], raw[4,2], 0)
91     time -= 86400 if time > now   # adjust date of beacons sent just before midnight
92   end
93   @time = time
94 end