Class: OpenC3::RouterTopic

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

Constant Summary collapse

COMMAND_ACK_TIMEOUT_S =
30

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



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

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, allow_nan: true) }, '*', 100)
  else
    Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'connect' => 'true' }, '*', 100)
  end
end

.disconnect_router(router_name, scope:) ⇒ Object



76
77
78
# File 'lib/openc3/topics/router_topic.rb', line 76

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



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

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 result and /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, 'id' => msg_id }, msg_id, 100)
      end
    end
  end
end

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



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

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

.router_details(router_name, timeout: nil, scope:) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openc3/topics/router_topic.rb', line 126

def self.router_details(router_name, timeout: nil, scope:)
  router_name = router_name.upcase

  timeout = COMMAND_ACK_TIMEOUT_S unless timeout
  ack_topic = "{#{scope}__ACKCMD}ROUTER__#{router_name}"
  Topic.update_topic_offsets([ack_topic])

  cmd_id = Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'router_details' => 'true' }, '*', 100)
  time = Time.now
  while (Time.now - time) < timeout
    Topic.read_topics([ack_topic]) do |_topic, _msg_id, msg_hash, _redis|
      if msg_hash["id"] == cmd_id
        return JSON.parse(msg_hash["result"], :allow_nan => true, :create_additions => true)
      end
    end
  end
  raise "Timeout of #{timeout}s waiting for cmd ack"
end

.router_target_disable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/openc3/topics/router_topic.rb', line 117

def self.router_target_disable(router_name, target_name, cmd_only: false, tlm_only: false, scope:)
  data = {}
  data['target_name'] = target_name.to_s.upcase
  data['cmd_only'] = cmd_only
  data['tlm_only'] = tlm_only
  data['action'] = 'disable'
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'target_control' => JSON.generate(data, allow_nan: true) }, '*', 100)
end

.router_target_enable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/openc3/topics/router_topic.rb', line 108

def self.router_target_enable(router_name, target_name, cmd_only: false, tlm_only: false, scope:)
  data = {}
  data['target_name'] = target_name.to_s.upcase
  data['cmd_only'] = cmd_only
  data['tlm_only'] = tlm_only
  data['action'] = 'enable'
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'target_control' => JSON.generate(data, allow_nan: true) }, '*', 100)
end

.shutdown(router, scope:) ⇒ Object



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

def self.shutdown(router, scope:)
  Topic.write_topic("{#{scope}__CMD}ROUTER__#{router.name}", { 'shutdown' => 'true' }, '*', 100)
end

.start_raw_logging(router_name, scope:) ⇒ Object



80
81
82
# File 'lib/openc3/topics/router_topic.rb', line 80

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



84
85
86
# File 'lib/openc3/topics/router_topic.rb', line 84

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.



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

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