Class: OpenC3::RouterTopic
- Defined in:
- lib/openc3/topics/router_topic.rb
Constant Summary collapse
- COMMAND_ACK_TIMEOUT_S =
30
Class Method Summary collapse
-
._db_shard_for_router(router_name, scope:) ⇒ Object
Look up db_shard from RouterModel.
- .connect_router(router_name, *router_params, scope:) ⇒ Object
- .disconnect_router(router_name, scope:) ⇒ Object
- .protocol_cmd(router_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1,, scope:) ⇒ Object
- .receive_telemetry(router, scope:, db_shard: 0) ⇒ Object
- .route_command(packet, target_names, scope:) ⇒ Object
- .router_cmd(router_name, cmd_name, *cmd_params, scope:) ⇒ Object
- .router_details(router_name, timeout: nil, scope:) ⇒ Object
- .router_target_disable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object
- .router_target_enable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object
- .shutdown(router, scope:) ⇒ Object
- .start_raw_logging(router_name, scope:) ⇒ Object
- .stop_raw_logging(router_name, scope:) ⇒ Object
-
.topics(router, scope:) ⇒ Object
Generate a list of topics for this router.
Methods inherited from Topic
all_same_db_shard?, clear_topics, del, get_cnt, get_last_offset, get_newest_message, get_oldest_message, group_topics_by_db_shard, method_missing, read_topics, trim_topic, update_topic_offsets, write_ack, write_topic
Class Method Details
._db_shard_for_router(router_name, scope:) ⇒ Object
Look up db_shard from RouterModel
25 26 27 28 |
# File 'lib/openc3/topics/router_topic.rb', line 25 def self._db_shard_for_router(router_name, scope:) json = Store.hget("#{scope}__openc3_routers", router_name) json ? (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i : 0 end |
.connect_router(router_name, *router_params, scope:) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/openc3/topics/router_topic.rb', line 93 def self.connect_router(router_name, *router_params, scope:) db_shard = _db_shard_for_router(router_name, scope: 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, db_shard: db_shard) else Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'connect' => 'true' }, '*', 100, db_shard: db_shard) end end |
.disconnect_router(router_name, scope:) ⇒ Object
102 103 104 105 |
# File 'lib/openc3/topics/router_topic.rb', line 102 def self.disconnect_router(router_name, scope:) db_shard = _db_shard_for_router(router_name, scope: scope) Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'disconnect' => 'true' }, '*', 100, db_shard: db_shard) end |
.protocol_cmd(router_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1,, scope:) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/openc3/topics/router_topic.rb', line 130 def self.protocol_cmd(router_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1, scope:) db_shard = _db_shard_for_router(router_name, scope: 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, db_shard: db_shard) end |
.receive_telemetry(router, scope:, db_shard: 0) ⇒ Object
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 |
# File 'lib/openc3/topics/router_topic.rb', line 43 def self.receive_telemetry(router, scope:, db_shard: 0) db_shard = db_shard.to_i router_cmd_topic = "{#{scope}__CMD}ROUTER__#{router.name}" target_topics = [] router.tlm_target_names.each do |target_name| System.telemetry.packets(target_name).each do |_packet_name, packet| target_topics << "#{scope}__TELEMETRY__{#{packet.target_name}}__#{packet.packet_name}" end end # Group telemetry topics by db_shard; include router cmd topic on db_shard db_shard_groups = Topic.group_topics_by_db_shard(target_topics, target_pattern: '__TELEMETRY__', scope: scope) db_shard_groups[db_shard] ||= [] db_shard_groups[db_shard] << router_cmd_topic all_same_db_shard = Topic.all_same_db_shard?(db_shard_groups) while true if all_same_db_shard # Fast path: everything on one db_shard, single read db_shard = db_shard_groups.keys.first || 0 Topic.read_topics(db_shard_groups[db_shard], db_shard: db_shard) do |topic, msg_id, msg_hash, redis| result = yield topic, msg_id, msg_hash, redis Topic.write_ack(topic, result, msg_id, db_shard: db_shard) if result and /CMD}ROUTER/.match?(topic) end else timeout_per_db_shard = [1000 / [db_shard_groups.length, 1].max, 100].max db_shard_groups.each do |db_shard, topics| Topic.read_topics(topics, nil, timeout_per_db_shard, db_shard: db_shard) do |topic, msg_id, msg_hash, redis| result = yield topic, msg_id, msg_hash, redis Topic.write_ack(topic, result, msg_id, db_shard: db_shard) if result and /CMD}ROUTER/.match?(topic) end end end end end |
.route_command(packet, target_names, scope:) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/openc3/topics/router_topic.rb', line 81 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
122 123 124 125 126 127 128 |
# File 'lib/openc3/topics/router_topic.rb', line 122 def self.router_cmd(router_name, cmd_name, *cmd_params, scope:) db_shard = _db_shard_for_router(router_name, scope: 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, db_shard: db_shard) end |
.router_details(router_name, timeout: nil, scope:) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/openc3/topics/router_topic.rb', line 160 def self.router_details(router_name, timeout: nil, scope:) router_name = router_name.upcase db_shard = _db_shard_for_router(router_name, scope: scope) timeout = COMMAND_ACK_TIMEOUT_S unless timeout ack_topic = "{#{scope}__ACKCMD}ROUTER__#{router_name}" Topic.update_topic_offsets([ack_topic], db_shard: db_shard) cmd_id = Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'router_details' => 'true' }, '*', 100, db_shard: db_shard) time = Time.now while (Time.now - time) < timeout Topic.read_topics([ack_topic], db_shard: db_shard) 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
150 151 152 153 154 155 156 157 158 |
# File 'lib/openc3/topics/router_topic.rb', line 150 def self.router_target_disable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) db_shard = _db_shard_for_router(router_name, scope: 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, db_shard: db_shard) end |
.router_target_enable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/openc3/topics/router_topic.rb', line 140 def self.router_target_enable(router_name, target_name, cmd_only: false, tlm_only: false, scope:) db_shard = _db_shard_for_router(router_name, scope: 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, db_shard: db_shard) end |
.shutdown(router, scope:) ⇒ Object
117 118 119 120 |
# File 'lib/openc3/topics/router_topic.rb', line 117 def self.shutdown(router, scope:) db_shard = _db_shard_for_router(router.name, scope: scope) Topic.write_topic("{#{scope}__CMD}ROUTER__#{router.name}", { 'shutdown' => 'true' }, '*', 100, db_shard: db_shard) end |
.start_raw_logging(router_name, scope:) ⇒ Object
107 108 109 110 |
# File 'lib/openc3/topics/router_topic.rb', line 107 def self.start_raw_logging(router_name, scope:) db_shard = _db_shard_for_router(router_name, scope: scope) Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'log_stream' => 'true' }, '*', 100, db_shard: db_shard) end |
.stop_raw_logging(router_name, scope:) ⇒ Object
112 113 114 115 |
# File 'lib/openc3/topics/router_topic.rb', line 112 def self.stop_raw_logging(router_name, scope:) db_shard = _db_shard_for_router(router_name, scope: scope) Topic.write_topic("{#{scope}__CMD}ROUTER__#{router_name}", { 'log_stream' => 'false' }, '*', 100, db_shard: db_shard) 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.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/openc3/topics/router_topic.rb', line 32 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 |