Class: RubyFit::Writer

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

Constant Summary collapse

PRODUCT_ID =

Garmin Connect

65534

Instance Method Summary collapse

Instance Method Details

#course_point(values) ⇒ Object



83
84
85
86
# File 'lib/rubyfit/writer.rb', line 83

def course_point(values)
  raise "Can only write course points inside 'course_points' block" if @state != :course_points
  write_message(:course_point, values)
end

#course_pointsObject



69
70
71
72
73
74
# File 'lib/rubyfit/writer.rb', line 69

def course_points
  raise "Can only start course points mode inside 'write' block" if @state != :write
  @state = :course_points
  yield
  @state = :write
end

#track_point(values) ⇒ Object



88
89
90
91
# File 'lib/rubyfit/writer.rb', line 88

def track_point(values)
  raise "Can only write track points inside 'track_points' block" if @state != :track_points
  write_message(:record, values)
end

#track_pointsObject



76
77
78
79
80
81
# File 'lib/rubyfit/writer.rb', line 76

def track_points
  raise "Can only write track points inside 'write' block" if @state != :write
  @state = :track_points
  yield
  @state = :write
end

#write(stream, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/rubyfit/writer.rb', line 6

def write(stream, opts = {})
  raise "Can't start write mode from #{@state}" if @state
  @state = :write
  @local_nums = {}
  @last_local_num = -1

  @stream = stream

  %i(start_time duration course_point_count track_point_count name
     total_distance time_created start_x start_y end_x end_y).each do |key|
    raise ArgumentError.new("Missing required option #{key}") unless opts[key]
  end

  start_time = opts[:start_time].to_i
  duration = opts[:duration].to_i
  
  @data_crc = 0

  data_size = calculate_data_size(opts[:course_point_count], opts[:track_point_count])
  write_data(RubyFit::MessageWriter.file_header(data_size))

  write_message(:file_id, {
    time_created: opts[:time_created],
    type: 6, # Course file
    manufacturer: 1, # Garmin
    product: PRODUCT_ID,
    serial_number: 0,
  })

  write_message(:course, { name: opts[:name] })

  write_message(:lap, {
    start_time: start_time,
    timestamp: start_time,
    total_elapsed_time: duration,
    total_timer_time: duration,
    start_x: opts[:start_x],
    start_y: opts[:start_y],
    end_x: opts[:end_x],
    end_y: opts[:end_y],
    total_distance: opts[:total_distance]
  })

  write_message(:event, {
    timestamp: start_time,
    event: :timer,
    event_type: :start,
    event_group: 0
  })

  yield

  write_message(:event, {
    timestamp: start_time + duration,
    event: :timer,
    event_type: :stop_disable_all,
    event_group: 0
  })

  write_data(RubyFit::MessageWriter.crc(@data_crc))
  @state = nil
end