Class: LiveF1::Packet::Header

Inherits:
Struct
  • Object
show all
Defined in:
lib/live_f1/packet/header.rb

Overview

A Header uses 2 bytes of data from a live timing stream to determine all the necessary information about the packet which follows it.

Defined Under Namespace

Classes: MissingData, MissingEventType, UnexpectedPacket, UnknownPacket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



19
20
21
# File 'lib/live_f1/packet/header.rb', line 19

def bytes
  @bytes
end

#carObject

Returns the value of attribute car

Returns:

  • (Object)

    the current value of car



18
19
20
# File 'lib/live_f1/packet/header.rb', line 18

def car
  @car
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



18
19
20
# File 'lib/live_f1/packet/header.rb', line 18

def data
  @data
end

#event_typeObject

Returns the value of attribute event_type

Returns:

  • (Object)

    the current value of event_type



18
19
20
# File 'lib/live_f1/packet/header.rb', line 18

def event_type
  @event_type
end

#packet_typeObject

Returns the value of attribute packet_type

Returns:

  • (Object)

    the current value of packet_type



18
19
20
# File 'lib/live_f1/packet/header.rb', line 18

def packet_type
  @packet_type
end

Class Method Details

.from_source(source, event_type) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/live_f1/packet/header.rb', line 21

def self.from_source source, event_type
  bytes = source.read_bytes(2)
  raise MissingData, "No data from #{source.inspect}" unless bytes.to_s.length == 2
  bits = bytes.to_s.reverse.unpack("B*").first
  _, data, packet_type, car = bits.match(/^(.{7})(.{4})(.{5})$/).to_a.map { |s| s.to_i(2) }

  new(data, packet_type, car, event_type).tap do |header|
    # TODO: Maybe need a nicer way of setting this
    header.instance_variable_set "@bytes", bytes
  end
end

Instance Method Details

#car?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/live_f1/packet/header.rb', line 33

def car?
  !car.zero?
end

#packet_klassObject



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/live_f1/packet/header.rb', line 37

def packet_klass
  case
  when car?
    case event_type
    when Event::RACE
      case packet_type
      when 0 then Packet::Car::PositionUpdate
      when 1 then Packet::Car::Position
      when 2 then Packet::Car::Number
      when 3 then Packet::Car::Driver
      when 4 then Packet::Car::Gap
      when 5 then Packet::Car::Interval
      when 6 then Packet::Car::LapTime
      when 7 then Packet::Car::Sector1
      when 8 then Packet::Car::PitLap1
      when 9 then Packet::Car::Sector2
      when 10 then Packet::Car::PitLap2
      when 11 then Packet::Car::Sector3
      when 12 then Packet::Car::PitLap3
      when 13 then Packet::Car::NumPits
      when 14 then nil
      when 15 then Packet::Car::PositionHistory
      end
    when Event::PRACTICE
      case packet_type
      when 0 then Packet::Car::PositionUpdate
      when 1 then Packet::Car::Position
      when 2 then Packet::Car::Number
      when 3 then Packet::Car::Driver
      when 4 then Packet::Car::BestLapTime
      when 5 then Packet::Car::Gap
      when 6 then Packet::Car::Sector1
      when 7 then Packet::Car::Sector2
      when 8 then Packet::Car::Sector3
      when 9 then Packet::Car::LapCount
      when 10 then Packet::Car::LapCount
      when 15 then nil
      end
    when Event::QUALIFYING
      case packet_type
      when 0 then Packet::Car::PositionUpdate
      when 1 then Packet::Car::Position
      when 2 then Packet::Car::Number
      when 3 then Packet::Car::Driver
      when 4 then Packet::Car::Period1
      when 5 then Packet::Car::Period2
      when 6 then Packet::Car::Period3
      when 7 then Packet::Car::Sector1
      when 8 then Packet::Car::Sector2
      when 9 then Packet::Car::Sector3
      when 10 then Packet::Car::LapCount
      when 15 then nil
      end
    else
      raise MissingEventType, "Unrecognised event type (#{event_type.inspect}), can't determine class for car packet #{packet_type.inspect}"
    end
  else
    case packet_type
    when 0 then Packet::Unknown
    when 1 then Packet::Sys::SessionStart
    when 2 then Packet::Sys::KeyFrame
    when 3 then Packet::Unknown
    when 4 then Packet::Sys::Commentary
    when 5 then Packet::Unknown
    when 6 then Packet::Sys::Notice
    when 7 then Packet::Sys::Timestamp
    when 8 then nil # Packet::Unknown
    when 9 then Packet::Sys::Weather
    when 10 then Packet::Sys::Speed
    when 11 then Packet::Sys::TrackStatus
    when 12 then Packet::Sys::Copyright
    when 13 then nil # Packet::Unknown
    when 14 then nil # Packet::Unknown
    when 15 then nil # Packet::Unknown
    end
  end or raise UnexpectedPacket, "Unexpected #{car? ? 'car' : 'sys'} packet type #{packet_type.inspect} for #{Event::Type.name_for event_type} event"
end