Class: OpenC3::RouterTlmHandlerThread

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/microservices/interface_microservice.rb

Instance Method Summary collapse

Constructor Details

#initialize(router, tlm, logger: nil, metric: nil, scope:) ⇒ RouterTlmHandlerThread

Returns a new instance of RouterTlmHandlerThread.



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/openc3/microservices/interface_microservice.rb', line 377

def initialize(router, tlm, logger: nil, metric: nil, scope:)
  @router = router
  @tlm = tlm
  @scope = scope
  @logger = logger
  @logger = Logger unless @logger
  @metric = metric
  @count = 0
  @directive_count = 0
  @metric.set(name: 'router_directive_total', value: @directive_count, type: 'counter') if @metric
  @metric.set(name: 'router_tlm_total', value: @count, type: 'counter') if @metric
end

Instance Method Details

#graceful_killObject



404
405
406
407
# File 'lib/openc3/microservices/interface_microservice.rb', line 404

def graceful_kill
  RouterTopic.shutdown(@router, scope: @scope)
  sleep(0.001) # Allow other threads to run
end

#runObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/openc3/microservices/interface_microservice.rb', line 409

def run
  RouterTopic.receive_telemetry(@router, scope: @scope) do |topic, msg_id, msg_hash, _redis|
    msgid_seconds_from_epoch = msg_id.split('-')[0].to_i / 1000.0
    delta = Time.now.to_f - msgid_seconds_from_epoch
    @metric.set(name: 'router_topic_delta_seconds', value: delta, type: 'gauge', unit: 'seconds', help: 'Delta time between data written to stream and router tlm start') if @metric

    # Check for commands to the router itself
    if /CMD}ROUTER/.match?(topic)
      @directive_count += 1
      @metric.set(name: 'router_directive_total', value: @directive_count, type: 'counter') if @metric

      if msg_hash['shutdown']
        @logger.info "#{@router.name}: Shutdown requested"
        RouterTopic.clear_topics(RouterTopic.topics(@router, scope: @scope))
        return
      end
      if msg_hash['connect']
        @logger.info "#{@router.name}: Connect requested"
        params = []
        if msg_hash['params']
          params = JSON.parse(msg_hash['params'], allow_nan: true, create_additions: true)
        end
        @router = @tlm.attempting(*params)
        next 'SUCCESS'
      end
      if msg_hash['disconnect']
        @logger.info "#{@router.name}: Disconnect requested"
        @tlm.disconnect(false)
        next 'SUCCESS'
      end
      if msg_hash.key?('log_stream')
        if msg_hash['log_stream'] == 'true'
          @logger.info "#{@router.name}: Enable stream logging"
          @router.start_raw_logging
        else
          @logger.info "#{@router.name}: Disable stream logging"
          @router.stop_raw_logging
        end
        next 'SUCCESS'
      end
      if msg_hash.key?('router_cmd')
        params = JSON.parse(msg_hash['router_cmd'], allow_nan: true, create_additions: true)
        begin
          @logger.info "#{@router.name}: router_cmd: #{params['cmd_name']} #{params['cmd_params'].join(' ')}"
          @router.interface_cmd(params['cmd_name'], *params['cmd_params'])
          RouterStatusModel.set(@router.as_json(), queued: true, scope: @scope)
        rescue => e
          @logger.error "#{@router.name}: router_cmd: #{e.formatted}"
          next e.message
        end
        next 'SUCCESS'
      end
      if msg_hash.key?('protocol_cmd')
        params = JSON.parse(msg_hash['protocol_cmd'], allow_nan: true, create_additions: true)
        begin
          @logger.info "#{@router.name}: protocol_cmd: #{params['cmd_name']} #{params['cmd_params'].join(' ')} read_write: #{params['read_write']} index: #{params['index']}"
          @router.protocol_cmd(params['cmd_name'], *params['cmd_params'], read_write: params['read_write'], index: params['index'])
          RouterStatusModel.set(@router.as_json(), queued: true, scope: @scope)
        rescue => e
          @logger.error "#{@router.name}: protocol_cmd: #{e.formatted}"
          next e.message
        end
        next 'SUCCESS'
      end
      if msg_hash.key?('target_control')
        begin
          params = JSON.parse(msg_hash['target_control'], allow_nan: true, create_additions: true)
          target_name = params['target_name']
          cmd_only = params['cmd_only']
          tlm_only = params['tlm_only']
          action = params['action']
          if action == 'disable'
            @router.cmd_target_enabled[target_name] = false unless tlm_only
            @router.tlm_target_enabled[target_name] = false unless cmd_only
            @logger.info "#{@router.name}: target_disable: #{target_name} cmd_only:#{cmd_only} tlm_only:#{tlm_only}"
          else # enable
            @router.cmd_target_enabled[target_name] = true unless tlm_only
            @router.tlm_target_enabled[target_name] = true unless cmd_only
            @logger.info "#{@router.name}: target_enable: #{target_name} cmd_only:#{cmd_only} tlm_only:#{tlm_only}"
          end
        rescue => e
          @logger.error "#{@router.name}: target_control: #{e.formatted}"
          next e.message
        end
        next 'SUCCESS'
      end
      if msg_hash.key?('router_details')
        next @router.details.as_json.to_json(allow_nan: true)
      end
      next 'SUCCESS'
    end

    if @router.connected?
      @count += 1
      @metric.set(name: 'router_tlm_total', value: @count, type: 'counter') if @metric

      target_name = msg_hash["target_name"]
      packet_name = msg_hash["packet_name"]

      if @router.tlm_target_enabled[target_name]
        packet = System.telemetry.packet(target_name, packet_name)
        packet.stored = ConfigParser.handle_true_false(msg_hash["stored"])
        packet.received_time = Time.from_nsec_from_epoch(msg_hash["time"].to_i)
        packet.received_count = msg_hash["received_count"].to_i
        packet.buffer = msg_hash["buffer"]

        begin
          @router.write(packet)
          RouterStatusModel.set(@router.as_json(), queued: true, scope: @scope)
          next 'SUCCESS'
        rescue => e
          @logger.error "#{@router.name}: #{e.formatted}"
          next e.message
        end
      end
    else
      next nil # Don't ack disabled targets
    end
  end
end

#startObject



390
391
392
393
394
395
396
397
398
# File 'lib/openc3/microservices/interface_microservice.rb', line 390

def start
  @thread = Thread.new do
    run()
  rescue Exception => e
    @logger.error "#{@router.name}: Telemetry handler thread died: #{e.formatted}"
    raise e
  end
  ThreadManager.instance.register(@thread, stop_object: self)
end

#stopObject



400
401
402
# File 'lib/openc3/microservices/interface_microservice.rb', line 400

def stop
  OpenC3.kill_thread(self, @thread)
end