Method: OpenC3::Commands#build_cmd_output_string

Defined in:
lib/openc3/packets/commands.rb

#build_cmd_output_string(target_name, cmd_name, cmd_params, raw = false) ⇒ Object



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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/openc3/packets/commands.rb', line 199

def build_cmd_output_string(target_name, cmd_name, cmd_params, raw = false)
  if raw
    output_string = 'cmd_raw("'
  else
    output_string = 'cmd("'
  end
  output_string << target_name + ' ' + cmd_name
  if cmd_params.nil? or cmd_params.empty?
    output_string << '")'
  else
    begin
      command_items = packet(target_name, cmd_name).items
    rescue
    end

    params = []
    cmd_params.each do |key, value|
      next if Packet::RESERVED_ITEM_NAMES.include?(key)

      begin
        item_type = command_items[key].data_type
      rescue
        item_type = nil
      end

      if value.is_a?(String)
        value = value.dup
        if item_type == :BLOCK or item_type == :STRING
          if !value.is_printable?
            value = "0x" + value.simple_formatted
          else
            value = value.inspect
          end
        else
          value = value.convert_to_value.to_s
        end
        if value.length > 256
          value = value[0..255] + "...'"
        end
        value.tr!('"', "'")
      elsif value.is_a?(Array)
        value = "[#{value.join(", ")}]"
      end
      params << "#{key} #{value}"
    end
    params = params.join(", ")
    output_string << ' with ' + params + '")'
  end
  return output_string
end