Method: OpenC3::PacketLogWriter#write_entry

Defined in:
lib/openc3/logs/packet_log_writer.rb

#write_entry(entry_type, cmd_or_tlm, target_name, packet_name, time_nsec_since_epoch, stored, data, id, received_time_nsec_since_epoch: nil, extra: nil) ⇒ Object

Raises:

  • (ArgumentError)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/openc3/logs/packet_log_writer.rb', line 237

def write_entry(entry_type, cmd_or_tlm, target_name, packet_name, time_nsec_since_epoch, stored, data, id, received_time_nsec_since_epoch: nil, extra: nil)
  raise ArgumentError.new("Length of id must be 64, got #{id.length}") if id and id.length != 64 # 64 hex digits, gets packed to 32 bytes with .pack('H*')

  length = OPENC3_PRIMARY_FIXED_SIZE
  flags = 0
  flags |= OPENC3_STORED_FLAG_MASK if stored
  flags |= OPENC3_ID_FLAG_MASK if id
  case entry_type
  when :TARGET_DECLARATION
    target_index = @next_target_index
    @target_indexes[target_name] = target_index
    @next_target_index += 1
    if target_index > OPENC3_MAX_TARGET_INDEX
      raise "Target Index Overflow"
    end

    flags |= OPENC3_TARGET_DECLARATION_ENTRY_TYPE_MASK
    length += OPENC3_TARGET_DECLARATION_SECONDARY_FIXED_SIZE + target_name.length
    length += OPENC3_ID_FIXED_SIZE if id
    @entry.clear
    @entry << [length, flags].pack(OPENC3_TARGET_DECLARATION_PACK_DIRECTIVE) << target_name
    @entry << [id].pack('H*') if id
    @target_dec_entries << @entry.dup
  when :PACKET_DECLARATION
    target_index = @target_indexes[target_name]
    flags |= OPENC3_PACKET_DECLARATION_ENTRY_TYPE_MASK
    if cmd_or_tlm == :CMD
      flags |= OPENC3_CMD_FLAG_MASK
    end
    length += OPENC3_PACKET_DECLARATION_SECONDARY_FIXED_SIZE + packet_name.length
    length += OPENC3_ID_FIXED_SIZE if id
    @entry.clear
    @entry << [length, flags, target_index].pack(OPENC3_PACKET_DECLARATION_PACK_DIRECTIVE) << packet_name
    @entry << [id].pack('H*') if id
    @packet_dec_entries << @entry.dup
  when :KEY_MAP
    flags |= OPENC3_KEY_MAP_ENTRY_TYPE_MASK
    flags |= OPENC3_CBOR_FLAG_MASK if @data_format == :CBOR
    length += OPENC3_KEY_MAP_SECONDARY_FIXED_SIZE + data.length
    packet_index = get_packet_index(cmd_or_tlm, target_name, packet_name, entry_type, data)
    @entry.clear
    @entry << [length, flags, packet_index].pack(OPENC3_KEY_MAP_PACK_DIRECTIVE) << data
  when :OFFSET_MARKER
    flags |= OPENC3_OFFSET_MARKER_ENTRY_TYPE_MASK
    length += OPENC3_OFFSET_MARKER_SECONDARY_FIXED_SIZE + data.length
    @entry.clear
    @entry << [length, flags].pack(OPENC3_OFFSET_MARKER_PACK_DIRECTIVE) << data
  when :RAW_PACKET, :JSON_PACKET
    target_name = 'UNKNOWN'.freeze unless target_name
    packet_name = 'UNKNOWN'.freeze unless packet_name
    packet_index = get_packet_index(cmd_or_tlm, target_name, packet_name, entry_type, data)
    if entry_type == :RAW_PACKET
      flags |= OPENC3_RAW_PACKET_ENTRY_TYPE_MASK
    else
      flags |= OPENC3_JSON_PACKET_ENTRY_TYPE_MASK
      key_map = @key_map_table[packet_index]
      if key_map
        # Compress data using key map
        data = JSON.parse(data, :allow_nan => true, :create_additions => true) if String === data
        compressed = {}
        data.each do |key, value|
          compressed_key = key_map[key]
          compressed_key = key unless compressed_key
          compressed[compressed_key] = value
        end
        if @data_format == :CBOR
          flags |= OPENC3_CBOR_FLAG_MASK
          data = compressed.to_cbor
        else
          data = JSON.generate(compressed, :allow_nan => true)
        end
      end
    end
    if cmd_or_tlm == :CMD
      flags |= OPENC3_CMD_FLAG_MASK
    end
    if received_time_nsec_since_epoch
      flags |= OPENC3_RECEIVED_TIME_FLAG_MASK
      length += OPENC3_RECEIVED_TIME_FIXED_SIZE
    end
    extra_encoded = nil
    if extra
      flags |= OPENC3_EXTRA_FLAG_MASK
      extra = JSON.parse(extra, :allow_nan => true, :create_additions => true) if String === extra
      length += OPENC3_EXTRA_LENGTH_FIXED_SIZE
      if @data_format == :CBOR
        extra_encoded = extra.as_json.to_cbor
      else
        extra_encoded = JSON.generate(extra.as_json, :allow_nan => true)
      end
      length += extra_encoded.length
    end
    length += OPENC3_PACKET_SECONDARY_FIXED_SIZE + data.length
    @entry.clear
    @entry << [length, flags, packet_index, time_nsec_since_epoch].pack(OPENC3_PACKET_PACK_DIRECTIVE)
    @entry << [received_time_nsec_since_epoch].pack(OPENC3_RECEIVED_TIME_PACK_DIRECTIVE) if received_time_nsec_since_epoch
    @entry << [extra_encoded.length].pack(OPENC3_EXTRA_LENGTH_PACK_DIRECTIVE) << extra_encoded if extra_encoded
    @entry << data.force_encoding('ASCII-8BIT')
    @first_time = time_nsec_since_epoch if !@first_time or time_nsec_since_epoch < @first_time
    @last_time = time_nsec_since_epoch if !@last_time or time_nsec_since_epoch > @last_time
  else
    raise "Unknown entry_type: #{entry_type}"
  end
  @file.write(@entry)
  @file_size += @entry.length
end