Class: OpenC3::RouterTopic

Inherits:
Topic show all
Defined in:
lib/openc3/topics/router_topic.rb

Class Method Summary collapse

Methods inherited from Topic

clear_topics, get_cnt, method_missing

Class Method Details

.connect_router(router_name, *router_params, scope:) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/openc3/topics/router_topic.rb', line 66

def self.connect_router(router_name, *router_params, scope:)
  if router_params && !router_params.empty?
    Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'connect' => 'true', 'params' => JSON.generate(router_params) }, '*', 100)
  else
    Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'connect' => 'true' }, '*', 100)
  end
end

.disconnect_router(router_name, scope:) ⇒ Object



74
75
76
# File 'lib/openc3/topics/router_topic.rb', line 74

def self.disconnect_router(router_name, scope:)
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'disconnect' => 'true' }, '*', 100)
end

.protocol_cmd(router_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1,, scope:) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/openc3/topics/router_topic.rb', line 99

def self.protocol_cmd(router_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1, scope:)
  data = {}
  data['cmd_name'] = cmd_name
  data['cmd_params'] = cmd_params
  data['read_write'] = read_write.to_s.upcase
  data['index'] = index
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'protocol_cmd' => JSON.generate(data, allow_nan: true) }, '*', 100)
end

.receive_telemetry(router, scope:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openc3/topics/router_topic.rb', line 40

def self.receive_telemetry(router, scope:)
  while true
    Topic.read_topics(RouterTopic.topics(router, scope: scope)) do |topic, msg_id, msg_hash, redis|
      result = yield topic, msg_id, msg_hash, redis
      if /CMD}ROUTER/.match?(topic)
        ack_topic = topic.split("__")
        ack_topic[1] = 'ACK' + ack_topic[1]
        ack_topic = ack_topic.join("__")
        Topic.write_topic(ack_topic, { 'result' => result }, msg_id, 100)
      end
    end
  end
end

.route_command(packet, target_names, scope:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/openc3/topics/router_topic.rb', line 54

def self.route_command(packet, target_names, scope:)
  if packet.identified?
    topic = "{#{scope}__CMD}TARGET__#{packet.target_name}"
    Topic.write_topic(topic, { 'target_name' => packet.target_name, 'cmd_name' => packet.packet_name, 'cmd_buffer' => packet.buffer(false) }, '*', 100)
  elsif target_names.length == 1
    topic = "{#{scope}__CMD}TARGET__#{target_names[0]}"
    Topic.write_topic(topic, { 'target_name' => packet.target_name ? packet.target_name : 'UNKNOWN', 'cmd_name' => 'UNKNOWN', 'cmd_buffer' => packet.buffer(false) }, '*', 100)
  else
    raise "No route for command: #{packet.target_name ? packet.target_name : 'UNKNOWN'} #{packet.packet_name ? packet.packet_name : 'UNKNOWN'}"
  end
end

.router_cmd(router_name, cmd_name, *cmd_params, scope:) ⇒ Object



92
93
94
95
96
97
# File 'lib/openc3/topics/router_topic.rb', line 92

def self.router_cmd(router_name, cmd_name, *cmd_params, scope:)
  data = {}
  data['cmd_name'] = cmd_name
  data['cmd_params'] = cmd_params
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'router_cmd' => JSON.generate(data, allow_nan: true) }, '*', 100)
end

.shutdown(router, scope:) ⇒ Object



86
87
88
89
90
# File 'lib/openc3/topics/router_topic.rb', line 86

def self.shutdown(router, scope:)
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router.name}", { 'shutdown' => 'true' }, '*', 100)
  sleep 1 # Give some time for the interface to shutdown
  RouterTopic.clear_topics(RouterTopic.topics(router, scope: scope))
end

.start_raw_logging(router_name, scope:) ⇒ Object



78
79
80
# File 'lib/openc3/topics/router_topic.rb', line 78

def self.start_raw_logging(router_name, scope:)
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'log_stream' => 'true' }, '*', 100)
end

.stop_raw_logging(router_name, scope:) ⇒ Object



82
83
84
# File 'lib/openc3/topics/router_topic.rb', line 82

def self.stop_raw_logging(router_name, scope:)
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'log_stream' => 'false' }, '*', 100)
end

.topics(router, scope:) ⇒ Object

Generate a list of topics for this router. This includes the router itself and all the targets which are assigned to this router.



29
30
31
32
33
34
35
36
37
38
# File 'lib/openc3/topics/router_topic.rb', line 29

def self.topics(router, scope:)
  topics = []
  topics << "{#{scope}__CMD}ROUTER__#{router.name}"
  router.tlm_target_names.each do |target_name|
    System.telemetry.packets(target_name).each do |packet_name, packet|
      topics << "#{scope}__TELEMETRY__{#{packet.target_name}}__#{packet.packet_name}"
    end
  end
  topics
end