Method: OpenC3::TemplateProtocol#read_packet

Defined in:
lib/openc3/interfaces/protocols/template_protocol.rb

#read_packet(packet) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/openc3/interfaces/protocols/template_protocol.rb', line 122

def read_packet(packet)
  if @response_template && @response_packet
    # If lines make it this far they are part of a response
    @response_packets << packet
    return :STOP if @response_packets.length < (@ignore_lines + @response_lines)

    @ignore_lines.times do
      @response_packets.shift
    end
    response_string = ''
    @response_lines.times do
      response = @response_packets.shift
      response_string << response.buffer
    end

    # Grab the response packet specified in the command
    result_packet = System.telemetry.packet(@response_target_name, @response_packet).clone
    result_packet.received_time = nil
    result_packet.id_items.each do |item|
      result_packet.write_item(item, item.id_value, :RAW)
    end

    # Convert the response template into a Regexp
    response_item_names = []
    response_template = @response_template.clone
    response_template_items = @response_template.scan(/<.*?>/)

    response_template_items.each do |item|
      response_item_names << item[1..-2]
      response_template.gsub!(item, "(.*)")
    end
    response_regexp = Regexp.new(response_template)

    # Scan the response for the variables in brackets <VARIABLE>
    # Write the packet value with each of the values received
    response_values = response_string.scan(response_regexp)[0]
    if !response_values || (response_values.length != response_item_names.length)
      handle_error("#{@interface ? @interface.name : ""}: Unexpected response: #{response_string}")
    else
      response_values.each_with_index do |value, i|
        result_packet.write(response_item_names[i], value)
      rescue => error
        handle_error("#{@interface ? @interface.name : ""}: Could not write value #{value} due to #{error.message}")
        break
      end
    end

    @response_packets.clear

    # Release the write
    if @response_template && @response_packet
      @write_block_queue << nil
    end

    return result_packet
  else
    return packet
  end
end