Method: OpenC3::InterfaceMicroservice#initialize

Defined in:
lib/openc3/microservices/interface_microservice.rb

#initialize(name) ⇒ InterfaceMicroservice



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/openc3/microservices/interface_microservice.rb', line 352

def initialize(name)
  @mutex = Mutex.new
  super(name)

  @interface_or_router = self.class.name.to_s.split("Microservice")[0].upcase.split("::")[-1]
  if @interface_or_router == 'INTERFACE'
    @metric.set(name: 'interface_tlm_total', value: @count, type: 'counter')
  else
    @metric.set(name: 'router_cmd_total', value: @count, type: 'counter')
  end

  interface_name = name.split("__")[2]
  if @interface_or_router == 'INTERFACE'
    @interface = InterfaceModel.get_model(name: interface_name, scope: @scope).build
  else
    @interface = RouterModel.get_model(name: interface_name, scope: @scope).build
  end
  @interface.name = interface_name
  # Map the interface to the interface's targets
  @interface.target_names.each do |target_name|
    target = System.targets[target_name]
    target.interface = @interface
  end
  @interface.tlm_target_names.each do |target_name|
    # Initialize the target's packet counters based on the Topic stream
    # Prevents packet count resetting to 0 when interface restarts
    begin
      System.telemetry.packets(target_name).each do |packet_name, packet|
        topic = "#{@scope}__TELEMETRY__{#{target_name}}__#{packet_name}"
        msg_id, msg_hash = Topic.get_newest_message(topic)
        if msg_id
          packet.received_count = msg_hash['received_count'].to_i
        else
          packet.received_count = 0
        end
      end
    rescue
      # Handle targets without telemetry
    end
  end
  if @interface.connect_on_startup
    @interface.state = 'ATTEMPTING'
  else
    @interface.state = 'DISCONNECTED'
  end
  if @interface_or_router == 'INTERFACE'
    InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope)
  else
    RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope)
  end

  @queued = false
  @interface.options.each do |option_name, option_values|
    case option_name.upcase
    when 'OPTIMIZE_THROUGHPUT'
      @queued = true
      update_interval = option_values[0].to_f
      EphemeralStoreQueued.instance.set_update_interval(update_interval)
      StoreQueued.instance.set_update_interval(update_interval)
    end
  end

  @interface_thread_sleeper = Sleeper.new
  @cancel_thread = false
  @connection_failed_messages = []
  @connection_lost_messages = []
  if @interface_or_router == 'INTERFACE'
    @handler_thread = InterfaceCmdHandlerThread.new(@interface, self, logger: @logger, metric: @metric, scope: @scope)
  else
    @handler_thread = RouterTlmHandlerThread.new(@interface, self, logger: @logger, metric: @metric, scope: @scope)
  end
  @handler_thread.start
end