Class: ADSB::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, created_at = Time.now) ⇒ Message

Create a new message.

Attributes

  • body - The body of the message as a hexadecimal string

  • created_at - The time at which the message was created

Examples

message = ADSB::Message.new('8D4840D6202CC371C32CE0576098')
message = ADSB::Message.new('8D4840D6202CC371C32CE0576098', Time.now)


14
15
16
17
18
19
# File 'lib/adsb/message.rb', line 14

def initialize body, created_at = Time.now
  @body = body.hex.to_s(2)
  @created_at = created_at
  decoder = Kernel.const_get("ADSB::Messages::#{type.to_s.capitalize}")
  extend(decoder)
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/adsb/message.rb', line 3

def created_at
  @created_at
end

Instance Method Details

#addressObject

Get the address of the sender.

Examples

message = ADSB::Message.new('8D4840D6202CC371C32CE0576098')
address = message.address


26
27
28
# File 'lib/adsb/message.rb', line 26

def address
   '%02x' % @body[8..31].to_i(2)
end

#dataObject



30
31
32
# File 'lib/adsb/message.rb', line 30

def data
  @body[32..87]
end


34
35
36
# File 'lib/adsb/message.rb', line 34

def downlink_format
  @body[0..4].to_i(2)
end

#typeObject

Get type of message.

Examples

message = ADSB::Message.new('8D4840D6202CC371C32CE0576098')
type = message.type


43
44
45
46
47
48
49
# File 'lib/adsb/message.rb', line 43

def type
  case type_code
    when 1, 2, 3, 4 then :identification
    when 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 then :position
    when 19 then :velocity
  end
end

#type_codeObject



51
52
53
# File 'lib/adsb/message.rb', line 51

def type_code
  @body[32..36].to_i(2)
end