Class: BWA::Message

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

Defined Under Namespace

Classes: Unrecognized

Constant Summary collapse

IGNORED_MESSAGES =

Ignore (parse and throw away) messages of these types.

[
  (+"\xbf\x00").force_encoding(Encoding::ASCII_8BIT), # request for new clients
  (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT),
  (+"\xbf\x07").force_encoding(Encoding::ASCII_8BIT) # nothing to send
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



148
149
150
151
# File 'lib/bwa/message.rb', line 148

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.



146
147
148
# File 'lib/bwa/message.rb', line 146

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

.common_messagesObject

Don’t log messages of these types, even in DEBUG mode. They are very frequent and would swamp the logs.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bwa/message.rb', line 39

def common_messages
  @common_messages ||= begin
    msgs = []
    unless BWA.verbosity >= 1
      msgs += [
        Messages::Status::MESSAGE_TYPE,
        (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT)
      ]
    end
    unless BWA.verbosity >= 2
      msgs += [
        (+"\xbf\x00").force_encoding(Encoding::ASCII_8BIT),
        (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT),
        Messages::Ready::MESSAGE_TYPE,
        (+"\xbf\x07").force_encoding(Encoding::ASCII_8BIT)
      ]
    end
    msgs
  end
end

.format_duration(minutes) ⇒ Object



141
142
143
# File 'lib/bwa/message.rb', line 141

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

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



131
132
133
134
135
136
137
138
139
# File 'lib/bwa/message.rb', line 131

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



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bwa/message.rb', line 60

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?
  unless common_messages.include?(message_type)
    BWA.logger.debug " read: #{BWA.raw2str(data.slice(offset,
                                                      length + 2))}"
  end

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

  # Ignore these message types
  return [nil, offset + length + 2] if IGNORED_MESSAGES.include?(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
      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)
  BWA.logger.debug "from spa: #{message.inspect}" unless common_messages.include?(message_type)
  [message, offset + length + 2]
end

Instance Method Details

#inspectObject



163
164
165
# File 'lib/bwa/message.rb', line 163

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

#parse(_data) ⇒ Object



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

def parse(_data); end

#serialize(message = "") ⇒ Object



155
156
157
158
159
160
161
# File 'lib/bwa/message.rb', line 155

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