Class: OpenC3::RouterMicroservice

Inherits:
InterfaceMicroservice show all
Defined in:
lib/openc3/microservices/router_microservice.rb

Constant Summary

Constants inherited from InterfaceMicroservice

InterfaceMicroservice::UNKNOWN_BYTES_TO_PRINT

Instance Attribute Summary

Attributes inherited from Microservice

#count, #custom, #error, #logger, #microservice_status_thread, #name, #scope, #secrets, #state

Instance Method Summary collapse

Methods inherited from InterfaceMicroservice

#attempting, #connect, #disconnect, #graceful_kill, #handle_connection_failed, #handle_connection_lost, #initialize, #run, #shutdown, #stop

Methods inherited from Microservice

#as_json, #initialize, #microservice_cmd, #run, run, #setup_microservice_topic, #shutdown

Constructor Details

This class inherits a constructor from OpenC3::InterfaceMicroservice

Instance Method Details

#handle_packet(packet) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openc3/microservices/router_microservice.rb', line 27

def handle_packet(packet)
  RouterStatusModel.set(@interface.as_json(), scope: @scope)
  if !packet.identified?
    # Need to identify so we can find the target
    identified_packet = System.commands.identify(packet.buffer(false), @interface.cmd_target_names)
    packet = identified_packet if identified_packet
  end

  unless packet.defined?
    if packet.target_name and packet.packet_name
      begin
        defined_packet = System.commands.packet(packet.target_name, packet.packet_name)
        defined_packet.received_time = packet.received_time
        defined_packet.stored = packet.stored
        defined_packet.buffer = packet.buffer
        packet = defined_packet
      rescue => err
        @logger.warn "Error defining packet of #{packet.length} bytes"
      end
    end
  end

  target_name = packet.target_name
  target_name = 'UNKNOWN' unless target_name
  target = System.targets[target_name]

  # Don't route disabled target names
  if @interface.cmd_target_enabled[target_name]
    begin
      begin
        log_message = true # Default is true
        # If the packet has the DISABLE_MESSAGES keyword then no messages by default
        log_message = false if packet.messages_disabled
        # Check if any of the parameters have DISABLE_MESSAGES
        packet.sorted_items.each do |item|
          if item.states and item.messages_disabled
            value = packet.read_item(item)
            if item.messages_disabled[value]
              log_message = false
              break
            end
          end
        end

        if log_message
          if target and target_name != 'UNKNOWN'
            @logger.info System.commands.format(packet, target.ignored_parameters)
          else
            @logger.warn "Unidentified packet of #{packet.length} bytes being routed to target #{@interface.cmd_target_names[0]}"
          end
        end
      rescue => err
        @logger.error "Problem formatting command from router:\n#{err.formatted}"
      end

      RouterTopic.route_command(packet, @interface.cmd_target_names, scope: @scope)
    rescue Exception => err
      @error = err
      @logger.error "Error routing command from #{@interface.name}\n#{err.formatted}"
    end
  end
end