Method: OpenC3::TemplateProtocol#write_packet

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

#write_packet(packet) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/openc3/interfaces/protocols/template_protocol.rb', line 182

def write_packet(packet)
  # Make sure we are past the initial data dropping period
  if @initial_read_delay and @initial_read_delay_needed and @connect_complete_time and Time.now < @connect_complete_time
    delay_needed = @connect_complete_time - Time.now
    sleep(delay_needed) if delay_needed > 0
  end

  # First grab the response template and response packet (if there is one)
  begin
    @response_template = packet.read("RSP_TEMPLATE").strip
    @response_packet = packet.read("RSP_PACKET").strip
    @response_target_name = packet.target_name
    # If the template or packet are empty set them to nil. This allows for
    # the user to remove the RSP_TEMPLATE and RSP_PACKET values and avoid
    # any response timeouts
    if @response_template.empty? || @response_packet.empty?
      @response_template = nil
      @response_packet = nil
      @response_target_name = nil
    end
  rescue
    # If there is no response template we set to nil
    @response_template = nil
    @response_packet = nil
    @response_target_name = nil
  end

  # Grab the command template because that is all we eventually send
  @template = packet.read("CMD_TEMPLATE")
  # Create a new packet to populate with the template
  raw_packet = Packet.new(nil, nil)
  raw_packet.buffer = @template
  raw_packet = super(raw_packet)
  return raw_packet if Symbol === raw_packet

  data = raw_packet.buffer(false)
  # Scan the template for variables in brackets <VARIABLE>
  # Read these values from the packet and substitute them in the template
  # and in the @response_packet name
  @template.scan(/<(.*?)>/).each do |variable|
    value = packet.read(variable[0], :RAW).to_s
    data.gsub!("<#{variable[0]}>", value)
    @response_packet.gsub!("<#{variable[0]}>", value) if @response_packet
  end

  return raw_packet
end