Class: NMEAPlus::SourceDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/nmea_plus.rb

Overview

The NMEA source decoder wraps an IO object, converting each_line functionality to #each_message or #each_complete_message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_reader) ⇒ SourceDecoder

Returns a new instance of SourceDecoder.

Parameters:

  • line_reader (IO)

    The source stream for messages



24
25
26
27
28
29
30
31
# File 'lib/nmea_plus.rb', line 24

def initialize(line_reader)
  unless line_reader.respond_to? :each_line
    fail ArgumentError, "line_reader must inherit from type IO (or implement each_line)"
  end
  @throw_on_parse_fail = false
  @source = line_reader
  @decoder = NMEAPlus::Decoder.new
end

Instance Attribute Details

#throw_on_parse_failbool

False by default.

Returns:

  • (bool)

    whether to throw an exception on lines that don’t properly parse



17
18
19
# File 'lib/nmea_plus.rb', line 17

def throw_on_parse_fail
  @throw_on_parse_fail
end

#throw_on_unrecognized_typebool

False by default. Typically for development.

Returns:

  • (bool)

    whether to throw an exception on message types that aren’t supported



21
22
23
# File 'lib/nmea_plus.rb', line 21

def throw_on_unrecognized_type
  @throw_on_unrecognized_type
end

Instance Method Details

#each_complete_message {|NMEAPlus::Message| ... } ⇒ void

This method returns an undefined value.

Executes the block for every valid NMEA message in the source stream, attempting to group multipart messages into message chains.

Yields:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nmea_plus.rb', line 56

def each_complete_message
  partials = {}
  each_message do |msg|
    slot = msg.data_type

    if partials[slot].nil?
      partials[slot] = msg
    else
      # the message was already in there
      if 1 != (msg.message_number - partials[slot].message_number)
        # error! just overwrite what was there
        partials[slot] = msg
      else
        partials[slot].add_message_part(msg)
      end
    end

    # take action if we've completed the chain
    maybe_full = partials[slot]
    if maybe_full.all_messages_received?
      partials[slot] = nil
      yield maybe_full
    end
  end
end

#each_message {|NMEAPlus::Message| ... } ⇒ void

This method returns an undefined value.

Executes the block for every valid NMEA message in the source stream

Yields:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nmea_plus.rb', line 36

def each_message
  @source.each_line do |line|
    if @throw_on_parse_fail
      yield @decoder.parse(line)
    else
      got_error = false
      begin
        y = @decoder.parse(line)
      rescue
        got_error = true
      end
      yield y unless got_error
    end
  end
end