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



63
64
65
66
67
68
69
# File 'lib/openc3/topics/router_topic.rb', line 63

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



71
72
73
# File 'lib/openc3/topics/router_topic.rb', line 71

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



94
95
96
97
98
99
100
101
# File 'lib/openc3/topics/router_topic.rb', line 94

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openc3/topics/router_topic.rb', line 37

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openc3/topics/router_topic.rb', line 51

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



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/openc3/topics/router_topic.rb', line 121

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



112
113
114
115
116
117
118
119
# File 'lib/openc3/topics/router_topic.rb', line 112

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



103
104
105
106
107
108
109
110
# File 'lib/openc3/topics/router_topic.rb', line 103

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



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

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

.start_raw_logging(router_name, scope:) ⇒ Object



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

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



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

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.



26
27
28
29
30
31
32
33
34
35
# File 'lib/openc3/topics/router_topic.rb', line 26

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