Class: OpenC3::InterfaceTopic

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

Constant Summary collapse

COMMAND_ACK_TIMEOUT_S =
30

Class Method Summary collapse

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_interface(interface_name, scope:) ⇒ Object

Look up db_shard from Interface



25
26
27
28
# File 'lib/openc3/topics/interface_topic.rb', line 25

def self._db_shard_for_interface(interface_name, scope:)
  json = Store.hget("#{scope}__openc3_interfaces", interface_name)
  json ? (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i : 0
end

.connect_interface(interface_name, *interface_params, scope:) ⇒ Object



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

def self.connect_interface(interface_name, *interface_params, scope:)
  db_shard = _db_shard_for_interface(interface_name, scope: scope)
  if interface_params && !interface_params.empty?
    Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'connect' => 'true', 'params' => JSON.generate(interface_params, allow_nan: true) }, '*', 100, db_shard: db_shard)
  else
    Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'connect' => 'true' }, '*', 100, db_shard: db_shard)
  end
end

.disconnect_interface(interface_name, scope:) ⇒ Object



113
114
115
116
# File 'lib/openc3/topics/interface_topic.rb', line 113

def self.disconnect_interface(interface_name, scope:)
  db_shard = _db_shard_for_interface(interface_name, scope: scope)
  Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'disconnect' => 'true' }, '*', 100, db_shard: db_shard)
end

.inject_tlm(interface_name, target_name, packet_name, item_hash = nil, type: :CONVERTED, stored: false, timeout: nil, scope:) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/openc3/topics/interface_topic.rb', line 151

def self.inject_tlm(interface_name, target_name, packet_name, item_hash = nil, type: :CONVERTED, stored: false, timeout: nil, scope:)
  interface_name = interface_name.upcase
  db_shard = _db_shard_for_interface(interface_name, scope: scope)

  timeout = COMMAND_ACK_TIMEOUT_S unless timeout
  ack_topic = "{#{scope}__ACKCMD}INTERFACE__#{interface_name}"
  Topic.update_topic_offsets([ack_topic], db_shard: db_shard)

  data = {}
  data['target_name'] = target_name.to_s.upcase
  data['packet_name'] = packet_name.to_s.upcase
  data['item_hash'] = item_hash
  data['type'] = type
  data['stored'] = stored
  cmd_id = Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'inject_tlm' => JSON.generate(data, allow_nan: 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
        if msg_hash["result"] == "SUCCESS"
          return
        else
          raise msg_hash["result"]
        end
      end
    end
  end
  raise "Timeout of #{timeout}s waiting for cmd ack"
end

.interface_cmd(interface_name, cmd_name, *cmd_params, scope:) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/openc3/topics/interface_topic.rb', line 133

def self.interface_cmd(interface_name, cmd_name, *cmd_params, scope:)
  db_shard = _db_shard_for_interface(interface_name, scope: scope)
  data = {}
  data['cmd_name'] = cmd_name
  data['cmd_params'] = cmd_params
  Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'interface_cmd' => JSON.generate(data, allow_nan: true) }, '*', 100, db_shard: db_shard)
end

.interface_details(interface_name, timeout: nil, scope:) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/openc3/topics/interface_topic.rb', line 201

def self.interface_details(interface_name, timeout: nil, scope:)
  interface_name = interface_name.upcase
  db_shard = _db_shard_for_interface(interface_name, scope: scope)

  timeout = COMMAND_ACK_TIMEOUT_S unless timeout
  ack_topic = "{#{scope}__ACKCMD}INTERFACE__#{interface_name}"
  Topic.update_topic_offsets([ack_topic], db_shard: db_shard)

  cmd_id = Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'interface_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

.interface_target_disable(interface_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/openc3/topics/interface_topic.rb', line 191

def self.interface_target_disable(interface_name, target_name, cmd_only: false, tlm_only: false, scope:)
  db_shard = _db_shard_for_interface(interface_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}INTERFACE__#{interface_name}", { 'target_control' => JSON.generate(data, allow_nan: true) }, '*', 100, db_shard: db_shard)
end

.interface_target_enable(interface_name, target_name, cmd_only: false, tlm_only: false, scope:) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/openc3/topics/interface_topic.rb', line 181

def self.interface_target_enable(interface_name, target_name, cmd_only: false, tlm_only: false, scope:)
  db_shard = _db_shard_for_interface(interface_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}INTERFACE__#{interface_name}", { 'target_control' => JSON.generate(data, allow_nan: true) }, '*', 100, db_shard: db_shard)
end

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



141
142
143
144
145
146
147
148
149
# File 'lib/openc3/topics/interface_topic.rb', line 141

def self.protocol_cmd(interface_name, cmd_name, *cmd_params, read_write: :READ_WRITE, index: -1, scope:)
  db_shard = _db_shard_for_interface(interface_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}INTERFACE__#{interface_name}", { 'protocol_cmd' => JSON.generate(data, allow_nan: true) }, '*', 100, db_shard: db_shard)
end

.receive_commands(interface, scope:, db_shard: 0) ⇒ Object



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
# File 'lib/openc3/topics/interface_topic.rb', line 42

def self.receive_commands(interface, scope:, db_shard: 0)
  db_shard = db_shard.to_i
  interface_cmd_topic = "{#{scope}__CMD}INTERFACE__#{interface.name}"
  system_events_topic = "OPENC3__SYSTEM__EVENTS"

  target_topics = []
  interface.cmd_target_names.each do |target_name|
    target_topics << "{#{scope}__CMD}TARGET__#{target_name}"
  end

  # Group target command topics by db_shard; include interface cmd and system events on db_shard
  db_shard_groups = Topic.group_topics_by_db_shard(target_topics, target_pattern: 'CMD}TARGET__', scope: scope)
  db_shard_groups[db_shard] ||= []
  db_shard_groups[db_shard] << interface_cmd_topic
  db_shard_groups[db_shard] << system_events_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
      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
        end
      end
    end
  end
end

.shutdown(interface, scope:) ⇒ Object



128
129
130
131
# File 'lib/openc3/topics/interface_topic.rb', line 128

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

.start_raw_logging(interface_name, scope:) ⇒ Object



118
119
120
121
# File 'lib/openc3/topics/interface_topic.rb', line 118

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

.stop_raw_logging(interface_name, scope:) ⇒ Object



123
124
125
126
# File 'lib/openc3/topics/interface_topic.rb', line 123

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

.topics(interface, scope:) ⇒ Object

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



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

def self.topics(interface, scope:)
  topics = []
  topics << "{#{scope}__CMD}INTERFACE__#{interface.name}"
  interface.cmd_target_names.each do |target_name|
    topics << "{#{scope}__CMD}TARGET__#{target_name}"
  end
  topics << "OPENC3__SYSTEM__EVENTS" # Add System Events
  topics
end

.write_raw(interface_name, data, timeout: nil, scope:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/topics/interface_topic.rb', line 80

def self.write_raw(interface_name, data, timeout: nil, scope:)
  interface_name = interface_name.upcase
  db_shard = _db_shard_for_interface(interface_name, scope: scope)

  timeout = COMMAND_ACK_TIMEOUT_S unless timeout
  ack_topic = "{#{scope}__ACKCMD}INTERFACE__#{interface_name}"
  Topic.update_topic_offsets([ack_topic], db_shard: db_shard)

  cmd_id = Topic.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'raw' => data }, '*', 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
        if msg_hash["result"] == "SUCCESS"
          return
        else
          raise msg_hash["result"]
        end
      end
    end
  end
  raise "Timeout of #{timeout}s waiting for cmd ack"
end