Class: BWA::Message

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

Defined Under Namespace

Classes: Unrecognized

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



116
117
118
119
# File 'lib/bwa/message.rb', line 116

def initialize
  # most messages we're sending come from this address
  @src = 0x0a
end

Instance Attribute Details

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



114
115
116
# File 'lib/bwa/message.rb', line 114

def raw_data
  @raw_data
end

#srcObject

Returns the value of attribute src.



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

def src
  @src
end

Class Method Details

.format_duration(minutes) ⇒ Object



109
110
111
# File 'lib/bwa/message.rb', line 109

def format_duration(minutes)
  format("%d:%02d", minutes / 60, minutes % 60)
end

.format_time(hour, minute, twenty_four_hour_time: true) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/bwa/message.rb', line 99

def format_time(hour, minute, twenty_four_hour_time: true)
  if twenty_four_hour_time
    format("%02d:%02d", hour, minute)
  else
    print_hour = hour % 12
    print_hour = 12 if print_hour.zero?
    format("%d:%02d%s", print_hour, minute, (hour >= 12) ? "PM" : "AM")
  end
end

.inherited(klass) ⇒ Object



23
24
25
26
27
28
# File 'lib/bwa/message.rb', line 23

def inherited(klass)
  super

  @messages ||= []
  @messages << klass
end

.parse(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bwa/message.rb', line 30

def parse(data)
  offset = -1
  message_type = length = nil
  loop do
    offset += 1
    # Not enough data for a full message; return and hope for more
    return nil if data.length - offset < 5

    # Keep scanning until message start char
    next unless data[offset] == "~"

    # Read length (safe since we have at least 5 chars)
    length = data[offset + 1].ord

    # No message is this short or this long; keep scanning
    next if (length < 5) || (length >= "~".ord)

    # don't have enough data for what this message wants;
    # return and hope for more (yes this might cause a
    # delay, but the protocol is very chatty so it won't
    # be long)
    return nil if length + 2 > data.length - offset

    # Not properly terminated; keep scanning
    next unless data[offset + length + 1] == "~"

    # Not a valid checksum; keep scanning
    next unless CRC.checksum(data.slice(offset + 1, length - 1)) == data[offset + length].ord

    # Got a valid message!
    break
  end

  message_type = data.slice(offset + 3, 2)
  BWA.logger.debug "discarding invalid data prior to message #{BWA.raw2str(data[0...offset])}" unless offset.zero?

  src = data[offset + 2].ord
  klass = @messages.find { |k| message_type == k::MESSAGE_TYPE }

  if klass
    valid_length = if klass::MESSAGE_LENGTH.respond_to?(:include?)
                     klass::MESSAGE_LENGTH.include?(length - 5)
                   else
                     length - 5 == klass::MESSAGE_LENGTH
                   end
    unless valid_length
      BWA.logger.debug " read: #{BWA.raw2str(data.slice(offset, length + 2))}"

      raise InvalidMessage.new("Unrecognized data length (#{length}) for message #{klass}",
                               data)
    end
  else
    BWA.logger.info(
      "Unrecognized message type #{BWA.raw2str(message_type)}: #{BWA.raw2str(data.slice(offset, length + 2))}"
    )
    klass = Unrecognized
  end

  message = klass.new
  message.parse(data.slice(offset + 5, length - 5))
  message.instance_variable_set(:@raw_data, data.slice(offset, length + 2))
  message.instance_variable_set(:@src, src)
  if message.log?
    BWA.logger.debug " read: #{BWA.raw2str(data.slice(offset, length + 2))}"
    BWA.logger.info "from spa: #{message.inspect}"
  end
  [message, offset + length + 2]
end

Instance Method Details

#inspectObject



134
135
136
# File 'lib/bwa/message.rb', line 134

def inspect
  "#<#{self.class.name} #{raw_data.unpack1("H*")}>"
end

#log?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/bwa/message.rb', line 121

def log?
  true
end

#parse(_data) ⇒ Object



125
# File 'lib/bwa/message.rb', line 125

def parse(_data); end

#serialize(message = "") ⇒ Object



127
128
129
130
131
132
# File 'lib/bwa/message.rb', line 127

def serialize(message = "")
  length = message.length + 5
  full_message = "#{length.chr}#{src.chr}#{self.class::MESSAGE_TYPE}#{message}"
  checksum = CRC.checksum(full_message)
  "\x7e#{full_message}#{checksum.chr}\x7e".b
end